-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomAnimator.src.js
171 lines (131 loc) · 3.31 KB
/
domAnimator.src.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
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
/*!
* DomAnimator.js
*
* MIT licensed
* Copyright (C) 2015 Tim Holman, http://tholman.com
*/
/*********************************************
* DomAnimator
*********************************************/
var DomAnimator = (function() {
'use strict';
var currentFrame = 0;
var frames = [];
var nodes = [];
// Chrome console shows new lines, others don't...
// so multiple comments are used on others, to look good.
var multiNode = !!!window.chrome;
var interval = null;
var defaultTime = 500; // ms
var attached = false;
var whiteSpaceString = '\u00A0';
// Soft object augmentation
function extend( target, source ) {
for ( var key in source ) {
if ( !( key in target ) ) {
target[ key ] = source[ key ];
}
}
return target;
}
// Frame passed through as a list []
function parseMultilineFrame( frame ) {
if(multiNode) {
return swapWhitespace(frame);
} else {
return padString(frame.join('\n'));
}
}
// Frame passed through as a string.
function parseSingleLineFrame( frame ) {
if (multiNode) {
return swapWhitespace(frame.split('\n'));
} else {
return padString(frame);
}
}
function swapWhitespace( array ) {
var i = 0;
for( i; i < array.length; i++ ) {
array[i] = array[i].replace(/ /g, whiteSpaceString);
}
return array;
}
function animate( time ) {
// No time set, just use default!
if( !time ) {
time = defaultTime;
}
// No frames
if( frames.length === 0) {
console.log( "I need frames to animate. You can add them with .addFrame( string )" );
return;
}
if( attached === false ) {
attachToDocument();
}
interval = setInterval( function() {
renderFrame();
}, time);
}
function renderFrame() {
var frameData = frames[currentFrame];
if( multiNode ) {
var i = 0;
for( i; i < nodes.length; i++ ) {
nodes[i].nodeValue = frameData[i];
}
} else {
nodes[0].nodeValue = frameData;
}
currentFrame = currentFrame + 1;
if ( currentFrame === frames.length ) {
currentFrame = 0;
}
}
function attachToDocument() {
var head = document.head;
var parent = head.parentNode;
// This assumes you have the same amount of frames in each section.
if ( multiNode ) {
var i = 0;
var totalNodes = frames[0].length;
for( i; i < totalNodes; i++ ) {
var node = document.createComment('');
nodes.push(node);
parent.insertBefore( node, head);
}
} else {
var node = document.createComment('');
nodes.push(node);
parent.insertBefore(node, head);
}
}
function stop() {
clearInterval(interval);
}
function addFrame( frameData ) {
if( !frameData ) {
frameData = 'no frame data';
}
var frameType = typeof(frameData);
// Multi line data.
if( frameType === 'object' ) {
frames.push( parseMultilineFrame(frameData) );
// String data
} else if ( frameType === 'string' ) {
frames.push( parseSingleLineFrame(frameData) );
}
}
function padString(string) {
return "\n" + string + '\n';
}
function main() {
}
return extend( main, {
addFrame: addFrame,
animate: animate,
stop: stop
});
});
module.exports = DomAnimator;