-
Notifications
You must be signed in to change notification settings - Fork 3
/
watercolour.js
212 lines (191 loc) · 6.16 KB
/
watercolour.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
Command,
GUI,
Float,
Key,
Control
} from '../libraries/gui/gui.js'
import { getLargeCanvas, mod } from '../libraries/misc.js'
// Vaguely based on polygon deformation: https://tylerxhobbs.com/essays/2017/a-generative-approach-to-simulating-watercolor-paints
const sketch = (s) => {
let poly, background, dots;
let cnt = 0;
let dotSize = 150
let alphaMax = 6, alphaMin=0.5
let gui
s.preload = () => {
background = s.loadImage('../resources/wc.jpg');
}
s.setup = () => {
let {w, h} = getLargeCanvas(s, 1600)
s.createCanvas(w, h)
s.frameRate(30)
gui = createGUI()
gui.toggle()
resetCanvas()
}
function resetCanvas() {
cnt = 0
s.clear()
background.resize(s.windowWidth, 0)
s.image(background, 0, 0)
let midX = background.width / 2
let midY = background.height / 2
dots = [{
colour1: s.color(50, 50, s.random(100, 255), s.random(alphaMin,
alphaMax)),
colour2: s.color(50, 50, s.random(100, 255), alphaMin),
poly: polygon(0, 0, dotSize, 17),
pos: [midX - dotSize, midY],
scale: 1
},
{
colour1: s.color(50, s.random(100, 200), 50, s.random(alphaMin,
alphaMin)),
colour2: s.color(20, s.random(100, 150), 20, alphaMax),
poly: polygon(0, 0, dotSize, 17),
pos: [midX, midY],
scale: 1
},
{
colour1: s.color(s.random(100, 255), 50, 50, s.random(alphaMin,
alphaMax)),
colour2: s.color(s.random(100, 255), 50, 50, alphaMin),
poly: polygon(0, 0, dotSize, 17),
pos: [midX + dotSize, midY],
scale: 1
},
{
colour1: s.color(s.random(0, 10), s.random(0, 10), s
.random(0, 10), s.random(alphaMin, alphaMax)),
colour2: s.color(s.random(0, 10), s.random(0, 10), s
.random(0, 10), alphaMin),
poly: polygon(150, 150, 50, 17),
pos: [50, 50],
scale: 1
}
]
s.loop()
}
s.draw = () => {
cnt++
if (cnt > 200) s.noLoop()
drawDots()
}
function drawDots() {
for (let d=0; d< dots.length; d++) {
let dot = dots[d]
s.push()
let {
colour1,
colour2,
poly,
pos,
scale
} = dot
let [x, y] = pos
s.translate(x, y)
for (let i = 0; i < 7; i++) {
poly = subdivide(poly, 10)
}
let lerped = s.lerpColor(colour1, colour2, s.random(cnt /
200.0))
s.fill(lerped)
drawPolygon(poly)
s.pop()
}
}
function drawPolygon(pts) {
s.beginShape();
s.noStroke();
for (let pt of pts) {
let [i, j] = pt
s.curveVertex(s.int(i), s.int(j));
}
s.endShape();
}
function polygon(x, y, radius, npoints) {
var pts = [];
let angle = s.TWO_PI / npoints;
for (let a = 0; a < s.TWO_PI; a += angle) {
let sx = x + s.cos(s.random(a)) * s.random(radius / 4, radius);
let sy = s.random(y / 2, y) + s.sin(s.random(a)) * s.random(
radius / 2, radius);
pts.push([sx, sy])
}
return pts;
}
function randMid(p, q, rnd) {
let midx = p[0] + 0.5 * (q[0] - p[0]);
let midy = p[1] + 0.5 * (q[1] - p[1]);
return [s.randomGaussian(midx, rnd), s.randomGaussian(midy, rnd)]
}
function subdivide(points, noise) {
let nextPoints = []
for (let i = 0; i < points.length - 1; i++) {
let p = points[i];
let q = points[i + 1];
let mid = randMid(p, q, noise)
nextPoints.push(p);
nextPoints.push(mid);
}
let q = points[0];
let p = points[points.length - 1];
let mid = randMid(p, q);
nextPoints.push(p);
nextPoints.push(mid);
nextPoints.push(q);
return nextPoints;
}
function createGUI(){
let info =
"Experiment based on polygon subdivision (see code/README for details)"
let subinfo = "Adjusting alpha levels will redraw the canvas"
let R = new Key("r", () => {
resetCanvas()
})
let resetCanvasCmd = new Command(R, "reset the canvas and repaint")
let S = new Key("s", () => {
s.save("img.png")
})
let saveCmd = new Command(S, "save the canvas")
let incAM = new Key(")", () => {
alphaMax += 0.5
resetCanvas()
})
let decAM = new Key("(", () => {
if (alphaMax >= 0) {
alphaMax -= 0.5
}
resetCanvas()
})
let alphaMaxFloat = new Float(() => alphaMax)
let alphaMaxControl = new Control([decAM, incAM],
"+/- max alpha", alphaMaxFloat)
let incAm = new Key(".", () => {
alphaMin += 0.1
resetCanvas()
})
let decAm = new Key(",", () => {
if (alphaMin >= 0) {
alphaMin -= 0.1
}
resetCanvas()
})
let alphaMinFloat = new Float(() => alphaMin)
let alphaMinControl = new Control([decAm, incAm],
"+/- min alpha", alphaMinFloat)
let gui = new GUI("Watercolour, RB 2020/05", info, subinfo, [resetCanvasCmd, saveCmd],
[alphaMinControl, alphaMaxControl])
let QM = new Key("?", () => gui.toggle())
let hide = new Command(QM, "hide this")
gui.addCmd(hide)
gui.update()
return gui
}
s.keyReleased = () => {
gui.dispatch(s.key)
}
}
p5.disableFriendlyErrors = true
let p5sketch = new p5(sketch)