-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsketch.js
More file actions
executable file
·243 lines (213 loc) · 6.24 KB
/
sketch.js
File metadata and controls
executable file
·243 lines (213 loc) · 6.24 KB
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
235
236
237
238
239
240
241
242
243
// plotSvg_hatched_shapes Hatched Shapes Example
// Requires https://cdn.jsdelivr.net/npm/p5.plotsvg@latest/lib/p5.plotSvg.js
// Golan Levin, December 2024
//
// This sketch presents a hack for hatching SVG shapes.
// Note: This method uses pixel analysis and is resolution-dependent.
//
// Click mouse or press ' ' to get a new composition
// Press 'd' to toggle debug view.
// Press 's' to export SVG.
let bDoExportSvg = false;
let bShowDebug = false;
let HATCH_INTERVAL = 3; // the hatch spacing
let HATCH_ANGLE = 0;
let W, H;
let shapes = [];
let hatchBuffer;
let hatchLines;
let exportCount = 0;
let myRandomSeed = 12345678;
p5.disableFriendlyErrors = true;
//======================================
function setup() {
createCanvas(6 * 96, 4 * 96); // Postcard, 6"x4" @96dpi
W = width;
H = height;
hatchBuffer = createGraphics(W*2, H*2, P2D);
hatchBuffer.pixelDensity(1);
hatchLines = [];
makeThreeNewShapes();
// Set values for our SVG export:
setSvgCoordinatePrecision(4);
setSvgIndent(SVG_INDENT_SPACES, 2);
setSvgDefaultStrokeColor('black');
setSvgDefaultStrokeWeight(1);
}
//======================================
function makeFilledShape() {
// Note: shapes must not overlap edge of canvas
// or else significant extra effort must be made.
let pointsX = [];
let pointsY = [];
let nPts = int(round(random(5, 8)));
let cx = random(0.25, 0.75) * W;
let cy = random(0.25, 0.75) * H;
for (let i = 0; i < nPts; i++) {
let t = map(i, 0, nPts, 0, TWO_PI);
let rx = random(0.10, 0.25) * W;
let ry = random(0.10, 0.25) * H;
let px = cx + rx * cos(t);
let py = cy + ry * sin(t);
pointsX[i] = px;
pointsY[i] = py;
}
shapes.push([pointsX, pointsY]);
}
//======================================
function makeThreeNewShapes(){
randomSeed(myRandomSeed);
shapes = [];
makeFilledShape();
makeFilledShape();
makeFilledShape();
}
//======================================
function draw() {
background(245);
if (bDoExportSvg) {
let svgFilename = "plotSvg_hatched_shapes_" + nf(exportCount,3) + ".svg";
beginRecordSvg(this, svgFilename);
}
stroke(0);
drawShapeOutlines();
drawShapeHatchlines();
if (bDoExportSvg) {
endRecordSvg();
save("plotSvg_hatched_shapes_" + nf(exportCount,3) + ".png");
bDoExportSvg = false;
exportCount++;
}
}
//======================================
function drawShapeOutlines(){
for (let s = 0; s < shapes.length; s++) {
let pointsX = shapes[s][0];
let pointsY = shapes[s][1];
noFill();
stroke(0);
beginShape();
for (let i = 0; i < pointsX.length; i++) {
let px = pointsX[i];
let py = pointsY[i];
vertex(px, py);
}
endShape(CLOSE);
}
}
//======================================
function drawShapeHatchlines(){
for (let s=0; s<shapes.length; s++){
HATCH_ANGLE = mouseX/width + s*radians(60);
computeHatchedShape(s);
beginSvgGroup("hatchlines-for-shape-" + s);
for (let i=0; i<hatchLines.length; i+=2) {
let x1 = hatchLines[i].x;
let y1 = hatchLines[i].y;
let x2 = hatchLines[i+1].x;
let y2 = hatchLines[i+1].y;
line(x1,y1, x2,y2);
}
endSvgGroup();
if (bShowDebug){
// Display the hatching buffers
push();
scale(1/8);
translate(s + s*hatchBuffer.width,0);
image(hatchBuffer,0,0);
pop();
}
}
}
//======================================
function computeHatchedShape(s) {
const cx = hatchBuffer.width / 2;
const cy = hatchBuffer.height / 2;
const hbh = hatchBuffer.height;
const hbw = hatchBuffer.width;
// 1. Draw a rotated version of the input
// graphics into the offscreen buffer.
// Shapes to be hatched should be drawn as
// white shapes on a black background.
let pointsX = shapes[s][0];
let pointsY = shapes[s][1];
if (pointsX.length >= 3) {
hatchBuffer.background(0, 0, 0);
hatchBuffer.fill(255);
hatchBuffer.noStroke();
hatchBuffer.push();
hatchBuffer.translate(cx, cy);
hatchBuffer.rotate(HATCH_ANGLE);
hatchBuffer.translate(-cx, -cy);
hatchBuffer.push();
hatchBuffer.translate(W/2, H/2);
hatchBuffer.beginShape();
for (let i=0; i<pointsX.length; i++){
hatchBuffer.vertex(pointsX[i], pointsY[i]);
}
hatchBuffer.endShape(CLOSE);
hatchBuffer.pop();
hatchBuffer.pop();
}
// 2. Compute hatch lines in the rotated graphics.
hatchLines = [];
hatchBuffer.loadPixels();
for (let y = 0; y < hbh; y += HATCH_INTERVAL) {
let row = y * hbw;
let bActive = false;
let prevR = 0;
for (let x = 0; x < hbw; x++) {
let index = (row + x) * 4;
let currR = hatchBuffer.pixels[index]; // red byte
if (x == hbw - 1) {
if (bActive) {
hatchLines.push(createVector(x, y)); // line end
bActive = false;
}
} else {
if (currR >= 128 && prevR < 128) {
hatchLines.push(createVector(x + 1, y)); // line start
bActive = true;
} else if (currR < 128 && prevR >= 128 && bActive) {
hatchLines.push(createVector(x - 1, y)); // line end
bActive = false;
}
}
prevR = currR;
}
}
// 3. Un-rotate the hatch lines.
for (let i = 0; i < hatchLines.length; i += 2) {
let st = hatchLines[i]; // start
let en = hatchLines[i+1]; // end
let sxo = st.x - cx;
let syo = st.y - cy;
let sxr = sxo * Math.cos(-HATCH_ANGLE) - syo * Math.sin(-HATCH_ANGLE) + cx;
let syr = syo * Math.cos(-HATCH_ANGLE) + sxo * Math.sin(-HATCH_ANGLE) + cy;
let exo = en.x - cx;
let eyo = en.y - cy;
let exr = exo * Math.cos(-HATCH_ANGLE) - eyo * Math.sin(-HATCH_ANGLE) + cx;
let eyr = eyo * Math.cos(-HATCH_ANGLE) + exo * Math.sin(-HATCH_ANGLE) + cy;
hatchLines[i].set(sxr, syr);
hatchLines[i+1].set(exr, eyr);
}
for (let i = 0; i < hatchLines.length; i++) {
let px = hatchLines[i].x - W/2;
let py = hatchLines[i].y - H/2;
hatchLines[i].set(px, py);
}
}
//======================================
function mousePressed(){
myRandomSeed = round(millis());
makeThreeNewShapes();
}
function keyPressed() {
if (key == " ") {
makeThreeNewShapes();
} else if (key == "s") {
bDoExportSvg = true;
} else if (key == "d") {
bShowDebug = !bShowDebug;
}
}