<template>
    <div class="canvasProjectInfoPanel">
        <canvas id="canvasProjectInfo" width="1000" height="500"></canvas>
        <canvas id="canvasProjectInfoA" width="1000" height="500"></canvas>
    </div>
</template>

<script>
//import  from '';

export default {
    name: "canvasProjectInfo", //
    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: {
        configList: {
            type: Object,
            default: {
                camera: [],
                ogt: [],
                lidar: [],
                gps: [],
            },
        },
        modelImgSrc: {
            type: String,
            default: require("@/assets/common/image/others/carTopView.png"),
        },
    },

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

    methods: {
        draw(data) {
            data.x = data.sensorX;
            data.y = data.sensorY;
            data.h = data.sensorH;
            if (data.x === "" || data.y === "" || data.h === "") return;
            this.ctx.drawImage(
                this.imgIcon,
                data.sensorX,
                data.sensorY,
                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();
        },
        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("canvasProjectInfo")
                .getContext("2d");
            this.ctxBg = document
                .getElementById("canvasProjectInfoA")
                .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.imgIcon.onload = () => {
                    this.configList.camera.forEach((i) => {
                        this.draw(i);
                    });
                    this.configList.ogt.forEach((i) => {
                        this.draw(i);
                    });
                    this.configList.lidar.forEach((i) => {
                        this.draw(i);
                    });
                    this.configList.gps.forEach((i) => {
                        this.draw(i);
                    });
                };
            };
        });
    },
};
</script>

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

    canvas {
        width: 100%;
    }

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

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