-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiczoomer.js
364 lines (314 loc) · 11.1 KB
/
piczoomer.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
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
var picZoomer = function(container, data, colours)
{
var cont = document.getElementById(container);
// Number of blocks to fit in
var blockNum = 100;
// or row count
var rowCount = 7;
// Zoom ratio on hover
var zoomRatio = 3; // How many block area is hover effect
// Loading bar
var barHeight = 2; // Pixels
var barWidth = 0.6; // Percents * 100
var loaderBack = '#000';
var barColor = '#FFF';
// Fade color
var fadeBack = '#000';
var realBlockNum = blockNum;
var blockDim = 50;
var blockCols = 10;
var blockRows = 5;
var xRelat = 0;
var yRelat = 0;
var actData = [];
var imagesLoaded = 0;
var imagesLoader = [];
var ctx;
var mousex = -100;
var mousey = -100;
var currentMainAlpha = 0.01;
var allPics = null;
var activated = [];
var current;
var forceDraw = false;
var waiting;
var label;
var labelFor = 0;
var mainAnimation;
self = this;
this.init = function()
{
cont.innerHTML = '<canvas id="'+container+'_kK"></canvas>';
canvas = document.getElementById(container+"_kK");
var labelNode = document.createElement("div");
cont.appendChild(labelNode).setAttribute("class", "mainLabel");;
label = cont.getElementsByClassName('mainLabel')[0];
ctx = canvas.getContext('2d');
this.resize();
this.setup();
}
this.setup = function()
{
imageLoad = [];
actData = [];
id = 1;
for (i = 0; i < blockRows; i++)
{
actData[i] = []
for (j = 0; j < blockCols; j++)
{
var current = [];
current = data[Math.floor(Math.random() * data.length)];
actData[i][j] = {
id: id++,
row: i,
col: j,
name: current[0],
pic: current[1],
object: current,
posx: blockDim * j + xRelat,
posy: blockDim * i + yRelat,
zoomRatio: 1,
currentZoom: 1,
currentAlpha: 1
}
if (imagesLoader.indexOf(current[1]) == -1)
imagesLoader.push(current[1]);
}
}
/**
* Image preloader
*/
ctx.fillStyle = loaderBack;
ctx.fillRect(0,0, self.w, self.h);
imagesLoader.forEach(function(el) {
imageTemp = new Image;
imageTemp.src = el;
imageTemp.onload = function() {
imagesLoaded++;
ctx.fillStyle = barColor;
ctx.fillRect(
(self.w - (self.w*barWidth)) / 2,
self.h/2-(barHeight/2),
self.w*barWidth*imagesLoaded/imagesLoader.length,
barHeight
);
if (imagesLoaded == imagesLoader.length)
{
actData.forEach(function(rowArr) {
rowArr.forEach(function(col) {
var tempImage = new Image;
tempImage.src = col.pic;
actData[col.row][col.col].image = tempImage
offset = (actData[col.row][col.col].zoomRatio - 1) / 2 * blockDim;
ctx.drawImage(tempImage, col.posx-offset, col.posy-offset, blockDim, blockDim);
offset = (actData[col.row][col.col].currentZoom - 1) / 2 * blockDim;
});
});
imagesLoaded = 0;
allPics = ctx.getImageData(0,0,self.w,self.h);
self.draw();
}
}
});
}
this.draw = function()
{
mainAnimation = requestAnimationFrame(self.draw)
/**
* Define draw loop variables and clear canvas
*/
ctx.fillStyle = fadeBack;
ctx.fillRect(0,0, self.w, self.h);
animateAlpha = false;
/**
* Draw and afterwards redraw background from initial draw
* Saves resources from drawing it every time.
*/
if (!allPics)
{
actData.forEach(function(rowArr) {
rowArr.forEach(function(col) {
offset = (actData[col.row][col.col].zoomRatio - 1) / 2 * blockDim;
ctx.drawImage(col.image, col.posx-offset, col.posy-offset, blockDim, blockDim);
offset = (actData[col.row][col.col].currentZoom - 1) / 2 * blockDim;
});
});
allPics = ctx.getImageData(0,0,self.w,self.h);
} else {
if (
(mousex < self.w && mousex > 0) &&
(mousey < self.h && mousey > 0)
)
{
animateAlpha = true;
} else {
animateAlpha = false;
}
ctx.putImageData( allPics, 0,0);
ctx.globalAlpha = currentMainAlpha;
ctx.fillRect(0,0, self.w, self.h);
ctx.globalAlpha = 1;
}
/**
* Apply transperancy to rectangle if mouse hovers
* canvas to replicate fading effect for images
* Saves resources. A lot.
*/
if (animateAlpha && currentMainAlpha < 0.65)
{
currentMainAlpha = currentMainAlpha * 1.7;
if (currentMainAlpha > 0.65)
currentMainAlpha = 0.65;
} else if (!animateAlpha && currentMainAlpha > 0.01) {
currentMainAlpha = currentMainAlpha * 0.7;
if (currentMainAlpha < 0.01)
currentMainAlpha = 0.01;
}
/**
* Animate zoomRatio for hovered images
*/
if (mousey > 0 && mousey < self.h && mousex > 0 && mousex < self.w)
{
selCol = Math.floor((mousex-xRelat) / blockDim);
selRow = Math.floor((mousey-yRelat) / blockDim);
curr = actData[selRow][selCol];
curr.zoomRatio = zoomRatio;
if (curr.currentZoom < curr.zoomRatio)
curr.currentZoom = curr.currentZoom * 1.13;
if (curr.zoomRatio != 1)
{
tempImage = new Image;
tempImage.src = curr.pic;
positionX = curr.posx - (curr.currentZoom - 1) / 2 * blockDim;
positionY = curr.posy - (curr.currentZoom - 1) / 2 * blockDim;
if (positionX < 0) positionX = 0;
if (positionY < 0) positionY = 0;
if (positionX+(blockDim*curr.currentZoom) > self.w) positionX = self.w-(blockDim*curr.currentZoom);
if (positionY+(blockDim*curr.currentZoom) > self.h) positionY = self.h-(blockDim*curr.currentZoom);
ctx.drawImage(
tempImage,
positionX,
positionY,
blockDim*curr.currentZoom,
blockDim*curr.currentZoom
);
if (labelFor != curr.id && curr.currentZoom > curr.zoomRatio)
{
self.setLabel(positionX, positionY, curr, (blockDim * zoomRatio), true);
labelFor = curr.id;
}
current = curr;
if (activated[curr.row] == undefined)
activated[curr.row] = [];
activated[curr.row][curr.col] = curr;
}
} else {
if (labelFor != 0)
{
self.setLabel(null, null, null);
}
labelFor = 0;
}
/**
* Reset zoom ratio for hovered images
*/
activated.forEach(function(row) {
row.forEach(function(elem) {
if (elem != current)
{
actData[elem.row][elem.col].currentZoom = 1;
actData[elem.row][elem.col].zoomRatio = 1;
index = activated[elem.row].indexOf(elem);
activated[elem.row].splice(index, 1);
}
});
});
}
this.setLabel = function(posx, posy, obj, blockDim, animate)
{
// Can be used with default function, and overridden
if (posx != null)
{
if (animate)
{
$('.mainLabel').fadeIn();
}
$(label).html('<h3>'+obj.object[3]+'</h3><h1>'+obj.name+'</h1><h2>CLASS OF '+obj.object[2]+'</h2>')
positionX = posx+blockDim;
positionY = posy;
if (positionX-blockDim > $('#piczoomer').width()/2)
{
positionX = positionX - blockDim - $('.mainLabel').outerWidth();
}
$('.mainLabel').css('left', positionX+'px');
$('.mainLabel').css('top', positionY+'px');
$('.mainLabel').css('height', blockDim+'px');
} else {
$('.mainLabel').fadeOut();
}
}
this.resize = function()
{
console.log('resize')
this.w = cont.offsetWidth;
this.h = cont.offsetHeight;
/* S = this.w * this.h;
S = S / blockNum;
S = Math.ceil(Math.sqrt(S));
blockDim = S;
*/
blockDim = this.h / rowCount;
blockRows = Math.ceil(this.h / blockDim)
blockCols = Math.ceil(this.w / blockDim)
realBlockNum = blockRows * blockCols;
xRelat = (this.w - (blockCols * blockDim)) / 2;
yRelat = (this.h - (blockRows * blockDim)) / 2;
canvas.width = this.w;
canvas.height = this.h;
}
window.onresize = function() {
clearTimeout(waiting);
window.cancelAnimationFrame(mainAnimation)
waiting = setTimeout(function() {
self.resize();
self.setup();
forceDraw = true;
allPics = false;
}, 200);
}
/**
* Simple cursor tracker
*/
cont.onmousemove = function(e) {
mousex = e.offsetX == undefined ? e.layerX : e.offsetX;
mousey = e.offsetY == undefined ? e.layerY : e.offsetY;
};
cont.onmouseout = function(e) {
mousex = -100;
mousey = -100;
}
}
/**
* Correct way of calling animation, so they dont crash on mobile devices
*/
var lastTime = 0;
var vendors = ['webkit', 'moz'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};