longyvfengyun
2023-12-25 d8d792a6842832e8f6af6604274c438b25053afe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<template>
    <div class="three-home" ref="threeHome" :style="baseStyle">
        <div class="three-home-content" v-if="showSlot">
            <div class="tree-home-slot-wrapper">
                <slot></slot>
            </div>
        </div>
    </div>
</template>
 
<script>
    export default {
        name: "ThreeHome",
        props: {
            bg: {
                type: String,
                default: "1.png"
            },
            activeArea: {
                type: Array,
                default() {
                    return []
                },
            }
        },
        data() {
            return {
                canvas: "",
                context: "",
                width: 0,
                height: 0,
                showSlot: true
            }
        },
        watch: {
            bg(){
                this.changeBg();
            }
        },
        methods: {
            init() {
                // 初始化canvas
                this.canvas = document.createElement('canvas');
                this.$refs.threeHome.appendChild(this.canvas);
                this.context = this.canvas.getContext("2d");
                this.initBg();
                // 对点击事件做响应
                this.$refs.threeHome.addEventListener('click', (e)=>{
                    let point = {
                        x: e.offsetX,
                        y: e.offsetY
                    };
                    // 处理点击事件
                    this.handleClick(point);
                });
            },
            initBg() {
                try {
                    let baseUrl= window.location.origin+"/fg_photo_3DStation/";
                    if(process.env.NODE_ENV == 'dev') {
                        baseUrl = "http://192.168.10.79:8919/fg_photo_3DStation/"
                    }
                    const imageSrc = baseUrl+this.bg;
                    const image = new Image();
                    image.src=imageSrc;
                    image.onload = (()=>{
                        this.canvas.width = image.width;
                        this.canvas.height = image.height;
                        this.width = image.width;
                        this.height = image.height;
                        this.context.drawImage(image, 0, 0, image.width, image.height);
                        this.showSlot = false;
                        this.$emit('loadSuccess', {
                            width: image.width,
                            height: image.height
                        });
                        this.$nextTick(()=>{
                            this.showSlot = true;
                        });
                    });
                }catch (e) {
                    console.log(e);
                }
 
            },
            changeBg() {
                // 清除背景
                this.context.clearRect(0, 0, this.width, this.height);
                // 初始化背景
                this.$nextTick(()=>{
                    this.initBg();
                });
            },
            handleClick(point) {
                let activeArea = this.activeArea;
                let isActiveClick = false;        // 标识是否点击到响应点
                let tmp = {};
                for(let i=0; i<activeArea.length; i++) {
                    let item = activeArea[i];
                    let width = point.x-item.positionX;
                    let height = point.y-item.positionY;
                    if(0<= width && width <= item.length && 0<= height && height <= item.width) {
                        isActiveClick = true;
                        tmp = item;
                        tmp.clickPoint = point;
                        break;
                    }
                }
                // 向伏击响应点击事件,并返回激活点的信息
                if(isActiveClick) {
                    this.$emit("handleClick", tmp);
                }
            },
        },
        computed: {
            baseStyle() {
                return {
                    width: this.width+'px',
                    height: this.height+'px'
                };
            }
        },
        mounted() {
            this.init();
        }
    }
</script>
 
<style scoped>
 
</style>