-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-image.js
53 lines (46 loc) · 1.42 KB
/
set-image.js
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
/* global AFRAME */
/**
* Component that listens to an event, fades out an entity, swaps the texture, and fades it
* back in.
*/
AFRAME.registerComponent('set-image', {
schema: {
on: { type: 'string' },
target: { type: 'selector' },
src: { type: 'string' },
dur: { type: 'number', default: 300 }
},
init: function () {
var data = this.data;
var el = this.el;
this.setupFadeAnimation();
el.addEventListener(data.on, function () {
// Fade out image.
data.target.emit('set-image-fade');
// Wait for fade to complete.
setTimeout(function () {
// Set image.
data.target.setAttribute('material', 'src', data.src);
}, data.dur);
});
},
/**
* Setup fade-in + fade-out.
*/
setupFadeAnimation: function () {
var data = this.data;
var targetEl = this.data.target;
// Only set up once.
if (targetEl.dataset.setImageFadeSetup) { return; }
targetEl.dataset.setImageFadeSetup = true;
// Create animation.
targetEl.setAttribute('animation__fade', {
property: 'material.color',
startEvents: 'set-image-fade',
dir: 'alternate',
dur: data.dur,
from: '#FFF',
to: '#000'
});
}
});