-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathpen-plotter-cubic-disarray.js
133 lines (115 loc) · 3.45 KB
/
pen-plotter-cubic-disarray.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
/**
* A Canvas2D + SVG Pen Plotter example of "Cubic Disarray"
* (a recreation of an artwork by Georg Nees in 1968-71).
*
* @author Stephane Tombeur (https://github.com/stombeur)
*/
const canvasSketch = require('canvas-sketch');
const { polylinesToSVG } = require('canvas-sketch-util/penplot');
const lines = [];
const settings = {
dimensions: 'A4',
orientation: 'portrait',
pixelsPerInch: 300,
scaleToView: true,
units: 'cm'
};
// function to generate a random number between min and max
const random = (min, max) => Math.random() * (max - min) + min;
const sketch = context => {
let marginBetweenElements = 0.05;
let elementWidth = 1.5;
let elementHeight = 1.5;
let columns = 8;
let rows = 14;
// position drawing in center of page
let drawingWidth =
columns * (elementWidth + marginBetweenElements) - marginBetweenElements;
let drawingHeight =
rows * (elementHeight + marginBetweenElements) - marginBetweenElements;
let marginPageLeft = (context.width - drawingWidth) / 2;
let marginPageTop = (context.height - drawingHeight) / 2;
let o = [];
for (let r = 0; r < rows; r++) {
o[r] = [];
for (let i = 0; i < columns; i++) {
let angle = 0;
let move = 0;
if (r >= 2) {
angle = random(-r, r); // introduce a random rotation
move = random(0, r * 0.1); // introduce a random movement
}
o[r].push({ angle, move });
}
}
return ({ context, width, height, units }) => {
// white background
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
let posX = marginPageLeft;
let posY = marginPageTop;
for (let r = 0; r < rows; r++) {
for (let i = 0; i < columns; i++) {
drawSquare(
context,
posX,
posY + o[r][i].move,
elementWidth,
o[r][i].angle
);
posX = posX + elementWidth + marginBetweenElements;
}
posX = marginPageLeft;
posY = posY + elementHeight + marginBetweenElements;
}
return [
context.canvas,
{
data: polylinesToSVG(lines, {
width,
height,
units
}),
extension: '.svg'
}
];
};
// rotate [x,y] around the center [cx, cy] with angle in degrees
// and y-axis moving downward
function rotate(cx, cy, x, y, angle) {
if (angle === 0) return [x, y];
var radians = (Math.PI / 180) * angle,
cos = Math.cos(radians),
sin = Math.sin(radians),
nx = cos * (x - cx) - sin * (y - cy) + cx,
ny = cos * (y - cy) + sin * (x - cx) + cy;
return [nx, ny];
}
// draw a square in a single line
// and rotate it if needed
function drawSquare(context, cx, cy, width, angle) {
// calculate rotated coordinates
let xy1 = rotate(cx, cy, cx, cy, angle);
let xy2 = rotate(cx, cy, cx + width, cy, angle);
let xy3 = rotate(cx, cy, cx + width, cy + width, angle);
let xy4 = rotate(cx, cy, cx, cy + width, angle);
context.beginPath();
context.strokeStyle = 'black';
context.lineWidth = 0.02;
context.lineCap = 'square';
context.lineJoin = 'miter';
// draw square on context
context.moveTo(...xy1);
context.lineTo(...xy2);
context.lineTo(...xy3);
context.lineTo(...xy4);
context.lineTo(...xy1);
context.stroke();
// draw square for svg polylines
lines.push([xy1, xy2]);
lines.push([xy2, xy3]);
lines.push([xy3, xy4]);
lines.push([xy4, xy1]);
}
};
canvasSketch(sketch, settings);