-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.ink
103 lines (97 loc) · 2.6 KB
/
canvas.ink
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
` stdlib is implied `
f := format
Pi := Math.PI
Tau := 2 * Pi
` constants `
Width := 1000
Height := 1000
` make the canvas `
Canvas := bind(document, 'getElementById')('canvas')
Ctx := bind(Canvas, 'getContext')('2d')
Canvas.width := Width
Canvas.height := Height
` utilities `
sqrt := x => pow(x, 0.5)
distanceSq := (a, b) => pow(a.0 - b.0, 2) + pow(a.1 - b.1, 2)
distance := (a, b) => sqrt(distanceSq(a, b))
rgb := (r, g, b) => f('rgb({{0}}, {{1}}, {{2}})', [r * 255.99, g * 255.99, b * 255.99])
rgba := (r, g, b, a) => f('rgb({{0}}, {{1}}, {{2}}, {{3}})'
[r * 255.99, g * 255.99, b * 255.99, a])
Black := rgb(0, 0, 0)
White := 'rgb(255, 255, 255)'
coinflip := () => rand() > 0.5
randCenterBias := (min, max, resolution) => (
` random center-biased distribution `
parts := map(
range(0, resolution, 1)
() => rand() / resolution
)
min + reduce(parts, (a, b) => a + b, 0) * (max - min)
)
randRange := (min, max) => min + rand() * (max - min)
randInt := (min, max) => floor(randRange(min, max))
randColor := () => rgb(rand(), rand(), rand())
randColorAlpha := () => rgba(rand(), rand(), rand(), rand())
randColorGreyscale := () => (
r := rand()
rgb(r, r, r)
)
randColorGreyscaleAlpha := () => (
r := rand()
rgba(r, r, r, rand())
)
choose := list => list.floor(randRange(0, len(list)))
` canvas state functions `
setFill := color => Ctx.fillStyle := color
setLineWidth := width => Ctx.lineWidth := width
setStroke := color => Ctx.strokeStyle := color
` canvas draw functions `
fillRect := bind(Ctx, 'fillRect')
strokeRect := bind(Ctx, 'strokeRect')
clearRect := bind(Ctx, 'clearRect')
beginPath := bind(Ctx, 'beginPath')
moveTo := bind(Ctx, 'moveTo')
lineTo := bind(Ctx, 'lineTo')
arc := bind(Ctx, 'arc')
stroke := bind(Ctx, 'stroke')
fill := bind(Ctx, 'fill')
rotate := bind(Ctx, 'rotate')
translate := bind(Ctx, 'translate')
` drawing lines `
drawLine := (start, end) => (
beginPath()
moveTo(start.0, start.1)
lineTo(end.0, end.1)
stroke()
)
drawSinglePath := points => (
beginPath()
start := points.0
moveTo(start.0, start.1)
each(
slice(points, 1, len(points))
next => lineTo(next.0, next.1)
)
stroke()
)
drawPaths := points => reduce(slice(points, 1, len(points)), (last, next) => (
drawLine(last, next)
next
), points.0)
strokeArc := (x, y, r, start, end) => (
beginPath()
arc(x, y, r, start, end)
stroke()
)
fillArc := (x, y, r, start, end) => (
beginPath()
arc(x, y, r, start, end)
fill()
)
strokeCircle := (x, y, r) => strokeArc(x, y, r, 0, Tau)
fillCircle := (x, y, r) => fillArc(x, y, r, 0, Tau)
rotateCanvas := angle => (
translate(Width / 2, Height / 2)
rotate(angle)
translate(~Width / 2, ~Height / 2)
)