-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__empty-grid-starter.js
84 lines (68 loc) · 1.8 KB
/
__empty-grid-starter.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
// starter file with grid
const canvasSketch = require('canvas-sketch');
const penplot = require('./penplot');
const utils = require('./utils');
const poly = require('./poly');
let svgFile = new penplot.SvgFile();
// grid settings
let margin = 0.1;
let elementWidth = 2;
let elementHeight = 2;
let columns = 4;
let rows = 4;
const settings = {
dimensions: 'A3',
orientation: 'portrait',
pixelsPerInch: 300,
scaleToView: true,
units: 'cm',
};
const sketch = (context) => {
let drawingWidth = (columns * (elementWidth + margin)) - margin;
let drawingHeight = (rows * (elementHeight + margin)) - margin;
let marginLeft = (context.width - drawingWidth) / 2;
let marginTop = (context.height - drawingHeight) / 2;
// randomness
let o = [];
for (let r = 0; r < rows; r++) {
o[r] = [];
for (let c = 0; c < columns; c++) {
let rnd = utils.getRandomInt(3,0);
o[r].push(rnd);
}
}
return ({ context, width, height, units }) => {
svgFile = new penplot.SvgFile();
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
context.strokeStyle = 'black';
context.lineWidth = 0.02;
// grid repeat starts here
let posX = marginLeft;
let posY = marginTop;
for (let r = 0; r < rows; r++) {
for (let i = 0; i < columns; i++) {
//draw element here
//advance grid
posX = posX + (elementWidth) + margin;
}
//advance
posX = marginLeft;
posY = posY + elementHeight + margin;
}
return [
// Export PNG as first layer
context.canvas,
// Export SVG for pen plotter as second layer
{
data: svgFile.toSvg({
width,
height,
units
}),
extension: '.svg',
}
];
};
};
canvasSketch(sketch, settings);