-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAnimationCanvas.vue
194 lines (148 loc) · 5.2 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
<template>
<div id="app-animation" ref="app-animation">
<div class="container" style="position: absolute; margin: 0px 0px 0px 0px; bottom: 10px; left: 10px; 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>
</div>
</template>
<script>
export default {
name: "app-animation",
// https://github.com/vuejs/vue/issues/1988
created(){
// Non-reactive variables
this.animations = [];
this.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.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);
},
// Destroys the animation clicked
destroyAnim: function(id){
// Find selected animation and index
let animSel;
let idxSel;
this.animations.forEach((a, idx) => {if (id == a.id) {animSel = a; idxSel = idx}});
// Destroy animation item
let animObj = this.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
// Creates a new animation
createAnimation: function(info, gribFileU, gribFileV, OLMap, animType){ // Called from GRIBFileManager.vue
// Declare OLMap
this.OLMap = OLMap;
// Create canvas
let canvas = document.createElement("canvas");
canvas.className = "position-absolute pe-none vh-100 vw-100";
canvas.id = info.name;
// Append to HTML DOM
this.$refs["app-animation"].appendChild(canvas);
// Get max-min long and lat
let gridU = gribFileU.data.grid;
let minLat = Math.min(gridU.latStart, gridU.latEnd);
let maxLat = Math.max(gridU.latStart, gridU.latEnd);
let minLong = gridU.lonStart;//Math.min(gribFile.data.grid.lonStart, gribFile.data.grid.lonEnd);
let maxLong = gridU.lonEnd;//Math.max(gribFile.data.grid.lonStart, gribFile.data.grid.lonEnd);
// TODO: check that the V has the same numbers
if (minLong > 180) {
minLong = minLong - 360;
}
// Create data matrix
let dataMatrix = [];
let index = 0;
for (let i = 0; i < gridU.numLatPoints; i++){
dataMatrix[i] = [];
for (let j = 0; j < gridU.numLongPoints; j++){
dataMatrix[i][j] = [gribFileU.data.values[index], gribFileV.data.values[index]];
index++;
}
}
// Create animation engine
//(inCanvas, inMap, dataMatrix, minLat, minLong, maxLat, maxLong, dataFormat, animType)
let animEngine = new AnimationEngineGRIB(canvas, this.OLMap, dataMatrix, minLat, minLong, maxLat, maxLong, "east_north", animType);
// Define map events for animation
// Update canvas and positions
this.OLMap.on('moveend', animEngine.onMapMoveEnd);
// Clear canvas
this.OLMap.on('movestart', animEngine.onMapMoveStart);
// Store animation in array
this.animations.push({
id: info.name,
name: info.name,
info: info,
animEngine: animEngine,
canvas: canvas
});
// Create reactive data
this.animHTML.push({
id: info.name,
name: info.name,
tooltip: info.tooltip,
class: ''
})
},
},
components: {
},
computed: {
}
}
</script>
<style scoped>
#animationCanvas {
background: none;
}
.opacity-75{
opacity: 0.75;
}
</style>