-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathAframeCanvasManipulator.js
More file actions
411 lines (354 loc) · 16.8 KB
/
AframeCanvasManipulator.js
File metadata and controls
411 lines (354 loc) · 16.8 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
var canvasManipulator = (function () {
var colors = {
darkred: "darkred",
red: "red",
black: "black",
orange: "orange",
darkorange: "darkorange"
};
var scene = {};
var initialCameraView = {};
function initialize() {
scene = document.querySelector("a-scene");
initialCameraView.target = globalCamera.target;
initialCameraView.position = globalCamera.object.position;
initialCameraView.spherical = globalCamera.spherical;
}
function reset() {
let offset = new THREE.Vector3();
offset.subVectors(initialCameraView.target, globalCamera.target).multiplyScalar(globalCamera.data.panSpeed);
globalCamera.panOffset.add(offset);
globalCamera.sphericalDelta.phi = 0.25 * (initialCameraView.spherical.phi - globalCamera.spherical.phi);
globalCamera.sphericalDelta.theta = 0.25 * (initialCameraView.spherical.theta - globalCamera.spherical.theta);
globalCamera.scale = initialCameraView.spherical.radius/globalCamera.spherical.radius;
}
function changeTransparencyOfEntities(entities, value) {
entities.forEach(function (entity2) {
// getting the entity again here, because without it the check if originalTransparency is defined fails sometimes
let entity = model.getEntityById(entity2.id);
let component = document.getElementById(entity.id);
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - changeTransparencyOfEntities - components for entityIds not found"});
return;
}
if (entity.originalTransparency === undefined) {
entity.originalTransparency = {};
entity.currentTransparency = {};
if(component.getAttribute("material").opacity) {
entity.originalTransparency = 1 - component.getAttribute("material").opacity;
}
}
entity.currentTransparency = value;
setTransparency(component, value);
});
}
function resetTransparencyOfEntities(entities) {
entities.forEach(function (entity) {
let component = document.getElementById(entity.id);
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - resetTransparencyOfEntities - components for entityIds not found"});
return;
}
if (!(entity.originalTransparency == undefined)) {
entity.currentTransparency = entity.originalTransparency;
setTransparency(component, entity.originalTransparency);
}
});
}
/**
* Starts the metric dependent color animation for the entity.
*/
function startColorAnimationForEntity(entity, colorAnimation) {
if (!(entity == undefined)) {
var component = document.getElementById(entity.id);
}
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - startColorAnimationForEntity - component for entityId not found"});
return;
}
let originalColor = component.getAttribute("color");
component.setAttribute("color-before-animation", originalColor);
let colorAnimationFrequency = colorAnimation.getAnimationFrequency();
if (colorAnimation.colors.length === 1){
// if there is only one color: just start a normal animation loop between the original color and the specified one
component.setAttribute("animation__color",
"property: components.material.material.color; type: color; from: " + originalColor +
"; to: " + colorAnimation.getNextToColor() + "; dur: " + colorAnimationFrequency + "; loop: true; dir: alternate");
} else {
// if there are multiple colors: loop through the color animations triggered by events
for (i = 1; i <= colorAnimation.colors.length; i++) {
// in general each color animation starts when the predecessor ends
let startEvents = "animationcomplete__color_" + (i -1);
if (i === 1){
// the first color animation starts when the last ends
startEvents = "animationcomplete__color_" + colorAnimation.colors.length;
} else if (i === 2){
// the second color animation starts after the first and after the initializer
startEvents = "animationcomplete__color_" + 1 + ", animationcomplete__color_" + 0;
}
component.setAttribute("animation__color_" + i,
"property: components.material.material.color; type: color; from: " + colorAnimation.getNextFromColor() +
"; to: " + colorAnimation.getNextToColor() + "; dur: " + colorAnimationFrequency + "; startEvents: " + startEvents);
}
// the initializing color animation has no start event
component.setAttribute("animation__color_" + 0,
"property: components.material.material.color; type: color; from: " + colorAnimation.getNextFromColor() +
"; to: " + colorAnimation.getNextToColor() + "; dur: " + colorAnimationFrequency);
}
colorAnimation.resetColorIndices();
}
/**
* Stops the metric animated color animation for the entity and resets its color to the value before the animation started.
*/
function stopColorAnimationForEntity(entity) {
if (!(entity == undefined)) {
var component = document.getElementById(entity.id);
}
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - stopColorAnimationForEntity - component for entityId not found"});
return;
}
let attributeNames = component.getAttributeNames();
attributeNames.forEach(function (attributeName) {
if (attributeName.startsWith("animation__color")){
component.removeAttribute(attributeName);
}
});
let originalColor = component.getAttribute("color-before-animation");
if (originalColor !== null){
// only change the color back if there was a color animation before
component.setAttribute("animation__color_off",
"property: components.material.material.color; type: color; from: " + originalColor +
"; to: " + originalColor + "; dur: 0; loop: false");
}
// remark:
// A nice way would be to remove all of the color animation attributes.
// But this leads to components staying in their current animation color.
// Changing the color of the component back to original color does not work,
// because aframe thinks its still in this color.
// So this workaround is used: the components blink immediately back to its original color without a loop.
// This costs no further performance. Just the "animation__color_off" attribute is left.
//
// setColor(component, originalColor); // looks nice, does not work
}
/**
* Starts the metric dependent expanding animation for the entity.
*/
function startExpandingAnimationForEntity(entity, expandingAnimation) {
if (!(entity == undefined)) {
var component = document.getElementById(entity.id);
}
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - startExpandingAnimationForEntity - component for entityId not found"});
return;
}
let animationFrequency = expandingAnimation.getAnimationFrequency();
let growSize = expandingAnimation.getScale();
let scale = growSize + " " + growSize + " " + growSize;
component.setAttribute("animation__expanding",
"property: scale; from: 1 1 1; to: " + scale + "; dur: " + animationFrequency + "; loop: true; dir: alternate");
}
/**
* Stops the metric dependent expanding animation for the entity.
* Resets the entities scale back to its original value.
*/
function stopExpandingAnimationForEntity(entity) {
if (!(entity == undefined)) {
var component = document.getElementById(entity.id);
}
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - stopExpandingAnimationForEntity - component for entityId not found"});
return;
}
component.removeAttribute("animation__expanding");
component.setAttribute("scale", "1 1 1");
}
function changeColorOfEntities(entities, color) {
entities.forEach(function (entity) {
if (!(entity === undefined)) {
var component = document.getElementById(entity.id);
}
if (component == undefined) {
events.log.error.publish({text: "CanvasManipualtor - changeColorOfEntities - components for entityIds not found"});
return;
}
if (entity.originalColor === undefined) {
entity.originalColor = component.getAttribute("color");
}
entity.currentColor = color;
setColor(component, color);
}
);
}
function resetColorOfEntities(entities) {
entities.forEach(function (entity) {
let component = document.getElementById(entity.id);
if (component === undefined) {
events.log.error.publish({text: "CanvasManipualtor - resetColorOfEntities - components for entityIds not found"});
return;
}
if (entity.originalColor) {
entity.currentColor = entity.originalColor;
setColor(component, entity.originalColor);
}
});
}
function setColor(object, color) {
// color == colors.darkred ? color = colors.red : color = color;
let colorValues = color.split(" ");
if (colorValues.length === 3) {
color = "#" + parseInt(colorValues[0]).toString(16) + "" + parseInt(colorValues[1]).toString(16) + "" + parseInt(colorValues[2]).toString(16);
}
console.log(color);
console.log("object")
console.log(object);
object.setAttribute("color", color);
}
function hideEntities(entities) {
entities.forEach(function (entity) {
let component = document.getElementById(entity.id);
if (component === undefined) {
events.log.error.publish({text: "CanvasManipualtor - hideEntities - components for entityIds not found"});
return;
}
setVisibility(component, false)
});
}
function showEntities(entities) {
entities.forEach(function (entity) {
let component = document.getElementById(entity.id);
if (component === undefined) {
events.log.error.publish({text: "CanvasManipualtor - showEntities - components for entityIds not found"});
return;
}
setVisibility(component, true)
});
}
function highlightEntities(entities, color) {
entities.forEach(function (entity2) {
// getting the entity again here, because without it the check if originalTransparency is defined fails sometimes
let entity = model.getEntityById(entity2.id);
console.log("id:" + entity2.id)
let component = document.getElementById(entity.id);
console.log(component);
if (component === undefined) {
events.log.error.publish({text: "CanvasManipualtor - highlightEntities - components for entityIds not found"});
return;
}
if (entity.originalColor === undefined) {
console.log("XXX: " + component.getAttribute("color"));
entity.originalColor = component.getAttribute("color");
console.log(entity.originalColor);
entity.currentColor = entity.originalColor;
setColor(component, "green");
}
if (entity["originalTransparency"] === undefined) {
// in case "material".opacity is undefined originalTransparency gets set to 0 which would be the default value anyways
entity.originalTransparency = {};
entity.currentTransparency = {};
if(component.getAttribute("material").opacity) {
entity.originalTransparency = 1 - component.getAttribute("material").opacity;
} else entity.originalTransparency = 0;
entity.currentTransparency = entity.originalTransparency;
}
setColor(component, color);
setTransparency(component, 0);
});
}
function unhighlightEntities(entities) {
entities.forEach(function (entity) {
let component = document.getElementById(entity.id);
if (component === undefined) {
events.log.error.publish({text: "CanvasManipualtor - unhighlightEntities - components for entityIds not found"});
return;
}
setTransparency(component, entity.currentTransparency);
setColor(component, entity.currentColor);
});
}
function flyToEntity(entity) {
setCenterOfRotation(entity);
let object = document.getElementById(entity.id);
let boundingSphereRadius = object.object3DMap.mesh.geometry.boundingSphere.radius;
globalCamera.scale = boundingSphereRadius/globalCamera.spherical.radius;
}
function addElement(element) {
var addedElements = document.getElementById("addedElements");
addedElements.appendChild(element);
}
function removeElement(element) {
element.parentNode.removeChild(element);
}
function setCenterOfRotation(entity) {
let offset = new THREE.Vector3();
offset.subVectors(getCenterOfEntity(entity), globalCamera.target).multiplyScalar(globalCamera.data.panSpeed);
globalCamera.panOffset.add(offset);
}
function getCenterOfEntity(entity) {
var center = new THREE.Vector3();
var object = document.getElementById(entity.id).object3DMap.mesh;
center.x = object.geometry.boundingSphere.center["x"];
center.y = object.geometry.boundingSphere.center["y"];
center.z = object.geometry.boundingSphere.center["z"];
return object.localToWorld(center);
}
function setTransparency(object, value) {
object.setAttribute('material', {
opacity: 1 - value
});
}
function setVisibility(object, visibility) {
object.setAttribute("visible", visibility);
}
function getElementIds() {
let sceneArray = Array.from(scene.children);
sceneArray.shift(); // so camera entity needs to be first in model.html
sceneArray.pop(); // last element is of class "a-canvas"
let elementIds = [];
sceneArray.forEach(function (object) {
elementIds.push(object.id);
});
return elementIds;
}
function setMaterialOfEntities(entities, material) {
entities.forEach(function (entity) {
if (!(entity == undefined)) {
var component = document.getElementById(entity.id);
}
if (component == undefined) {
events.log.error.publish({text: "CanvasManipulator - setMaterialOfEntities - components for entityIds not found"});
return;
}
setColor(component, '');
setMaterial(component, material);
});
}
function setMaterial(object, material) {
object.setAttribute("material", material);
}
return {
initialize: initialize,
reset: reset,
colors: colors,
changeTransparencyOfEntities: changeTransparencyOfEntities,
resetTransparencyOfEntities: resetTransparencyOfEntities,
startColorAnimationForEntity : startColorAnimationForEntity,
stopColorAnimationForEntity : stopColorAnimationForEntity,
startExpandingAnimationForEntity : startExpandingAnimationForEntity,
stopExpandingAnimationForEntity : stopExpandingAnimationForEntity,
changeColorOfEntities: changeColorOfEntities,
resetColorOfEntities: resetColorOfEntities,
hideEntities: hideEntities,
showEntities: showEntities,
highlightEntities: highlightEntities,
unhighlightEntities: unhighlightEntities,
flyToEntity: flyToEntity,
addElement: addElement,
removeElement: removeElement,
setCenterOfRotation: setCenterOfRotation,
getCenterOfEntity: getCenterOfEntity,
getElementIds: getElementIds,
setMaterialOfEntities: setMaterialOfEntities,
};
})
();