-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.js
77 lines (70 loc) · 1.87 KB
/
draw.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
allDots = 0
const drawDot = (p) => {
drawDotXY(p.x, p.y)
}
nx = 0
nxs = 0.06
nx2 = 0
nxs2 = 0.05
async function drawDotXY(x, y, opacity = 1) {
if (allDots++ % 20 == 0) await timeout(0)
nx += nxs
nx2 += nxs2
stroke((40+noise(nx2+0.5)*40)/pencilMultiplier, (noise(nx2)*60+100) * opacity)
strokeWeight((2 + noise(nx) * 2) * pixelSize*pencilThickness)
line(x, y, x, y)
if (random() < 0.1) {
const rx = random(-1, 1) * .5
const ry = random(-1, 1) * .5
line(x + rx, y + ry, x + rx, y + ry)
}
}
function fillShape(ps, x = 0, y = 0) {
beginShape()
ps.forEach(p => vertex(p.x + x, p.y + y))
endShape()
}
function drawShape(ps, x = 0, y = 0) {
ps.forEach(p => drawDotXY(p.x + x, p.y + y))
}
function timeout(ms) {
// return waitForKey(32).then(() => new Promise(resolve => setTimeout(resolve, max(ms, 100))))
return new Promise(resolve => setTimeout(resolve, ms));
}
function waitForKey(ms, title) {
return
if (title) console.log(title)
return waitForKeyFunc(32).then(() => new Promise(resolve => setTimeout(resolve, max(ms, 100))))
}
function waitForKeyFunc(key) {
return new Promise(resolve => {
if (keyIsDown(key)) resolve()
else window.addEventListener('keydown', e => {
if (e.keyCode == key) resolve()
})
})
}
function addEffect() {
filter(ERODE)
filter(DILATE)
}
function pathToPoints(path) {
const l = path.length
const ps = []
for (let i = 0; i < l; i++) ps.push(path.getPointAt(i))
return ps
}
async function drawPath(path, opacity = 1) {
if (path.children) {
path.children.forEach(drawPath)
return
}
const ps = pathToPoints(path)
for (const p of ps) await drawDotXY(p.x, p.y, opacity)
}
function fillPath(path,clr) {
if (clr) fill(clr)
noStroke()
const ps = pathToPoints(path)
fillShape(ps)
}