-
Notifications
You must be signed in to change notification settings - Fork 1
/
timemachine.js
283 lines (254 loc) · 10.9 KB
/
timemachine.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
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
/**
* Original Timemachine with DOM Elements by:
*
* Copyright (c) 2008 Kristian Thornley (thornleyk@cpit.ac.nz)
* Licensed under a:
* Creative Commons Attribution 3.0 New Zealand License { http://creativecommons.org/licenses/by/3.0/nz/ }.
*
* -------------
*
* Major rewrite of the Original Timemachine with Canvas
* - now modularized
* - contains image preloading
* Copyright (c) 2009 Stefan Wehrmeyer (stefanwehrmeyer.com)
* Creative Commons Attribution 3.0 Germany License { http://creativecommons.org/licenses/by/3.0/de/ }.
*/
var TimeMachine = (function(){
var tmachine = {};
var preloadImages = function(imgs, cllbck, tobj){
var images = [],
loadCount = 0,
failCount = 0,
successCount = 0;
var preloadImage = function(url, index){
var img = new Image(),
check = function(){
if (successCount + failCount >= loadCount){
cllbck(images, successCount, failCount);
}
},
fail = function(){
failCount += 1;
check();
},
success = function(){
images[index] = img;
successCount += 1;
check();
};
img.onload = success;
img.onerror = fail;
img.onabort = fail;
img.src = url;
};
// init loadcount before preloading images
for(var i=0;i<imgs.length;i++){
if(typeof(imgs[i]) !== "object"){
loadCount += 1;
}
};
for(var i=0;i<imgs.length;i++){
if(typeof(imgs[i]) == "object"){
// assuming it's an image object
images.push(imgs[i]);
} else{
images.push(null);
preloadImage(imgs[i], i);
}
}
};
var createTimeMachinePanel = function(startId, element, timemachine){
var panel = {},
original_width,
original_height,
width,
height,
top = 0,
opacity = 1,
currentDeltaZ = 0,
widthDelta = 0,
heightDelta = 0,
topDelta = 0;
panel.id = startId;
/* Could be more concise, but now it might make be easier to understand */
if (element.width > timemachine.ctx.canvas.width || element.height > timemachine.ctx.canvas.height) {
if (element.width > timemachine.ctx.canvas.width && element.height > timemachine.ctx.canvas.height){
if (element.width < element.height) {
original_width = timemachine.ctx.canvas.width * timemachine.settings.size_ratio;
original_height = element.height / element.width * (timemachine.ctx.canvas.width * timemachine.settings.size_ratio);
} else {
original_height = timemachine.ctx.canvas.height * timemachine.settings.size_ratio;
original_width = element.width / element.height * (timemachine.ctx.canvas.height * timemachine.settings.size_ratio);
}
}
else {
if (element.width > timemachine.ctx.canvas.width){
original_width = timemachine.ctx.canvas.width * timemachine.settings.size_ratio;
original_height = element.height / element.width * (timemachine.ctx.canvas.width * timemachine.settings.size_ratio);
} else {
original_height = timemachine.ctx.canvas.height * timemachine.settings.size_ratio;
original_width = element.width / element.height * (timemachine.ctx.canvas.height * timemachine.settings.size_ratio);
}
}
}
else {
original_width = element.width;
original_height = element.height;
}
top = timemachine.settings.offsetTop * Math.pow(1-timemachine.settings.decay_constant,panel.id);
width = original_width * Math.pow(1-timemachine.settings.decay_constant,panel.id);
height = original_height * Math.pow(1-timemachine.settings.decay_constant,panel.id);
var fadeIn = function(){
if(panel.id == -1){
opacity = (currentDeltaZ / timemachine.settings.delta_z);
}
};
var fadeOut = function(){
if (panel.id == 0 && currentDeltaZ == timemachine.settings.delta_z){
opacity = 0;
}
else if (panel.id == 0){
opacity = (timemachine.settings.delta_z-currentDeltaZ+1)/timemachine.settings.delta_z;
}
};
var display = function(){
if(opacity >= 0) {
timemachine.ctx.globalAlpha = opacity;
timemachine.ctx.fillStyle = "rgba(200, 200, 200, "+(opacity*0.8)+")";
timemachine.ctx.fillRect (timemachine.ctx.canvas.width/2 - width/2, top, width, height);
timemachine.ctx.drawImage(element, timemachine.ctx.canvas.width/2 - width/2, top, width, height);
}
};
panel.expand = function(currentDZ){
currentDeltaZ = currentDZ;
topDelta = timemachine.settings.offsetTop * Math.pow(1-timemachine.settings.decay_constant,(panel.id -1)) - timemachine.settings.offsetTop * Math.pow(1-timemachine.settings.decay_constant,(panel.id));
widthDelta = original_width * Math.pow(1-timemachine.settings.decay_constant,(panel.id -1)) - original_width * Math.pow(1-timemachine.settings.decay_constant,(panel.id));
heightDelta = original_height * Math.pow(1-timemachine.settings.decay_constant,(panel.id -1)) - original_height * Math.pow(1-timemachine.settings.decay_constant,(panel.id));
top += (topDelta/(timemachine.settings.delta_z+1));
width += (widthDelta/(timemachine.settings.delta_z+1));
height += (heightDelta/(timemachine.settings.delta_z+1));
fadeOut();
if(currentDeltaZ == timemachine.settings.delta_z){
panel.id -= 1;
}
if(panel.id == 0){
timemachine.setCurrentPanel(panel);
}
display();
};
panel.shrink = function(currentDZ){
currentDeltaZ = currentDZ;
topDelta = timemachine.settings.offsetTop * Math.pow(1-timemachine.settings.decay_constant,(panel.id )) - timemachine.settings.offsetTop * Math.pow(1-timemachine.settings.decay_constant,(panel.id +1));
widthDelta = original_width * Math.pow(1-timemachine.settings.decay_constant,(panel.id )) - original_width * Math.pow(1-timemachine.settings.decay_constant,(panel.id +1));
heightDelta = original_height * Math.pow(1-timemachine.settings.decay_constant,(panel.id )) - original_height * Math.pow(1-timemachine.settings.decay_constant,(panel.id +1 ));
top -= (topDelta/(timemachine.settings.delta_z+1));
width -= (widthDelta/(timemachine.settings.delta_z+1));
height -= (heightDelta/(timemachine.settings.delta_z+1));
fadeIn();
if(currentDeltaZ == timemachine.settings.delta_z){
panel.id += 1;
}
if(panel.id == 0){
timemachine.setCurrentPanel(panel);
}
display();
};
panel.refresh = function(){
display();
};
panel.getTitle = function(){
return element.title;
};
return panel;
};
tmachine.create = function(imagesInput, ctx, userSettings, callback){
var that = {},
images = [],
panels = [],
currentPanel,
currentDeltaZ = 0,
settings = {},
defaultSettings = {
speed: 0, //Delay of the transition
offsetTop: 40, //How far down to start
delta_z: 8, //frame rate of transition
decay_constant: 0.5, //decay (shrink) of the boxes
size_ratio : 0.75
};
userSettings = userSettings || {};
for(var key in defaultSettings){
if(typeof(userSettings[key])!=="undefined"){
settings[key] = userSettings[key];
} else {
settings[key] = defaultSettings[key];
}
}
that.settings = settings;
that.ctx = ctx;
that.registerPanels = function(images){
console.log(images);
for(var i=0;i<images.length;i++){
if (images[i] != null){
panels.push(createTimeMachinePanel(images.length - i - 1, images[i], that));
panels[panels.length-1].refresh();
}
}
that.setCurrentPanel(panels[panels.length-1]);
};
that.move = function(direction, action, callback){
if(currentDeltaZ <= settings.delta_z){
ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
for(i=0;i<panels.length;i++){
panels[i][action](currentDeltaZ);
}
currentDeltaZ++;
setTimeout(function(){
that.move(direction, action, callback);
},settings.speed);
}
else{
currentDeltaZ = 0;
that.refreshControls();
if(callback){
callback();
}
}
};
that.forward = function (callback){
that.move("forward", "shrink", callback);
};
that.backward = function (callback){
that.move("backwards", "expand", callback);
};
that.moveTo = function(index, callback){
var toId = panels[index].id;
if(toId < 0){
//Big move Back
for(r=0;r > toId; r--){
that.forward(callback);
}
}
if(toId > 0){
//Big move Forward
for(r=0;r < toId; r++){
that.backward(callback);
}
}
};
that.setCurrentPanel = function(cPanel){
currentPanel = cPanel;
};
that.refreshControls = function(){
// huh? may be point for a hook
};
var wrapCallback = function(images, successCount, failCount){
if (typeof(callback) !== "undefined"){
callback(that, successCount, failCount);
}
that.registerPanels(images);
};
preloadImages(imagesInput, wrapCallback, that);
return that;
};
return tmachine;
}());