-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla-raptorize.js
executable file
·71 lines (54 loc) · 1.75 KB
/
vanilla-raptorize.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var Raptorize = (function(extended) {
'use strict';
var body, defaults, options,
audioTemplate, sourceAudioTemplate, imageTemplate,
audio, image;
body = document.body;
options = {};
//--- OPTIONS ---//
defaults = {
audioPath: ['assets/sounds/raptor.mp3', 'assets/sounds/raptor.ogg'],
imagePath: 'assets/images/raptor.png',
className: 'raptor',
animationTime: 2000
};
extend(options, defaults, extended);
//--- SETUP ---//
audioTemplate = document.createElement('audio');
audioTemplate.className = options.className + '-source';
for (var source in options.audioPath) {
sourceAudioTemplate = document.createElement('source');
sourceAudioTemplate.src = options.audioPath[source];
audioTemplate.appendChild(sourceAudioTemplate);
}
imageTemplate = document.createElement('img');
imageTemplate.className = options.className;
imageTemplate.src = options.imagePath;
audio = body.appendChild(audioTemplate);
image = body.appendChild(imageTemplate);
image.style.display = 'none';
//--- THE HILARITY ---//
function go() {
setTimeout(function () {
audio.play();
}, (options.animationTime / 3));
image.style.display = 'block';
image.classList.add(options.className + '-go');
setTimeout(function () {
image.classList.remove(options.className + '-go');
}, options.animationTime);
}
//--- EXTEND (COMMON) ---//
// Use Object.assign() for EcmaScript 6.
function extend(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i]) { continue; }
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) { out[key] = arguments[i][key]; }
}
}
return out;
}
return { go: go }
});