-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
318 lines (291 loc) · 10.6 KB
/
Copy pathbackground.js
File metadata and controls
318 lines (291 loc) · 10.6 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
/* jshint esversion: 9 */
import { getGaussiumWeightMatrix, textureBlur, put } from "./gl.js"
import * as twgl from './twgl-full.module.js'
import { LyricPlayer } from "./lyrics.js"
function random(min, max) {
let span = max - min
let r = Math.random() * span + min
return r
}
function getSpinCenterOfBackground(out = 0) {
let y = random(-out, 1 + out), x = random(-out, 1 + out)
return [x, y]
}
function getSpinCenterOfImage() {
let x = random(0, 1), y = random(0, 1)
return [x, y]
}
function spin(a, s, c) {
/*
@param a: the vector
@param s: sin value of the angle
@param c: cos value of the angle
*/
let x, y
[x, y] = a
return [x * c - y * s, y * c + x * s]
}
class Spinner {
constructor(image, put, gl, part) {
/*
image: ImageData
*/
const w = gl.canvas.width, h = gl.canvas.height
this.image = image
this.put = put
this.part = part
const bgsc = getSpinCenterOfBackground() //The spin center's position on the background
this.bgSpinCenter = [bgsc[0] * w, bgsc[1] * h]
const zoom = w / this.image.width
this.scale = random(zoom * 0.7, zoom * 1.2)
this.srcSize = [image.width * this.scale, image.height * this.scale]
const imgsc = getSpinCenterOfImage() //The spin center's position on this image
this.imgSpinCenter = [imgsc[0] * image.width * this.scale, imgsc[1] * image.height * this.scale]
this.angle = 0
this.v = random(Math.PI * 2 / 800, Math.PI * 2 / 400)
}
move(t, dt) {
this.angle += this.v * dt
}
render() {
this.put(this.angle, this.bgSpinCenter, this.imgSpinCenter, this.part, this.srcSize)
}
}
export class Background {
constructor(image, gl) {
/*
image: imagedata
*/
//const gl = document.getElementById("background").getContext("webgl")
this.gl = gl
this.playing = false
//Cut the image
let ul, ur, bl, br
{
const w = image.width, h = image.height
const canvas = document.createElement("canvas")
canvas.id = "tmp canvas for cutting the alum artwork"
canvas.width = w
canvas.height = h
const ctx = canvas.getContext("2d")
ctx.putImageData(image, 0, 0)
//upper left, upper right, bottom left, bottom right
ul = ctx.getImageData(0, 0, w / 2, h / 2); ur = ctx.getImageData(w / 2, 0, w / 2, h / 2)
bl = ctx.getImageData(0, h / 2, w / 2, h / 2); br = ctx.getImageData(w / 2, h / 2, w / 2, h / 2)
canvas.remove()
}
//Scale for background
{
const ratio = Math.max(gl.drawingBufferWidth / image.width, gl.drawingBufferHeight / image.height)
this.srcSize = [ratio * image.width, ratio * image.height]
}
//WebGL put
const shrink = 20 //the noBlurFb could be smaller
let spinAndPut, noBlurFb
{
const putProgramInfo = twgl.createProgramInfo(gl, [
`
attribute vec2 a_targetPos; //pixel space
attribute vec2 a_srcPos; //clip space
uniform vec2 targetSize;
varying vec2 srcPos; //clip space
void main(){
gl_Position=vec4(a_targetPos/targetSize*2.0-1.0,0,1);
srcPos=a_srcPos;
}
`,
`
precision mediump float;
varying vec2 srcPos; //clip space
uniform sampler2D srcImage;
void main(){
gl_FragColor=texture2D(srcImage,srcPos);
}
`
])
const textures = twgl.createTextures(gl, {
ul: {
src: ul
},
ur: {
src: ur
},
bl: {
src: bl
},
br: {
src: br
},
bg: {
src: image
},
})
const fbs = {
ul: twgl.createFramebufferInfo(gl, [{ attachment: textures.ul }]),
ur: twgl.createFramebufferInfo(gl, [{ attachment: textures.ur }]),
bl: twgl.createFramebufferInfo(gl, [{ attachment: textures.bl }]),
br: twgl.createFramebufferInfo(gl, [{ attachment: textures.br }]),
bg: twgl.createFramebufferInfo(gl, [{ attachment: textures.bg }])
}
noBlurFb = twgl.createFramebufferInfo(gl, [
{
format: gl.RGBA
}
], gl.canvas.width / shrink, gl.canvas.height / shrink)
spinAndPut = function (angle, bgPos, imgPos, part, srcSize) {
const w = srcSize[0], h = srcSize[1], c = Math.cos(angle), s = Math.sin(angle)
const rawPos = [
[-imgPos[0], h - imgPos[1]],
[-imgPos[0], -imgPos[1]],
[w - imgPos[0], h - imgPos[1]],
[w - imgPos[0], -imgPos[1]],
]
let rotatedPos = []
for (let pos of rawPos) {
rotatedPos.push(spin(pos, s, c))
}
let targetPos = []
for (let pos of rotatedPos) {
targetPos.push([pos[0] + bgPos[0], pos[1] + bgPos[1]])
}
let targetPositions = []
for (let pos of targetPos) {
targetPositions.push(pos[0])
targetPositions.push(pos[1])
}
put(gl, targetPositions, fbs[part], noBlurFb)
}
}
this.spinAndPut = spinAndPut
//WebGL blur
let blur
{
const blur_radius = 500 / shrink
const secondary_radius = 10
//FrameBuffer
const blurTmpFb = twgl.createFramebufferInfo(gl, [{ format: gl.RGBA }], gl.canvas.width, gl.canvas.height),
blurTmpShrinkedFb = twgl.createFramebufferInfo(gl, [{ format: gl.RGBA }], gl.canvas.width / shrink, gl.canvas.height / shrink),
blurredShrinkedFb = twgl.createFramebufferInfo(gl, [{ format: gl.RGBA }], gl.canvas.width / shrink, gl.canvas.height / shrink)
//Attributes
const arrays = {
a_position: {
numComponents: 2,
data: [
-1, -1,
-1, 1,
1, -1,
1, 1
],
},
a_textureCoordinate: {
numComponents: 2,
data: [
0, 0,
0, 1,
1, 0,
1, 1
],
}
}
const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays)
const bufferInfos = {
x: bufferInfo,
y: bufferInfo
}
//Textures
const matrix = getGaussiumWeightMatrix(blur_radius)
const textures = twgl.createTextures(gl, {
matrix: {
src: matrix.matrix,
format: gl.LUMINANCE,
width: 2 * blur_radius + 1,
}
})
const secondary_matrix = getGaussiumWeightMatrix(secondary_radius)
const secondary_textures = twgl.createTextures(gl, {
matrix: {
src: secondary_matrix.matrix,
format: gl.LUMINANCE,
width: 2 * secondary_radius + 1,
}
})
//Uniforms
//u_image and u_textureSize will be updated in blur_once
const uniform = {
u_image: 0,
u_textureSize: [0, 0],
matrix: textures.matrix,
matrix_sum: matrix.sum
}
const uniforms = {
x: uniform,
y: uniform
}
const secondary_uniform = {
u_image: 0,
u_textureSize: [0, 0],
matrix: secondary_textures.matrix,
matrix_sum: secondary_matrix.sum
}
const secondary_uniforms = {
x: secondary_uniform,
y: secondary_uniform
}
blur = function () {
textureBlur(gl, noBlurFb, blurTmpShrinkedFb, blurredShrinkedFb, bufferInfos, uniforms, blur_radius)
textureBlur(gl, blurredShrinkedFb, blurTmpFb, null, bufferInfos, secondary_uniforms, secondary_radius)
}
}
this.blur = blur
//Create Spinners
this.spinners = [
new Spinner(ul, this.spinAndPut, gl, 'ul'), new Spinner(ur, this.spinAndPut, gl, 'ur'),
new Spinner(bl, this.spinAndPut, gl, 'bl'), new Spinner(br, this.spinAndPut, gl, 'br'),
]
}
move(t, dt) {
for (let i of this.spinners) {
i.move(t, dt)
}
}
render() {
this.spinAndPut(0, [0, 0], [0, 0], 'bg', this.srcSize)
for (let i of this.spinners) {
i.render()
}
this.blur()
}
play(time = 0) {
/*
param time: start playing from where?
in seconds.
Internally, we use ms.
When passing things to move(), we use seconds, which can have decimal.
*/
this.starttime = (new Date()).getTime() - time * 1000
this.lasttime = 0
this.playing = true
let bg = this
let move_wrapper = function (timestamp) {
let ms = (new Date()).getTime() - bg.starttime,
t = ms / 1000,
dt = t - bg.lasttime
bg.lasttime = t
bg.move(t, dt)
bg.render()
if (bg.playing) {
//window.requestAnimationFrame(move_wrapper)
}
}
//window.requestAnimationFrame(move_wrapper)
//It's not 60 fps on iOS anyway
//So the performance is something Apple can't even tackle with
//So I'm better off not messing with that
const fps = 10
this.intervalId = setInterval(move_wrapper, 1000 / fps)
move_wrapper()
}
}
//background.js uses a custom blur program, because the source image is a texture
//and the target is simply the canvas.
//That's quite different from blur.js:blur which uses ImageData as input/output