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
|
import {
browser, requestAudioContext,
randint, randrange, clamp, mod,
} from './lib/util'
import './lib/vendor/hidpi-canvas'
import mouse from './lib/mouse'
import color from './lib/color'
let w, h
let rx, ry
const pixels_per_second = 512 // 1024
const canvas = document.createElement('canvas')
// document.body.appendChild(canvas)
// document.body.addEventListener('resize', resize)
resize()
recenter()
requestAnimationFrame(animate)
// must request context after resizing
const ctx = canvas.getContext('2d')
const scratch = document.createElement('canvas')
const scratchCtx = scratch.getContext('2d-lodpi')
function resize(ww, hh){
w = canvas.width = ww || window.innerWidth
h = canvas.height = hh || window.innerHeight
canvas.style.width = w + 'px'
canvas.style.height = h + 'px'
}
function recenter(){
rx = randint(w), ry = randint(h)
}
let frame = null
function onFrame(fn){
frame = fn
}
function animate(t){
requestAnimationFrame(animate)
if (frame) {
frame(t)
frame = null
}
// ctx.save()
// ctx.globalAlpha = 0.0001
// ctx.translate(w/2, h/2)
// ctx.rotate(0.1)
// ctx.translate(-rx, -ry)
// ctx.drawImage(canvas, 0, 0)
// ctx.restore()
}
function clear(n, x, y, ww, hh){
ctx.fillStyle = 'rgba(255,255,255,' + (n || 0.9) + ')'
ctx.fillRect(x || 0, y || 0, ww || w, hh || h)
recenter()
}
function triangle(px,py,r){
setTimeout( () => tri(px,py,r), Math.random()*10)
// setTimeout( () => tri(px,py,r), Math.random()*200)
// setTimeout( () => tri(px,py,r), Math.random()*300)
}
function tri(px, py, r) {
ctx.save()
ctx.globalCompositeOperation = 'multiply'
ctx.fillStyle = color.color((px+py)/(w+h), 0, 1, 0.2)
function p(){
let theta = randrange(0, Math.PI*2)
let x = px + Math.cos(theta) * r
let y = py + Math.sin(theta) * r
return { x, y }
}
ctx.beginPath()
const p0 = p(), p1 = p(), p2 = p()
ctx.moveTo(p0.x, p0.y)
ctx.lineTo(p1.x, p1.y)
ctx.lineTo(p2.x, p2.y)
ctx.lineTo(p0.x, p0.y)
ctx.fill()
ctx.restore()
}
function line(y){
ctx.beginPath()
ctx.moveTo(0, y)
ctx.lineTo(w, y)
ctx.strokeStyle = "#888"
ctx.strokeWidth = 1
ctx.stroke()
}
function dot(x, y, r){
ctx.fillStyle = "#f00"
ctx.beginPath()
ctx.moveTo(x, y)
ctx.arc(x, y, r, 0, 2*Math.PI)
ctx.fill()
}
function waveform(pcm, sr, pos, zoom){
sr = sr || 44100
pos = pos || 0
var width = w
var height = Math.floor(h/4)
var half_height = Math.floor(height/2)
var x0 = 0
var y0 = 20
var ymid = y0 + half_height
var max_width_in_seconds = width / pixels_per_second
var max_width_in_samples = max_width_in_seconds * sr
var pcm_length = pcm.length
var len = Math.min(pcm_length, max_width_in_samples)
var pcm_step = sr / pixels_per_second
var i
ctx.save()
clear(1, x0, y0, width, height)
line(ymid)
ctx.beginPath()
for (i = 0; i < width; i += 0.5) {
var si = Math.floor(pcm_step * i + pos)
if (si > pcm_length) break
var val = pcm[si] // -1, 1
// ctx.moveTo(x0 + i, ymid)
ctx.lineTo(x0 + i, ymid + val * half_height)
}
ctx.strokeStyle = "rgba(250,20,0,0.9)"
ctx.strokeWidth = 1
ctx.stroke()
ctx.restore()
}
export default {
canvas, ctx, onFrame, resize,
triangle, clear, line, dot,
waveform, spectrum, raw_spectrum,
}
|