<template>
    <div>
        <canvas :id="id" width="800" height="400" tabindex="0"/>
    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { v1 as uuid } from "uuid";
import { SCanvasView, SColor, SPainter, SPath } from "@persagy-web/draw/lib";

/**
 * 裁剪
 *
 * @author 郝洁 <haojie@persagy.com>
 */
class ClipView extends SCanvasView {
    /** 图片的路径 */
    img: CanvasImageSource;
    /** 圆心 x 坐标*/
    arcX = 100;
    /** 圆心 y 坐标*/
    arcY = 100;
    /** 圆心移动 x 坐标*/
    stepX = 6;
    /** 圆心移动 y 坐标*/
    stepY = 8;
    /** 定时器 */
    timer: any;
    /** 图片的url */
    _url: string = '';
    set url(v: string) {
        if (this._url == v) {
            return;
        }
        this._url = v;
        // @ts-ignore
        this.img.src = v;
    }
    get url(): string {
        return this._url;
    }

    /** 绘制完成指令 */
    isLoadOver: boolean = false;

    /**
     * 构造函数
     * @param id    图 Id
     */
    constructor(id: string) {
        super(id);
        this.img = new Image();
        this.img.onload = (e) => {
            this.isLoadOver = true;
            this.update();
        };
        this.img.onerror = (e) => {
            this.isLoadOver = false;
            this.update();
            console.log("加载图片错误！", e);
        };
    }

        /**
         * Item 绘制操作
         * @param painter   绘制对象
         */
        onDraw(painter: SPainter): void {
            // @ts-ignore
            painter.engine._canvas.save();
            // painter.save();
            //清空画布
            painter.clearRect(0, 0, 800, 400);
            clearTimeout(this.timer);
            //绘制操作
            painter.pen.color = SColor.Black;
            painter.brush.color = SColor.Black;
            painter.drawRect(0, 0, 800, 400);

            painter.brush.color = SColor.Transparent;
            let path = new SPath();
            path.arc(this.arcX, this.arcY, 100, 0, Math.PI * 2, 1);
            painter.clip = path;
            //绘制路径
            painter.drawPath(path);
            // console.log('------->');
            if (this.isLoadOver) { // 绘制指令完成时
                //绘制图片
                painter.drawImage(this.img, 0, 0, 800, 400);
            }
            // painter.restore();
            // @ts-ignore
            painter.engine._canvas.restore();
            this.timer = setTimeout(() => {
                if (this.arcX + 100 >= 800) { // 左移
                    this.stepX *= -1;
                }

                if (this.arcX - 100 < 0) { // 右移
                    this.stepX *= -1;
                }

                if (this.arcY + 100 >= 400) { // 下移
                    this.stepY *= -1;
                }

                if (this.arcY - 100 < 0) { // 上移
                    this.stepY *= -1;
                }

                this.arcX += this.stepX;
                this.arcY += this.stepY;
                this.update()
            }, 17);
        }
    }

    @Component
    export default class SelectContainerCanvas extends Vue {
        /** 图 Id */
        id: string = uuid();
        /** 实例化 view */
        view: ClipView | undefined;
        /** 图片 */
        img = require('../../public/assets/img/2.jpg');

        /**
         * 页面挂载
         */
        mounted(): void {
            this.init();
        };

        /**
         * 初始化加载
         */
        init(): void {
            this.view = new ClipView(this.id);
            this.view.url = this.img;
        };

        /**
         * 页面销毁前
         */
        beforeDestroy(): void {
            clearTimeout(this.view!!.timer);
        }
    }
</script>
