-
Notifications
You must be signed in to change notification settings - Fork 0
/
directionx.js
531 lines (505 loc) · 18.7 KB
/
directionx.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/** @preserve directionX.js (c) 2021 Ogewan, MIT*/
/**
* @suppress {globalThis}
*/
/**
* @function direction
* @param {string|string[]} [input] - An space delimited list of image sources,
* or an array of image sources
* @param {Object} [config] - Configuration options
* @param {number} [config.overwrite] - Integer ID of starting page.
* [1].
* @param {HTMLElement} [config.anchor] - HTMLElement to append the carousel
* to. [document.body]
* @param {string} [config.dir] - The directory containing the images.
* @param {Object} [config.size] - Size options.
* @param {number} [config.size.width] - Configuration options
* @param {number} [config.size.height] - Configuration options
* @param {boolean} [config.size.scaleToWidth] - Configuration options
* @param {boolean} [config.size.scaleToHeight] - Configuration options
* @param {string} [config.dir] - The directory containing the images.
* @param {number} [config.imgprebuffer] - The # of images to preload
* that precede the displayed image. [5]
* @param {number} [config.imgpostbuffer] - The # of images to preload
* that follow the displayed image. [5]
* @param {string} [config.back] - Hexstring for the display canvas's
* color. ["#373737"]
* @param {GPU} [config.gpu] - GPU class constructor [Requires
* GPU.js]
* @param {Function} [config.shader] - Kernel function that manipulates
* pixel data [Requires GPU.js]
* @param {Function} [config.pixelfn] - Function to manually compute
* pixel data if GPU.js is not avaliable.
* [Spinner]
* @param {boolean} [config.hideSpin] - Remove the spinner from the page
* temporarily. [false]
* @param {boolean} [config.disableSpin] - Deactivate the spinner
* forever. [false]
* @param {number} [config.lines] - The # of lines for the spinner.
* [16]
* @param {number} [config.rate] - Speed of spinner relative to refresh
* rate. [1000/30]
* @param {number} [config.diameter] - The diameter of the spinner.
* [250]
* @param {string} [config.loaderback] - Hexstring for the spinner
* background's color. ["#FFF"]
* @param {string} [config.color] - Hexstring for the spinner's color.
* ["#FFF"]
*
*/
// TODO: Shader change mechanism for pixelfn and shader, including
// iimg.shaderTime change when shader is changed
// TODO: Redraw on resize canvas, clear canvas and then draw imageBitmap again
// with scale equaling new canvas size
// TODO: reduce size from 6 to under 5 kb
// only body has resize event; <body onresize="myFunction()">, tie canvas size
// to body size perhaps?
direction = function d(input = [], config = {}) {
// METHODS - private
const spin =
() => {
if (!spinner) return;
layers[0].style.paddingLeft = (layers[1].width - 300) / 2 + 'px';
let rotation =
Math.floor((Date.now() - spinner.str) / (1000) * spinner.lne) /
spinner.lne,
color = spinner.clr;
spinner.ctx.save();
spinner.ctx.clearRect(0, 0, 300, layers[1].height);
spinner.ctx.translate(150, layers[1].height / 2);
spinner.ctx.rotate((Math.PI * 2 * rotation) % (Math.PI * 2));
// spinner.ctx.rotate(rotation);
if (color.length == 3) {
color = `#${color[1]}${color[1]}${color[2]}${color[2]}${color[3]}${
color[3]}`;
} else {
pi(color, 16).toString();
}
for (let i = 0; i < spinner.lne; i++) {
spinner.ctx.beginPath();
spinner.ctx.rotate(Math.PI * 2 / spinner.lne);
spinner.ctx.moveTo(spinner.dia / 10, 0);
spinner.ctx.lineTo(spinner.dia / 4, 0);
spinner.ctx.lineWidth = spinner.dia / 30;
spinner.ctx.strokeStyle = color;
spinner.ctx.stroke();
}
spinner.ctx.restore();
if (spinning)
raf(spin);
else
spinner.ctx.clearRect(0, 0, 300, layers[1].height);
},
scrollit =
(to, time) => {
// format inputs
if (to === null || void 0 === to)
to = {x: 0, y: 0};
else if (!isNaN(to))
to = {x: 0, y: to};
// if to is num assume its y
else {
if (to.y === null || void 0 === to.y) to.y = 0;
if (to.x === null || void 0 === to.x) to.x = 0;
}
if (time === null || void 0 === time || time <= 0)
time = 400; // ignore given zero time
// if x or y is less than 0 then go to the bottom
if (to.y < 0)
to.y = window.innerHeight || de.clientHeight || db.clientHeight;
if (to.x < 0)
to.x = window.innerWidth || de.clientWidth || db.clientWidth;
// calculate distance needed to travel
let dis = {
x: window.pageXOffset !== void 0 ? to.x - window.pageXOffset :
to.x - de.scrollLeft,
y: window.pageYOffset !== void 0 ? to.y - window.pageYOffset :
to.y - de.scrollTop
};
// console.log("to", to, "dis" ,dis, "(x", window.pageXOffset,
// de.scrollLeft, "| y", window.pageYOffset, de.scrollTop, ")" ,
// time, time/5);
// if that distance is 0 on both x and y, no scrolling required
if (dis == {x: 0, y: 0}) return dis;
let clock = function(c, b, a) {
window.scrollBy(Math.floor(c.x) / b, Math.floor(c.y) / b);
if (a + 1 < b * 5)
scrolling = window.setTimeout(clock, 5, c, b, a + 1);
};
scrolling =
window.setTimeout(clock, 5, dis, Math.floor(time / 5), 0);
// window.clearInterval(clock);
return dis;
},
draw =
img => {
const {width, height} = img;
ctx.clearRect(
0, 0, layers[1].width,
layers[1].height); // clear the canvas based on its size
cb.run('slidn');
if (options.sz) {
// scale to specific dimensions or canvas
const {w, h, s2w, s2h} = options.sz;
let difw = difh = 1;
let setW = (w || layers[1].width);
let setH = (h || layers[1].height);
if (s2w) {
difw = setW / width;
}
if (s2h) {
difh = setH / height;
}
layers[1].width = setW * difh;
layers[1].height = setH * difw;
} else {
// Scale to Image (Display Image at its native res)
layers[1].width = width;
layers[1].height = height;
}
if (spinner) layers[0].height = layers[1].height;
ctx.drawImage(img, 0, 0, ...[layers[1].width, layers[1].height]);
spinning = false;
if (skroll) scrollit();
cb.run('slidd');
},
offload =
img => {
let siz = [img.width, img.height], vimg = iimg[img.virID],
storeImgbit = imgbit => {
if (vimg) vimg.ib = imgbit;
if (img.master) draw(imgbit);
};
if (!img.ui) {
off.clearRect(0, 0, layers[2].width, layers[2].height);
layers[2].width = siz[0];
layers[2].height = siz[1];
off.drawImage(img, 0, 0);
try {
vimg.ui = off.getImageData(0, 0, ...siz);
} catch (e) {
console.log('Cannot get imageData from cross-origin');
let ib = layers[2].transferToImageBitmap();
return storeImgbit(ib);
}
} else {
vimg = img;
}
// Does not work with CORS, limited to Electron for now (new
// BrowserWindow({webPreferences: {webSecurity: false}});)
if (gpu) {
const shader = config.shader ||
function(data) {
var x = this.thread.x;
var y = this.thread.y;
var n =
4 * (x + this.constants.w * (this.constants.h - y));
var red = data[n] / 256;
var green = data[n + 1] / 256;
var blue = data[n + 2] / 256;
var alpha = data[n + 3] / 256;
this.color(red, green, blue, alpha);
},
// the kernel runs for each pixel, with:
// - this.thread.x = horizontal position in pixels from
// the left edge
// - this.thread.y = vertical position in pixels from the
// bottom edge (*opposite of canvas*)
render = gpu.createKernel(shader)
.setConstants({w: siz[0], h: siz[1]})
.setOutput(siz)
.setGraphical(true);
// RESET canvas to deal with bug
render.canvas.width = 1;
render.canvas.height = 1;
render.maxTexSize = [1, 1];
render(vimg.ui.data);
createImageBitmap(render.canvas).then(storeImgbit);
} else {
let pixelfn = config.pixelfn;
createImageBitmap((pixelfn) ? pixelfn(vimg.ui) : vimg.ui)
.then(storeImgbit);
}
},
assign =
idd => {
/**
* iimg = {
* s: (image source url),
* d: (image order: -1 = first, 0 = mid, 1 = last),
* ui: (imageData Uint8ClampedArray),
* ib: (imageBitmap drawn image)
* }
*/
if (!iimg.length) return;
spinning = true;
raf(spin);
cb.run('start');
// clamp between 0 and iimg len
idd = Math.min(Math.max(0, idd), iimg.length - 1);
// if not loaded, clear top layer to show spinner
if (!iimg[idd].ib) {
ctx.clearRect(0, 0, layers[1].width, layers[1].height);
master.virID = idd;
master.src = options.dir + iimg[idd].s;
} else {
if (iimg[idd].st != iimg.shaderTime) {
offload(iimg[idd]);
} else {
draw(iimg[idd].ib);
}
}
current = idd;
let q, r = 0;
for (q = idd - 1; (r < options.irb) && (q >= 0); q--) {
if (iimg[q].ib)
continue;
else if (iimg[q].st != iimg.shaderTime) {
offload(iimg[q]);
continue;
}
setPreload(r, q, options.dir + iimg[q].s);
r++;
}
for (q = idd + 1; (r < options.itb) && (q < iimg.length); q++) {
if (iimg[q].ib)
continue;
else if (iimg[q].st != iimg.shaderTime) {
offload(iimg[q]);
continue;
}
setPreload(r, q, options.dir + iimg[q].s);
r++;
}
},
xtndLmt =
(org, src) => {
// add value from src if its key exists in org
if (!org) return;
for (var key in src)
if (org.hasOwnProperty(key)) org[key] = src[key];
},
setupLoader = count => Array(count).fill(1).map((e, i) => {
let img = new Image();
img.unique = i;
img.virID = -1;
img.addEventListener('load', offload.bind(null, img));
return img;
}),
format_iimg =
raw => {
let st = Date.now(),
form = ((typeof raw == 'string') ? raw.split(' ') : raw)
.map((v, i, a) => ({
s: v,
st: st,
d: (i ? (i == a.length - 1 ? 1 : 0) : -1)
}));
form.shaderTime = st;
return form;
},
setPreload =
(pid, id, src) => {
preload[pid].virID = id;
preload[pid].src = src;
},
jq =
() => {
try {
jQuery.fn.direction = function(a, c) {
return this.each(function() {
c.anchor = $(this);
d(a, c);
});
};
} catch (e) {
console.log(e);
}
},
// GLOBAL shortcuts
du = document, db = du.body, de = du.documentElement, pi = parseInt,
raf = window.requestAnimationFrame, dim = [640, 480];
// PROPERTIES - private
let owrite = config.overwrite || 0, anchor = config.anchor || db,
iimg = format_iimg(input),
// is the spinner spinning?
spinning = true,
// scroll ID
scrolling = -1,
//-1 for unset, corresponds to current page
current = -1, layers = {
0: config.disableSpin ? null : du.createElement('canvas'),
1: du.createElement('canvas'),
2: new OffscreenCanvas(dim[0], dim[1])
},
spinner = layers[0] ? {
ctx: layers[0].getContext('2d'),
clr: config.color || '#373737',
str: Date.now(),
lne: config.lines || 16,
rte: config.rate || 1000 / 30,
dia: config.diameter || 250,
lbk: config.loaderback || '#FFF',
dat: Date.now(),
sav: []
} :
null,
options = {
dir: config.dir || '',
irb: config.imgprebuffer || 5,
itb: config.imgpostbuffer || 5,
bck: config.back || '#FFF',
sz: config.size || 0,
},
// MASTER = directly draws, preload only request image
preload = setupLoader(options.itb + options.irb),
master = setupLoader(1)[0], skroll = true,
ctx = layers[1].getContext('2d'), off = layers[2].getContext('2d'), cb = {
run: a => {
for (var b = 0; b < cb[a].length; b++) {
cb[a][b]();
}
},
start: [],
slidn: [],
slidd: []
},
gpu;
// Library extensions
if (window.jQuery) jq();
// PROPERTIES - public
this.canvi = layers;
this.cb = cb;
// METHODS - public
// TODO: temp patch for shader setup
this.setupShaders = (opts) => {
if (opts.gpu) {
const canvas = new OffscreenCanvas(dim[0], dim[1]),
webGl = canvas.getContext('webgl2', {premultipliedAlpha: false});
gpu = new opts.gpu({canvas, webGl});
}
if (opts.shader) config.shader = opts.shader;
if (opts.pixelfn) config.pixelfn = opts.pixelfn;
}, this.cnl = () => {
// stop scrolling
window.clearTimeout(scrolling);
};
this.swap = (arr, opts, start) => {
iimg = Array.isArray(arr) ? format_iimg(arr) : iimg;
if (opts) {
xtndLmt(spinner, opts);
xtndLmt(options, opts);
}
this.go(start || 0);
};
this.count = () => {
return iimg.length;
};
this.current = () => {
return current;
};
this.callback = (type, callback, index, remove) => {
if (type === null || void 0 === type) return cb.slidn;
var typeMap = {'-1': cb.start, '0': cb.slidn, '1': cb.slidd},
select = typeMap[index || 0];
if (remove) {
return select.splice(index || select.length - 1, 1);
}
if (callback === null || void 0 === callback) {
return index === null || void 0 === index ? select : select[index];
}
if (index === null || void 0 === index) {
select.push(callback);
} else
select[index] = callback;
return 1;
};
this.go = (to) => {
var sre = to === null || void 0 === to ? 0 : pi(to, 10);
// console.log(sre);
sre = isNaN(sre) ? 0 : sre;
assign(Math.floor(Math.max(0, Math.min(iimg.length - 1, sre))));
return sre;
};
this.prev = () => {
var sre = current -
1; // avoids possible race condition, assign loads in new image which
// can call draw which can change self.current before it gets to the
// return call. storing it premptively will preserve the value
if (sre >= 0) assign(sre);
return sre;
};
this.next = () => {
// console.log("Hello");
var sre = current + 1;
if (sre < iimg.length) assign(sre);
return sre;
};
this.frst = () => {
if (current >= 0) assign(0);
return 0;
};
this.last = () => {
assign(iimg.length - 1);
return iimg.length - 1;
};
this.rand = () => {
var sre = Math.floor(Math.random() * (iimg.length - 1));
// console.log(sre);
assign(sre);
return sre;
};
this.data = (to) => {
// returns info about slide
var sre = to === null || void 0 === to ? current : pi(to, 10);
return isNaN(sre) ?
iimg[current] :
iimg[Math.floor(Math.max(0, Math.min(iimg.length - 1, sre)))];
};
this.scroll = (bool) => {
// toggles Auto Scrolling
if (!(bool === null || void 0 === bool)) skroll = bool;
return skroll;
};
this.scrollTo = (to, time) => {
return scrollit(to, time);
}; // public wrapper for scrollit
this.resize = (sz, redraw = true, reset = false) => {
options.sz = reset ? {...sz, w: dim[0], h: dim[1]} : sz;
if (redraw) {
this.go(current);
}
return sz;
};
if (spinner) {
// LOADER - setup
layers[0].height = 480;
layers[0].style.background = spinner.lbk;
layers[0].style.paddingLeft = '170px';
layers[0].style.zIndex = 0;
layers[0].style.position = 'absolute';
anchor.appendChild(layers[0]);
}
if (options.sz) {
options.sz = {
w: options.sz.width,
h: options.sz.height,
s2w: options.sz.scaleToWidth,
s2h: options.sz.scaleToHeight
}
}
// init
master.master = true;
master.unique = -1;
assign(options.startpage || owrite);
// end init
layers[1].height = dim[0];
layers[1].width = dim[1];
layers[1].background = options.bck;
layers[1].style.zIndex = 1;
layers[1].style.position = 'relative';
// layers[1].style.visibility="hidden";
// if (anchor) anchor.appendChild(layers[1]);
// else du.body.appendChild(layers[1]);
anchor.appendChild(layers[1]);
};