Skip to content

Commit 2e15156

Browse files
committed
ignores and more things
1 parent 92dc465 commit 2e15156

10 files changed

+694
-26
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ node_modules
33
*.log
44
.DS_Store
55
bundle.js
6-
tmp/
6+
tmp/
7+
*.mp4
8+
*.gif

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"babylon": "^6.18.0",
1515
"canvas-sketch": "0.0.25",
1616
"canvas-sketch-util": "^1.2.0",
17+
"color-contrast": "0.0.1",
1718
"delaunay-triangulate": "^1.1.6",
1819
"earcut": "^2.1.3",
1920
"gl-matrix": "^2.8.1",

src/advanced/instructions-1.js

+65-25
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,32 @@ const settings = {
1212

1313
const sketch = ({ width, height }) => {
1414
const margin = 2;
15-
const gridXCount = 2;
15+
const gridMargin = margin;
16+
const gridXCount = 10;
1617
const gridYCount = gridXCount;
18+
const skipEdge = true;
19+
const count = 40;
20+
const randomColors = false;
21+
const splicing = true;
1722

18-
const palette = random.pick(palettes);
23+
// Use a palette inspired by Sol LeWitt
24+
const palette = [ '#ea3f3f', '#76b9ed', '#f2d843' ];
25+
26+
// Or, use a random palette
27+
// const palette = random.shuffle(random.pick(palettes)).slice(0, 3);
1928

2029
// Make a grid of points
2130
const gridPoints = [];
2231
for (let x = 0; x < gridXCount; x++) {
2332
for (let y = 0; y < gridYCount; y++) {
33+
if (skipEdge) {
34+
if (x === 0 || x === gridXCount - 1 || y === 0 || y === gridYCount - 1) continue;
35+
}
2436
const u = gridXCount <= 1 ? 0.5 : x / (gridXCount - 1);
2537
const v = gridYCount <= 1 ? 0.5 : y / (gridYCount - 1);
2638

27-
const px = lerp(margin, width - margin, u);
28-
const py = lerp(margin, height - margin, v);
39+
const px = lerp(gridMargin, width - gridMargin, u);
40+
const py = lerp(gridMargin, height - gridMargin, v);
2941
gridPoints.push([ px, py ]);
3042
}
3143
}
@@ -41,35 +53,64 @@ const sketch = ({ width, height }) => {
4153
return [ px, py ];
4254
});
4355

44-
// Connect a random point along one of the four edges of the grid
45-
// to a random point in the grid
46-
const connect = () => {
47-
const cornerIndex = random.rangeFloor(0, corners.length);
48-
const nextCornerIndex = (cornerIndex + 1) % corners.length;
49-
const edgeStart = corners[cornerIndex];
50-
const edgeEnd = corners[nextCornerIndex];
51-
const t = random.value();
52-
const start = lerpArray(edgeStart, edgeEnd, t);
53-
56+
const connect = (start, color) => {
57+
if (gridPoints.length === 0) return null;
5458
const gridIndex = random.rangeFloor(0, gridPoints.length);
5559
const end = gridPoints[gridIndex];
56-
gridPoints.splice(gridIndex, 1);
60+
if (splicing) gridPoints.splice(gridIndex, 1);
5761

5862
return {
59-
color: palette[cornerIndex % palette.length],
63+
color: randomColors ? random.pick(palette) : color,
6064
path: [ start, end ]
6165
};
6266
};
6367

64-
const maxLines = 30;
65-
const items = [];
66-
for (let i = 0; i < maxLines; i++) {
67-
if (gridPoints.length > 0) {
68-
const item = connect();
69-
items.push(item);
68+
const fromMidpoint = () => {
69+
// Midpoint of line
70+
const t = 0.5;
71+
72+
// Can instead be random along edge
73+
// const t = random.value();
74+
75+
const cornerIndex = random.rangeFloor(0, corners.length);
76+
const nextCornerIndex = (cornerIndex + 1) % corners.length;
77+
const edgeStart = corners[cornerIndex];
78+
const edgeEnd = corners[nextCornerIndex];
79+
return lerpArray(edgeStart, edgeEnd, t);
80+
};
81+
82+
const fromCorner = () => {
83+
const cornerIndex = random.rangeFloor(0, corners.length);
84+
return corners[cornerIndex % corners.length];
85+
};
86+
87+
const fromCenter = () => {
88+
return [ width / 2, height / 2 ];
89+
};
90+
91+
let items = [];
92+
93+
// Gather all types..
94+
const types = [
95+
{ count: count / 2, emitter: fromMidpoint, color: palette[0] },
96+
{ count: count / 2, emitter: fromCorner, color: palette[1] },
97+
{ count: count, emitter: fromCenter, color: palette[2] }
98+
];
99+
100+
// Emit each line type
101+
types.forEach(type => {
102+
for (let i = 0; i < type.count; i++) {
103+
if (gridPoints.length === 0) return;
104+
const start = type.emitter();
105+
items.push(connect(start, type.color));
70106
}
71-
}
107+
})
108+
109+
// Cleanup lines
110+
items = items.filter(Boolean);
111+
items = random.shuffle(items);
72112

113+
// Draw lines
73114
return ({ context, width, height }) => {
74115
context.fillStyle = 'white';
75116
context.fillRect(0, 0, width, height);
@@ -80,9 +121,8 @@ const sketch = ({ width, height }) => {
80121
context.lineTo(point[0], point[1]);
81122
});
82123
context.fillStyle = context.strokeStyle = item.color;
83-
context.lineWidth = 0.15;
124+
context.lineWidth = 0.075;
84125
context.globalAlpha = 1;
85-
context.globalCompositeOperation = 'multiply';
86126
context.lineJoin = 'round';
87127
context.lineCap = 'round';
88128
context.stroke();

src/advanced/instructions-2.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const canvasSketch = require('canvas-sketch');
2+
const random = require('canvas-sketch-util/random');
3+
const { lerp, lerpArray } = require('canvas-sketch-util/math');
4+
const palettes = require('nice-color-palettes');
5+
6+
const settings = {
7+
dimensions: 'A4',
8+
orientation: 'landscape',
9+
units: 'cm',
10+
pixelsPerInch: 300
11+
};
12+
13+
const sketch = ({ width, height }) => {
14+
const margin = 2;
15+
const gridXCount = 5;
16+
const gridYCount = gridXCount;
17+
18+
const palette = random.shuffle(random.pick(palettes)).slice(0, 4);
19+
20+
// Make a grid of points
21+
const gridPoints = [];
22+
for (let x = 0; x < gridXCount; x++) {
23+
for (let y = 0; y < gridYCount; y++) {
24+
const u = gridXCount <= 1 ? 0.5 : x / (gridXCount - 1);
25+
const v = gridYCount <= 1 ? 0.5 : y / (gridYCount - 1);
26+
27+
const px = lerp(margin, width - margin, u);
28+
const py = lerp(margin, height - margin, v);
29+
gridPoints.push([ px, py ]);
30+
}
31+
}
32+
33+
// Make a list of the four "corner" points
34+
const corners = [
35+
[ 0, 0 ], [ 1, 0 ],
36+
[ 1, 1 ], [ 0, 1 ]
37+
].map(uv => {
38+
const [u, v] = uv;
39+
const px = lerp(margin, width - margin, u);
40+
const py = lerp(margin, height - margin, v);
41+
return [ px, py ];
42+
});
43+
44+
// Connect a random point along one of the four edges of the grid
45+
// to a random point in the grid
46+
const connect = () => {
47+
const cornerIndex = random.rangeFloor(0, corners.length);
48+
const nextCornerIndex = (cornerIndex + 1) % corners.length;
49+
const edgeStart = corners[cornerIndex];
50+
const edgeEnd = corners[nextCornerIndex];
51+
const t = random.value();
52+
const start = lerpArray(edgeStart, edgeEnd, t);
53+
54+
const gridIndex = random.rangeFloor(0, gridPoints.length);
55+
const end = gridPoints[gridIndex];
56+
gridPoints.splice(gridIndex, 1);
57+
58+
return {
59+
color: palette[cornerIndex % palette.length],
60+
path: [ start, end ]
61+
};
62+
};
63+
64+
const maxLines = 50;
65+
const items = [];
66+
for (let i = 0; i < maxLines; i++) {
67+
if (gridPoints.length > 0) {
68+
const item = connect();
69+
items.push(item);
70+
}
71+
}
72+
73+
return ({ context, width, height }) => {
74+
context.fillStyle = 'white';
75+
context.fillRect(0, 0, width, height);
76+
77+
items.forEach(item => {
78+
context.beginPath();
79+
item.path.forEach(point => {
80+
context.lineTo(point[0], point[1]);
81+
});
82+
context.fillStyle = context.strokeStyle = item.color;
83+
context.lineWidth = 0.15;
84+
context.globalAlpha = 1;
85+
context.globalCompositeOperation = 'multiply';
86+
context.lineJoin = 'round';
87+
context.lineCap = 'round';
88+
context.stroke();
89+
});
90+
};
91+
};
92+
93+
canvasSketch(sketch, settings);

0 commit comments

Comments
 (0)