-
Notifications
You must be signed in to change notification settings - Fork 1
/
recursive_squares2.js
231 lines (190 loc) · 6.58 KB
/
recursive_squares2.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
// variant of the same name
const canvasSketch = require('canvas-sketch');
const penplot = require('./utils/penplot');
const utils = require('./utils/random');
const poly = require('./utils/poly');
let svgFile = new penplot.SvgFile();
let mainContext = null;
const settings = {
dimensions: 'A3',
orientation: 'portrait',
pixelsPerInch: 300,
scaleToView: true,
units: 'cm',
};
const createRandomDashedLine = (x, y, angle, length, segments, clip) => {
// let q = [[x, y], poly.rotatePoint([x+length, y], [x,y], angle)];
// // console.log(q);
// // console.log(clip);
// let clippedLine;
// try {
// clippedLine = poly.clip(q, clip);
// console.log(clippedLine);
// }
// catch {}
// if (!clippedLine) return;
let polyline = [];
let currentX = x;//clippedLine[0][0],
currentY = y;//clippedLine[0][1];
polyline.push([currentX, currentY]);
for (let index = 0; index < segments; index++) {
let len = utils.random(0.5, (length / segments) * 2);
let point = [currentX + len, currentY];
let rotPoint = poly.rotatePoint(point, [currentX, currentY], angle);
polyline.push(rotPoint);
currentX = rotPoint[0];
currentY = rotPoint[1];
}
//console.log(polyline);
let result = [];
for (let index = 0; index < polyline.length -1; index++) {
let seg = [polyline[index], polyline[index+1]];
if (index % 2 === 0) {
result.push(seg);
}
}
return result;
}
const drawSquare = (origX, origY, side, height = side) => {
let squarePoly = poly.createSquarePolygon(origX, origY, side, height);
poly.drawPolygonOnCanvas(mainContext, squarePoly);
svgFile.addLine(squarePoly, true);
}
const drawTopLeft = (origX, origY, side, options) => {
let x = origX,
y = origY,
invertX = false,
invertY = false;
drawCorner(x, y, side, invertX, invertY, options);
}
const drawTopRight = (origX, origY, side, options) => {
let x = origX + side,
y = origY,
invertX = true,
invertY = false;
drawCorner(x, y, side, invertX, invertY, options);
}
const drawBottomLeft = (origX, origY, side, options) => {
let x = origX,
y = origY + side,
invertX = false,
invertY = true;
drawCorner(x, y, side, invertX, invertY, options);
}
const drawBottomRight = (origX, origY, side, options) => {
let x = origX + side,
y = origY + side,
invertX = true,
invertY = true;
drawCorner(x, y, side, invertX, invertY, options);
}
const drawCorner = (origX, origY, side, invertX, invertY, options) => {
let width = invertX ? -side : side;
let height = invertY ? -side : side;
let levels = 9; // smaller and smaller square
for (let level = 2; level < levels+1; level++) {
let div = Math.pow(2,level);
let w = width / div;
let h = height / div;
let repeatX = div;//utils.getRandomInt(div, 0);
let repeatY = div; //utils.getRandomInt(div, 0);
let totalWeight = 1;
for (let itemX = 1; itemX < repeatX+1; itemX++) {
for (let itemY = 1; itemY < repeatY+1; itemY++) {
totalWeight = totalWeight + (itemX * itemY);
}
}
for (let itemX = 1; itemX < repeatX+1; itemX++) {
for (let itemY = 1; itemY < repeatY+1; itemY++) {
//drawSquare(origX + (w * itemX), origY + (h * itemY), w, h);
let rnd = utils.random(0, totalWeight);
if ((itemX*itemY) - (rnd/(Math.pow(10, level-1))) <= 0) {drawSquare(origX + (w * itemX), origY + (h * itemY), w, h); }
}
}
}
svgFile.newPath();
}
const sketch = (context) => {
poly.init(context);
let margin = 0;
let elementWidth = 14;
let elementHeight = 14;
let columns = 1;
let rows = 2;
let drawingWidth = (columns * (elementWidth + margin)) - margin;
let drawingHeight = (rows * (elementHeight + margin)) - margin;
let marginLeft = (context.width - drawingWidth) / 2;
let marginTop = (context.height - drawingHeight) / 2;
let o = [];
for (let r = 0; r < rows; r++) {
o[r] = [];
for (let i = 0; i < columns; i++) {
let corners = [utils.getRandomInt(2),utils.getRandomInt(2),utils.getRandomInt(2),utils.getRandomInt(2)];//utils.getRandomBitMask(4);
let space = utils.random(0.21,0.27);
let skew1 = utils.random(-0.21,0.17);
let skew2 = utils.random(-0.21,0.17);
o[r].push([corners, space, skew1, skew2]);
}
}
return ({ context, width, height, units }) => {
svgFile = new penplot.SvgFile();
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
context.strokeStyle = 'black';
context.lineWidth = 0.01;
mainContext = context;
let posX = marginLeft;
let posY = marginTop;
// let clip = [[posX, posY],[posX+drawingWidth, posY],[posX+drawingWidth, posY+drawingHeight],[posX, posY+drawingHeight]];
// let a = drawingHeight / 20;
// for (let index = 0; index < 60; index++) {
// let x = 0;
// let y = -drawingHeight + (index * a);
// let dash = createRandomDashedLine(x, y, 50, 40, 20, poly.createSquarePolygon(posX, posY, drawingWidth, drawingHeight));
// dash.forEach(element => {
// try {
// x = poly.clip(element, clip);
// } catch {}
// if (x) {
// x.map(l => {
// poly.drawLineOnCanvas(mainContext, [l[0], l[1]]);
// svgFile.addLine([l[0], l[1]]);
// });
// }
// });
// }
// svgFile.newPath();
for (let row = 0; row < rows-1; row++) {
for (let col = 0; col < columns; col++) {
//drawSquare(posX, posY, elementWidth);
let corners = o[row][col][0];
//corners = [1,0,1,0];//o[row][col][0];
// if (row === 0) { corners[0] = 0; corners[1] = 0; }
// if (row === rows - 1) { corners[2] = 0; corners[3] = 0; }
// if (col === 0) { corners[0] = 0; corners[3] = 0; }
// if (col === columns - 1) { corners[1] = 0; corners[2] = 0; }
if (corners[0]) { drawTopLeft(posX, posY, elementWidth, {}); }
if (corners[1]) { drawTopRight(posX, posY, elementWidth, {}); }
if (corners[2]) { drawBottomRight(posX, posY + elementHeight, elementWidth, {}); }
if (corners[3]) { drawBottomLeft(posX, posY + elementHeight, elementWidth, {}); }
posX = posX + elementWidth + margin;
}
posX = marginLeft;
posY = posY + elementWidth + margin;
}
return [
// Export PNG as first layer
mainContext.canvas,
// Export SVG for pen plotter as second layer
{
data: svgFile.toSvg({
width,
height,
units
}),
extension: '.svg',
}
];
};
};
canvasSketch(sketch, settings);