<template>
    <div class="canvasSensorModelAPanel">
        <canvas id="canvasSensorModelA" width="1000" height="500"></canvas>
        <canvas id="canvasSensorModelABg" width="1000" height="500"></canvas>
    </div>
</template>

<script>
//import  from '';

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

    props: {
        coordinate: {
            type: Object,
            default: { r: 0, hl: 60, hr: 60 },
        },
    },

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

    methods: {
        draw(data) {
            if (data.r === "" || data.hl === "" || data.hr === "") return;
            this.ctx.clearRect(-250, -220, 500, 1000);

            this.ctx.beginPath();

            let hr1 = 90 - +data.hl / 2;
            let hr2 = 90 + +data.hl / 2;
            //定义起点
            this.ctx.moveTo(-40, 0);
            this.ctx.arc(
                -40,
                0,
                data.r,
                (hr1 * Math.PI) / 180,
                (hr2 * Math.PI) / 180
            );
            this.ctx.closePath();
            this.ctx.fillStyle = "rgba(252,222,147,0.54)";
            this.ctx.fill();

            this.ctx.beginPath();

            let hl1 = 90 - +data.hr / 2;
            let hl2 = 90 + +data.hr / 2;
            //定义起点
            this.ctx.moveTo(50, 0);
            this.ctx.arc(
                50,
                0,
                data.r,
                (hl1 * Math.PI) / 180,
                (hl2 * Math.PI) / 180
            );
            this.ctx.closePath();
            this.ctx.fillStyle = "rgba(252,222,147,0.54)";
            this.ctx.fill();
        },
        clear() {
            this.ctx.clearRect(-250, -220, 500, 1000);
        },
    },

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

            this.ctx.translate(780, 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);
            };
        });
    },
};
</script>

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

    canvas {
        width: 100%;
    }

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

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