-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
567 lines (459 loc) · 11.1 KB
/
index.js
File metadata and controls
567 lines (459 loc) · 11.1 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import Color from "colorjs.io"
/**
* The average color calculator.
*/
const ColorAverage = new FastAverageColor();
/**
* Listing class.
*/
class Listing {
/**
* Listing constructor.
*/
constructor(position, image) {
this.position = position;
this.image = image;
}
/**
* Returns the position.
*/
getPosition() {
return this.position;
}
/**
* Returns the image resource.
*
* @returns {*}
*/
getImage() {
return this.image;
}
}
/**
* Grid class.
*/
class Grid {
/**
* Grid constructor.
*/
constructor() {
this.width = 3;
this.scale = 50;
this.height = 500;
// Prepare the canvas.
this.element = document.querySelector('#canvas');
this.element.width = this.getWidth() * this.getScale();
this.element.height = this.getHeight() * this.getScale();
this.getContext().imageSmoothingEnabled = false;
// Determine are series.
this.images = [];
this.listings = [];
this.excluded = [];
this.onLoad = () => {
};
}
/**
* Adds an excluded path to avoid re-using.
*
* @param path
* @returns {this}
*/
addExclusion(path) {
this.excluded.push(path);
return this;
}
/**
* Used to register multiple exclusions.
*
* @param paths
* @returns {this}
*/
addExclusions(paths) {
for (let i = 0; i < paths.length; i++) {
this.addExclusion(paths[i])
}
return this;
}
/**
* Returns the excluded paths.
*
* @returns {*}
*/
getExcluded() {
return this.excluded;
}
/**
* Indicates whether or not an image is being excluded.
*
* @param path
* @returns {boolean}
*/
isExcluded(path) {
return this.getExcluded()
.includes(path);
}
/**
* Interpolates images between designated listings.
*/
interpolate() {
let listings = this.getListings().sort((a, b) => (a.getPosition() > b.getPosition()) ? 1 : -1);
// Ignore if we don't have enough data.
if (listings.length <= 1) {
return;
}
// Interpolate between each listing.
for (let i = 1; i < listings.length; i++) {
let current = listings[i];
let previous = listings[i - 1];
// Determine the tween gradient.
let color = new Color(previous.getImage().getHex());
let steps = color.steps(current.getImage().getHex(), {
space: "lch",
outputSpace: "srgb",
steps: current.getPosition() - previous.getPosition()
});
for (let j = 0; j < current.getPosition() - previous.getPosition(); j++) {
if (!j) {
this.addPreview(previous.getImage())
.setIndex(previous.getPosition(), previous.getImage().getHex());
continue;
}
// Find nearest image.
let nearest = this.getNearestImage(steps[j].hex);
this.addExclusion(nearest.getPath())
.addPreview(nearest)
.setIndex(previous.getPosition() + j, nearest.getHex());
}
}
// Handle the ending image.
let latest = listings[listings.length - 1];
this.addPreview(latest.getImage())
.setIndex(latest.getPosition(), latest.getImage().getHex());
}
/**
* Returns the nearest, visually similar image.
*
* @param hex
* @returns {Image}
*/
getNearestImage(hex) {
let minimum = 9999.99;
let nearest = new Image();
let base = new Color(hex);
let images = this.getImages();
for (let i = 0; i < images.length; i++) {
// Ignore if we're excluding the path.
if (this.isExcluded(images[i].getPath())) {
continue;
}
let comparator = new Color(images[i].getHex());
let distance = base.deltaE(comparator, "76");
// Check for a new minimum. If found, replace the nearest calculated.
if (distance < minimum) {
minimum = distance;
nearest = images[i];
}
}
return nearest;
}
/**
* Assigns the onLoad event.
*/
setOnLoad(callback) {
this.onLoad = callback;
}
/**
* Pushes a new preview image.
*
* @param image
* @returns this
*/
addPreview(image) {
let column = document.querySelector('#images');
// The outer element.
let block = document.createElement('div');
block.classList.add('container');
block.style = `background: ${image.getHex()}`;
// The image element.
let preview = document.createElement('img');
preview.src = image.getPath();
// The wrapper element.
let wrapper = document.createElement('div');
wrapper.appendChild(preview);
block.appendChild(wrapper);
column.prepend(block);
return this;
}
/**
* Pings the Grid element to decide whether or not we can call the load event.
*/
update() {
let images = this.getImages();
// Ignore if we don't have any images.
if (!images.length) {
return this.onLoad();
}
for (let i = 0; i < images.length; i++) {
if (images[i].getLoading()) {
return;
}
}
return this.onLoad();
}
/**
* Returns the series of images.
*/
getImages() {
return this.images;
}
/**
* Assigns a new image.
*/
addImage(image) {
this.images.push(image);
}
/**
* Returns the configured width.
*
* @returns {*}
*/
getWidth() {
return this.width;
}
/**
* Returns the configured height.
*
* @returns {*}
*/
getHeight() {
return this.height;
}
/**
* Returns the element.
*
* @returns {*}
*/
getElement() {
return this.element;
}
/**
* Returns the drawing context.
*
* @returns {CanvasRenderingContext2D | WebGLRenderingContext}
*/
getContext() {
return this.getElement().getContext('2d');
}
/**
* Returns the scale.
*/
getScale() {
return this.scale;
}
/**
* Stores a series of listings.
*
* @param listings
* @returns {this}
*/
addListings(listings) {
for (let i = 0; i < listings.length; i++) {
this.addListing(listings[i]);
}
return this;
}
/**
* Registers a new listing.
*
* @returns {this}
*/
addListing(listing) {
this.addExclusion(listing.getImage().getPath());
this.listings.push(listing);
return this;
}
/**
* Assigns the index.
*
* @returns {this}
*/
setIndex(index, color) {
return this.setBlock(
index % this.getWidth(),
Math.floor(index / this.getWidth()),
color
);
}
/**
* Assigns a particular index on the canvas.
*
* @param x
* @param y
* @param color
* @returns {this}
*/
setBlock(x, y, color) {
let context = this.getContext();
context.fillStyle = color;
context.fillRect(
x * this.getScale(),
y * this.getScale(),
this.getScale(), this.getScale()
);
return this;
}
/**
* Returns an image resource by path.
*
* @param path
* @returns {Image}
*/
getImageByPath(path) {
let images = this.getImages();
for (let i = 0; i < images.length; i++) {
if (images[i].getPath() === path) {
return images[i];
}
}
return new Image(0, 0);
}
/**
* Returns the listing series.
*
* @returns {[]}
*/
getListings() {
return this.listings;
}
}
/**
* The color layout.
*/
const Layout = new Grid();
/**
* Image class.
*/
class Image {
/**
* Image constructor.
*
* @param path
*/
constructor(path) {
this.path = path;
this.color = [];
this.isLoading = false;
// Verify that we have a path.
if (!this.path) {
return;
}
// Determine the image color information.
this.isLoading = true;
let image = document.createElement('img');
image.src = this.path;
image.style = 'display: none;';
image.onload = () => {
ColorAverage.getColorAsync(image)
.then(color =>
Layout.addImage(this.setColor(color))
)
.catch(e => {
console.log(e);
});
this.isLoading = false;
Layout.update();
};
// Append the image.
document.body.appendChild(image);
this.element = image;
}
/**
* Returns the hex color.
*
* @returns {*}
*/
getHex() {
return this.getColor().hex;
}
/**
* Returns the image path.
*/
getPath() {
return this.path;
}
/**
* Indicates whether or not the image is loading.
*/
getLoading() {
return this.isLoading;
}
/**
* Returns the internal element.
*
* @returns {*}
*/
getElement() {
return this.element;
}
/**
* Returns the X coordinate.
*
* @returns {*}
*/
getX() {
return this.x;
}
/**
* Returns the Y coordinate.
*
* @returns {*}
*/
getY() {
return this.y;
}
/**
* Assigns the color.
*
* @returns {this}
*/
setColor(color) {
this.color = color;
return this;
}
/**
* Returns the calculated color.
*
* @returns {*}
*/
getColor() {
return this.color;
}
}
(() => {
Layout.setOnLoad(() => {
Layout.addExclusions([
'./images/AAA036.jpg',
'./images/AAA016.jpg',
'./images/DSCF9275.jpg',
'./images/DSCF9498.jpg',
'./images/DSCF9303.jpg',
'./images/DSCF9071.jpg',
'./images/DSCF9318.jpg',
'./images/DSCF9368.jpg',
]).addListings([
new Listing(0, Layout.getImageByPath('./images/DSCF9369.jpg')),
new Listing(10, Layout.getImageByPath('./images/DSCF9435.jpg')),
new Listing(20, Layout.getImageByPath('./images/DSCF9500.jpg')),
]).interpolate();
});
// Load all of the available images.
fetch("/images.json")
.then(response => response.json())
.then(json => {
json.map((path) => {
Layout.addImage(
new Image(`./images/${path}`)
);
})
});
})();