-
Notifications
You must be signed in to change notification settings - Fork 147
/
browser.js
130 lines (107 loc) · 3.2 KB
/
browser.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
require('fastclick')(document.body);
var assign = require('object-assign');
var createConfig = require('./config');
var createRenderer = require('./lib/createRenderer');
var createLoop = require('raf-loop');
var contrast = require('wcag-contrast');
var canvas = document.querySelector('#canvas');
var background = new window.Image();
var context = canvas.getContext('2d');
var loop = createLoop();
var seedContainer = document.querySelector('.seed-container');
var seedText = document.querySelector('.seed-text');
var isIOS = /(iPad|iPhone|iPod)/i.test(navigator.userAgent);
if (isIOS) { // iOS bugs with full screen ...
const fixScroll = () => {
setTimeout(() => {
window.scrollTo(0, 1);
}, 500);
};
fixScroll();
window.addEventListener('orientationchange', () => {
fixScroll();
}, false);
}
window.addEventListener('resize', resize);
document.body.style.margin = '0';
document.body.style.overflow = 'hidden';
canvas.style.position = 'absolute';
var randomize = (ev) => {
if (ev) ev.preventDefault();
reload(createConfig());
};
randomize();
resize();
const addEvents = (element) => {
element.addEventListener('mousedown', (ev) => {
if (ev.button === 0) {
randomize(ev);
}
});
element.addEventListener('touchstart', randomize);
};
const targets = [ document.querySelector('#fill'), canvas ];
targets.forEach(t => addEvents(t));
function reload (config) {
loop.removeAllListeners('tick');
loop.stop();
var opts = assign({
backgroundImage: background,
context: context
}, config);
var pixelRatio = typeof opts.pixelRatio === 'number' ? opts.pixelRatio : 1;
canvas.width = opts.width * pixelRatio;
canvas.height = opts.height * pixelRatio;
document.body.style.background = opts.palette[0];
seedContainer.style.color = getBestContrast(opts.palette[0], opts.palette.slice(1));
seedText.textContent = opts.seedName;
background.onload = () => {
var renderer = createRenderer(opts);
if (opts.debugLuma) {
renderer.debugLuma();
} else {
renderer.clear();
var stepCount = 0;
loop.on('tick', () => {
renderer.step(opts.interval);
stepCount++;
if (!opts.endlessBrowser && stepCount > opts.steps) {
loop.stop();
}
});
loop.start();
}
};
background.src = config.backgroundSrc;
}
function resize () {
letterbox(canvas, [ window.innerWidth, window.innerHeight ]);
}
function getBestContrast (background, colors) {
var bestContrastIdx = 0;
var bestContrast = 0;
colors.forEach((p, i) => {
var ratio = contrast.hex(background, p);
if (ratio > bestContrast) {
bestContrast = ratio;
bestContrastIdx = i;
}
});
return colors[bestContrastIdx];
}
// resize and reposition canvas to form a letterbox view
function letterbox (element, parent) {
var aspect = element.width / element.height;
var pwidth = parent[0];
var pheight = parent[1];
var width = pwidth;
var height = Math.round(width / aspect);
var y = Math.floor(pheight - height) / 2;
if (isIOS) { // Stupid iOS bug with full screen nav bars
width += 1;
height += 1;
}
element.style.top = y + 'px';
element.style.width = width + 'px';
element.style.height = height + 'px';
}