-
Notifications
You must be signed in to change notification settings - Fork 3
/
blot.js
196 lines (178 loc) · 5.95 KB
/
blot.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
import {
createBlots,
drawCloud,
drawBlot,
canvasUpdate,
blotPtsInMesh,
paintPalette
} from '../libraries/blotLibraries.js'
import {
Command,
GUI,
Boolean,
Integer,
String,
Float,
Key,
Control
} from '../libraries/gui/gui.js'
import {
getLargeCanvas,
mod
} from '../libraries/misc.js'
const sketch = (s) => {
let randomColor = false
let palette = []
let touched = 0
let gui
let blotCount = 20
let blotSpread = 100
let blotStrength = 1000
let vectors = false
let paint = 0
let drawPotential = false
let blotPoints, blotPointsArray
let background, canvas
let mesh400
s.setup = () => {
let {
w,
h
} = getLargeCanvas(s, 1600)
let p5canvas = s.createCanvas(w, h)
background = s.createGraphics(p5canvas.width, p5canvas.height)
canvas = s.createGraphics(p5canvas.width, p5canvas.height)
mesh400 = baseEvenMesh(400)
let palette = paintPalette(s)
p5canvas.mousePressed(() => {
gui.update()
if (touched >= 0) {
let ink = s.color(s.random(20))
if (randomColor)
ink = palette[mod(touched - 1, palette
.length)]
background.clear()
drawBlot({
s: s,
canvas: canvas,
background: background,
paint: paint,
drawPotential: drawPotential,
}, s.mouseX, s.mouseY, ink, {
mesh: mesh400,
blotPointsArray: blotPointsArray
}, {
blotCount: blotCount,
blotStrength: blotStrength,
blotSpread: blotSpread,
vectors: vectors,
})
}
touched++
})
gui = createGUI()
gui.toggle()
blotPointsArray = new Array(5000)
blotPoints = 0
}
function baseEvenMesh(spread) {
let pxs = []
for (let j = 0; j < spread; j++) {
for (let i = 0; i < spread; i++) {
let cx = spread / 2.0 - i
let cy = spread / 2.0 - j
if (Math.sqrt(cx * cx + cy * cy) > 200) continue
pxs.push([i, j])
}
}
return pxs
}
function createGUI() {
let info =
`Tap/click on the canvas to trigger an ink blot. It will take a bit (it's expensive to compute)`
let subinfo =
`If on mobile, make sure the canvas <br/>is focused first`
let C = new Key("c", () => {
canvas.clear()
s.clear()
})
let resetCanvas = new Command(C, "clear the canvas")
let S = new Key("s", () => {
canvas.save("img.png")
})
let save = new Command(S, "save the canvas")
let P = new Key("p", () => console.log(performance.getEntriesByType(
"measure")))
let perfLog = new Command(P, "log performance to console")
let cmds = [resetCanvas, save, perfLog]
let incB = new Key(">", () => blotCount++)
let decB = new Key("<", () => blotCount--)
let blotCountInt = new Integer(() => blotCount)
let blotCountControl = new Control([decB, incB],
"+/- blot count", blotCountInt)
let incS = new Key(".", () => {
blotSpread += 5
})
let decS = new Key(",", () => {
blotSpread -= 5
})
let blotSpreadInt = new Integer(() => blotSpread)
let blotSpreadControl = new Control([decS, incS],
"+/- blot spread", blotSpreadInt)
let incI = new Key(")", () => {
blotStrength *= 2
})
let decI = new Key("(", () => {
blotStrength /= 2
})
let blotStrengthFlt = new Float(() => blotStrength)
let blotStrengthControl = new Control([decI, incI],
"+/- blot strength", blotStrengthFlt)
let R = new Key("r", () => randomColor = !randomColor)
let randomColorBool = new Boolean(() => randomColor)
let rndColorControl = new Control([R], "toggle random colors",
randomColorBool)
let T = new Key("t", () => {
paint = mod(paint + 1, 3)
if(paint!=0)drawPotential=true
canvasUpdate({
s: s,
canvas: canvas,
background: background,
paint: paint,
})
})
let backgroundStates = ["solid/none", "solid/solid", "alpha/solid"]
let paintString = new String(() => backgroundStates[paint])
let paintControl = new Control([T],
"Paint/Field", paintString)
let V = new Key("v", () => vectors = !vectors)
let vectorsBool = new Boolean(() => vectors)
let vectorControl = new Control([V], "show vectors", vectorsBool)
let F = new Key("F", () => drawPotential = !drawPotential)
let potentialBool = new Boolean(() => drawPotential)
let potentialControl = new Control([F], "generate field strength",
potentialBool)
let focusedBool = new Boolean(() => s.focused)
let focusedStatus = new Control(undefined, "canvas focused?",
focusedBool)
let controls = [focusedStatus, paintControl, potentialControl,
rndColorControl, blotCountControl, blotSpreadControl,
blotStrengthControl, vectorControl
]
let gui = new GUI(
"<b>Blot</b>/<a href=\"painting.html\">Painting</a>, RB 2020/05",
info, subinfo, cmds,
controls)
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)