-
Notifications
You must be signed in to change notification settings - Fork 0
/
colour-field-2.js
151 lines (131 loc) · 4.22 KB
/
colour-field-2.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
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Based on animated-grid by Matt DesLauriers (@mattdesl)
* https://github.com/mattdesl/canvas-sketch/blob/master/examples/animated-grid.js
*/
const canvasSketch = require('canvas-sketch');
const lerp = require('lerp');
const SimplexNoise = require('simplex-noise');
const chroma = require('chroma-js');
const { heading, calcVec, normalize } = require('./math');
const clrs = require('./clrs').clrs();
const simplex = new SimplexNoise('81234n32478320');
const settings = {
animate: true,
duration: 8,
// dimensions: [1600, 1600],
dimensions: [1080, 1080],
// dimensions: [1080, 1920],
scaleToView: true,
};
const colourScale = chroma.scale([clrs.ink(), clrs.ink()]).domain([-1, 1]);
canvasSketch(() => {
let z = 0;
const foreground = clrs.ink();
const foreground2 = clrs.ink();
const background = clrs.bg;
return ({ context, frame, width, height, playhead }) => {
context.clearRect(0, 0, width, height);
context.fillStyle = background;
context.fillRect(0, 0, width, height);
const gridSize = 30;
const padding = height * 0.15;
const tileSize = (height - padding * 2) / gridSize;
const length = tileSize * 0.65;
const thickness = tileSize * 0.1;
const time = Math.sin(playhead * 2 * Math.PI);
z = z + 0.01;
// context.save();
// context.translate(width / 2, height / 2);
// context.rotate(Math.PI / 16);
// context.translate(-width / 2, -height / 2);
// for (let x = 0; x < gridSize; x++) {
// for (let y = 0; y < gridSize; y++) {
// // get a 0..1 UV coordinate
// const u = gridSize <= 1 ? 0.5 : x / (gridSize - 1);
// const v = gridSize <= 1 ? 0.5 : y / (gridSize - 1);
// // scale to dimensions with a border padding
// const t = {
// x: lerp(padding, width - padding, u),
// y: lerp(padding, height - padding, v),
// };
// // Draw
// context.save();
// const clr = simplex.noise3D(
// x / (gridSize * 2) + 10000,
// y / (gridSize * 2) + 10000,
// time
// );
// context.fillStyle = foreground; // colourScale(clr);
// const rotation =
// simplex.noise3D(x / gridSize, y / gridSize, time) * Math.PI;
// const l =
// length / 2 +
// (normalize(
// simplex.noise3D(
// x / (gridSize * 2) + 10000,
// y / (gridSize * 2) + 10000,
// time
// ),
// -1,
// 1,
// -0.5,
// 1
// ) *
// length) /
// 2;
// // Rotate in place
// context.translate(t.x, t.y);
// context.rotate(rotation);
// context.translate(-t.x, -t.y);
// // Draw the line
// context.fillRect(t.x, t.y - thickness, l, thickness);
// context.restore();
// }
// }
// context.restore();
for (let x = 0; x < gridSize; x++) {
for (let y = 0; y < gridSize; y++) {
// get a 0..1 UV coordinate
const u = gridSize <= 1 ? 0.5 : x / (gridSize - 1);
const v = gridSize <= 1 ? 0.5 : y / (gridSize - 1);
// scale to dimensions with a border padding
const t = {
x: lerp(padding, width - padding, u),
y: lerp(padding, height - padding, v),
};
// Draw
context.save();
const clr = simplex.noise3D(
x / (gridSize * 2) + 10000,
y / (gridSize * 2) + 10000,
time
);
context.fillStyle = colourScale(clr); //foreground2;
const rotation =
simplex.noise3D(x / gridSize, y / gridSize, time) * Math.PI;
const l =
length / 2 +
(normalize(
simplex.noise3D(
x / (gridSize * 2) + 10000,
y / (gridSize * 2) + 10000,
time
),
-1,
1,
-0.5,
1
) *
length) /
2;
// Rotate in place
context.translate(t.x, t.y);
context.rotate(rotation);
context.translate(-t.x, -t.y);
// Draw the line
context.fillRect(t.x, t.y - thickness, l, thickness);
context.restore();
}
}
};
}, settings);