<template>
    <div class="canvasPanel">
        <canvas id="canvas" width="1000" height="500"></canvas>
        <canvas id="canvasA" width="1000" height="500"></canvas>
    </div>
</template>

<script>
//import  from '';

export default {
    name: "canvasSensor", //
    components: {},
    data() {
        return {
            // modelImgSrc: require("@/assets/common/image/others/carTopView.png"),
            aSrc: require("@/assets/common/image/others/sensorIcon.png"),
            ctx: null,
            ctxBg: null,
            imgBg: null,
            imgIcon: null,
        };
    },

    props: {
        coordinate: {
            type: Object,
            default: { x: 0, y: 0, h: 60 },
        },
        modelImgSrc: {
            type: String,
            default: require("@/assets/common/image/others/carTopView.png"),
        },
    },

    watch: {
        coordinate(val) {
            this.draw(val);
        },
        modelImgSrc(val) {
            this.drawBg();
        },
    },

    methods: {
        draw1(data) {
            // var ctx = document.getElementById("canvas").getContext("2d");
            // var ctxA = document.getElementById("canvasA").getContext("2d");
            // ctx.translate(960, 250);
            // ctx.rotate(Math.PI / 2);
            // var img = new Image();
            // img.onload = () => {
            // ctxA.drawImage(img, 0, 0, 1000, 500);
            // ctx.translate(960, 250);
            // ctx.rotate(Math.PI/2);
            // ctxA.translate(60, 60);
            // img1.src = "";

            // ctx.save();
            // };
            // console.log(ctx);
            // ctx.save();
            var img1 = new Image();
            img.src = this.modelImgSrc;
            img1.src = this.aSrc;
            img1.onload = () => {
                ctx.drawImage(img1, 0, 0, 41, 29);
                ctx.rotate(-Math.PI / 2);
                ctx.translate(-960, -250);
            };

            setTimeout((_) => {
                // ctx.save();
                // img1.src = this.aSrc;
                ctx.clearRect(-250, -40, 500, 1000);
                ctx.drawImage(img1, 30, 30, 41, 29);
                // ctx.restore();
            }, 1000);
            // setTimeout((_) => {
            //     // ctx.save();
            //     // img1.src = this.aSrc;
            //     // ctx.clearRect(-250,960,1000,500)
            //     ctx.clearRect(-250,-40,500,1000)
            //     ctx.drawImage(img1, -10, 510, 29, 41);
            //     // ctx.restore();
            // }, 2000);
            // setTimeout((_) => {
            //     ctx.clearRect(-250,-40,500,1000)
            //     ctx.drawImage(img1, -180, 810, 29, 41);
            // }, 3000);
            // setTimeout((_) => {
            //     ctx.save();
            //     img1.src = this.aSrc;
            //     ctx.drawImage(img, -60, -60, this.width, this.height);
            //     ctx.drawImage(img1, 0, 0, 15, 20);
            // }, 4000);
        },
        draw(data) {
            if (data.x === "" || data.y === "" || data.h === "") return;
            this.ctx.clearRect(-250, -40, 500, 1000);
            this.ctx.drawImage(this.imgIcon, data.x, data.y, 20.5, 14.5);
            this.ctx.beginPath();

            let x = +data.x + 6;
            let y = +data.y + 12;
            let h1 = 90 - +data.h / 2;
            let h2 = 90 + +data.h / 2;
            //定义起点
            this.ctx.moveTo(x, y);
            //以起点为圆心，画一个半径为100的圆弧
            this.ctx.arc(x, y, 100, (h1 * Math.PI) / 180, (h2 * Math.PI) / 180);
            // this.ctx.arc(0, 0, 100, (30 * Math.PI) / 180, (60 * Math.PI) / 180);
            this.ctx.closePath();
            this.ctx.fillStyle = "rgba(252,222,147,0.54)";
            this.ctx.fill();
        },
        clear() {
            this.ctx.clearRect(-250, -40, 500, 1000);
        },
        drawBg() {
            this.ctxBg.clearRect(0, 0, 1000, 500);
            this.imgBg.src = this.modelImgSrc;
            this.ctxBg.drawImage(this.imgBg, 0, 0, 1000, 500);
        },
    },

    mounted() {
        $(document).ready(() => {
            this.ctx = document.getElementById("canvas").getContext("2d");
            this.ctxBg = document.getElementById("canvasA").getContext("2d");

            this.ctx.translate(960, 250);
            this.ctx.rotate(Math.PI / 2);

            this.imgBg = new Image();
            this.imgBg.src = this.modelImgSrc;
            this.imgBg.onload = () => {
                this.ctxBg.drawImage(this.imgBg, 0, 0, 1000, 500);
            };

            this.imgIcon = new Image();
            this.imgIcon.src = this.aSrc;

            // this.ctx.moveTo(100, 100);
            // this.ctx.lineTo(200, 100);

            // this.imgIcon.onload = () => {
            //     this.ctx.drawImage(this.imgIcon, 0, 0, 29, 41);
            // };
        });
    },
};
</script>

<style lang="less" scoped>
.canvasPanel {
    position: relative;

    canvas {
        width: 100%;
    }

    #canvas {
        position: relative;
        z-index: 1;
    }

    #canvasA {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 0;
    }
}
</style>