<template>
    <div class="canvasSensorModelPanel">
        <canvas id="canvasSensorModel" width="1000" height="500"></canvas>
        <canvas id="canvasSensorModelBg" width="1000" height="500"></canvas>
    </div>
</template>

<script>
//import  from '';

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

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

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

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

            let h1 = 90 - +data.h / 2;
            let h2 = 90 + +data.h / 2;
            //定义起点
            this.ctx.moveTo(6, 0);
            //以起点为圆心，画一个半径为100的圆弧
            this.ctx.arc(6, 0, data.r, (h1 * Math.PI) / 180, (h2 * 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("canvasSensorModel").getContext("2d");
            this.ctxBg = document.getElementById("canvasSensorModelBg").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>
.canvasSensorModelPanel {
    position: relative;

    canvas {
        width: 100%;
    }

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

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