-
Notifications
You must be signed in to change notification settings - Fork 18
/
swirling-circles.js
59 lines (50 loc) · 1.38 KB
/
swirling-circles.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
/*
This is an example of swirling circles clipped by a 1.5 cm margin.
*/
import { PaperSize, Orientation } from 'penplot';
import { polylinesToSVG } from 'penplot/util/svg';
import { clipPolylinesToBox } from 'penplot/util/geom';
export const orientation = Orientation.PORTRAIT;
export const dimensions = PaperSize.LETTER;
export default function createPlot (context, dimensions) {
const [ width, height ] = dimensions;
let lines = [];
// Fill the array of lines
const steps = 256;
const circle = [];
for (let i = 0; i < steps; i++) {
const t = i / Math.max(1, steps - 1);
const radius = 20 * t;
const swirl = 30;
const angle = Math.PI * 2 * t * swirl;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
const cx = width / 2;
const cy = height / 2;
circle.push([ x + cx, y + cy ]);
}
lines.push(circle);
// Clip all the lines to a margin
const margin = 1.5;
const box = [ margin, margin, width - margin, height - margin ];
lines = clipPolylinesToBox(lines, box);
return {
draw,
print,
background: 'white',
animate: false,
clear: true
};
function draw () {
lines.forEach(points => {
context.beginPath();
points.forEach(p => context.lineTo(p[0], p[1]));
context.stroke();
});
}
function print () {
return polylinesToSVG(lines, {
dimensions
});
}
}