-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeish.html
248 lines (228 loc) · 6.41 KB
/
timeish.html
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
<style>
body {
background: black;
color: white;
overflow: hidden;
}
body.nocursor {
cursor: none;
}
#status {
position: absolute;
z-index: 99;
}
canvas {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: 1s;
}
</style>
<body class="nocursor">
<div id="status"></div>
<script>
const SPEED_ADJUST = 0.1
canvas = document.createElement('canvas')
canvas.width = screen.width
canvas.height = screen.height
document.body.appendChild(canvas)
ctx = canvas.getContext('2d')
filters = [ 'source-over', 'difference', 'difference', 'difference', 'color', 'overlay', 'color-dodge', 'overlay', 'overlay', 'multiply' ]
files = [ 'timeish1.png', 'timeish2.png', 'timeish3.png', 'timeish4.png', '#ACC1D9', 'timeish6.png', 'timeish7.png', '#1D3677', 'timeish9.png', 'timeish10.png' ]
var logsize = 10, eventlog = [], eventtimers = []
function log(msg) {
let stat = document.querySelector('#status')
if (msg === null) {
eventlog = []
eventtimers.forEach(t => clearTimeout(t))
stat.innerHTML = ''
} else {
eventlog.push(msg)
stat.innerHTML = eventlog.slice(-logsize).join('<br>')
eventtimers.push(setTimeout(function () {
eventlog.shift()
stat = document.querySelector('#status')
stat.innerHTML = eventlog.slice(-logsize).join('<br>')
}, 5000))
}
}
var speeds = {
stop : function () {
log(null)
log('<b>Stop</b>')
for (let i = 0; i < layers.length-1; i++) {
<!-- for (let i = layers.length-1; i >= 0; i--) { -->
layers[i].twist = 0
layers[i].degree = 0
}
},
default : function () {
log(null)
log('<b>Default twist speeds:</b>')
let twist = 0
for (let i = 0; i < layers.length-1; i++) {
<!-- for (let i = layers.length-1; i >= 0; i--) { -->
if (layers[i].noTwist) continue
layers[i].twist = twist
twist += SPEED_ADJUST/5
log(`- Layer ${i}: ${layers[i].twist.toFixed(2)}`)
}
},
random : function () {
log(null)
log('<b>All random twist speeds:</b>')
for (let i = 0; i < layers.length-1; i++) {
<!-- for (let i = layers.length-1; i >= 0; i--) { -->
if (layers[i].noTwist) continue
layers[i].twist = Math.random()*2-1
log(`- Layer ${i}: ${layers[i].twist.toFixed(2)}`)
}
},
up : function () {
log(null)
log('<b>All speeds up:</b>')
for (let i = 0; i < layers.length-1; i++) {
<!-- for (let i = layers.length-1; i >= 0; i--) { -->
if (layers[i].noTwist) continue
layers[i].twist += SPEED_ADJUST
log(`- Layer ${i}: ${layers[i].twist.toFixed(2)}`)
}
},
down : function () {
log(null)
log('<b>All speeds down:</b>')
for (let i = 0; i < layers.length-1; i++) {
<!-- for (let i = layers.length-1; i >= 0; i--) { -->
if (layers[i].noTwist) continue
layers[i].twist -= SPEED_ADJUST
log(`- Layer ${i}: ${layers[i].twist.toFixed(2)}`)
}
}
}
var cursorTimer
document.body.addEventListener('mousemove', function () {
document.body.classList.remove('nocursor')
clearTimeout(cursorTimer)
cursorTimer = setTimeout(function () {
document.body.classList.add('nocursor')
}, 1000)
})
document.body.addEventListener('keydown', function (event) {
<!-- if (event.repeat) return -->
switch (event.code) {
case 'Backspace':
speeds.default()
break
case 'Escape':
speeds.stop()
break
case 'Space':
speeds.random()
break
default:
if (event.code.indexOf('Digit') === 0) {
let digit = Number(event.code[5])
for (let i=0;i<layers.length-1;i++) {
if (digit == i) {
if (layers[i].noTwist) {
log(`No twist on layer ${i} (${layers[i].file})`)
} else {
let SPDADJ = SPEED_ADJUST
if (event.ctrlKey) SPDADJ/=10
if (event.shiftKey) {
layers[i].twist -= SPDADJ
log(`Layer ${i} speed down: ${layers[i].twist.toFixed(2)} (${layers[i].file})`)
} else {
layers[i].twist += SPDADJ
log(`Layer ${i} speed up: ${layers[i].twist.toFixed(2)} (${layers[i].file})`)
}
}
break
}
}
} else {
if (!event.repeat) console.log(event.code, event)
}
}
})
canvas.addEventListener('mousewheel', function(event) {
if (event.deltaY < 0) { // up
speeds.up()
} else if (event.deltaY > 0) { // down
speeds.down()
}
})
canvas.addEventListener('mousedown', function (event) {
//console.log(event.which)
switch (event.which) {
case 1: // left
speeds.default()
break
case 2: // middle
speeds.random()
event.preventDefault()
break
case 3: // right
}
})
<!-- canvas.addEventListener('contextmenu', function() { event.preventDefault() }) -->
layers = files.slice()
for (idx in layers) {
if (files[idx][0] == '#') {
layers[idx] = document.createElement('canvas')
layers[idx].width = 2000
layers[idx].height = 2000
layers[idx].ctx = layers[idx].getContext('2d')
layers[idx].ctx.fillStyle = files[idx]
layers[idx].ctx.fillRect(0, 0, 2000, 2000)
layers[idx].noTwist = true
layers[idx].isLoaded = true
} else {
layers[idx] = new Image()
layers[idx].isLoaded = false
layers[idx].onload = function () {
this.isLoaded = true
if (layers.every(item => item.isLoaded)) {
let stat = document.querySelector('#status')
clearInterval(statusTimer)
speeds.default()
stat.innerHTML = ''
drawProc()
canvas.style.opacity = '1'
}
}
layers[idx].twist = 0
layers[idx].src = files[idx]
}
layers[idx].file = files[idx]
layers[idx].degree = 0
layers[idx].filter = filters[idx]
}
layers[5].noTwist = true
layers[9].noTwist = true
ctx.textBaseline = 'middle'
ctx.textAlign = 'center'
statusTimer = setInterval(function () {
if (!layers.every(item => item.isLoaded)) {
let statText = 'Loading images:'
layers.forEach(item => {
statText += `<br> · ${item.file} [${item.isLoaded ? 'Loaded' : 'Loading'}]`
})
document.querySelector('#status').innerHTML = statText
}
}, 100)
function drawProc() {
for (idx in layers) {
ctx.globalCompositeOperation = layers[idx].filter
layers[idx].degree+=layers[idx].twist
drawImage(layers[idx], canvas.width/2, canvas.height/2, 1, layers[idx].degree*Math.PI/180)
}
requestAnimationFrame(drawProc)
}
function drawImage(image, x, y, scale, rotation){
ctx.setTransform(scale, 0, 0, scale, x, y) // sets scale and origin
if (!image.noTwist) ctx.rotate(rotation)
ctx.drawImage(image, -image.width / 2, -image.height / 2)
}
</script>