-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpunch-it-chewie.js
118 lines (89 loc) · 2.77 KB
/
punch-it-chewie.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
const canvasSketch = require('canvas-sketch');
const { renderGroups, renderPaths, createPath } = require('canvas-sketch-util/penplot');
const postcards = require('./utils/postcards');
const random = require('canvas-sketch-util/random');
const { distanceBetween } = require('./utils/poly');
const poly = require('./utils/poly');
const settings = {
dimensions: 'A4',//[ 2048, 2048 ]
orientation: 'portrait',
pixelsPerInch: 300,
//scaleToView: true,
units: 'mm',
};
const drawArc = (center, radius, sAngle, eAngle) => {
return createPath(ctx => {
let c = {}
if (center.x) { c.x = center.x; c.y = center.y; }
else { c.x = center[0]; c.y = center[1]; }
drawArcOnCanvas(ctx, c.x, c.y, radius, sAngle, eAngle);
});
}
const drawArcOnCanvas = (ctx, cx, cy, radius, sAngle, eAngle) => {
//ctx.beginPath();
ctx.arc(
cx,
cy,
radius,
(Math.PI / 180) * sAngle,
(Math.PI / 180) * eAngle
);
//ctx.stroke();
};
const drawLine = (l) => {
return createPath(ctx => {
drawLineOnCanvas(ctx, l);
});
}
const drawLineOnCanvas = (ctx, line) => {
let x1 = line[0].x || line[0][0],
x2 = line[1].x || line[1][0],
y1 = line[0].y || line[0][1],
y2 = line[1].y || line[1][1];
//console.log({line:[[x1,y1],[x2,y2]]})
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
};
let paths = [];
let background = [];
const sketch = ({ width, height }) => {
return ({ context, width, height, units }) => {
context.fillStyle = 'white';//background;
context.fillRect(0, 0, width, height);
paths = [];
background = [];
const draw = (origin, w, h, options) => {
// let type = options[options.index];
// console.log(type)
let margin = w * 0.09;
//if (type === "single") { margin = w * 0.14; }
let ww = w - (margin*2);
let hh = h - (margin*2);
// draw background
let xmin = origin[0]+margin;
let xmax = origin[0]+ww+margin;
let ymin = origin[1]+margin;
let ymax = origin[1]+hh+margin;
let box = [[xmin,ymin],[xmax,ymin],[xmax,ymax],[xmin,ymax]];
//debugger;
let hatchlines = poly.hyperspacePolygon(box, 2);
hatchlines.forEach(l => {
let nr = random.rangeFloor(4, 17);
let segments = [...new Array(nr)].map(()=> random.range(0.001, 10000));
let dashes = poly.dashLine(l, segments);
let blank = false;
dashes.forEach(d => {
if (!blank) {paths.push(drawLine(d));}
blank = !blank;
});
//paths.push(drawLine(l));
});
};
let options = {1:"few", 2:"more", 3:"more", 4:"few"};
postcards.drawSingle(draw, width, height, options);
return renderGroups([paths, background], {
context, width, height, units
});
};
};
canvasSketch(sketch, settings);