-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationCanvas.vue
198 lines (151 loc) · 5.39 KB
/
AnimationCanvas.vue
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<template>
<div id="app-animation" ref="app-animation">
<!-- Current Animations -->
<!--div style="position: absolute; margin: 0px; font-size: .875rem">
<button type="button" class="btn btn-dark rounded-pill"
style="font-size: smaller"
:class="anim.class" :key="anim.id" :id="anim.id" :title=anim.tooltip @click.prevent="animClicked" v-for="anim in animHTML">
{{anim.name}}
<button type="button" class="btn-close btn-close-white" style="font-size: smaller" aria-label="Close" @click.stop ="closeAnimClicked"></button>
</button>
</div-->
<div class="container" style="position: absolute; margin: 70px 0px 0px 0px; font-size: .875rem">
<div class="row row-cols-auto p-1" :key="anim.id" v-for="anim in animHTML">
<button type="button" class="col-sm-auto btn btn-dark rounded-pill"
style="font-size: smaller"
:class="anim.class" :id="anim.id" :title=anim.tooltip @click.prevent="animClicked">
{{anim.name}}
<button type="button" class="btn-close btn-close-white" style="font-size: smaller" aria-label="Close" @click.stop ="closeAnimClicked"></button>
</button>
</div>
</div>
<!-- Animation Canvas will be appended by code -->
</div>
</template>
<script>
export default {
name: "app-animation",
// https://github.com/vuejs/vue/issues/1988
created(){
// Non-reactive variables
this.$options.animations = [];
this.$options.OLMap = undefined;
},
mounted () {
},
data () {
return {
animHTML: [],
}
},
methods: {
// USER HTML ACTIONS
// Animation HTML EL clicked
// Hides/unhides canvas element
animClicked: function(event) {
// Get animObj id
let id = event.currentTarget.id;
// Find animEl
let animSel;
this.$options.animations.forEach(a => {if (id == a.id) animSel = a;});
animSel.canvas.hidden = !animSel.canvas.hidden;
// Change button class
if (animSel.canvas.hidden){ // Add opacity class
this.animHTML.forEach(item => {if (id == item.id) item.class = 'opacity-75'});
} else // Remove class
this.animHTML.forEach(item => {if (id == item.id) item.class = ''});
},
// Closed animation clicked. Destroys the animation
closeAnimClicked: function(event) {
// Get id
let id = event.currentTarget.parentElement.id;
// Destroy animation
this.destroyAnim(id);
// Update visibility of remaining buttons
let delIdx;
this.animHTML.forEach((el, idx) => {if (el.id == id) delIdx = idx});
this.animHTML.splice(delIdx, 1);
},
// PRIVATE METHODS
start: function(infoWMS, animation, OLMap){
// Declare OLMap
this.$options.OLMap = OLMap;
// Create canvas
let canvas = document.createElement("canvas");
canvas.className = "position-absolute pe-none vh-100 vw-100";
canvas.id = infoWMS.name;
// Append to HTML DOM
this.$refs["app-animation"].appendChild(canvas);
// Create animation engine
let animEngine = new AnimationEngine(canvas, this.$options.OLMap, infoWMS.exampleWMSURL, animation);
// Define map events for animation
// Update canvas and positions
this.$options.OLMap.on('moveend', animEngine.onMapMoveEnd);
// Clear canvas
this.$options.OLMap.on('movestart', animEngine.onMapMoveStart);
// Store animation in array
this.$options.animations.push({
id: infoWMS.name,
name: infoWMS.name,
infoWMS: infoWMS,
animEngine: animEngine,
canvas: canvas
});
// Create reactive data
this.animHTML.push({
id: infoWMS.name,
name: infoWMS.name,
tooltip: infoWMS.tooltip,
class: ''
})
},
// Destroys the animation clicked
destroyAnim: function(id){
// Find selected animation and index
let animSel;
let idxSel;
this.$options.animations.forEach((a, idx) => {if (id == a.id) {animSel = a; idxSel = idx}});
// Destroy animation item
let animObj = this.$options.animations.splice(idxSel, 1)[0]; // The animation will stop because the canvas has no parent element
// TODO: GC (reuse anim object?)
// Remove canvas from HTML DOM
animObj.canvas.remove();
// Remove event listeners
let animEng = animObj.animEngine;
//console.log("Deleted "+ animObj.id + ". Canvas parent element: " + animEng.canvasParticles.parentElement)
animEng.map.un('movestart', animEng.onMapMoveStart);
animEng.map.un('moveend', animEng.onMapMoveEnd);
// Call destroy function
animEng.destroyer();
animEng = null;
},
// PUBLIC METHODS
// Defines the new source to use
defineWMSSource: function(infoWMS, animation, OLMap){ // Called from AppManager.vue
// Check if an animation of the same type exists
let animExists = false;
let animObj;
this.$options.animations.forEach(a => {if (infoWMS.name == a.name) {animExists = true; animObj = a;}});
if (!animExists){
// Create new animation
this.start(infoWMS, animation, OLMap);
} else {
// Update WMS source
animObj.animEngine.setSource(infoWMS.exampleWMSURL, animation);
}
},
},
components: {
},
computed: {
}
}
</script>
<style scoped>
#animationCanvas {
background: none;
}
.opacity-75{
opacity: 0.75;
}
</style>