-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadmouth1.js
139 lines (113 loc) · 3.41 KB
/
badmouth1.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
131
132
133
134
135
136
137
138
139
const canvasSketch = require('canvas-sketch');
const random = require('canvas-sketch-util/random');
const load = require('load-asset');
random.setSeed('1');
const settings = {
dimensions: [ Math.floor(1600 * 0.4), Math.floor(793 * 0.4) ],
animate: true,
duration: 2,
fps: 24,
};
const createGrid = () => {
const points = [];
const count = 50;
for (let x = 0; x < count; x++) {
for (let y = 0; y < count; y++) {
const u = x / (count - 1);
const v = y / (count - 1);
const size = 0.5 + 0.5 * random.noise2D(u, v, 0.01, 10);
const colorIndex = Math.floor(size * palette.length);
points.push({
position: [ u, v ],
size,
});
}
}
return points;
};
const getChannelData = (imgData, x, y, c) => {
if (x < 0) {
x = imgData.width + x;
}
if (y < 0) {
y = imgData.height + y;
}
const wrappedX = x % imgData.width;
const wrappedY = y % imgData.height;
return imgData.data[(wrappedX + wrappedY * imgData.width) * 4 + c];
};
const setChannelData = (imgData, x, y, c, v) => {
if (x < 0) {
x = imgData.width + x;
}
if (y < 0) {
y = imgData.height + y;
}
const wrappedX = x % imgData.width;
const wrappedY = y % imgData.height;
imgData.data[(wrappedX + wrappedY * imgData.width) * 4 + c] = v;
};
const getR = (imgData, x, y) => getChannelData(imgData, x, y, 0);
const getG = (imgData, x, y) => getChannelData(imgData, x, y, 1);
const getB = (imgData, x, y) => getChannelData(imgData, x, y, 2);
const getA = (imgData, x, y) => getChannelData(imgData, x, y, 3);
const imgCanvas = document.createElement("canvas");
const getImageData = (img, scale = 1) => {
let { width, height } = img;
width = Math.floor(width * scale);
height = Math.floor(height * scale);
imgCanvas.width = width;
imgCanvas.height = height;
let ctx = imgCanvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
return ctx.getImageData(0, 0, width, height);
};
function arcRotation(pixels, {
centerX,
centerY,
innerRadius,
outerRadius,
angle,
origin,
}) {
for (let phi = 0; phi >= -Math.PI * 2; phi -= 0.003) {
for (let r = innerRadius; r <= outerRadius; r++) {
const oldX = Math.floor(centerX + Math.cos(phi) * r);
const oldY = Math.floor(centerY + Math.sin(phi) * r);
const newX = Math.floor(centerX + Math.cos(phi + angle) * r);
const newY = Math.floor(centerY + Math.sin(phi + angle) * r);
for (let c = 0; c <= 3; c++) {
const value = getChannelData(origin, oldX, oldY, c);
setChannelData(pixels, newX, newY, c, value);
}
}
}
}
const loadAndSketch = async ({ context, width, height }) => {
const image = await load('assets/band-2.jpg');
const moveInc = width;
context.drawImage(image, 0, 0, width, height);
const imgData = context.getImageData(0, 0, width, height);
return {
resize({ width, height }) {},
render({ playhead, time }) {
const pixels = context.createImageData(
imgData.width,
imgData.height
);
pixels.data.set(imgData.data);
arcRotation(pixels, {
centerX: Math.round(width * .3),
centerY: Math.round(height * .4),
innerRadius: 0, //Math.round(height * 0.1),
outerRadius: Math.round(height * 0.25),
angle: playhead * 2 * Math.PI,
origin: imgData
});
context.putImageData(pixels, 0, 0);
},
unload() {
},
};
};
canvasSketch(loadAndSketch, settings);