-
Notifications
You must be signed in to change notification settings - Fork 1
/
genart-004.js
142 lines (117 loc) · 3.6 KB
/
genart-004.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
const canvasSketch = require('canvas-sketch');
const { lerp } = require('canvas-sketch-util/math');
const random = require('canvas-sketch-util/random');
//const palettes = require('nice-color-palettes');
const poly = require('./utils/poly.js');
const {SvgFileWithGroups} = require('./utils/penplot');
let svgFile = new SvgFileWithGroups();
random.setSeed(random.getRandomSeed());
console.log(`seed: ${random.getSeed()}`);
// 469789
// 219376
//let palette = random.pick(palettes);
let groups = ['1_group','2_group','3_group','4_group'];
// palette = random.shuffle(palette);
// palette = palette.slice(0, random.rangeFloor(2, palette.length + 1));
// const background = palette.shift();
const settings = {
suffix: random.getSeed(),
dimensions: 'A4',//[ 2048, 2048 ]
orientation: 'portrait',
pixelsPerInch: 300,
//scaleToView: true,
units: 'mm',
};
const sketch = () => {
const countX = 50;
const countY = Math.floor(countX / 21 * 29.7);
const createGrid = () => {
const points = [];
for (let x = 0; x < countX; x++) {
for (let y = 0; y < countY; y++) {
const u = x / (countX - 1);
const v = y / (countY - 1);
const position = [ u, v ];
const radius = random.noise2D(u,v) * 0.035;
points.push({
//color: random.pick(palette), //'black',
radius: Math.abs(radius),
position,
rotation: random.noise2D(u,v),
group: random.pick(groups)
});
}
}
return points;
};
let points = createGrid().filter(() => {
return Math.random() > 0.4;
});
points = random.shuffle(points);
return ({ context, width, height, units }) => {
poly.init(context);
svgFile = new SvgFileWithGroups();
const margin = width * 0.175;
context.fillStyle = 'white';//background;
context.fillRect(0, 0, width, height);
points.forEach(data => {
const {
position,
radius,
//color,
rotation,
group
} = data;
const x = lerp(margin, width - margin, position[0]);
const y = lerp(margin, height - margin, position[1]);
let options = {
lineWidth: 0.1
};
const triangle = () => {
let h = Math.abs(rotation*30),
w1 = Math.abs(rotation*3),
w2 = Math.abs(rotation);
return {line1:[[x-w1/2,y-h/2],[x-w2/2,y+h/2]],
line2:[[x+w2/2,y+h/2],[x+w1/2,y-h/2]],
arc1:[x,y-h/2,w1/2,180,0],
arc2:[x,y+h/2,w2/2,0,180]};
}
const rotate = (line) => {
return poly.rotatePolygon(line, [x,y], radius*width*10);
}
const rotateArc = (arc) => {
let rot = radius*width*10;
let [x1,y1,r,s,e] = arc;
let [rotx1,roty1] = poly.rotatePoint([x1,y1], [x,y], rot);
return [rotx1,roty1,r,s+rot,e+rot];
}
let t = triangle();
let l1 = rotate(t.line1);
let l2 = rotate(t.line2);
let a1 = rotateArc(t.arc1);
let a2 = rotateArc(t.arc2);
poly.drawLineOnCanvas(l1, options);
poly.drawLineOnCanvas(l2, options);
poly.drawArcOnCanvas(...a1, options);
poly.drawArcOnCanvas(...a2, options);
svgFile.addLineToGroup(l1, group);
svgFile.addLineToGroup(l2, group);
svgFile.addArcToGroup(...a1, group);
svgFile.addArcToGroup(...a2, group);
});
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);