-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiles.js
234 lines (200 loc) · 5.6 KB
/
tiles.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
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
import haltonSeq from './haltonSequence';
import canvasSketch from 'canvas-sketch';
import { lerp, wrap, clamp } from 'canvas-sketch-util/math';
import random from 'canvas-sketch-util/random';
import load from 'load-asset';
function getBaseLog(base, x) {
return Math.log(x) / Math.log(base);
}
const settings = {
dimensions: [ 1024, 1024 ],
// dimensions: [ 608, 1080, ],
// dimensions: [ 1200, 628 ],
// animate: true,
// duration: 15,
};
let seed = random.getRandomSeed();
console.log('seed', seed);
random.setSeed(seed);
function pointOnCirlce(centerX, centerY, radius, angle) {
return [
centerX + Math.cos(angle) * radius,
centerY + Math.sin(angle) * radius
];
}
const C1 = {
guide0: 'hsla(0, 0%, 20%, 0)',
guide1: 'hsla(0, 0%, 80%, 0)',
guide2: 'hsla(0, 0%, 60%, 1)',
guide3: 'hsla(0, 80%, 80%, 1)',
guide4: 'hsla(100, 80%, 80%, 1)',
};
const C2 = {
guide0: 'hsla(0, 0%, 20%, 1)',
guide1: 'hsla(0, 0%, 20%, 1)',
guide2: 'hsla(0, 0%, 20%, 1)',
guide3: 'hsla(0, 0%, 20%, 1)',
guide4: 'hsla(0, 0%, 20%, 1)',
};
const C = C1;
function clipOrRestore(c, p) {
if (p > 0.5) {
c.clip();
c.save();
}
if (p <= 0.5) {
c.restore();
}
}
const sketch = async ({ width, height }) => {
const maxDimension = Math.max(width, height);
const marginX = 0.2 * maxDimension;
const marginY = 0.2 * maxDimension;
function drawTile(c, pos, minX, maxX, minY, maxY) {
c.lineWidth = 0.005 * (maxX - minX);
c.strokeStyle = C.guide1;
c.strokeRect(minX, minY, maxX - minX, maxY - minY);
const centerX = minX + (maxX - minX) / 2;
const centerY = minY + (maxY - minY) / 2;
const radius = Math.min(
(maxX - minX) / 2,
(maxY - minY) / 2
);
c.beginPath();
c.arc(
centerX,
centerY,
radius,
0, 2 * Math.PI
);
c.strokeStyle = C.guide0;
c.stroke();
const foldsCount = 4;
for (let i = 0; i < foldsCount; i++) {
const angle = i * Math.PI / foldsCount;
let x1;
let x2;
let y1;
let y2;
if (angle >= Math.PI / 4 && angle <= 3 * Math.PI / 4) {
x1 = minX;
y1 = centerY + radius / Math.tan(angle + Math.PI);
x2 = maxX;
y2 = centerY - radius / Math.tan(angle + Math.PI);
}
if (angle < Math.PI / 4 || angle > 3 * Math.PI / 4) {
x1 = centerY + radius * Math.tan(angle + Math.PI);
y1 = minY;
x2 = centerY - radius * Math.tan(angle + Math.PI);
y2 = maxY;
}
if (angle === 0) {
x1 = minX + (maxX - minX) / 2;
y1 = minY;
x2 = x1;
y2 = maxY;
}
c.moveTo(x1, y1);
c.lineTo(x2, y2);
// c.lineWidth = 0.0025 * (maxX - minX);
c.strokeStyle = C.guide1;
c.stroke();
}
const cornersX = [ 0, 1, 1, 0 ];
const cornersY = [ 0, 0, 1, 1 ];
// circles from the corners of the square
// for (let i = 0; i < 4; i++) {
// const centerX = lerp(minX, maxX, cornersX[i]);
// const centerY = lerp(minY, maxY, cornersY[i]);
// c.beginPath();
// c.arc(
// centerX,
// centerY,
// radius,
// i * Math.PI / 2,
// (i + 1) * Math.PI / 2
// );
// c.strokeStyle = C.guide3;
// // c.stroke();
// }
// c.closePath();
// c.clip();
const midPointsX = [ 0.5, 1.0, 0.5, 0.0 ];
const midPointsY = [ 0.0, 0.5, 1.0, 0.5 ];
const patternCenterX = minX + (maxX - minX) / 2;
const patternCenterY = minY + (maxY - minY) / 2;
c.beginPath();
for (let i = 0; i < 4; i++) {
const point1X = lerp(minX, maxX, midPointsX[i]);
const point1Y = lerp(minY, maxY, midPointsY[i]);
const point2X = lerp(minX, maxX, midPointsX[(i + 1) % 4]);
const point2Y = lerp(minY, maxY, midPointsY[(i + 1) % 4]);
c.moveTo(point1X, point1Y);
c.quadraticCurveTo(
patternCenterX,
patternCenterY,
point2X,
point2Y
);
c.lineTo(point2X, point2Y);
// c.stroke();
}
// c.closePath();
c.strokeStyle = C.guide3;
c.stroke();
// c.clip();
// circle from the center of the square
for (let i = 0; i < 2; i++) {
const centerX = minX + (maxX - minX) * 0.5;
const centerY = i === 0 ? minY : maxY;
c.beginPath();
c.arc(centerX, centerY, radius, i * Math.PI, (i + 1) * Math.PI);
c.strokeStyle = C.guide3;
c.stroke();
}
for (let i = 0; i < 2; i++) {
const centerY = minY + (maxY - minY) * 0.5;
const centerX = i === 0 ? minX : maxX;
c.beginPath();
c.arc(
centerX,
centerY,
radius,
i * Math.PI - Math.PI / 2,
(i + 1) * Math.PI - Math.PI / 2
);
c.strokeStyle = C.guide3;
c.stroke();
// c.fill();
}
// for (let point of splitFragment(1, 0.5)) {
// c.beginPath();
// const incX = lerp(0, maxX - minX, point);
// c.moveTo(minX + incX, minY);
// c.lineTo(maxX - incX, maxY);
// c.lineWidth = 0.0025 * (maxX - minX);
// c.strokeStyle = C.guide1;
// c.stroke();
// }
// for (let point of splitFragment(1, 0.5)) {
// c.beginPath();
// const incY = lerp(0, maxY - minY, point);
// c.moveTo(minX, minY + incY);
// c.lineTo(maxX, maxY - incY);
// c.lineWidth = 0.0025 * (maxX - minX);
// c.strokeStyle = C.guide1;
// c.stroke();
// }
}
return ({ context, width, height, playhead, }) => {
const c = context;
c.fillStyle = 'white';
c.fillRect(0, 0, width, height);
drawTile(
c, 1,
marginX, width - marginX,
marginY, height - marginY
);
};
};
canvasSketch(sketch, settings);