q5 4.6.6 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/deno.json +1 -1
- package/package.json +2 -3
- package/py.typed +0 -0
- package/q5.js +51 -32
- package/q5.pyi +4435 -0
- package/q5.min.js +0 -10
- package/q5.min.js.map +0 -1
package/q5.pyi
ADDED
|
@@ -0,0 +1,4435 @@
|
|
|
1
|
+
from typing import Any, Callable, Literal
|
|
2
|
+
|
|
3
|
+
class Image: ...
|
|
4
|
+
|
|
5
|
+
# ⭐ core
|
|
6
|
+
|
|
7
|
+
"""⭐
|
|
8
|
+
Welcome to q5's documentation! 🤩
|
|
9
|
+
|
|
10
|
+
First time coding? Check out the q5 Beginner's Brief.
|
|
11
|
+
|
|
12
|
+
On these Learn pages, you can experiment with editing the
|
|
13
|
+
interactive mini examples. Have fun! 😎
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def Canvas(w: float = ..., h: float = ..., opt: dict = ...) -> object:
|
|
19
|
+
"""⭐
|
|
20
|
+
Creates a canvas element, a section of the screen your program
|
|
21
|
+
can draw on.
|
|
22
|
+
|
|
23
|
+
Run this function to start using q5!
|
|
24
|
+
|
|
25
|
+
Note that in this example, the circle is located at position [0, 0], the origin of the canvas.
|
|
26
|
+
|
|
27
|
+
:param w: width or side lengths of the canvas
|
|
28
|
+
:param h: height of the canvas
|
|
29
|
+
:param opt: options
|
|
30
|
+
:returns: canvas element
|
|
31
|
+
|
|
32
|
+
Example::
|
|
33
|
+
|
|
34
|
+
Canvas(200, 100)
|
|
35
|
+
background('silver')
|
|
36
|
+
circle(0, 0, 80)
|
|
37
|
+
"""
|
|
38
|
+
...
|
|
39
|
+
|
|
40
|
+
def draw() -> None:
|
|
41
|
+
"""⭐
|
|
42
|
+
The q5 draw function is run 60 times per second by default.
|
|
43
|
+
|
|
44
|
+
Example::
|
|
45
|
+
|
|
46
|
+
def draw():
|
|
47
|
+
background('silver')
|
|
48
|
+
circle(mouseX, mouseY, 80)
|
|
49
|
+
"""
|
|
50
|
+
...
|
|
51
|
+
|
|
52
|
+
def log(message: Any) -> None:
|
|
53
|
+
"""⭐
|
|
54
|
+
Logs a message to the JavaScript console.
|
|
55
|
+
|
|
56
|
+
To view the console, open your browser's web developer tools
|
|
57
|
+
via the keyboard shortcut Ctrl + Shift + i or command + option + i,
|
|
58
|
+
then click the "Console" tab.
|
|
59
|
+
|
|
60
|
+
Use log when you're curious about what your code is doing!
|
|
61
|
+
|
|
62
|
+
:param message:
|
|
63
|
+
|
|
64
|
+
Example::
|
|
65
|
+
|
|
66
|
+
def draw():
|
|
67
|
+
circle(mouseX, mouseY, 80)
|
|
68
|
+
log('The mouse is at:', mouseX, mouseY)
|
|
69
|
+
"""
|
|
70
|
+
...
|
|
71
|
+
|
|
72
|
+
# 🧑🎨 shapes
|
|
73
|
+
|
|
74
|
+
def circle(x: float, y: float, diameter: float) -> None:
|
|
75
|
+
"""🧑🎨
|
|
76
|
+
Draws a circle.
|
|
77
|
+
|
|
78
|
+
:param x: x-coordinate
|
|
79
|
+
:param y: y-coordinate
|
|
80
|
+
:param diameter: diameter of the circle
|
|
81
|
+
|
|
82
|
+
Example::
|
|
83
|
+
|
|
84
|
+
Canvas(200, 100)
|
|
85
|
+
circle(0, 0, 80)
|
|
86
|
+
"""
|
|
87
|
+
...
|
|
88
|
+
|
|
89
|
+
def ellipse(x: float, y: float, width: float, height: float) -> None:
|
|
90
|
+
"""🧑🎨
|
|
91
|
+
Draws an ellipse.
|
|
92
|
+
|
|
93
|
+
:param x: x-coordinate
|
|
94
|
+
:param y: y-coordinate
|
|
95
|
+
:param width: width of the ellipse
|
|
96
|
+
:param height: height of the ellipse
|
|
97
|
+
|
|
98
|
+
Example::
|
|
99
|
+
|
|
100
|
+
Canvas(200, 100)
|
|
101
|
+
ellipse(0, 0, 160, 80)
|
|
102
|
+
"""
|
|
103
|
+
...
|
|
104
|
+
|
|
105
|
+
def rect(x: float, y: float, w: float, h: float, rounded: float = ...) -> None:
|
|
106
|
+
"""🧑🎨
|
|
107
|
+
Draws a rectangle or a rounded rectangle.
|
|
108
|
+
|
|
109
|
+
Also accepts 8 parameters to specify a
|
|
110
|
+
corner radius for each corner, in the order:
|
|
111
|
+
top-left, top-right, bottom-right, bottom-left.
|
|
112
|
+
|
|
113
|
+
:param x: x-coordinate
|
|
114
|
+
:param y: y-coordinate
|
|
115
|
+
:param w: width of the rectangle
|
|
116
|
+
:param h: height of the rectangle
|
|
117
|
+
:param rounded: radius for all corners
|
|
118
|
+
|
|
119
|
+
Example::
|
|
120
|
+
|
|
121
|
+
Canvas(200)
|
|
122
|
+
background(0.8)
|
|
123
|
+
|
|
124
|
+
rect(-70, -80, 40, 60)
|
|
125
|
+
rect(-20, -30, 40, 60, 10)
|
|
126
|
+
rect(30, 20, 40, 60, 20, 4, 0, 8)
|
|
127
|
+
"""
|
|
128
|
+
...
|
|
129
|
+
|
|
130
|
+
def square(x: float, y: float, size: float, rounded: float = ...) -> None:
|
|
131
|
+
"""🧑🎨
|
|
132
|
+
Draws a square or a rounded square.
|
|
133
|
+
|
|
134
|
+
Also accepts 7 parameters to specify a
|
|
135
|
+
corner radius for each corner, in the order:
|
|
136
|
+
top-left, top-right, bottom-right, bottom-left.
|
|
137
|
+
|
|
138
|
+
:param x: x-coordinate
|
|
139
|
+
:param y: y-coordinate
|
|
140
|
+
:param size: size of the sides of the square
|
|
141
|
+
:param rounded: radius for all corners
|
|
142
|
+
|
|
143
|
+
Example::
|
|
144
|
+
|
|
145
|
+
Canvas(200)
|
|
146
|
+
background(0.8)
|
|
147
|
+
|
|
148
|
+
square(-70, -70, 40)
|
|
149
|
+
square(-20, -20, 40, 10)
|
|
150
|
+
square(30, 30, 40, 20, 4, 0, 8)
|
|
151
|
+
"""
|
|
152
|
+
...
|
|
153
|
+
|
|
154
|
+
def point(x: float, y: float) -> None:
|
|
155
|
+
"""🧑🎨
|
|
156
|
+
Draws a point on the canvas.
|
|
157
|
+
|
|
158
|
+
:param x: x-coordinate
|
|
159
|
+
:param y: y-coordinate
|
|
160
|
+
|
|
161
|
+
Example::
|
|
162
|
+
|
|
163
|
+
Canvas(200, 100)
|
|
164
|
+
stroke('white')
|
|
165
|
+
point(-25, 0)
|
|
166
|
+
|
|
167
|
+
strokeWeight(10)
|
|
168
|
+
point(25, 0)
|
|
169
|
+
"""
|
|
170
|
+
...
|
|
171
|
+
|
|
172
|
+
def line(x1: float, y1: float, x2: float, y2: float) -> None:
|
|
173
|
+
"""🧑🎨
|
|
174
|
+
Draws a line on the canvas.
|
|
175
|
+
|
|
176
|
+
:param x1: x-coordinate of the first point
|
|
177
|
+
:param y1: y-coordinate of the first point
|
|
178
|
+
:param x2: x-coordinate of the second point
|
|
179
|
+
:param y2: y-coordinate of the second point
|
|
180
|
+
|
|
181
|
+
Example::
|
|
182
|
+
|
|
183
|
+
Canvas(200, 100)
|
|
184
|
+
stroke('lime')
|
|
185
|
+
line(-80, -30, 80, 30)
|
|
186
|
+
"""
|
|
187
|
+
...
|
|
188
|
+
|
|
189
|
+
def capsule(x1: float, y1: float, x2: float, y2: float, r: float) -> None:
|
|
190
|
+
"""🧑🎨
|
|
191
|
+
Draws a capsule.
|
|
192
|
+
|
|
193
|
+
:param x1: x-coordinate of the first point
|
|
194
|
+
:param y1: y-coordinate of the first point
|
|
195
|
+
:param x2: x-coordinate of the second point
|
|
196
|
+
:param y2: y-coordinate of the second point
|
|
197
|
+
:param r: radius of the capsule semi-circle ends
|
|
198
|
+
|
|
199
|
+
Example::
|
|
200
|
+
|
|
201
|
+
Canvas(200, 100)
|
|
202
|
+
background(0.8)
|
|
203
|
+
strokeWeight(5)
|
|
204
|
+
capsule(-60, -10, 60, 10, 10)
|
|
205
|
+
"""
|
|
206
|
+
...
|
|
207
|
+
|
|
208
|
+
def rectMode(mode: str) -> None:
|
|
209
|
+
"""🧑🎨
|
|
210
|
+
Set to CORNER (default), CENTER, RADIUS, or CORNERS.
|
|
211
|
+
|
|
212
|
+
Changes how the first four inputs to
|
|
213
|
+
rect and square are interpreted.
|
|
214
|
+
|
|
215
|
+
:param mode:
|
|
216
|
+
|
|
217
|
+
Example::
|
|
218
|
+
|
|
219
|
+
Canvas(200, 100)
|
|
220
|
+
background(0.8)
|
|
221
|
+
rectMode(CORNER)
|
|
222
|
+
|
|
223
|
+
# ( x, y, w, h)
|
|
224
|
+
rect(-50, -25, 100, 50)
|
|
225
|
+
"""
|
|
226
|
+
...
|
|
227
|
+
|
|
228
|
+
def ellipseMode(mode: str) -> None:
|
|
229
|
+
"""🧑🎨
|
|
230
|
+
Set to CENTER (default), RADIUS, CORNER, or CORNERS.
|
|
231
|
+
|
|
232
|
+
Changes how the first four inputs to
|
|
233
|
+
ellipse, circle, and arc are interpreted.
|
|
234
|
+
|
|
235
|
+
:param mode:
|
|
236
|
+
|
|
237
|
+
Example::
|
|
238
|
+
|
|
239
|
+
Canvas(200, 100)
|
|
240
|
+
background(0.8)
|
|
241
|
+
ellipseMode(CENTER)
|
|
242
|
+
|
|
243
|
+
# (x, y, w, h)
|
|
244
|
+
ellipse(0, 0, 100, 50)
|
|
245
|
+
"""
|
|
246
|
+
...
|
|
247
|
+
|
|
248
|
+
CORNER: Literal['corner']
|
|
249
|
+
"""🧑🎨
|
|
250
|
+
Shape alignment mode, for use in rectMode and ellipseMode.
|
|
251
|
+
"""
|
|
252
|
+
|
|
253
|
+
RADIUS: Literal['radius']
|
|
254
|
+
"""🧑🎨
|
|
255
|
+
Shape alignment mode, for use in rectMode and ellipseMode.
|
|
256
|
+
"""
|
|
257
|
+
|
|
258
|
+
CORNERS: Literal['corners']
|
|
259
|
+
"""🧑🎨
|
|
260
|
+
Shape alignment mode, for use in rectMode and ellipseMode.
|
|
261
|
+
"""
|
|
262
|
+
|
|
263
|
+
# 🌆 image
|
|
264
|
+
|
|
265
|
+
def loadImage(url: str) -> Image:
|
|
266
|
+
"""🌆
|
|
267
|
+
Loads an image from a URL.
|
|
268
|
+
|
|
269
|
+
By default, assets are loaded in parallel before q5 runs draw. Use await to wait for an image to load.
|
|
270
|
+
|
|
271
|
+
:param url: url of the image to load
|
|
272
|
+
:returns: image
|
|
273
|
+
|
|
274
|
+
Example::
|
|
275
|
+
|
|
276
|
+
Canvas(200)
|
|
277
|
+
|
|
278
|
+
logo = loadImage('/q5js_logo.avif')
|
|
279
|
+
|
|
280
|
+
def draw():
|
|
281
|
+
background(logo)
|
|
282
|
+
"""
|
|
283
|
+
...
|
|
284
|
+
|
|
285
|
+
def image(img: Image | object, dx: float, dy: float, dw: float = ..., dh: float = ..., sx: float = ..., sy: float = ..., sw: float = ..., sh: float = ...) -> None:
|
|
286
|
+
"""🌆
|
|
287
|
+
Draws an image or video frame to the canvas.
|
|
288
|
+
|
|
289
|
+
:param img: image or video to draw
|
|
290
|
+
:param dx: x position to draw the image at
|
|
291
|
+
:param dy: y position to draw the image at
|
|
292
|
+
:param dw: width of the destination image
|
|
293
|
+
:param dh: height of the destination image
|
|
294
|
+
:param sx: x position in the source to start clipping a subsection from
|
|
295
|
+
:param sy: y position in the source to start clipping a subsection from
|
|
296
|
+
:param sw: width of the subsection of the source image
|
|
297
|
+
:param sh: height of the subsection of the source image
|
|
298
|
+
|
|
299
|
+
Example::
|
|
300
|
+
|
|
301
|
+
Canvas(200)
|
|
302
|
+
|
|
303
|
+
logo = load('/q5js_logo.avif')
|
|
304
|
+
|
|
305
|
+
def draw():
|
|
306
|
+
image(logo, -100, -100, 200, 200)
|
|
307
|
+
"""
|
|
308
|
+
...
|
|
309
|
+
|
|
310
|
+
def imageMode(mode: str) -> None:
|
|
311
|
+
"""🌆
|
|
312
|
+
Set to CORNER (default), CORNERS, or CENTER.
|
|
313
|
+
|
|
314
|
+
Changes how inputs to image are interpreted.
|
|
315
|
+
|
|
316
|
+
:param mode:
|
|
317
|
+
|
|
318
|
+
Example::
|
|
319
|
+
|
|
320
|
+
Canvas(200)
|
|
321
|
+
logo = load('/q5js_logo.avif')
|
|
322
|
+
|
|
323
|
+
def draw():
|
|
324
|
+
imageMode(CORNER)
|
|
325
|
+
|
|
326
|
+
# ( img, x, y, w, h)
|
|
327
|
+
image(logo, -50, -50, 100, 100)
|
|
328
|
+
"""
|
|
329
|
+
...
|
|
330
|
+
|
|
331
|
+
def defaultImageScale(scale: float) -> float:
|
|
332
|
+
"""🌆
|
|
333
|
+
Sets the default image scale, which is applied to images when
|
|
334
|
+
they are drawn without a specified width or height.
|
|
335
|
+
|
|
336
|
+
By default it is 0.5 so images appear at their actual size
|
|
337
|
+
when pixel density is 2. Images will be drawn at a consistent
|
|
338
|
+
default size relative to the canvas regardless of pixel density.
|
|
339
|
+
|
|
340
|
+
This function must be called before images are loaded to
|
|
341
|
+
have an effect.
|
|
342
|
+
|
|
343
|
+
:param scale:
|
|
344
|
+
:returns: default image scale
|
|
345
|
+
"""
|
|
346
|
+
...
|
|
347
|
+
|
|
348
|
+
def resize(w: float, h: float) -> None:
|
|
349
|
+
"""🌆
|
|
350
|
+
Resizes the image.
|
|
351
|
+
|
|
352
|
+
:param w: new width
|
|
353
|
+
:param h: new height
|
|
354
|
+
|
|
355
|
+
Example::
|
|
356
|
+
|
|
357
|
+
Canvas(200)
|
|
358
|
+
|
|
359
|
+
logo = await load('/q5js_logo.avif')
|
|
360
|
+
|
|
361
|
+
logo.resize(128, 128)
|
|
362
|
+
image(logo, -100, -100, 200, 200)
|
|
363
|
+
"""
|
|
364
|
+
...
|
|
365
|
+
|
|
366
|
+
def trim() -> Image:
|
|
367
|
+
"""🌆
|
|
368
|
+
Returns a trimmed image, cropping out transparent pixels from the edges.
|
|
369
|
+
"""
|
|
370
|
+
...
|
|
371
|
+
|
|
372
|
+
def smooth() -> None:
|
|
373
|
+
"""🌆
|
|
374
|
+
Enables smooth rendering of images displayed larger than
|
|
375
|
+
their actual size. This is the default setting, so running this
|
|
376
|
+
function only has an effect if noSmooth has been called.
|
|
377
|
+
|
|
378
|
+
Example::
|
|
379
|
+
|
|
380
|
+
Canvas(200)
|
|
381
|
+
smooth()
|
|
382
|
+
|
|
383
|
+
icon = await load('/q5js_icon.png')
|
|
384
|
+
image(icon, -100, -100, 200, 200)
|
|
385
|
+
"""
|
|
386
|
+
...
|
|
387
|
+
|
|
388
|
+
def noSmooth() -> None:
|
|
389
|
+
"""🌆
|
|
390
|
+
Disables smooth image rendering for a pixelated look.
|
|
391
|
+
|
|
392
|
+
Example::
|
|
393
|
+
|
|
394
|
+
Canvas(200)
|
|
395
|
+
noSmooth()
|
|
396
|
+
|
|
397
|
+
icon = await load('/q5js_icon.png')
|
|
398
|
+
image(icon, -100, -100, 200, 200)
|
|
399
|
+
"""
|
|
400
|
+
...
|
|
401
|
+
|
|
402
|
+
def tint(color: str | float) -> None:
|
|
403
|
+
"""🌆
|
|
404
|
+
Applies a tint (color overlay) to the drawing.
|
|
405
|
+
|
|
406
|
+
The alpha value of the tint color determines the
|
|
407
|
+
strength of the tint. To change an image's opacity,
|
|
408
|
+
use the opacity function.
|
|
409
|
+
|
|
410
|
+
Tinting affects all subsequent images drawn. The tint
|
|
411
|
+
color is applied to images using the "multiply" blend mode.
|
|
412
|
+
|
|
413
|
+
Since the tinting process is performance intensive, each time
|
|
414
|
+
an image is tinted, q5 caches the result. image will draw the
|
|
415
|
+
cached tinted image unless the tint color has changed or the
|
|
416
|
+
image being tinted was edited.
|
|
417
|
+
|
|
418
|
+
If you need to draw an image multiple times each frame with
|
|
419
|
+
different tints, consider making copies of the image and tinting
|
|
420
|
+
each copy separately.
|
|
421
|
+
|
|
422
|
+
:param color: tint color
|
|
423
|
+
|
|
424
|
+
Example::
|
|
425
|
+
|
|
426
|
+
Canvas(200)
|
|
427
|
+
|
|
428
|
+
logo = await load('/q5js_logo.avif')
|
|
429
|
+
|
|
430
|
+
tint(1, 0, 0, 0.5)
|
|
431
|
+
image(logo, -100, -100, 200, 200)
|
|
432
|
+
"""
|
|
433
|
+
...
|
|
434
|
+
|
|
435
|
+
def noTint() -> None:
|
|
436
|
+
"""🌆
|
|
437
|
+
Images drawn after this function is run will not be tinted.
|
|
438
|
+
"""
|
|
439
|
+
...
|
|
440
|
+
|
|
441
|
+
def mask(img: Image) -> None:
|
|
442
|
+
"""🌆
|
|
443
|
+
Masks the image with another image.
|
|
444
|
+
|
|
445
|
+
:param img: image to use as a mask
|
|
446
|
+
"""
|
|
447
|
+
...
|
|
448
|
+
|
|
449
|
+
def copy() -> Image:
|
|
450
|
+
"""🌆
|
|
451
|
+
Returns a copy of the image.
|
|
452
|
+
"""
|
|
453
|
+
...
|
|
454
|
+
|
|
455
|
+
def inset(sx: float, sy: float, sw: float, sh: float, dx: float, dy: float, dw: float, dh: float) -> None:
|
|
456
|
+
"""🌆
|
|
457
|
+
Displays a region of the image on another region of the image.
|
|
458
|
+
Can be used to create a detail inset, aka a magnifying glass effect.
|
|
459
|
+
|
|
460
|
+
:param sx: x-coordinate of the source region
|
|
461
|
+
:param sy: y-coordinate of the source region
|
|
462
|
+
:param sw: width of the source region
|
|
463
|
+
:param sh: height of the source region
|
|
464
|
+
:param dx: x-coordinate of the destination region
|
|
465
|
+
:param dy: y-coordinate of the destination region
|
|
466
|
+
:param dw: width of the destination region
|
|
467
|
+
:param dh: height of the destination region
|
|
468
|
+
|
|
469
|
+
Example::
|
|
470
|
+
|
|
471
|
+
Canvas(200)
|
|
472
|
+
|
|
473
|
+
logo = await load('/q5js_logo.avif')
|
|
474
|
+
|
|
475
|
+
logo.inset(256, 256, 512, 512, 0, 0, 256, 256)
|
|
476
|
+
image(logo, -100, -100, 200, 200)
|
|
477
|
+
"""
|
|
478
|
+
...
|
|
479
|
+
|
|
480
|
+
def get(x: float, y: float, w: float = ..., h: float = ...) -> Image | list[float]:
|
|
481
|
+
"""🌆
|
|
482
|
+
Retrieves a subsection of an image or canvas as a new Q5 Image
|
|
483
|
+
or the color of a pixel in the image or canvas.
|
|
484
|
+
|
|
485
|
+
If only x and y are specified, this function returns the color of the pixel
|
|
486
|
+
at the given coordinate in [R, G, B, A] array format. If loadPixels
|
|
487
|
+
has never been run, it's run by this function.
|
|
488
|
+
|
|
489
|
+
If you make changes to the canvas or image, you must call loadPixels
|
|
490
|
+
before using this function to get current color data.
|
|
491
|
+
|
|
492
|
+
Not applicable to WebGPU canvases.
|
|
493
|
+
|
|
494
|
+
:param x:
|
|
495
|
+
:param y:
|
|
496
|
+
:param w: width of the area, default is 1
|
|
497
|
+
:param h: height of the area, default is 1
|
|
498
|
+
|
|
499
|
+
Example::
|
|
500
|
+
|
|
501
|
+
Canvas(200)
|
|
502
|
+
|
|
503
|
+
logo = await load('/q5js_logo.avif')
|
|
504
|
+
|
|
505
|
+
cropped = logo.get(256, 256, 512, 512)
|
|
506
|
+
image(cropped, -100, -100, 200, 200)
|
|
507
|
+
"""
|
|
508
|
+
...
|
|
509
|
+
|
|
510
|
+
def set(x: float, y: float, val: Any) -> None:
|
|
511
|
+
"""🌆
|
|
512
|
+
Sets a pixel's color in the image or canvas. Color mode must be RGB.
|
|
513
|
+
|
|
514
|
+
Or if a canvas or image is provided, it's drawn on top of the
|
|
515
|
+
destination image or canvas, ignoring its tint setting.
|
|
516
|
+
|
|
517
|
+
Run updatePixels to apply the changes.
|
|
518
|
+
|
|
519
|
+
Not applicable to WebGPU canvases.
|
|
520
|
+
|
|
521
|
+
:param x:
|
|
522
|
+
:param y:
|
|
523
|
+
:param val: color, canvas, or image
|
|
524
|
+
|
|
525
|
+
Example::
|
|
526
|
+
|
|
527
|
+
Canvas(200)
|
|
528
|
+
noSmooth()
|
|
529
|
+
c = color('lime')
|
|
530
|
+
img = createImage(50, 50)
|
|
531
|
+
|
|
532
|
+
def draw():
|
|
533
|
+
img.set(random(50), random(50), c)
|
|
534
|
+
img.updatePixels()
|
|
535
|
+
background(img)
|
|
536
|
+
"""
|
|
537
|
+
...
|
|
538
|
+
|
|
539
|
+
pixels: list[float]
|
|
540
|
+
"""🌆
|
|
541
|
+
Array of pixel color data from a canvas or image.
|
|
542
|
+
|
|
543
|
+
Empty by default, get the data by running loadPixels.
|
|
544
|
+
|
|
545
|
+
Each pixel is represented by four consecutive values in the array,
|
|
546
|
+
corresponding to its red, green, blue, and alpha channels.
|
|
547
|
+
|
|
548
|
+
The top left pixel's data is at the beginning of the array
|
|
549
|
+
and the bottom right pixel's data is at the end, going from
|
|
550
|
+
left to right and top to bottom.
|
|
551
|
+
"""
|
|
552
|
+
|
|
553
|
+
def loadPixels() -> None:
|
|
554
|
+
"""🌆
|
|
555
|
+
Loads pixel data into pixels from the canvas or image.
|
|
556
|
+
|
|
557
|
+
The example below sets some pixels' green channel
|
|
558
|
+
to a random value.
|
|
559
|
+
|
|
560
|
+
Not applicable to WebGPU canvases.
|
|
561
|
+
|
|
562
|
+
Example::
|
|
563
|
+
|
|
564
|
+
Canvas(200)
|
|
565
|
+
frameRate(5)
|
|
566
|
+
icon = load('/q5js_icon.png')
|
|
567
|
+
|
|
568
|
+
def draw():
|
|
569
|
+
icon.loadPixels()
|
|
570
|
+
for i in range(0, icon.pixels.length, 16):
|
|
571
|
+
icon.pixels[i + 1] = random(1)
|
|
572
|
+
icon.updatePixels()
|
|
573
|
+
background(icon)
|
|
574
|
+
"""
|
|
575
|
+
...
|
|
576
|
+
|
|
577
|
+
def updatePixels() -> None:
|
|
578
|
+
"""🌆
|
|
579
|
+
Applies changes in the pixels array to the canvas or image.
|
|
580
|
+
|
|
581
|
+
Not applicable to WebGPU canvases.
|
|
582
|
+
|
|
583
|
+
Example::
|
|
584
|
+
|
|
585
|
+
Canvas(200)
|
|
586
|
+
c = color('pink')
|
|
587
|
+
|
|
588
|
+
img = createImage(50, 50)
|
|
589
|
+
for x in range(0, 50, 3):
|
|
590
|
+
for y in range(0, 50, 3):
|
|
591
|
+
img.set(x, y, c)
|
|
592
|
+
img.updatePixels()
|
|
593
|
+
|
|
594
|
+
background(img)
|
|
595
|
+
"""
|
|
596
|
+
...
|
|
597
|
+
|
|
598
|
+
def filter(type: str, value: float = ...) -> None:
|
|
599
|
+
"""🌆
|
|
600
|
+
Applies a filter to the image.
|
|
601
|
+
|
|
602
|
+
See the documentation for q5's filter constants below for more info.
|
|
603
|
+
|
|
604
|
+
A CSS filter string can also be used.
|
|
605
|
+
https://developer.mozilla.org/docs/Web/CSS/filter
|
|
606
|
+
|
|
607
|
+
Not applicable to WebGPU canvases.
|
|
608
|
+
|
|
609
|
+
:param type: filter type or a CSS filter string
|
|
610
|
+
:param value: optional value, depends on filter type
|
|
611
|
+
|
|
612
|
+
Example::
|
|
613
|
+
|
|
614
|
+
Canvas(200)
|
|
615
|
+
logo = await load('/q5js_logo.avif')
|
|
616
|
+
logo.filter(INVERT)
|
|
617
|
+
image(logo, -100, -100, 200, 200)
|
|
618
|
+
"""
|
|
619
|
+
...
|
|
620
|
+
|
|
621
|
+
THRESHOLD: Literal[1]
|
|
622
|
+
"""🌆
|
|
623
|
+
Converts the image to black and white pixels depending if they are above or below a certain threshold.
|
|
624
|
+
"""
|
|
625
|
+
|
|
626
|
+
GRAY: Literal[2]
|
|
627
|
+
"""🌆
|
|
628
|
+
Converts the image to grayscale by setting each pixel to its luminance.
|
|
629
|
+
"""
|
|
630
|
+
|
|
631
|
+
OPAQUE: Literal[3]
|
|
632
|
+
"""🌆
|
|
633
|
+
Sets the alpha channel to fully opaque.
|
|
634
|
+
"""
|
|
635
|
+
|
|
636
|
+
INVERT: Literal[4]
|
|
637
|
+
"""🌆
|
|
638
|
+
Inverts the color of each pixel.
|
|
639
|
+
"""
|
|
640
|
+
|
|
641
|
+
POSTERIZE: Literal[5]
|
|
642
|
+
"""🌆
|
|
643
|
+
Limits each channel of the image to the number of colors specified as an argument.
|
|
644
|
+
"""
|
|
645
|
+
|
|
646
|
+
DILATE: Literal[6]
|
|
647
|
+
"""🌆
|
|
648
|
+
Increases the size of bright areas.
|
|
649
|
+
"""
|
|
650
|
+
|
|
651
|
+
ERODE: Literal[7]
|
|
652
|
+
"""🌆
|
|
653
|
+
Increases the size of dark areas.
|
|
654
|
+
"""
|
|
655
|
+
|
|
656
|
+
BLUR: Literal[8]
|
|
657
|
+
"""🌆
|
|
658
|
+
Applies a Gaussian blur to the image.
|
|
659
|
+
"""
|
|
660
|
+
|
|
661
|
+
def createImage(w: float, h: float, opt: Any = ...) -> Image:
|
|
662
|
+
"""🌆
|
|
663
|
+
Creates a new image.
|
|
664
|
+
|
|
665
|
+
:param w: width
|
|
666
|
+
:param h: height
|
|
667
|
+
:param opt: optional settings for the image
|
|
668
|
+
"""
|
|
669
|
+
...
|
|
670
|
+
|
|
671
|
+
def createGraphics(w: float, h: float, opt: dict = ...) -> Q5:
|
|
672
|
+
"""🌆
|
|
673
|
+
Creates a graphics buffer.
|
|
674
|
+
|
|
675
|
+
Graphics looping is disabled by default in q5 WebGPU.
|
|
676
|
+
See issue #104 for details.
|
|
677
|
+
|
|
678
|
+
:param w: width
|
|
679
|
+
:param h: height
|
|
680
|
+
:param opt: options
|
|
681
|
+
:returns: a new Q5 graphics buffer
|
|
682
|
+
|
|
683
|
+
Example::
|
|
684
|
+
|
|
685
|
+
Canvas(200)
|
|
686
|
+
|
|
687
|
+
g = createGraphics(100)
|
|
688
|
+
g.noLoop()
|
|
689
|
+
g.stroke('pink')
|
|
690
|
+
g.fill('red')
|
|
691
|
+
g.circle(50, 50, 120)
|
|
692
|
+
|
|
693
|
+
image(g, -50, -50, 100, 100)
|
|
694
|
+
"""
|
|
695
|
+
...
|
|
696
|
+
|
|
697
|
+
# 📘 text
|
|
698
|
+
|
|
699
|
+
def text(str: str, x: float, y: float, wrapWidth: float = ..., lineLimit: float = ...) -> None:
|
|
700
|
+
"""📘
|
|
701
|
+
Renders text on the canvas.
|
|
702
|
+
|
|
703
|
+
:param str: string of text to display
|
|
704
|
+
:param x: x-coordinate of the text's position
|
|
705
|
+
:param y: y-coordinate of the text's position
|
|
706
|
+
:param wrapWidth: maximum line width in characters
|
|
707
|
+
:param lineLimit: maximum number of lines
|
|
708
|
+
|
|
709
|
+
Example::
|
|
710
|
+
|
|
711
|
+
Canvas(200, 100)
|
|
712
|
+
background('silver')
|
|
713
|
+
|
|
714
|
+
textSize(32)
|
|
715
|
+
text('Hello, world!', -88, 10)
|
|
716
|
+
"""
|
|
717
|
+
...
|
|
718
|
+
|
|
719
|
+
def loadFont(url: str) -> object:
|
|
720
|
+
"""📘
|
|
721
|
+
Loads a font from a URL.
|
|
722
|
+
|
|
723
|
+
The first example below loads Robotica.
|
|
724
|
+
|
|
725
|
+
The second example loads
|
|
726
|
+
Pacifico from Google fonts.
|
|
727
|
+
|
|
728
|
+
By default, assets are loaded in parallel before q5 runs draw. Use await to wait for a font to load.
|
|
729
|
+
|
|
730
|
+
:param url: URL of the font to load
|
|
731
|
+
:returns: font
|
|
732
|
+
|
|
733
|
+
Example::
|
|
734
|
+
|
|
735
|
+
Canvas(200, 56)
|
|
736
|
+
|
|
737
|
+
await loadFont('/assets/Robotica.ttf')
|
|
738
|
+
|
|
739
|
+
fill('skyblue')
|
|
740
|
+
textSize(64)
|
|
741
|
+
text('Hello!', -98, 24)
|
|
742
|
+
"""
|
|
743
|
+
...
|
|
744
|
+
|
|
745
|
+
def textFont(fontName: str) -> None:
|
|
746
|
+
"""📘
|
|
747
|
+
Sets the current font to be used for rendering text.
|
|
748
|
+
|
|
749
|
+
By default, the font is set to the CSS font family
|
|
750
|
+
"sans-serif" or the last font loaded.
|
|
751
|
+
|
|
752
|
+
:param fontName: name of the font family or a FontFace object
|
|
753
|
+
|
|
754
|
+
Example::
|
|
755
|
+
|
|
756
|
+
Canvas(200, 160)
|
|
757
|
+
background(0.8)
|
|
758
|
+
|
|
759
|
+
textFont('serif')
|
|
760
|
+
|
|
761
|
+
text('Hello, world!', -96, 10)
|
|
762
|
+
"""
|
|
763
|
+
...
|
|
764
|
+
|
|
765
|
+
def textSize(size: float = ...) -> float | None:
|
|
766
|
+
"""📘
|
|
767
|
+
Sets or gets the current font size. If no argument is provided, returns the current font size.
|
|
768
|
+
|
|
769
|
+
:param size: size of the font in pixels
|
|
770
|
+
:returns: current font size when no argument is provided
|
|
771
|
+
|
|
772
|
+
Example::
|
|
773
|
+
|
|
774
|
+
def draw():
|
|
775
|
+
background(0.8)
|
|
776
|
+
|
|
777
|
+
textSize(abs(mouseX))
|
|
778
|
+
text('A', -90, 90)
|
|
779
|
+
"""
|
|
780
|
+
...
|
|
781
|
+
|
|
782
|
+
def textLeading(leading: float = ...) -> float | None:
|
|
783
|
+
"""📘
|
|
784
|
+
Sets or gets the current line height. If no argument is provided, returns the current line height.
|
|
785
|
+
|
|
786
|
+
:param leading: line height in pixels
|
|
787
|
+
:returns: current line height when no argument is provided
|
|
788
|
+
|
|
789
|
+
Example::
|
|
790
|
+
|
|
791
|
+
def draw():
|
|
792
|
+
background(0.8)
|
|
793
|
+
|
|
794
|
+
textSize(abs(mouseX))
|
|
795
|
+
text('A', -90, 90)
|
|
796
|
+
rect(-90, 90, 5, -textLeading())
|
|
797
|
+
"""
|
|
798
|
+
...
|
|
799
|
+
|
|
800
|
+
def textStyle(style: Literal['normal'] | Literal['italic'] | Literal['bold'] | Literal['bolditalic']) -> None:
|
|
801
|
+
"""📘
|
|
802
|
+
Sets the current text style.
|
|
803
|
+
|
|
804
|
+
:param style: font style
|
|
805
|
+
|
|
806
|
+
Example::
|
|
807
|
+
|
|
808
|
+
Canvas(200)
|
|
809
|
+
background(0.8)
|
|
810
|
+
|
|
811
|
+
textStyle(ITALIC)
|
|
812
|
+
|
|
813
|
+
textSize(32)
|
|
814
|
+
text('Hello, world!', -88, 6)
|
|
815
|
+
"""
|
|
816
|
+
...
|
|
817
|
+
|
|
818
|
+
def textAlign(horiz: Literal['left'] | Literal['center'] | Literal['right'], vert: Literal['top'] | Literal['middle'] | Literal['bottom'] | Literal['alphabetic'] = ...) -> None:
|
|
819
|
+
"""📘
|
|
820
|
+
Sets the horizontal and vertical alignment of text.
|
|
821
|
+
|
|
822
|
+
Alignment constants like CENTER can be used with this function.
|
|
823
|
+
|
|
824
|
+
:param horiz: horizontal alignment
|
|
825
|
+
:param vert: vertical alignment
|
|
826
|
+
|
|
827
|
+
Example::
|
|
828
|
+
|
|
829
|
+
Canvas(200)
|
|
830
|
+
background(0.8)
|
|
831
|
+
textSize(32)
|
|
832
|
+
|
|
833
|
+
textAlign(CENTER, CENTER)
|
|
834
|
+
text('Hello, world!', 0, 0)
|
|
835
|
+
"""
|
|
836
|
+
...
|
|
837
|
+
|
|
838
|
+
def textWeight(weight: float | str) -> None:
|
|
839
|
+
"""📘
|
|
840
|
+
Sets the text weight.
|
|
841
|
+
|
|
842
|
+
- 100: thin
|
|
843
|
+
- 200: extra-light
|
|
844
|
+
- 300: light
|
|
845
|
+
- 400: normal/regular
|
|
846
|
+
- 500: medium
|
|
847
|
+
- 600: semi-bold
|
|
848
|
+
- 700: bold
|
|
849
|
+
- 800: bolder/extra-bold
|
|
850
|
+
- 900: black/heavy
|
|
851
|
+
|
|
852
|
+
:param weight: font weight
|
|
853
|
+
|
|
854
|
+
Example::
|
|
855
|
+
|
|
856
|
+
Canvas(200)
|
|
857
|
+
background(0.8)
|
|
858
|
+
textSize(32)
|
|
859
|
+
textAlign(CENTER, CENTER)
|
|
860
|
+
|
|
861
|
+
textWeight(100)
|
|
862
|
+
text('Hello, world!', 0, 0)
|
|
863
|
+
"""
|
|
864
|
+
...
|
|
865
|
+
|
|
866
|
+
def textWidth(str: str) -> float:
|
|
867
|
+
"""📘
|
|
868
|
+
Calculates and returns the width of a given string of text.
|
|
869
|
+
|
|
870
|
+
:param str: string to measure
|
|
871
|
+
:returns: width of the text in pixels
|
|
872
|
+
|
|
873
|
+
Example::
|
|
874
|
+
|
|
875
|
+
def draw():
|
|
876
|
+
background(0.8)
|
|
877
|
+
|
|
878
|
+
textSize(abs(mouseX))
|
|
879
|
+
rect(-90, 90, textWidth('A'), -textLeading())
|
|
880
|
+
text('A', -90, 90)
|
|
881
|
+
"""
|
|
882
|
+
...
|
|
883
|
+
|
|
884
|
+
def textAscent(str: str) -> float:
|
|
885
|
+
"""📘
|
|
886
|
+
Calculates and returns the ascent (the distance from the baseline to the top of the highest character) of the current font.
|
|
887
|
+
|
|
888
|
+
:param str: string to measure
|
|
889
|
+
:returns: ascent of the text in pixels
|
|
890
|
+
|
|
891
|
+
Example::
|
|
892
|
+
|
|
893
|
+
def draw():
|
|
894
|
+
background(0.8)
|
|
895
|
+
|
|
896
|
+
textSize(abs(mouseX))
|
|
897
|
+
rect(-90, 90, textWidth('A'), -textAscent())
|
|
898
|
+
text('A', -90, 90)
|
|
899
|
+
"""
|
|
900
|
+
...
|
|
901
|
+
|
|
902
|
+
def textDescent(str: str) -> float:
|
|
903
|
+
"""📘
|
|
904
|
+
Calculates and returns the descent (the distance from the baseline to the bottom of the lowest character) of the current font.
|
|
905
|
+
|
|
906
|
+
:param str: string to measure
|
|
907
|
+
:returns: descent of the text in pixels
|
|
908
|
+
|
|
909
|
+
Example::
|
|
910
|
+
|
|
911
|
+
Canvas(200)
|
|
912
|
+
background(0.8)
|
|
913
|
+
textSize(64)
|
|
914
|
+
|
|
915
|
+
rect(-100, 0, 200, textDescent('q5'))
|
|
916
|
+
text('q5', -90, 0)
|
|
917
|
+
"""
|
|
918
|
+
...
|
|
919
|
+
|
|
920
|
+
def createTextImage(str: str, wrapWidth: float = ..., lineLimit: float = ...) -> Image:
|
|
921
|
+
"""📘
|
|
922
|
+
Creates an image from a string of text.
|
|
923
|
+
|
|
924
|
+
:param str: string of text
|
|
925
|
+
:param wrapWidth: maximum line width in characters
|
|
926
|
+
:param lineLimit: maximum number of lines
|
|
927
|
+
:returns: an image object representing the rendered text
|
|
928
|
+
|
|
929
|
+
Example::
|
|
930
|
+
|
|
931
|
+
Canvas(200)
|
|
932
|
+
textSize(96)
|
|
933
|
+
|
|
934
|
+
img = createTextImage('🐶')
|
|
935
|
+
img.filter(INVERT)
|
|
936
|
+
|
|
937
|
+
def draw():
|
|
938
|
+
image(img, -45, -90)
|
|
939
|
+
"""
|
|
940
|
+
...
|
|
941
|
+
|
|
942
|
+
def textImage(img: Image | str, x: float, y: float) -> None:
|
|
943
|
+
"""📘
|
|
944
|
+
Renders an image generated from text onto the canvas.
|
|
945
|
+
|
|
946
|
+
If the first parameter is a string, an image of the text will be
|
|
947
|
+
created and cached automatically.
|
|
948
|
+
|
|
949
|
+
The positioning of the image is affected by the current text
|
|
950
|
+
alignment and baseline settings.
|
|
951
|
+
|
|
952
|
+
:param img: image or text
|
|
953
|
+
:param x: x-coordinate where the image should be placed
|
|
954
|
+
:param y: y-coordinate where the image should be placed
|
|
955
|
+
|
|
956
|
+
Example::
|
|
957
|
+
|
|
958
|
+
Canvas(200)
|
|
959
|
+
background(0.8)
|
|
960
|
+
textSize(96)
|
|
961
|
+
textAlign(CENTER, CENTER)
|
|
962
|
+
|
|
963
|
+
textImage('🐶', 0, 0)
|
|
964
|
+
"""
|
|
965
|
+
...
|
|
966
|
+
|
|
967
|
+
def textToPoints(str: str) -> None:
|
|
968
|
+
"""📘
|
|
969
|
+
Converts a string of text to an array of points.
|
|
970
|
+
|
|
971
|
+
Samples opaque pixels in a text image made with createTextImage.
|
|
972
|
+
|
|
973
|
+
It's influenced by text settings, such as font, size, and alignment.
|
|
974
|
+
|
|
975
|
+
Uses a Z-order curve to improve spatial distribution, which preserves the shape of text better than purely random sampling.
|
|
976
|
+
|
|
977
|
+
:param str: string of text
|
|
978
|
+
|
|
979
|
+
Example::
|
|
980
|
+
|
|
981
|
+
Canvas(200)
|
|
982
|
+
textSize(220)
|
|
983
|
+
textAlign(CENTER, CENTER)
|
|
984
|
+
|
|
985
|
+
points = textToPoints('5')
|
|
986
|
+
|
|
987
|
+
for (let pt of points)
|
|
988
|
+
rect(pt.x, pt.y, 5, 20)
|
|
989
|
+
"""
|
|
990
|
+
...
|
|
991
|
+
|
|
992
|
+
def nf(n: float, l: float, r: float) -> str:
|
|
993
|
+
"""📘
|
|
994
|
+
Number formatter, can be used to display a number as a string with
|
|
995
|
+
a specified number of digits before and after the decimal point,
|
|
996
|
+
optionally adding padding with zeros.
|
|
997
|
+
|
|
998
|
+
:param n: number to format
|
|
999
|
+
:param l: minimum number of digits to appear before the decimal point; the number is padded with zeros if necessary
|
|
1000
|
+
:param r: number of digits to appear after the decimal point
|
|
1001
|
+
:returns: a string representation of the number, formatted accordingly
|
|
1002
|
+
|
|
1003
|
+
Example::
|
|
1004
|
+
|
|
1005
|
+
Canvas(200, 100)
|
|
1006
|
+
background(0.8)
|
|
1007
|
+
|
|
1008
|
+
textSize(32)
|
|
1009
|
+
text(nf(PI, 4, 5), -90, 10)
|
|
1010
|
+
"""
|
|
1011
|
+
...
|
|
1012
|
+
|
|
1013
|
+
NORMAL: Literal['normal']
|
|
1014
|
+
"""📘
|
|
1015
|
+
Normal font style.
|
|
1016
|
+
"""
|
|
1017
|
+
|
|
1018
|
+
ITALIC: Literal['italic']
|
|
1019
|
+
"""📘
|
|
1020
|
+
Italic font style.
|
|
1021
|
+
"""
|
|
1022
|
+
|
|
1023
|
+
BOLD: Literal['bold']
|
|
1024
|
+
"""📘
|
|
1025
|
+
Bold font weight.
|
|
1026
|
+
"""
|
|
1027
|
+
|
|
1028
|
+
BOLDITALIC: Literal['italic bold']
|
|
1029
|
+
"""📘
|
|
1030
|
+
Bold and italic font style.
|
|
1031
|
+
"""
|
|
1032
|
+
|
|
1033
|
+
LEFT: Literal['left']
|
|
1034
|
+
"""📘
|
|
1035
|
+
Align text to the left.
|
|
1036
|
+
"""
|
|
1037
|
+
|
|
1038
|
+
CENTER: Literal['center']
|
|
1039
|
+
"""📘
|
|
1040
|
+
Align text to the center.
|
|
1041
|
+
"""
|
|
1042
|
+
|
|
1043
|
+
RIGHT: Literal['right']
|
|
1044
|
+
"""📘
|
|
1045
|
+
Align text to the right.
|
|
1046
|
+
"""
|
|
1047
|
+
|
|
1048
|
+
TOP: Literal['top']
|
|
1049
|
+
"""📘
|
|
1050
|
+
Align text to the top.
|
|
1051
|
+
"""
|
|
1052
|
+
|
|
1053
|
+
MIDDLE: Literal['middle']
|
|
1054
|
+
"""📘
|
|
1055
|
+
Align text to the middle.
|
|
1056
|
+
"""
|
|
1057
|
+
|
|
1058
|
+
BOTTOM: Literal['bottom']
|
|
1059
|
+
"""📘
|
|
1060
|
+
Align text to the bottom.
|
|
1061
|
+
"""
|
|
1062
|
+
|
|
1063
|
+
BASELINE: Literal['alphabetic']
|
|
1064
|
+
"""📘
|
|
1065
|
+
Align text to the baseline (alphabetic).
|
|
1066
|
+
"""
|
|
1067
|
+
|
|
1068
|
+
# 🖲 input
|
|
1069
|
+
|
|
1070
|
+
"""🖲
|
|
1071
|
+
q5's input handling is very basic.
|
|
1072
|
+
|
|
1073
|
+
For better input handling, including game controller support, consider using the q5play addon with q5.
|
|
1074
|
+
|
|
1075
|
+
Note that input responses inside draw can be delayed by
|
|
1076
|
+
up to one frame cycle: from the exact moment an input event occurs
|
|
1077
|
+
to the next time a frame is drawn.
|
|
1078
|
+
|
|
1079
|
+
Play sounds or trigger other non-visual feedback immediately
|
|
1080
|
+
by responding to input events inside functions like
|
|
1081
|
+
mousePressed and keyPressed.
|
|
1082
|
+
"""
|
|
1083
|
+
|
|
1084
|
+
mouseX: float
|
|
1085
|
+
"""🖲
|
|
1086
|
+
Current X position of the mouse.
|
|
1087
|
+
|
|
1088
|
+
Example::
|
|
1089
|
+
|
|
1090
|
+
def draw():
|
|
1091
|
+
background(0.8)
|
|
1092
|
+
textSize(64)
|
|
1093
|
+
text(round(mouseX), -50, 20)
|
|
1094
|
+
"""
|
|
1095
|
+
|
|
1096
|
+
mouseY: float
|
|
1097
|
+
"""🖲
|
|
1098
|
+
Current Y position of the mouse.
|
|
1099
|
+
|
|
1100
|
+
Example::
|
|
1101
|
+
|
|
1102
|
+
def draw():
|
|
1103
|
+
background(0.8)
|
|
1104
|
+
circle(0, mouseY, 100)
|
|
1105
|
+
"""
|
|
1106
|
+
|
|
1107
|
+
pmouseX: float
|
|
1108
|
+
"""🖲
|
|
1109
|
+
Previous X position of the mouse.
|
|
1110
|
+
"""
|
|
1111
|
+
|
|
1112
|
+
pmouseY: float
|
|
1113
|
+
"""🖲
|
|
1114
|
+
Previous Y position of the mouse.
|
|
1115
|
+
"""
|
|
1116
|
+
|
|
1117
|
+
mouseButton: str
|
|
1118
|
+
"""🖲
|
|
1119
|
+
The current button being pressed: 'left', 'right', 'center').
|
|
1120
|
+
|
|
1121
|
+
The default value is an empty string.
|
|
1122
|
+
|
|
1123
|
+
Example::
|
|
1124
|
+
|
|
1125
|
+
def draw():
|
|
1126
|
+
background(0.8)
|
|
1127
|
+
textSize(64)
|
|
1128
|
+
text(mouseButton, -80, 20)
|
|
1129
|
+
"""
|
|
1130
|
+
|
|
1131
|
+
mouseIsPressed: bool
|
|
1132
|
+
"""🖲
|
|
1133
|
+
True if the mouse is currently pressed, false otherwise.
|
|
1134
|
+
|
|
1135
|
+
Example::
|
|
1136
|
+
|
|
1137
|
+
def draw():
|
|
1138
|
+
if mouseIsPressed: background(0.4)
|
|
1139
|
+
else: background(0.8)
|
|
1140
|
+
"""
|
|
1141
|
+
|
|
1142
|
+
def mousePressed() -> None:
|
|
1143
|
+
"""🖲
|
|
1144
|
+
Define this function to respond to mouse down events.
|
|
1145
|
+
|
|
1146
|
+
Example::
|
|
1147
|
+
|
|
1148
|
+
Canvas(200)
|
|
1149
|
+
gray = 0.4
|
|
1150
|
+
|
|
1151
|
+
def mousePressed():
|
|
1152
|
+
background(gray % 1)
|
|
1153
|
+
gray += 0.1
|
|
1154
|
+
"""
|
|
1155
|
+
...
|
|
1156
|
+
|
|
1157
|
+
def mouseReleased() -> None:
|
|
1158
|
+
"""🖲
|
|
1159
|
+
Define this function to respond to mouse up events.
|
|
1160
|
+
|
|
1161
|
+
Example::
|
|
1162
|
+
|
|
1163
|
+
Canvas(200)
|
|
1164
|
+
gray = 0.4
|
|
1165
|
+
|
|
1166
|
+
def mouseReleased():
|
|
1167
|
+
background(gray % 1)
|
|
1168
|
+
gray += 0.1
|
|
1169
|
+
"""
|
|
1170
|
+
...
|
|
1171
|
+
|
|
1172
|
+
def mouseMoved() -> None:
|
|
1173
|
+
"""🖲
|
|
1174
|
+
Define this function to respond to mouse move events.
|
|
1175
|
+
|
|
1176
|
+
On touchscreen devices this function is not called
|
|
1177
|
+
when the user drags their finger on the screen.
|
|
1178
|
+
|
|
1179
|
+
Example::
|
|
1180
|
+
|
|
1181
|
+
Canvas(200)
|
|
1182
|
+
gray = 0.4
|
|
1183
|
+
|
|
1184
|
+
def mouseMoved():
|
|
1185
|
+
background(gray % 1)
|
|
1186
|
+
gray += 0.005
|
|
1187
|
+
"""
|
|
1188
|
+
...
|
|
1189
|
+
|
|
1190
|
+
def mouseDragged() -> None:
|
|
1191
|
+
"""🖲
|
|
1192
|
+
Define this function to respond to mouse drag events.
|
|
1193
|
+
|
|
1194
|
+
Dragging the mouse is defined as moving the mouse
|
|
1195
|
+
while a mouse button is pressed.
|
|
1196
|
+
|
|
1197
|
+
Example::
|
|
1198
|
+
|
|
1199
|
+
Canvas(200)
|
|
1200
|
+
gray = 0.4
|
|
1201
|
+
|
|
1202
|
+
def mouseDragged():
|
|
1203
|
+
background(gray % 1)
|
|
1204
|
+
gray += 0.005
|
|
1205
|
+
"""
|
|
1206
|
+
...
|
|
1207
|
+
|
|
1208
|
+
def doubleClicked() -> None:
|
|
1209
|
+
"""🖲
|
|
1210
|
+
Define this function to respond to mouse double click events.
|
|
1211
|
+
|
|
1212
|
+
Example::
|
|
1213
|
+
|
|
1214
|
+
Canvas(200)
|
|
1215
|
+
gray = 0.4
|
|
1216
|
+
|
|
1217
|
+
def doubleClicked():
|
|
1218
|
+
background(gray % 1)
|
|
1219
|
+
gray += 0.1
|
|
1220
|
+
"""
|
|
1221
|
+
...
|
|
1222
|
+
|
|
1223
|
+
key: str
|
|
1224
|
+
"""🖲
|
|
1225
|
+
The name of the last key pressed.
|
|
1226
|
+
|
|
1227
|
+
Example::
|
|
1228
|
+
|
|
1229
|
+
def draw():
|
|
1230
|
+
background(0.8)
|
|
1231
|
+
textSize(64)
|
|
1232
|
+
text(key, -80, 20)
|
|
1233
|
+
"""
|
|
1234
|
+
|
|
1235
|
+
keyIsPressed: bool
|
|
1236
|
+
"""🖲
|
|
1237
|
+
True if a key is currently pressed, false otherwise.
|
|
1238
|
+
|
|
1239
|
+
Example::
|
|
1240
|
+
|
|
1241
|
+
def draw():
|
|
1242
|
+
if keyIsPressed: background(0.4)
|
|
1243
|
+
else: background(0.8)
|
|
1244
|
+
"""
|
|
1245
|
+
|
|
1246
|
+
def keyIsDown(key: str) -> bool:
|
|
1247
|
+
"""🖲
|
|
1248
|
+
Returns true if the user is pressing the specified key, false
|
|
1249
|
+
otherwise. Accepts case-insensitive key names.
|
|
1250
|
+
|
|
1251
|
+
:param key: key to check
|
|
1252
|
+
:returns: true if the key is pressed, false otherwise
|
|
1253
|
+
|
|
1254
|
+
Example::
|
|
1255
|
+
|
|
1256
|
+
def draw():
|
|
1257
|
+
background(0.8)
|
|
1258
|
+
|
|
1259
|
+
if keyIsDown('f': && keyIsDown('j'))
|
|
1260
|
+
rect(-50, -50, 100, 100)
|
|
1261
|
+
"""
|
|
1262
|
+
...
|
|
1263
|
+
|
|
1264
|
+
def keyPressed() -> None:
|
|
1265
|
+
"""🖲
|
|
1266
|
+
Define this function to respond to key down events.
|
|
1267
|
+
|
|
1268
|
+
Example::
|
|
1269
|
+
|
|
1270
|
+
Canvas(200)
|
|
1271
|
+
gray = 0.4
|
|
1272
|
+
|
|
1273
|
+
def keyPressed():
|
|
1274
|
+
background(gray % 1)
|
|
1275
|
+
gray += 0.1
|
|
1276
|
+
"""
|
|
1277
|
+
...
|
|
1278
|
+
|
|
1279
|
+
def keyReleased() -> None:
|
|
1280
|
+
"""🖲
|
|
1281
|
+
Define this function to respond to key up events.
|
|
1282
|
+
|
|
1283
|
+
Example::
|
|
1284
|
+
|
|
1285
|
+
Canvas(200)
|
|
1286
|
+
gray = 0.4
|
|
1287
|
+
|
|
1288
|
+
def keyReleased():
|
|
1289
|
+
background(gray % 1)
|
|
1290
|
+
gray += 0.1
|
|
1291
|
+
"""
|
|
1292
|
+
...
|
|
1293
|
+
|
|
1294
|
+
touches: list[Any]
|
|
1295
|
+
"""🖲
|
|
1296
|
+
Array containing all current touch points within the
|
|
1297
|
+
browser window. Each touch being an object with
|
|
1298
|
+
id, x, and y properties.
|
|
1299
|
+
|
|
1300
|
+
Consider using the pointers array instead,
|
|
1301
|
+
which includes mouse, touch, and pen input.
|
|
1302
|
+
|
|
1303
|
+
Example::
|
|
1304
|
+
|
|
1305
|
+
def draw():
|
|
1306
|
+
background(0.8)
|
|
1307
|
+
for (let pt of pointers)
|
|
1308
|
+
circle(pt.x, pt.y, 100)
|
|
1309
|
+
"""
|
|
1310
|
+
|
|
1311
|
+
def touchStarted() -> None:
|
|
1312
|
+
"""🖲
|
|
1313
|
+
Define this function to respond to touch down events
|
|
1314
|
+
on the canvas.
|
|
1315
|
+
|
|
1316
|
+
Return true to enable touch gestures like pinch-to-zoom
|
|
1317
|
+
and scroll, which q5 disables on the canvas by default.
|
|
1318
|
+
|
|
1319
|
+
Example::
|
|
1320
|
+
|
|
1321
|
+
Canvas(200)
|
|
1322
|
+
gray = 0.4
|
|
1323
|
+
|
|
1324
|
+
def touchStarted():
|
|
1325
|
+
background(gray % 1)
|
|
1326
|
+
gray += 0.1
|
|
1327
|
+
"""
|
|
1328
|
+
...
|
|
1329
|
+
|
|
1330
|
+
def touchEnded() -> None:
|
|
1331
|
+
"""🖲
|
|
1332
|
+
Define this function to respond to touch down events
|
|
1333
|
+
on the canvas.
|
|
1334
|
+
|
|
1335
|
+
Return true to enable touch gestures like pinch-to-zoom
|
|
1336
|
+
and scroll, which q5 disables on the canvas by default.
|
|
1337
|
+
|
|
1338
|
+
Example::
|
|
1339
|
+
|
|
1340
|
+
Canvas(200)
|
|
1341
|
+
gray = 0.4
|
|
1342
|
+
|
|
1343
|
+
def touchEnded():
|
|
1344
|
+
background(gray % 1)
|
|
1345
|
+
gray += 0.1
|
|
1346
|
+
"""
|
|
1347
|
+
...
|
|
1348
|
+
|
|
1349
|
+
def touchMoved() -> None:
|
|
1350
|
+
"""🖲
|
|
1351
|
+
Define this function to respond to touch move events
|
|
1352
|
+
on the canvas.
|
|
1353
|
+
|
|
1354
|
+
Return true to enable touch gestures like pinch-to-zoom
|
|
1355
|
+
and scroll, which q5 disables on the canvas by default.
|
|
1356
|
+
|
|
1357
|
+
Example::
|
|
1358
|
+
|
|
1359
|
+
Canvas(200)
|
|
1360
|
+
gray = 0.4
|
|
1361
|
+
|
|
1362
|
+
def touchMoved():
|
|
1363
|
+
background(gray % 1)
|
|
1364
|
+
gray += 0.005
|
|
1365
|
+
"""
|
|
1366
|
+
...
|
|
1367
|
+
|
|
1368
|
+
def cursor(name: str, x: float = ..., y: float = ...) -> None:
|
|
1369
|
+
"""🖲
|
|
1370
|
+
Sets the cursor to a CSS cursor type or image.
|
|
1371
|
+
If an image is provided, optional x and y coordinates can
|
|
1372
|
+
specify the active point of the cursor.
|
|
1373
|
+
|
|
1374
|
+
:param name: name of the cursor or the url to an image
|
|
1375
|
+
:param x: x-coordinate of the cursor's point
|
|
1376
|
+
:param y: y-coordinate of the cursor's point
|
|
1377
|
+
|
|
1378
|
+
Example::
|
|
1379
|
+
|
|
1380
|
+
Canvas(200, 100)
|
|
1381
|
+
cursor('pointer')
|
|
1382
|
+
"""
|
|
1383
|
+
...
|
|
1384
|
+
|
|
1385
|
+
def noCursor() -> None:
|
|
1386
|
+
"""🖲
|
|
1387
|
+
Hides the cursor within the bounds of the canvas.
|
|
1388
|
+
|
|
1389
|
+
Example::
|
|
1390
|
+
|
|
1391
|
+
Canvas(200, 100)
|
|
1392
|
+
noCursor()
|
|
1393
|
+
"""
|
|
1394
|
+
...
|
|
1395
|
+
|
|
1396
|
+
def mouseWheel() -> None:
|
|
1397
|
+
"""🖲
|
|
1398
|
+
Define this function to respond to mouse wheel events.
|
|
1399
|
+
|
|
1400
|
+
event.deltaX and event.deltaY are the horizontal and vertical
|
|
1401
|
+
scroll amounts, respectively.
|
|
1402
|
+
|
|
1403
|
+
Return true to allow the default behavior of scrolling the page.
|
|
1404
|
+
|
|
1405
|
+
Example::
|
|
1406
|
+
|
|
1407
|
+
x = 0
|
|
1408
|
+
y = 0
|
|
1409
|
+
def draw():
|
|
1410
|
+
circle(x, y, 10)
|
|
1411
|
+
def mouseWheel(e):
|
|
1412
|
+
x += e.deltaX
|
|
1413
|
+
y += e.deltaY
|
|
1414
|
+
return False
|
|
1415
|
+
"""
|
|
1416
|
+
...
|
|
1417
|
+
|
|
1418
|
+
movedX: float
|
|
1419
|
+
"""🖲
|
|
1420
|
+
Distance the mouse has moved since the last frame in the horizontal direction.
|
|
1421
|
+
|
|
1422
|
+
Example::
|
|
1423
|
+
|
|
1424
|
+
def draw():
|
|
1425
|
+
background(0.8)
|
|
1426
|
+
if movedX > 0: text('>>', 0, 0)
|
|
1427
|
+
if movedX < 0: text('<<', 0, 0)
|
|
1428
|
+
"""
|
|
1429
|
+
|
|
1430
|
+
movedY: float
|
|
1431
|
+
"""🖲
|
|
1432
|
+
Distance the mouse has moved since the last frame in the vertical direction.
|
|
1433
|
+
|
|
1434
|
+
Example::
|
|
1435
|
+
|
|
1436
|
+
def draw():
|
|
1437
|
+
background(0.8)
|
|
1438
|
+
if movedY > 0: text('vv', 0, 0)
|
|
1439
|
+
if movedY < 0: text('^^', 0, 0)
|
|
1440
|
+
"""
|
|
1441
|
+
|
|
1442
|
+
def pointerLock(unadjustedMovement: bool) -> None:
|
|
1443
|
+
"""🖲
|
|
1444
|
+
Requests that the pointer be locked to the document body, hiding
|
|
1445
|
+
the cursor and allowing for unlimited movement.
|
|
1446
|
+
|
|
1447
|
+
Operating systems enable mouse acceleration by default, which is useful when you sometimes want slow precise movement (think about you might use a graphics package), but also want to move great distances with a faster mouse movement (think about scrolling, and selecting several files). For some games however, raw mouse input data is preferred for controlling camera rotation — where the same distance movement, fast or slow, results in the same rotation.
|
|
1448
|
+
|
|
1449
|
+
To exit pointer lock mode, call document.exitPointerLock().
|
|
1450
|
+
|
|
1451
|
+
:param unadjustedMovement: set to true to disable OS-level mouse acceleration and access raw mouse input
|
|
1452
|
+
|
|
1453
|
+
Example::
|
|
1454
|
+
|
|
1455
|
+
def draw():
|
|
1456
|
+
circle(mouseX / 10, mouseY / 10, 10)
|
|
1457
|
+
|
|
1458
|
+
def doubleClicked():
|
|
1459
|
+
if !document.pointerLockElement:
|
|
1460
|
+
pointerLock()
|
|
1461
|
+
} else
|
|
1462
|
+
document.exitPointerLock()
|
|
1463
|
+
"""
|
|
1464
|
+
...
|
|
1465
|
+
|
|
1466
|
+
# 🎨 color
|
|
1467
|
+
|
|
1468
|
+
def color(c0: str | float | Color | list[float], c1: float = ..., c2: float = ..., c3: float = ...) -> Color:
|
|
1469
|
+
"""🎨
|
|
1470
|
+
Creates a new Color object, which is primarily useful for storing
|
|
1471
|
+
a color that your sketch will reuse or modify later.
|
|
1472
|
+
|
|
1473
|
+
With the default color mode, RGB, colors have r/red, g/green,
|
|
1474
|
+
b/blue, and a/alpha components.
|
|
1475
|
+
|
|
1476
|
+
The fill, stroke, and background
|
|
1477
|
+
functions accept the same wide range of color representations as this function.
|
|
1478
|
+
|
|
1479
|
+
:param c0: color or first color component
|
|
1480
|
+
:param c1: second color component
|
|
1481
|
+
:param c2: third color component
|
|
1482
|
+
:param c3: fourth color component (alpha)
|
|
1483
|
+
:returns: a new Color object
|
|
1484
|
+
|
|
1485
|
+
Example::
|
|
1486
|
+
|
|
1487
|
+
Canvas(200)
|
|
1488
|
+
rect(-100, -100, 100, 200)
|
|
1489
|
+
|
|
1490
|
+
# ( r, g, b, a)
|
|
1491
|
+
bottle = color(0.35, 0.39, 1, 0.4)
|
|
1492
|
+
fill(bottle)
|
|
1493
|
+
stroke(bottle)
|
|
1494
|
+
strokeWeight(30)
|
|
1495
|
+
circle(0, 0, 155)
|
|
1496
|
+
"""
|
|
1497
|
+
...
|
|
1498
|
+
|
|
1499
|
+
def colorMode(mode: Literal['rgb'] | Literal['oklch'] | Literal['hsl'] | Literal['hsb'], format: Literal[1] | Literal[255], gamut: Literal['srgb'] | Literal['display-p3'] = ...) -> None:
|
|
1500
|
+
"""🎨
|
|
1501
|
+
Sets the color mode for the sketch, which changes how colors are
|
|
1502
|
+
interpreted and displayed.
|
|
1503
|
+
|
|
1504
|
+
Color gamut is 'display-p3' by default, if the device supports HDR.
|
|
1505
|
+
|
|
1506
|
+
:param mode: color mode
|
|
1507
|
+
:param format: color format (1 for float, 255 for integer)
|
|
1508
|
+
:param gamut: color gamut
|
|
1509
|
+
|
|
1510
|
+
Example::
|
|
1511
|
+
|
|
1512
|
+
Canvas(200)
|
|
1513
|
+
|
|
1514
|
+
colorMode(RGB, 1)
|
|
1515
|
+
fill(1, 0, 0)
|
|
1516
|
+
rect(-100, -100, 66, 200)
|
|
1517
|
+
fill(0, 1, 0)
|
|
1518
|
+
rect(-34, -100, 67, 200)
|
|
1519
|
+
fill(0, 0, 1)
|
|
1520
|
+
rect(33, -100, 67, 200)
|
|
1521
|
+
"""
|
|
1522
|
+
...
|
|
1523
|
+
|
|
1524
|
+
RGB: Literal['rgb']
|
|
1525
|
+
"""🎨
|
|
1526
|
+
RGB colors have components r/red, g/green, b/blue,
|
|
1527
|
+
and a/alpha.
|
|
1528
|
+
|
|
1529
|
+
By default when a canvas is using the HDR "display-p3" color space,
|
|
1530
|
+
rgb colors are mapped to the full P3 gamut, even when they use the
|
|
1531
|
+
legacy integer 0-255 format.
|
|
1532
|
+
|
|
1533
|
+
Example::
|
|
1534
|
+
|
|
1535
|
+
Canvas(200, 100)
|
|
1536
|
+
|
|
1537
|
+
colorMode(RGB)
|
|
1538
|
+
|
|
1539
|
+
background(1, 0, 0)
|
|
1540
|
+
"""
|
|
1541
|
+
|
|
1542
|
+
OKLCH: Literal['oklch']
|
|
1543
|
+
"""🎨
|
|
1544
|
+
OKLCH colors have components l/lightness, c/chroma,
|
|
1545
|
+
h/hue, and a/alpha. It's more intuitive for humans
|
|
1546
|
+
to work with color in these terms than RGB.
|
|
1547
|
+
|
|
1548
|
+
OKLCH is perceptually uniform, meaning colors at the
|
|
1549
|
+
same lightness and chroma (colorfulness) will appear to
|
|
1550
|
+
have equal luminance, regardless of the hue.
|
|
1551
|
+
|
|
1552
|
+
OKLCH can accurately represent all colors visible to the
|
|
1553
|
+
human eye, unlike many other color spaces that are bounded
|
|
1554
|
+
to a gamut. The maximum lightness and chroma values that
|
|
1555
|
+
correspond to sRGB or P3 gamut limits vary depending on
|
|
1556
|
+
the hue. Colors that are out of gamut will be clipped to
|
|
1557
|
+
the nearest in-gamut color.
|
|
1558
|
+
|
|
1559
|
+
Use the OKLCH color picker to find
|
|
1560
|
+
in-gamut colors.
|
|
1561
|
+
|
|
1562
|
+
- lightness: 0 to 1
|
|
1563
|
+
- chroma: 0 to ~0.4
|
|
1564
|
+
- hue: 0 to 360
|
|
1565
|
+
- alpha: 0 to 1
|
|
1566
|
+
|
|
1567
|
+
Example::
|
|
1568
|
+
|
|
1569
|
+
Canvas(200, 100)
|
|
1570
|
+
|
|
1571
|
+
colorMode(OKLCH)
|
|
1572
|
+
|
|
1573
|
+
background(0.64, 0.3, 30)
|
|
1574
|
+
"""
|
|
1575
|
+
|
|
1576
|
+
HSL: Literal['hsl']
|
|
1577
|
+
"""🎨
|
|
1578
|
+
HSL colors have components h/hue, s/saturation,
|
|
1579
|
+
l/lightness, and a/alpha.
|
|
1580
|
+
|
|
1581
|
+
HSL was created in the 1970s to approximate human perception
|
|
1582
|
+
of color, trading accuracy for simpler computations. It's
|
|
1583
|
+
not perceptually uniform, so colors with the same lightness
|
|
1584
|
+
can appear darker or lighter, depending on their hue
|
|
1585
|
+
and saturation. Yet, the lightness and saturation values that
|
|
1586
|
+
correspond to gamut limits are always 100, regardless of the
|
|
1587
|
+
hue. This can make HSL easier to work with than OKLCH.
|
|
1588
|
+
|
|
1589
|
+
HSL colors are mapped to the full P3 gamut when
|
|
1590
|
+
using the "display-p3" color space.
|
|
1591
|
+
|
|
1592
|
+
- hue: 0 to 360
|
|
1593
|
+
- saturation: 0 to 100
|
|
1594
|
+
- lightness: 0 to 100
|
|
1595
|
+
- alpha: 0 to 1
|
|
1596
|
+
|
|
1597
|
+
Example::
|
|
1598
|
+
|
|
1599
|
+
Canvas(200, 100)
|
|
1600
|
+
|
|
1601
|
+
colorMode(HSL)
|
|
1602
|
+
|
|
1603
|
+
background(0, 100, 50)
|
|
1604
|
+
"""
|
|
1605
|
+
|
|
1606
|
+
HSB: Literal['hsb']
|
|
1607
|
+
"""🎨
|
|
1608
|
+
HSB colors have components h/hue, s/saturation,
|
|
1609
|
+
b/brightness (aka v/value), and a/alpha.
|
|
1610
|
+
|
|
1611
|
+
HSB is similar to HSL, but instead of lightness
|
|
1612
|
+
(black to white), it uses brightness (black to
|
|
1613
|
+
full color). To produce white, set brightness
|
|
1614
|
+
to 100 and saturation to 0.
|
|
1615
|
+
|
|
1616
|
+
- hue: 0 to 360
|
|
1617
|
+
- saturation: 0 to 100
|
|
1618
|
+
- brightness: 0 to 100
|
|
1619
|
+
- alpha: 0 to 1
|
|
1620
|
+
|
|
1621
|
+
Example::
|
|
1622
|
+
|
|
1623
|
+
Canvas(200, 100)
|
|
1624
|
+
|
|
1625
|
+
colorMode(HSB)
|
|
1626
|
+
|
|
1627
|
+
background(0, 100, 100)
|
|
1628
|
+
"""
|
|
1629
|
+
|
|
1630
|
+
SRGB: Literal['srgb']
|
|
1631
|
+
"""🎨
|
|
1632
|
+
Limits the color gamut to the sRGB color space.
|
|
1633
|
+
|
|
1634
|
+
If your display is HDR capable, note that full red appears
|
|
1635
|
+
less saturated and darker in this example, as it would on
|
|
1636
|
+
an SDR display.
|
|
1637
|
+
|
|
1638
|
+
Example::
|
|
1639
|
+
|
|
1640
|
+
Canvas(200, 100)
|
|
1641
|
+
|
|
1642
|
+
colorMode(RGB, 1, SRGB)
|
|
1643
|
+
|
|
1644
|
+
background(1, 0, 0)
|
|
1645
|
+
"""
|
|
1646
|
+
|
|
1647
|
+
DISPLAY_P3: Literal['display-p3']
|
|
1648
|
+
"""🎨
|
|
1649
|
+
Expands the color gamut to the P3 color space.
|
|
1650
|
+
|
|
1651
|
+
This is the default color gamut on devices that support HDR.
|
|
1652
|
+
|
|
1653
|
+
If your display is HDR capable, note that full red appears
|
|
1654
|
+
fully saturated and bright in the following example.
|
|
1655
|
+
|
|
1656
|
+
Example::
|
|
1657
|
+
|
|
1658
|
+
Canvas(200, 100)
|
|
1659
|
+
|
|
1660
|
+
colorMode(RGB, 1, DISPLAY_P3)
|
|
1661
|
+
|
|
1662
|
+
background(1, 0, 0)
|
|
1663
|
+
"""
|
|
1664
|
+
|
|
1665
|
+
def background(filler: Color | Image) -> None:
|
|
1666
|
+
"""🎨
|
|
1667
|
+
Draws over the entire canvas with a color or image.
|
|
1668
|
+
|
|
1669
|
+
Like the color function,
|
|
1670
|
+
this function can accept colors in a wide range of formats:
|
|
1671
|
+
CSS color string, grayscale value, and color component values.
|
|
1672
|
+
|
|
1673
|
+
:param filler: a color or image to draw
|
|
1674
|
+
|
|
1675
|
+
Example::
|
|
1676
|
+
|
|
1677
|
+
Canvas(200, 100)
|
|
1678
|
+
background('crimson')
|
|
1679
|
+
"""
|
|
1680
|
+
...
|
|
1681
|
+
|
|
1682
|
+
class Color:
|
|
1683
|
+
"""🎨
|
|
1684
|
+
This constructor strictly accepts 4 numbers, which are the color
|
|
1685
|
+
components.
|
|
1686
|
+
|
|
1687
|
+
Use the color function for greater flexibility, it runs
|
|
1688
|
+
this constructor internally.
|
|
1689
|
+
"""
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
def equals(self) -> None:
|
|
1693
|
+
"""🎨 Checks if this color is exactly equal to another color."""
|
|
1694
|
+
...
|
|
1695
|
+
|
|
1696
|
+
def isSameColor(self) -> None:
|
|
1697
|
+
"""🎨 Checks if the color is the same as another color,
|
|
1698
|
+
disregarding their alpha values."""
|
|
1699
|
+
...
|
|
1700
|
+
|
|
1701
|
+
def toString(self) -> None:
|
|
1702
|
+
"""🎨 Produces a CSS color string representation."""
|
|
1703
|
+
...
|
|
1704
|
+
|
|
1705
|
+
def levels(self) -> None:
|
|
1706
|
+
"""🎨 An array of the color's components."""
|
|
1707
|
+
...
|
|
1708
|
+
|
|
1709
|
+
# 💅 styles
|
|
1710
|
+
|
|
1711
|
+
def fill(color: Color) -> None:
|
|
1712
|
+
"""💅
|
|
1713
|
+
Sets the fill color. The default is white.
|
|
1714
|
+
|
|
1715
|
+
Like the color function, this function
|
|
1716
|
+
can accept colors in a wide range of formats: as a CSS color string,
|
|
1717
|
+
a Color object, grayscale value, or color component values.
|
|
1718
|
+
|
|
1719
|
+
:param color: fill color
|
|
1720
|
+
|
|
1721
|
+
Example::
|
|
1722
|
+
|
|
1723
|
+
Canvas(200)
|
|
1724
|
+
background(0.8)
|
|
1725
|
+
|
|
1726
|
+
fill('red')
|
|
1727
|
+
circle(-20, -20, 80)
|
|
1728
|
+
|
|
1729
|
+
fill('lime')
|
|
1730
|
+
square(-20, -20, 80)
|
|
1731
|
+
"""
|
|
1732
|
+
...
|
|
1733
|
+
|
|
1734
|
+
def stroke(color: Color) -> None:
|
|
1735
|
+
"""💅
|
|
1736
|
+
Sets the stroke (outline) color. The default is black.
|
|
1737
|
+
|
|
1738
|
+
Like the color function, this function
|
|
1739
|
+
can accept colors in a wide range of formats: as a CSS color string,
|
|
1740
|
+
a Color object, grayscale value, or color component values.
|
|
1741
|
+
|
|
1742
|
+
:param color: stroke color
|
|
1743
|
+
|
|
1744
|
+
Example::
|
|
1745
|
+
|
|
1746
|
+
Canvas(200)
|
|
1747
|
+
background(0.8)
|
|
1748
|
+
fill(0.14)
|
|
1749
|
+
|
|
1750
|
+
stroke('red')
|
|
1751
|
+
circle(-20, -20, 80)
|
|
1752
|
+
|
|
1753
|
+
stroke('lime')
|
|
1754
|
+
square(-20, -20, 80)
|
|
1755
|
+
"""
|
|
1756
|
+
...
|
|
1757
|
+
|
|
1758
|
+
def noFill() -> None:
|
|
1759
|
+
"""💅
|
|
1760
|
+
After calling this function, drawing will not be filled.
|
|
1761
|
+
|
|
1762
|
+
Example::
|
|
1763
|
+
|
|
1764
|
+
Canvas(200)
|
|
1765
|
+
background(0.8)
|
|
1766
|
+
|
|
1767
|
+
noFill()
|
|
1768
|
+
|
|
1769
|
+
stroke('red')
|
|
1770
|
+
circle(-20, -20, 80)
|
|
1771
|
+
stroke('lime')
|
|
1772
|
+
square(-20, -20, 80)
|
|
1773
|
+
"""
|
|
1774
|
+
...
|
|
1775
|
+
|
|
1776
|
+
def noStroke() -> None:
|
|
1777
|
+
"""💅
|
|
1778
|
+
After calling this function, drawing will not have a stroke (outline).
|
|
1779
|
+
|
|
1780
|
+
Example::
|
|
1781
|
+
|
|
1782
|
+
Canvas(200)
|
|
1783
|
+
background(0.8)
|
|
1784
|
+
fill(0.14)
|
|
1785
|
+
stroke('red')
|
|
1786
|
+
circle(-20, -20, 80)
|
|
1787
|
+
|
|
1788
|
+
noStroke()
|
|
1789
|
+
square(-20, -20, 80)
|
|
1790
|
+
"""
|
|
1791
|
+
...
|
|
1792
|
+
|
|
1793
|
+
def strokeWeight(weight: float) -> None:
|
|
1794
|
+
"""💅
|
|
1795
|
+
Sets the size of the stroke used for lines and the border around drawings.
|
|
1796
|
+
|
|
1797
|
+
:param weight: size of the stroke in pixels
|
|
1798
|
+
|
|
1799
|
+
Example::
|
|
1800
|
+
|
|
1801
|
+
Canvas(200)
|
|
1802
|
+
background(0.8)
|
|
1803
|
+
stroke('red')
|
|
1804
|
+
circle(-50, 0, 80)
|
|
1805
|
+
|
|
1806
|
+
strokeWeight(12)
|
|
1807
|
+
circle(50, 0, 80)
|
|
1808
|
+
"""
|
|
1809
|
+
...
|
|
1810
|
+
|
|
1811
|
+
def opacity(alpha: float) -> None:
|
|
1812
|
+
"""💅
|
|
1813
|
+
Sets the global opacity, which affects all subsequent drawing operations, except background. Default is 1, fully opaque.
|
|
1814
|
+
|
|
1815
|
+
In q5 WebGPU this function only affects images.
|
|
1816
|
+
|
|
1817
|
+
:param alpha: opacity level, ranging from 0 to 1
|
|
1818
|
+
|
|
1819
|
+
Example::
|
|
1820
|
+
|
|
1821
|
+
Canvas(200)
|
|
1822
|
+
background(0.8)
|
|
1823
|
+
|
|
1824
|
+
opacity(1)
|
|
1825
|
+
circle(-20, -20, 80)
|
|
1826
|
+
|
|
1827
|
+
opacity(0.2)
|
|
1828
|
+
square(-20, -20, 80)
|
|
1829
|
+
"""
|
|
1830
|
+
...
|
|
1831
|
+
|
|
1832
|
+
def shadow(color: Color) -> None:
|
|
1833
|
+
"""💅
|
|
1834
|
+
Sets the shadow color. The default is transparent (no shadow).
|
|
1835
|
+
|
|
1836
|
+
Shadows apply to anything drawn to the canvas, including filled
|
|
1837
|
+
shapes, strokes, text, and images.
|
|
1838
|
+
|
|
1839
|
+
Like the color function, this function
|
|
1840
|
+
can accept colors in a wide range of formats: as a CSS color string,
|
|
1841
|
+
a Color object, grayscale value, or color component values.
|
|
1842
|
+
|
|
1843
|
+
Not available in q5 WebGPU.
|
|
1844
|
+
|
|
1845
|
+
:param color: shadow color
|
|
1846
|
+
"""
|
|
1847
|
+
...
|
|
1848
|
+
|
|
1849
|
+
def noShadow() -> None:
|
|
1850
|
+
"""💅
|
|
1851
|
+
Disables the shadow effect.
|
|
1852
|
+
|
|
1853
|
+
Not available in q5 WebGPU.
|
|
1854
|
+
"""
|
|
1855
|
+
...
|
|
1856
|
+
|
|
1857
|
+
def shadowBox(offsetX: float, offsetY: float, blur: float) -> None:
|
|
1858
|
+
"""💅
|
|
1859
|
+
Sets the shadow offset and blur radius.
|
|
1860
|
+
|
|
1861
|
+
When q5 starts, shadow offset is (10, 10) with a blur of 10.
|
|
1862
|
+
|
|
1863
|
+
Not available in q5 WebGPU.
|
|
1864
|
+
|
|
1865
|
+
:param offsetX: horizontal offset of the shadow
|
|
1866
|
+
:param offsetY: vertical offset of the shadow, defaults to be the same as offsetX
|
|
1867
|
+
:param blur: blur radius of the shadow, defaults to 0
|
|
1868
|
+
"""
|
|
1869
|
+
...
|
|
1870
|
+
|
|
1871
|
+
def blendMode(val: str) -> None:
|
|
1872
|
+
"""💅
|
|
1873
|
+
Set the global composite operation for the canvas context.
|
|
1874
|
+
|
|
1875
|
+
Not available in q5 WebGPU.
|
|
1876
|
+
|
|
1877
|
+
:param val: composite operation
|
|
1878
|
+
"""
|
|
1879
|
+
...
|
|
1880
|
+
|
|
1881
|
+
def strokeCap(val: str) -> None:
|
|
1882
|
+
"""💅
|
|
1883
|
+
|
|
1884
|
+
:param val: line cap style
|
|
1885
|
+
|
|
1886
|
+
Example::
|
|
1887
|
+
|
|
1888
|
+
Canvas(200)
|
|
1889
|
+
background(0.8)
|
|
1890
|
+
strokeWeight(20)
|
|
1891
|
+
|
|
1892
|
+
strokeCap(SQUARE)
|
|
1893
|
+
line(-50, -25, 50, -25)
|
|
1894
|
+
|
|
1895
|
+
strokeCap(PROJECT)
|
|
1896
|
+
line(-50, 25, 50, 25)
|
|
1897
|
+
"""
|
|
1898
|
+
...
|
|
1899
|
+
|
|
1900
|
+
def strokeJoin(val: str) -> None:
|
|
1901
|
+
"""💅
|
|
1902
|
+
|
|
1903
|
+
:param val: line join style
|
|
1904
|
+
|
|
1905
|
+
Example::
|
|
1906
|
+
|
|
1907
|
+
Canvas(200)
|
|
1908
|
+
background(0.8)
|
|
1909
|
+
strokeWeight(10)
|
|
1910
|
+
|
|
1911
|
+
strokeJoin(ROUND)
|
|
1912
|
+
triangle(-50, -30, 50, -30, -50, 20)
|
|
1913
|
+
|
|
1914
|
+
strokeJoin(MITER)
|
|
1915
|
+
triangle(50, 0, -50, 50, 50, 50)
|
|
1916
|
+
"""
|
|
1917
|
+
...
|
|
1918
|
+
|
|
1919
|
+
def erase(fillAlpha: float = ..., strokeAlpha: float = ...) -> None:
|
|
1920
|
+
"""💅
|
|
1921
|
+
Sets the canvas to erase mode, where shapes will erase what's
|
|
1922
|
+
underneath them instead of drawing over it.
|
|
1923
|
+
|
|
1924
|
+
Not available in q5 WebGPU.
|
|
1925
|
+
|
|
1926
|
+
:param fillAlpha: opacity level of the fill color
|
|
1927
|
+
:param strokeAlpha: opacity level of the stroke color
|
|
1928
|
+
"""
|
|
1929
|
+
...
|
|
1930
|
+
|
|
1931
|
+
def noErase() -> None:
|
|
1932
|
+
"""💅
|
|
1933
|
+
Resets the canvas from erase mode to normal drawing mode.
|
|
1934
|
+
|
|
1935
|
+
Not available in q5 WebGPU.
|
|
1936
|
+
"""
|
|
1937
|
+
...
|
|
1938
|
+
|
|
1939
|
+
def pushStyles() -> None:
|
|
1940
|
+
"""💅
|
|
1941
|
+
Saves the current drawing style settings.
|
|
1942
|
+
|
|
1943
|
+
This includes the fill, stroke, stroke weight, tint, image mode,
|
|
1944
|
+
rect mode, ellipse mode, text size, text align, text baseline, and
|
|
1945
|
+
shadow settings.
|
|
1946
|
+
|
|
1947
|
+
Example::
|
|
1948
|
+
|
|
1949
|
+
Canvas(200)
|
|
1950
|
+
background(0.8)
|
|
1951
|
+
|
|
1952
|
+
pushStyles()
|
|
1953
|
+
fill('blue')
|
|
1954
|
+
circle(-50, -50, 80)
|
|
1955
|
+
|
|
1956
|
+
popStyles()
|
|
1957
|
+
circle(50, 50, 80)
|
|
1958
|
+
"""
|
|
1959
|
+
...
|
|
1960
|
+
|
|
1961
|
+
def popStyles() -> None:
|
|
1962
|
+
"""💅
|
|
1963
|
+
Restores the previously saved drawing style settings.
|
|
1964
|
+
|
|
1965
|
+
Example::
|
|
1966
|
+
|
|
1967
|
+
Canvas(200)
|
|
1968
|
+
background(0.8)
|
|
1969
|
+
|
|
1970
|
+
pushStyles()
|
|
1971
|
+
fill('blue')
|
|
1972
|
+
circle(-50, -50, 80)
|
|
1973
|
+
|
|
1974
|
+
popStyles()
|
|
1975
|
+
circle(50, 50, 80)
|
|
1976
|
+
"""
|
|
1977
|
+
...
|
|
1978
|
+
|
|
1979
|
+
def clear() -> None:
|
|
1980
|
+
"""💅
|
|
1981
|
+
Clears the canvas, making every pixel completely transparent.
|
|
1982
|
+
|
|
1983
|
+
Note that the canvas can only be seen through if it has an alpha channel.
|
|
1984
|
+
|
|
1985
|
+
Example::
|
|
1986
|
+
|
|
1987
|
+
Canvas(200, { alpha: True })
|
|
1988
|
+
|
|
1989
|
+
def draw():
|
|
1990
|
+
clear()
|
|
1991
|
+
circle((frameCount % 200) - 100, 0, 80)
|
|
1992
|
+
"""
|
|
1993
|
+
...
|
|
1994
|
+
|
|
1995
|
+
ctx: Any
|
|
1996
|
+
"""💅
|
|
1997
|
+
The 2D rendering context for the canvas.
|
|
1998
|
+
|
|
1999
|
+
You can use it to create linear gradients, radial gradients, font stretching, and
|
|
2000
|
+
other advanced drawing features.
|
|
2001
|
+
|
|
2002
|
+
Not available in q5 WebGPU.
|
|
2003
|
+
"""
|
|
2004
|
+
|
|
2005
|
+
def inFill(x: float, y: float) -> bool:
|
|
2006
|
+
"""💅
|
|
2007
|
+
Checks if a given point is within the current path's fill area.
|
|
2008
|
+
|
|
2009
|
+
Not available in q5 WebGPU.
|
|
2010
|
+
|
|
2011
|
+
:param x: x-coordinate of the point
|
|
2012
|
+
:param y: y-coordinate of the point
|
|
2013
|
+
:returns: true if the point is within the fill area, false otherwise
|
|
2014
|
+
"""
|
|
2015
|
+
...
|
|
2016
|
+
|
|
2017
|
+
def inStroke(x: float, y: float) -> bool:
|
|
2018
|
+
"""💅
|
|
2019
|
+
Checks if a given point is within the current path's stroke.
|
|
2020
|
+
|
|
2021
|
+
Not available in q5 WebGPU.
|
|
2022
|
+
|
|
2023
|
+
:param x: x-coordinate of the point
|
|
2024
|
+
:param y: y-coordinate of the point
|
|
2025
|
+
:returns: true if the point is within the stroke, false otherwise
|
|
2026
|
+
"""
|
|
2027
|
+
...
|
|
2028
|
+
|
|
2029
|
+
# 🦋 transforms
|
|
2030
|
+
|
|
2031
|
+
def translate(x: float, y: float) -> None:
|
|
2032
|
+
"""🦋
|
|
2033
|
+
Translates the origin of the drawing context.
|
|
2034
|
+
|
|
2035
|
+
:param x: translation along the x-axis
|
|
2036
|
+
:param y: translation along the y-axis
|
|
2037
|
+
|
|
2038
|
+
Example::
|
|
2039
|
+
|
|
2040
|
+
def draw():
|
|
2041
|
+
background(0.8)
|
|
2042
|
+
|
|
2043
|
+
translate(50, 50)
|
|
2044
|
+
circle(0, 0, 80)
|
|
2045
|
+
"""
|
|
2046
|
+
...
|
|
2047
|
+
|
|
2048
|
+
def rotate(angle: float) -> None:
|
|
2049
|
+
"""🦋
|
|
2050
|
+
Rotates the drawing context.
|
|
2051
|
+
|
|
2052
|
+
:param angle: rotation angle in radians
|
|
2053
|
+
|
|
2054
|
+
Example::
|
|
2055
|
+
|
|
2056
|
+
def draw():
|
|
2057
|
+
background(0.8)
|
|
2058
|
+
|
|
2059
|
+
rotate(mouseX / 50)
|
|
2060
|
+
|
|
2061
|
+
rectMode(CENTER)
|
|
2062
|
+
square(0, 0, 120)
|
|
2063
|
+
"""
|
|
2064
|
+
...
|
|
2065
|
+
|
|
2066
|
+
def scale(x: float, y: float = ...) -> None:
|
|
2067
|
+
"""🦋
|
|
2068
|
+
Scales the drawing context.
|
|
2069
|
+
|
|
2070
|
+
If only one input parameter is provided,
|
|
2071
|
+
the drawing context will be scaled uniformly.
|
|
2072
|
+
|
|
2073
|
+
:param x: scaling factor along the x-axis
|
|
2074
|
+
:param y: scaling factor along the y-axis
|
|
2075
|
+
|
|
2076
|
+
Example::
|
|
2077
|
+
|
|
2078
|
+
def draw():
|
|
2079
|
+
background(0.8)
|
|
2080
|
+
|
|
2081
|
+
scale(mouseX / 10)
|
|
2082
|
+
circle(0, 0, 20)
|
|
2083
|
+
"""
|
|
2084
|
+
...
|
|
2085
|
+
|
|
2086
|
+
def shearX(angle: float) -> None:
|
|
2087
|
+
"""🦋
|
|
2088
|
+
Shears the drawing context along the x-axis.
|
|
2089
|
+
|
|
2090
|
+
:param angle: shear angle in radians
|
|
2091
|
+
|
|
2092
|
+
Example::
|
|
2093
|
+
|
|
2094
|
+
def draw():
|
|
2095
|
+
background(0.8)
|
|
2096
|
+
|
|
2097
|
+
translate(-75, -40)
|
|
2098
|
+
shearX(mouseX / 100)
|
|
2099
|
+
square(0, 0, 80)
|
|
2100
|
+
"""
|
|
2101
|
+
...
|
|
2102
|
+
|
|
2103
|
+
def shearY(angle: float) -> None:
|
|
2104
|
+
"""🦋
|
|
2105
|
+
Shears the drawing context along the y-axis.
|
|
2106
|
+
|
|
2107
|
+
:param angle: shear angle in radians
|
|
2108
|
+
|
|
2109
|
+
Example::
|
|
2110
|
+
|
|
2111
|
+
def draw():
|
|
2112
|
+
background(0.8)
|
|
2113
|
+
|
|
2114
|
+
translate(-75, -40)
|
|
2115
|
+
shearY(mouseX / 100)
|
|
2116
|
+
square(0, 0, 80)
|
|
2117
|
+
"""
|
|
2118
|
+
...
|
|
2119
|
+
|
|
2120
|
+
def applyMatrix(a: float, b: float, c: float, d: float, e: float, f: float) -> None:
|
|
2121
|
+
"""🦋
|
|
2122
|
+
Applies a transformation matrix.
|
|
2123
|
+
|
|
2124
|
+
Accepts a 3x3 matrix as either an array or multiple arguments.
|
|
2125
|
+
|
|
2126
|
+
:param a:
|
|
2127
|
+
:param b:
|
|
2128
|
+
:param c:
|
|
2129
|
+
:param d:
|
|
2130
|
+
:param e:
|
|
2131
|
+
:param f:
|
|
2132
|
+
|
|
2133
|
+
Example::
|
|
2134
|
+
|
|
2135
|
+
def draw():
|
|
2136
|
+
background(0.8)
|
|
2137
|
+
|
|
2138
|
+
applyMatrix(2, -1, 1, -1)
|
|
2139
|
+
circle(0, 0, 80)
|
|
2140
|
+
"""
|
|
2141
|
+
...
|
|
2142
|
+
|
|
2143
|
+
def resetMatrix() -> None:
|
|
2144
|
+
"""🦋
|
|
2145
|
+
Resets the transformation matrix.
|
|
2146
|
+
|
|
2147
|
+
q5 runs this function before every time the draw function is run,
|
|
2148
|
+
so that transformations don't carry over to the next frame.
|
|
2149
|
+
|
|
2150
|
+
Example::
|
|
2151
|
+
|
|
2152
|
+
Canvas(200)
|
|
2153
|
+
background(0.8)
|
|
2154
|
+
|
|
2155
|
+
translate(50, 50)
|
|
2156
|
+
circle(0, 0, 80)
|
|
2157
|
+
|
|
2158
|
+
resetMatrix()
|
|
2159
|
+
square(0, 0, 50)
|
|
2160
|
+
"""
|
|
2161
|
+
...
|
|
2162
|
+
|
|
2163
|
+
def pushMatrix() -> None:
|
|
2164
|
+
"""🦋
|
|
2165
|
+
Saves the current transformation matrix.
|
|
2166
|
+
|
|
2167
|
+
Example::
|
|
2168
|
+
|
|
2169
|
+
Canvas(200)
|
|
2170
|
+
background(0.8)
|
|
2171
|
+
|
|
2172
|
+
pushMatrix()
|
|
2173
|
+
rotate(QUARTER_PI)
|
|
2174
|
+
ellipse(0, 0, 120, 40)
|
|
2175
|
+
popMatrix()
|
|
2176
|
+
|
|
2177
|
+
ellipse(0, 0, 120, 40)
|
|
2178
|
+
"""
|
|
2179
|
+
...
|
|
2180
|
+
|
|
2181
|
+
def popMatrix() -> None:
|
|
2182
|
+
"""🦋
|
|
2183
|
+
Restores the previously saved transformation matrix.
|
|
2184
|
+
|
|
2185
|
+
Example::
|
|
2186
|
+
|
|
2187
|
+
Canvas(200)
|
|
2188
|
+
background(0.8)
|
|
2189
|
+
|
|
2190
|
+
pushMatrix()
|
|
2191
|
+
rotate(QUARTER_PI)
|
|
2192
|
+
ellipse(0, 0, 120, 40)
|
|
2193
|
+
popMatrix()
|
|
2194
|
+
|
|
2195
|
+
ellipse(0, 0, 120, 40)
|
|
2196
|
+
"""
|
|
2197
|
+
...
|
|
2198
|
+
|
|
2199
|
+
def push() -> None:
|
|
2200
|
+
"""🦋
|
|
2201
|
+
Saves the current drawing style settings and transformations.
|
|
2202
|
+
|
|
2203
|
+
Example::
|
|
2204
|
+
|
|
2205
|
+
Canvas(200)
|
|
2206
|
+
|
|
2207
|
+
push()
|
|
2208
|
+
fill('blue')
|
|
2209
|
+
translate(50, 50)
|
|
2210
|
+
circle(0, 0, 80)
|
|
2211
|
+
pop()
|
|
2212
|
+
|
|
2213
|
+
square(0, 0, 50)
|
|
2214
|
+
"""
|
|
2215
|
+
...
|
|
2216
|
+
|
|
2217
|
+
def pop() -> None:
|
|
2218
|
+
"""🦋
|
|
2219
|
+
Restores the previously saved drawing style settings and transformations.
|
|
2220
|
+
|
|
2221
|
+
Example::
|
|
2222
|
+
|
|
2223
|
+
Canvas(200)
|
|
2224
|
+
|
|
2225
|
+
push()
|
|
2226
|
+
fill('blue')
|
|
2227
|
+
translate(50, 50)
|
|
2228
|
+
circle(0, 0, 80)
|
|
2229
|
+
pop()
|
|
2230
|
+
|
|
2231
|
+
square(0, 0, 50)
|
|
2232
|
+
"""
|
|
2233
|
+
...
|
|
2234
|
+
|
|
2235
|
+
# 💻 display
|
|
2236
|
+
|
|
2237
|
+
def displayMode(mode: str, renderQuality: str, scale: float) -> None:
|
|
2238
|
+
"""💻
|
|
2239
|
+
Customize how your canvas is presented.
|
|
2240
|
+
|
|
2241
|
+
:param mode: NORMAL, CENTER, or MAXED
|
|
2242
|
+
:param renderQuality: SMOOTH or PIXELATED
|
|
2243
|
+
:param scale: can also be given as a string (for example "x2")
|
|
2244
|
+
|
|
2245
|
+
Example::
|
|
2246
|
+
|
|
2247
|
+
Canvas(50, 25)
|
|
2248
|
+
|
|
2249
|
+
displayMode(CENTER, PIXELATED, 4)
|
|
2250
|
+
|
|
2251
|
+
circle(0, 0, 16)
|
|
2252
|
+
"""
|
|
2253
|
+
...
|
|
2254
|
+
|
|
2255
|
+
MAXED: Literal['maxed']
|
|
2256
|
+
"""💻
|
|
2257
|
+
A displayMode setting.
|
|
2258
|
+
|
|
2259
|
+
The canvas will be scaled to fill the parent element,
|
|
2260
|
+
with letterboxing if necessary to preserve its aspect ratio.
|
|
2261
|
+
"""
|
|
2262
|
+
|
|
2263
|
+
SMOOTH: Literal['smooth']
|
|
2264
|
+
"""💻
|
|
2265
|
+
A displayMode render quality.
|
|
2266
|
+
|
|
2267
|
+
Smooth upscaling is used if the canvas is scaled.
|
|
2268
|
+
"""
|
|
2269
|
+
|
|
2270
|
+
PIXELATED: Literal['pixelated']
|
|
2271
|
+
"""💻
|
|
2272
|
+
A displayMode render quality.
|
|
2273
|
+
|
|
2274
|
+
Pixels are rendered as sharp squares if the canvas is scaled.
|
|
2275
|
+
"""
|
|
2276
|
+
|
|
2277
|
+
def fullscreen(v: bool = ...) -> None:
|
|
2278
|
+
"""💻
|
|
2279
|
+
Enables or disables fullscreen mode.
|
|
2280
|
+
|
|
2281
|
+
:param v: boolean indicating whether to enable or disable fullscreen mode
|
|
2282
|
+
"""
|
|
2283
|
+
...
|
|
2284
|
+
|
|
2285
|
+
windowWidth: float
|
|
2286
|
+
"""💻
|
|
2287
|
+
The width of the window.
|
|
2288
|
+
|
|
2289
|
+
Example::
|
|
2290
|
+
|
|
2291
|
+
def draw():
|
|
2292
|
+
background(0.8)
|
|
2293
|
+
textSize(64)
|
|
2294
|
+
textAlign(CENTER, CENTER)
|
|
2295
|
+
text(windowWidth, 0, 0)
|
|
2296
|
+
"""
|
|
2297
|
+
|
|
2298
|
+
windowHeight: float
|
|
2299
|
+
"""💻
|
|
2300
|
+
The height of the window.
|
|
2301
|
+
|
|
2302
|
+
Example::
|
|
2303
|
+
|
|
2304
|
+
def draw():
|
|
2305
|
+
background(0.8)
|
|
2306
|
+
textSize(64)
|
|
2307
|
+
textAlign(CENTER, CENTER)
|
|
2308
|
+
text(windowHeight, 0, 0)
|
|
2309
|
+
"""
|
|
2310
|
+
|
|
2311
|
+
width: float
|
|
2312
|
+
"""💻
|
|
2313
|
+
The width of the canvas.
|
|
2314
|
+
|
|
2315
|
+
Example::
|
|
2316
|
+
|
|
2317
|
+
Canvas(200, 120)
|
|
2318
|
+
circle(0, 0, width)
|
|
2319
|
+
"""
|
|
2320
|
+
|
|
2321
|
+
height: float
|
|
2322
|
+
"""💻
|
|
2323
|
+
The height of the canvas.
|
|
2324
|
+
|
|
2325
|
+
Example::
|
|
2326
|
+
|
|
2327
|
+
Canvas(200, 80)
|
|
2328
|
+
circle(0, 0, height)
|
|
2329
|
+
"""
|
|
2330
|
+
|
|
2331
|
+
halfWidth: float
|
|
2332
|
+
"""💻
|
|
2333
|
+
Half the width of the canvas.
|
|
2334
|
+
|
|
2335
|
+
Example::
|
|
2336
|
+
|
|
2337
|
+
Canvas(200, 80)
|
|
2338
|
+
circle(0, 0, halfWidth)
|
|
2339
|
+
"""
|
|
2340
|
+
|
|
2341
|
+
halfHeight: float
|
|
2342
|
+
"""💻
|
|
2343
|
+
Half the height of the canvas.
|
|
2344
|
+
|
|
2345
|
+
Example::
|
|
2346
|
+
|
|
2347
|
+
Canvas(200, 80)
|
|
2348
|
+
circle(0, 0, halfHeight)
|
|
2349
|
+
"""
|
|
2350
|
+
|
|
2351
|
+
canvas: object
|
|
2352
|
+
"""💻
|
|
2353
|
+
The canvas element associated with the Q5 instance.
|
|
2354
|
+
"""
|
|
2355
|
+
|
|
2356
|
+
def resizeCanvas(w: float, h: float) -> None:
|
|
2357
|
+
"""💻
|
|
2358
|
+
Resizes the canvas to the specified width and height.
|
|
2359
|
+
|
|
2360
|
+
:param w: width of the canvas
|
|
2361
|
+
:param h: height of the canvas
|
|
2362
|
+
|
|
2363
|
+
Example::
|
|
2364
|
+
|
|
2365
|
+
Canvas(200, 100)
|
|
2366
|
+
|
|
2367
|
+
def draw():
|
|
2368
|
+
background(0.8)
|
|
2369
|
+
|
|
2370
|
+
def mousePressed():
|
|
2371
|
+
resizeCanvas(200, 200)
|
|
2372
|
+
"""
|
|
2373
|
+
...
|
|
2374
|
+
|
|
2375
|
+
frameCount: float
|
|
2376
|
+
"""💻
|
|
2377
|
+
The number of frames that have been displayed since the program started.
|
|
2378
|
+
|
|
2379
|
+
Example::
|
|
2380
|
+
|
|
2381
|
+
def draw():
|
|
2382
|
+
background(0.8)
|
|
2383
|
+
textSize(64)
|
|
2384
|
+
text(frameCount, -92, 20)
|
|
2385
|
+
"""
|
|
2386
|
+
|
|
2387
|
+
def noLoop() -> None:
|
|
2388
|
+
"""💻
|
|
2389
|
+
Stops the draw loop.
|
|
2390
|
+
|
|
2391
|
+
Example::
|
|
2392
|
+
|
|
2393
|
+
def draw():
|
|
2394
|
+
circle(frameCount * 5 - 100, 0, 80)
|
|
2395
|
+
noLoop()
|
|
2396
|
+
"""
|
|
2397
|
+
...
|
|
2398
|
+
|
|
2399
|
+
def redraw(n: float = ...) -> None:
|
|
2400
|
+
"""💻
|
|
2401
|
+
Redraws the canvas n times. If no input parameter is provided,
|
|
2402
|
+
it calls the draw function once.
|
|
2403
|
+
|
|
2404
|
+
This is an async function.
|
|
2405
|
+
|
|
2406
|
+
:param n: number of times to redraw the canvas, default is 1
|
|
2407
|
+
|
|
2408
|
+
Example::
|
|
2409
|
+
|
|
2410
|
+
Canvas(200)
|
|
2411
|
+
noLoop()
|
|
2412
|
+
|
|
2413
|
+
def draw():
|
|
2414
|
+
circle(frameCount * 5 - 100, 0, 80)
|
|
2415
|
+
def mousePressed():
|
|
2416
|
+
redraw(10)
|
|
2417
|
+
"""
|
|
2418
|
+
...
|
|
2419
|
+
|
|
2420
|
+
def loop() -> None:
|
|
2421
|
+
"""💻
|
|
2422
|
+
Starts the draw loop again if it was stopped.
|
|
2423
|
+
|
|
2424
|
+
Example::
|
|
2425
|
+
|
|
2426
|
+
Canvas(200)
|
|
2427
|
+
noLoop()
|
|
2428
|
+
|
|
2429
|
+
def draw():
|
|
2430
|
+
circle(frameCount * 5 - 100, 0, 80)
|
|
2431
|
+
def mousePressed():
|
|
2432
|
+
loop()
|
|
2433
|
+
"""
|
|
2434
|
+
...
|
|
2435
|
+
|
|
2436
|
+
def frameRate(hertz: float = ...) -> float:
|
|
2437
|
+
"""💻
|
|
2438
|
+
Sets the target frame rate or gets an approximation of the
|
|
2439
|
+
sketch's current frame rate.
|
|
2440
|
+
|
|
2441
|
+
Even when the sketch is running at a consistent frame rate,
|
|
2442
|
+
the current frame rate value will fluctuate. Use your web browser's
|
|
2443
|
+
developer tools for more accurate performance analysis.
|
|
2444
|
+
|
|
2445
|
+
:param hertz: target frame rate, default is 60
|
|
2446
|
+
:returns: current frame rate
|
|
2447
|
+
|
|
2448
|
+
Example::
|
|
2449
|
+
|
|
2450
|
+
def draw():
|
|
2451
|
+
background(0.8)
|
|
2452
|
+
|
|
2453
|
+
if mouseIsPressed: frameRate(10)
|
|
2454
|
+
else: frameRate(60)
|
|
2455
|
+
|
|
2456
|
+
circle((frameCount % 200) - 100, 0, 80)
|
|
2457
|
+
"""
|
|
2458
|
+
...
|
|
2459
|
+
|
|
2460
|
+
def getTargetFrameRate() -> float:
|
|
2461
|
+
"""💻
|
|
2462
|
+
The desired frame rate of the sketch.
|
|
2463
|
+
:returns: target frame rate
|
|
2464
|
+
|
|
2465
|
+
Example::
|
|
2466
|
+
|
|
2467
|
+
def draw():
|
|
2468
|
+
background(0.8)
|
|
2469
|
+
textSize(64)
|
|
2470
|
+
|
|
2471
|
+
text(getTargetFrameRate(), -35, 20)
|
|
2472
|
+
"""
|
|
2473
|
+
...
|
|
2474
|
+
|
|
2475
|
+
def getFPS() -> float:
|
|
2476
|
+
"""💻
|
|
2477
|
+
Gets the current FPS, in terms of how many frames could be generated
|
|
2478
|
+
in one second, which can be higher than the target frame rate.
|
|
2479
|
+
|
|
2480
|
+
Use your web browser's developer tools for more in-depth
|
|
2481
|
+
performance analysis.
|
|
2482
|
+
:returns: frames per second
|
|
2483
|
+
|
|
2484
|
+
Example::
|
|
2485
|
+
|
|
2486
|
+
def draw():
|
|
2487
|
+
background(0.8)
|
|
2488
|
+
frameRate(1)
|
|
2489
|
+
textSize(64)
|
|
2490
|
+
|
|
2491
|
+
text(getFPS(), -92, 20)
|
|
2492
|
+
"""
|
|
2493
|
+
...
|
|
2494
|
+
|
|
2495
|
+
def postProcess() -> None:
|
|
2496
|
+
"""💻
|
|
2497
|
+
Runs after each draw function call and post-draw q5 addon processes, if any.
|
|
2498
|
+
|
|
2499
|
+
Useful for adding post-processing effects when it's not possible
|
|
2500
|
+
to do so at the end of the draw function, such as when using
|
|
2501
|
+
addons like q5play that auto-draw to the canvas after the draw
|
|
2502
|
+
function is run.
|
|
2503
|
+
"""
|
|
2504
|
+
...
|
|
2505
|
+
|
|
2506
|
+
def pixelDensity(v: float) -> float:
|
|
2507
|
+
"""💻
|
|
2508
|
+
Sets the pixel density of the canvas.
|
|
2509
|
+
|
|
2510
|
+
:param v: pixel density value
|
|
2511
|
+
:returns: pixel density
|
|
2512
|
+
|
|
2513
|
+
Example::
|
|
2514
|
+
|
|
2515
|
+
Canvas(200, 100)
|
|
2516
|
+
background(0.8)
|
|
2517
|
+
pixelDensity(1)
|
|
2518
|
+
circle(0, 0, 80)
|
|
2519
|
+
"""
|
|
2520
|
+
...
|
|
2521
|
+
|
|
2522
|
+
def displayDensity() -> float:
|
|
2523
|
+
"""💻
|
|
2524
|
+
Returns the current display density.
|
|
2525
|
+
|
|
2526
|
+
On most modern displays, this value will be 2 or 3.
|
|
2527
|
+
:returns: display density
|
|
2528
|
+
|
|
2529
|
+
Example::
|
|
2530
|
+
|
|
2531
|
+
Canvas(200, 100)
|
|
2532
|
+
background(0.8)
|
|
2533
|
+
textSize(64)
|
|
2534
|
+
text(displayDensity(), -90, 6)
|
|
2535
|
+
"""
|
|
2536
|
+
...
|
|
2537
|
+
|
|
2538
|
+
deltaTime: float
|
|
2539
|
+
"""💻
|
|
2540
|
+
The time passed since the last frame was drawn.
|
|
2541
|
+
|
|
2542
|
+
With the default frame rate of 60, delta time will be
|
|
2543
|
+
approximately 16.6
|
|
2544
|
+
|
|
2545
|
+
Can be used to keep movements tied to real time if the sketch
|
|
2546
|
+
is often dropping below the target frame rate. Although if frame
|
|
2547
|
+
rates are consistently low, consider reducing the target frame
|
|
2548
|
+
rate instead.
|
|
2549
|
+
|
|
2550
|
+
Example::
|
|
2551
|
+
|
|
2552
|
+
def draw():
|
|
2553
|
+
background(0.8)
|
|
2554
|
+
text(deltaTime, -90, 6)
|
|
2555
|
+
"""
|
|
2556
|
+
|
|
2557
|
+
C2D: Literal['c2d']
|
|
2558
|
+
"""💻
|
|
2559
|
+
A constant that can be passed as the third argument to Canvas to specify that the Canvas2D renderer should be used.
|
|
2560
|
+
|
|
2561
|
+
Note that in this example, the circle is located at position [0, 0], the origin of the canvas.
|
|
2562
|
+
|
|
2563
|
+
Example::
|
|
2564
|
+
|
|
2565
|
+
Canvas(200, 100, C2D)
|
|
2566
|
+
background('silver')
|
|
2567
|
+
circle(0, 0, 80)
|
|
2568
|
+
"""
|
|
2569
|
+
|
|
2570
|
+
WEBGPU: Literal['webgpu']
|
|
2571
|
+
"""💻
|
|
2572
|
+
Since WebGPU is the default renderer in JavaScript modules, it's not necessary to use this constant with Canvas, unless you want to make it explicit.
|
|
2573
|
+
|
|
2574
|
+
Example::
|
|
2575
|
+
|
|
2576
|
+
Canvas(200, 100, WEBGPU)
|
|
2577
|
+
background('silver')
|
|
2578
|
+
circle(0, 0, 80)
|
|
2579
|
+
"""
|
|
2580
|
+
|
|
2581
|
+
# 🧮 math
|
|
2582
|
+
|
|
2583
|
+
def random(low: float | list[Any] = ..., high: float = ...) -> float | Any:
|
|
2584
|
+
"""🧮
|
|
2585
|
+
Generates a random value.
|
|
2586
|
+
|
|
2587
|
+
- If no inputs are provided, returns a number between 0 and 1.
|
|
2588
|
+
- If one numerical input is provided, returns a number between 0 and the provided value.
|
|
2589
|
+
- If two numerical inputs are provided, returns a number between the two values.
|
|
2590
|
+
- If an array is provided, returns a random element from the array.
|
|
2591
|
+
|
|
2592
|
+
Return value can be the lower bound but can never exactly be the upper bound.
|
|
2593
|
+
|
|
2594
|
+
:param low: lower bound (inclusive) or an array
|
|
2595
|
+
:param high: upper bound (exclusive)
|
|
2596
|
+
:returns: a random number or element
|
|
2597
|
+
|
|
2598
|
+
Example::
|
|
2599
|
+
|
|
2600
|
+
Canvas(200)
|
|
2601
|
+
background(0.8)
|
|
2602
|
+
frameRate(5)
|
|
2603
|
+
|
|
2604
|
+
def draw():
|
|
2605
|
+
circle(0, 0, random(200))
|
|
2606
|
+
"""
|
|
2607
|
+
...
|
|
2608
|
+
|
|
2609
|
+
def jit(amount: float = ...) -> float:
|
|
2610
|
+
"""🧮
|
|
2611
|
+
Generates a random number within a symmetric range around zero.
|
|
2612
|
+
|
|
2613
|
+
Can be used to create a jitter effect (random displacement).
|
|
2614
|
+
|
|
2615
|
+
Equivalent to random(-amount, amount).
|
|
2616
|
+
|
|
2617
|
+
:param amount: absolute maximum amount of jitter, default is 1
|
|
2618
|
+
:returns: random number between -val and val
|
|
2619
|
+
|
|
2620
|
+
Example::
|
|
2621
|
+
|
|
2622
|
+
def draw():
|
|
2623
|
+
circle(mouseX + jit(3), mouseY + jit(3), 5)
|
|
2624
|
+
"""
|
|
2625
|
+
...
|
|
2626
|
+
|
|
2627
|
+
def noise(x: float = ..., y: float = ..., z: float = ...) -> float:
|
|
2628
|
+
"""🧮
|
|
2629
|
+
Generates a noise value based on the x, y, and z inputs.
|
|
2630
|
+
|
|
2631
|
+
Uses Perlin Noise by default.
|
|
2632
|
+
|
|
2633
|
+
:param x: x-coordinate input
|
|
2634
|
+
:param y: y-coordinate input
|
|
2635
|
+
:param z: z-coordinate input
|
|
2636
|
+
:returns: a noise value
|
|
2637
|
+
|
|
2638
|
+
Example::
|
|
2639
|
+
|
|
2640
|
+
def draw():
|
|
2641
|
+
background(0.8)
|
|
2642
|
+
n = noise(frameCount * 0.01)
|
|
2643
|
+
circle(0, 0, n * 200)
|
|
2644
|
+
"""
|
|
2645
|
+
...
|
|
2646
|
+
|
|
2647
|
+
def dist(x1: float, y1: float, x2: float, y2: float) -> float:
|
|
2648
|
+
"""🧮
|
|
2649
|
+
Calculates the distance between two points.
|
|
2650
|
+
|
|
2651
|
+
This function also accepts two objects with x and y properties.
|
|
2652
|
+
|
|
2653
|
+
:param x1: x-coordinate of the first point
|
|
2654
|
+
:param y1: y-coordinate of the first point
|
|
2655
|
+
:param x2: x-coordinate of the second point
|
|
2656
|
+
:param y2: y-coordinate of the second point
|
|
2657
|
+
:returns: distance between the points
|
|
2658
|
+
|
|
2659
|
+
Example::
|
|
2660
|
+
|
|
2661
|
+
def draw():
|
|
2662
|
+
background(0.8)
|
|
2663
|
+
line(0, 0, mouseX, mouseY)
|
|
2664
|
+
|
|
2665
|
+
d = dist(0, 0, mouseX, mouseY)
|
|
2666
|
+
text(round(d), -80, -80)
|
|
2667
|
+
"""
|
|
2668
|
+
...
|
|
2669
|
+
|
|
2670
|
+
def map(val: float, start1: float, stop1: float, start2: float, stop2: float) -> float:
|
|
2671
|
+
"""🧮
|
|
2672
|
+
Maps a number from one range to another.
|
|
2673
|
+
|
|
2674
|
+
:param val: incoming value to be converted
|
|
2675
|
+
:param start1: lower bound of the value's current range
|
|
2676
|
+
:param stop1: upper bound of the value's current range
|
|
2677
|
+
:param start2: lower bound of the value's target range
|
|
2678
|
+
:param stop2: upper bound of the value's target range
|
|
2679
|
+
:returns: mapped value
|
|
2680
|
+
"""
|
|
2681
|
+
...
|
|
2682
|
+
|
|
2683
|
+
def angleMode(mode: Literal['degrees'] | Literal['radians']) -> None:
|
|
2684
|
+
"""🧮
|
|
2685
|
+
Sets the mode for interpreting and drawing angles. Can be either 'degrees' or 'radians'.
|
|
2686
|
+
|
|
2687
|
+
:param mode: mode to set for angle interpretation
|
|
2688
|
+
"""
|
|
2689
|
+
...
|
|
2690
|
+
|
|
2691
|
+
def radians(degrees: float) -> float:
|
|
2692
|
+
"""🧮
|
|
2693
|
+
Converts degrees to radians.
|
|
2694
|
+
|
|
2695
|
+
:param degrees: angle in degrees
|
|
2696
|
+
:returns: angle in radians
|
|
2697
|
+
"""
|
|
2698
|
+
...
|
|
2699
|
+
|
|
2700
|
+
def degrees(radians: float) -> float:
|
|
2701
|
+
"""🧮
|
|
2702
|
+
Converts radians to degrees.
|
|
2703
|
+
|
|
2704
|
+
:param radians: angle in radians
|
|
2705
|
+
:returns: angle in degrees
|
|
2706
|
+
"""
|
|
2707
|
+
...
|
|
2708
|
+
|
|
2709
|
+
def lerp(start: float, stop: float, amt: float) -> float:
|
|
2710
|
+
"""🧮
|
|
2711
|
+
Calculates a number between two numbers at a specific increment.
|
|
2712
|
+
|
|
2713
|
+
:param start: first number
|
|
2714
|
+
:param stop: second number
|
|
2715
|
+
:param amt: amount to interpolate between the two values
|
|
2716
|
+
:returns: interpolated number
|
|
2717
|
+
"""
|
|
2718
|
+
...
|
|
2719
|
+
|
|
2720
|
+
def constrain(n: float, low: float, high: float) -> float:
|
|
2721
|
+
"""🧮
|
|
2722
|
+
Constrains a value between a minimum and maximum value.
|
|
2723
|
+
|
|
2724
|
+
:param n: number to constrain
|
|
2725
|
+
:param low: lower bound
|
|
2726
|
+
:param high: upper bound
|
|
2727
|
+
:returns: constrained value
|
|
2728
|
+
"""
|
|
2729
|
+
...
|
|
2730
|
+
|
|
2731
|
+
def norm(n: float, start: float, stop: float) -> float:
|
|
2732
|
+
"""🧮
|
|
2733
|
+
Normalizes a number from another range into a value between 0 and 1.
|
|
2734
|
+
|
|
2735
|
+
:param n: number to normalize
|
|
2736
|
+
:param start: lower bound of the range
|
|
2737
|
+
:param stop: upper bound of the range
|
|
2738
|
+
:returns: normalized number
|
|
2739
|
+
"""
|
|
2740
|
+
...
|
|
2741
|
+
|
|
2742
|
+
def fract(n: float) -> float:
|
|
2743
|
+
"""🧮
|
|
2744
|
+
Calculates the fractional part of a number.
|
|
2745
|
+
|
|
2746
|
+
:param n: a number
|
|
2747
|
+
:returns: fractional part of the number
|
|
2748
|
+
"""
|
|
2749
|
+
...
|
|
2750
|
+
|
|
2751
|
+
def abs(n: float) -> float:
|
|
2752
|
+
"""🧮
|
|
2753
|
+
Calculates the absolute value of a number.
|
|
2754
|
+
|
|
2755
|
+
:param n: a number
|
|
2756
|
+
:returns: absolute value of the number
|
|
2757
|
+
"""
|
|
2758
|
+
...
|
|
2759
|
+
|
|
2760
|
+
def round(n: float, d: float = ...) -> float:
|
|
2761
|
+
"""🧮
|
|
2762
|
+
Rounds a number.
|
|
2763
|
+
|
|
2764
|
+
:param n: number to round
|
|
2765
|
+
:param d: number of decimal places to round to
|
|
2766
|
+
:returns: rounded number
|
|
2767
|
+
|
|
2768
|
+
Example::
|
|
2769
|
+
|
|
2770
|
+
Canvas(200, 100)
|
|
2771
|
+
background(0.8)
|
|
2772
|
+
textSize(32)
|
|
2773
|
+
text(round(PI, 5), -90, 10)
|
|
2774
|
+
"""
|
|
2775
|
+
...
|
|
2776
|
+
|
|
2777
|
+
def ceil(n: float) -> float:
|
|
2778
|
+
"""🧮
|
|
2779
|
+
Rounds a number up.
|
|
2780
|
+
|
|
2781
|
+
:param n: a number
|
|
2782
|
+
:returns: rounded number
|
|
2783
|
+
|
|
2784
|
+
Example::
|
|
2785
|
+
|
|
2786
|
+
Canvas(200, 100)
|
|
2787
|
+
background(0.8)
|
|
2788
|
+
textSize(32)
|
|
2789
|
+
text(ceil(PI), -90, 10)
|
|
2790
|
+
"""
|
|
2791
|
+
...
|
|
2792
|
+
|
|
2793
|
+
def floor(n: float) -> float:
|
|
2794
|
+
"""🧮
|
|
2795
|
+
Rounds a number down.
|
|
2796
|
+
|
|
2797
|
+
:param n: a number
|
|
2798
|
+
:returns: rounded number
|
|
2799
|
+
|
|
2800
|
+
Example::
|
|
2801
|
+
|
|
2802
|
+
Canvas(200, 100)
|
|
2803
|
+
background(0.8)
|
|
2804
|
+
textSize(32)
|
|
2805
|
+
text(floor(-PI), -90, 10)
|
|
2806
|
+
"""
|
|
2807
|
+
...
|
|
2808
|
+
|
|
2809
|
+
def min(*args: float) -> float:
|
|
2810
|
+
"""🧮
|
|
2811
|
+
Returns the smallest value in a sequence of numbers.
|
|
2812
|
+
|
|
2813
|
+
:param args: numbers to compare
|
|
2814
|
+
:returns: minimum
|
|
2815
|
+
|
|
2816
|
+
Example::
|
|
2817
|
+
|
|
2818
|
+
def draw():
|
|
2819
|
+
background(min(-mouseX / 100, 0.5))
|
|
2820
|
+
circle(min(mouseX, 0), 0, 80)
|
|
2821
|
+
"""
|
|
2822
|
+
...
|
|
2823
|
+
|
|
2824
|
+
def max(*args: float) -> float:
|
|
2825
|
+
"""🧮
|
|
2826
|
+
Returns the largest value in a sequence of numbers.
|
|
2827
|
+
|
|
2828
|
+
:param args: numbers to compare
|
|
2829
|
+
:returns: maximum
|
|
2830
|
+
|
|
2831
|
+
Example::
|
|
2832
|
+
|
|
2833
|
+
def draw():
|
|
2834
|
+
background(max(-mouseX / 100, 0.5))
|
|
2835
|
+
circle(max(mouseX, 0), 0, 80)
|
|
2836
|
+
"""
|
|
2837
|
+
...
|
|
2838
|
+
|
|
2839
|
+
def pow(base: float, exponent: float) -> float:
|
|
2840
|
+
"""🧮
|
|
2841
|
+
Calculates the value of a base raised to a power.
|
|
2842
|
+
|
|
2843
|
+
For example, pow(2, 3) calculates 2 _ 2 _ 2 which is 8.
|
|
2844
|
+
|
|
2845
|
+
:param base: base
|
|
2846
|
+
:param exponent: exponent
|
|
2847
|
+
:returns: base raised to the power of exponent
|
|
2848
|
+
"""
|
|
2849
|
+
...
|
|
2850
|
+
|
|
2851
|
+
def sq(n: float) -> float:
|
|
2852
|
+
"""🧮
|
|
2853
|
+
Calculates the square of a number.
|
|
2854
|
+
|
|
2855
|
+
:param n: number to square
|
|
2856
|
+
:returns: square of the number
|
|
2857
|
+
"""
|
|
2858
|
+
...
|
|
2859
|
+
|
|
2860
|
+
def sqrt(n: float) -> float:
|
|
2861
|
+
"""🧮
|
|
2862
|
+
Calculates the square root of a number.
|
|
2863
|
+
|
|
2864
|
+
:param n: a number
|
|
2865
|
+
:returns: square root of the number
|
|
2866
|
+
"""
|
|
2867
|
+
...
|
|
2868
|
+
|
|
2869
|
+
def loge(n: float) -> float:
|
|
2870
|
+
"""🧮
|
|
2871
|
+
Calculates the natural logarithm (base e) of a number.
|
|
2872
|
+
|
|
2873
|
+
:param n: a number
|
|
2874
|
+
:returns: natural logarithm of the number
|
|
2875
|
+
"""
|
|
2876
|
+
...
|
|
2877
|
+
|
|
2878
|
+
def exp(exponent: float) -> float:
|
|
2879
|
+
"""🧮
|
|
2880
|
+
Calculates e raised to the power of a number.
|
|
2881
|
+
|
|
2882
|
+
:param exponent: power to raise e to
|
|
2883
|
+
:returns: e raised to the power of exponent
|
|
2884
|
+
"""
|
|
2885
|
+
...
|
|
2886
|
+
|
|
2887
|
+
def randomSeed(seed: float) -> None:
|
|
2888
|
+
"""🧮
|
|
2889
|
+
Sets the seed for the random number generator.
|
|
2890
|
+
|
|
2891
|
+
:param seed: seed value
|
|
2892
|
+
"""
|
|
2893
|
+
...
|
|
2894
|
+
|
|
2895
|
+
def randomGenerator(method: Any) -> None:
|
|
2896
|
+
"""🧮
|
|
2897
|
+
Sets the random number generation method.
|
|
2898
|
+
|
|
2899
|
+
:param method: method to use for random number generation
|
|
2900
|
+
"""
|
|
2901
|
+
...
|
|
2902
|
+
|
|
2903
|
+
def randomGaussian(mean: float, std: float) -> float:
|
|
2904
|
+
"""🧮
|
|
2905
|
+
Generates a random number following a Gaussian (normal) distribution.
|
|
2906
|
+
|
|
2907
|
+
:param mean: mean (center) of the distribution
|
|
2908
|
+
:param std: standard deviation (spread or "width") of the distribution
|
|
2909
|
+
:returns: a random number following a Gaussian distribution
|
|
2910
|
+
"""
|
|
2911
|
+
...
|
|
2912
|
+
|
|
2913
|
+
def randomExponential() -> float:
|
|
2914
|
+
"""🧮
|
|
2915
|
+
Generates a random number following an exponential distribution.
|
|
2916
|
+
:returns: a random number following an exponential distribution
|
|
2917
|
+
"""
|
|
2918
|
+
...
|
|
2919
|
+
|
|
2920
|
+
def noiseMode(mode: Literal['perlin'] | Literal['simplex'] | Literal['blocky']) -> None:
|
|
2921
|
+
"""🧮
|
|
2922
|
+
Sets the noise generation mode.
|
|
2923
|
+
|
|
2924
|
+
Only the default mode, "perlin", is included in q5.js. Use of the
|
|
2925
|
+
other modes requires the q5-noiser module.
|
|
2926
|
+
|
|
2927
|
+
:param mode: noise generation mode
|
|
2928
|
+
"""
|
|
2929
|
+
...
|
|
2930
|
+
|
|
2931
|
+
def noiseSeed(seed: float) -> None:
|
|
2932
|
+
"""🧮
|
|
2933
|
+
Sets the seed value for noise generation.
|
|
2934
|
+
|
|
2935
|
+
:param seed: seed value
|
|
2936
|
+
"""
|
|
2937
|
+
...
|
|
2938
|
+
|
|
2939
|
+
def noiseDetail(lod: float, falloff: float) -> None:
|
|
2940
|
+
"""🧮
|
|
2941
|
+
Sets the level of detail for noise generation.
|
|
2942
|
+
|
|
2943
|
+
:param lod: level of detail (number of octaves)
|
|
2944
|
+
:param falloff: falloff rate for each octave
|
|
2945
|
+
"""
|
|
2946
|
+
...
|
|
2947
|
+
|
|
2948
|
+
PI: float
|
|
2949
|
+
"""🧮
|
|
2950
|
+
The ratio of a circle's circumference to its diameter.
|
|
2951
|
+
Approximately 3.14159.
|
|
2952
|
+
"""
|
|
2953
|
+
|
|
2954
|
+
TWO_PI: float
|
|
2955
|
+
"""🧮
|
|
2956
|
+
2 * PI.
|
|
2957
|
+
Approximately 6.28319.
|
|
2958
|
+
"""
|
|
2959
|
+
|
|
2960
|
+
TAU: float
|
|
2961
|
+
"""🧮
|
|
2962
|
+
2 * PI.
|
|
2963
|
+
Approximately 6.28319.
|
|
2964
|
+
"""
|
|
2965
|
+
|
|
2966
|
+
HALF_PI: float
|
|
2967
|
+
"""🧮
|
|
2968
|
+
Half of PI.
|
|
2969
|
+
Approximately 1.5708.
|
|
2970
|
+
"""
|
|
2971
|
+
|
|
2972
|
+
QUARTER_PI: float
|
|
2973
|
+
"""🧮
|
|
2974
|
+
A quarter of PI.
|
|
2975
|
+
Approximately 0.7854.
|
|
2976
|
+
"""
|
|
2977
|
+
|
|
2978
|
+
# 🔊 sound
|
|
2979
|
+
|
|
2980
|
+
"""🔊
|
|
2981
|
+
q5 includes low latency sound playback and basic mixing capabilities
|
|
2982
|
+
powered by WebAudio.
|
|
2983
|
+
|
|
2984
|
+
For audio filtering, synthesis, and analysis, consider using the
|
|
2985
|
+
p5.sound addon with q5.
|
|
2986
|
+
"""
|
|
2987
|
+
|
|
2988
|
+
def loadSound(url: str) -> Sound:
|
|
2989
|
+
"""🔊
|
|
2990
|
+
Loads audio data from a file and returns a Sound object.
|
|
2991
|
+
|
|
2992
|
+
Use functions like play, pause, and stop to
|
|
2993
|
+
control playback. Note that sounds can only be played after the
|
|
2994
|
+
first user interaction with the page!
|
|
2995
|
+
|
|
2996
|
+
Set volume to a value between 0 (silent) and 1 (full volume).
|
|
2997
|
+
Set pan to a value between -1 (left) and 1 (right) to adjust
|
|
2998
|
+
the sound's stereo position. Set loop to true to loop the sound.
|
|
2999
|
+
|
|
3000
|
+
Use loaded, paused, and ended to check the sound's status.
|
|
3001
|
+
|
|
3002
|
+
The entire sound file must be loaded before playback can start, use await to wait for a sound to load. To stream larger audio files use the loadAudio function instead.
|
|
3003
|
+
|
|
3004
|
+
:param url: sound file
|
|
3005
|
+
:returns: sound
|
|
3006
|
+
|
|
3007
|
+
Example::
|
|
3008
|
+
|
|
3009
|
+
Canvas(200)
|
|
3010
|
+
|
|
3011
|
+
sound = loadSound('/assets/jump.wav')
|
|
3012
|
+
sound.volume = 0.3
|
|
3013
|
+
|
|
3014
|
+
def mousePressed():
|
|
3015
|
+
sound.play()
|
|
3016
|
+
"""
|
|
3017
|
+
...
|
|
3018
|
+
|
|
3019
|
+
def loadAudio() -> None:
|
|
3020
|
+
"""🔊
|
|
3021
|
+
Loads audio data from a file and returns an HTMLAudioElement.
|
|
3022
|
+
|
|
3023
|
+
Audio is considered loaded when the canplaythrough event is fired.
|
|
3024
|
+
|
|
3025
|
+
Note that audio can only be played after the first user
|
|
3026
|
+
interaction with the page!
|
|
3027
|
+
|
|
3028
|
+
Example::
|
|
3029
|
+
|
|
3030
|
+
Canvas(200)
|
|
3031
|
+
|
|
3032
|
+
audio = loadAudio('/assets/retro.flac')
|
|
3033
|
+
audio.volume = 0.4
|
|
3034
|
+
|
|
3035
|
+
def mousePressed():
|
|
3036
|
+
audio.play()
|
|
3037
|
+
"""
|
|
3038
|
+
...
|
|
3039
|
+
|
|
3040
|
+
def getAudioContext() -> Any:
|
|
3041
|
+
"""🔊
|
|
3042
|
+
Returns the AudioContext in use or undefined if it doesn't exist.
|
|
3043
|
+
:returns: AudioContext instance
|
|
3044
|
+
"""
|
|
3045
|
+
...
|
|
3046
|
+
|
|
3047
|
+
async def userStartAudio() -> object:
|
|
3048
|
+
"""🔊
|
|
3049
|
+
Creates a new AudioContext or resumes it if it was suspended.
|
|
3050
|
+
:returns: a promise that resolves when the AudioContext is resumed
|
|
3051
|
+
"""
|
|
3052
|
+
...
|
|
3053
|
+
|
|
3054
|
+
class Sound:
|
|
3055
|
+
"""🔊
|
|
3056
|
+
Creates a new Q5.Sound object.
|
|
3057
|
+
"""
|
|
3058
|
+
|
|
3059
|
+
|
|
3060
|
+
def volume(self) -> None:
|
|
3061
|
+
"""🔊 Set the sound's volume to a value between
|
|
3062
|
+
0 (silent) and 1 (full volume)."""
|
|
3063
|
+
...
|
|
3064
|
+
|
|
3065
|
+
def pan(self) -> None:
|
|
3066
|
+
"""🔊 Set the sound's stereo position between -1 (left) and 1 (right)."""
|
|
3067
|
+
...
|
|
3068
|
+
|
|
3069
|
+
def loop(self) -> None:
|
|
3070
|
+
"""🔊 Set to true to make the sound loop continuously."""
|
|
3071
|
+
...
|
|
3072
|
+
|
|
3073
|
+
def loaded(self) -> None:
|
|
3074
|
+
"""🔊 True if the sound data has finished loading."""
|
|
3075
|
+
...
|
|
3076
|
+
|
|
3077
|
+
def paused(self) -> None:
|
|
3078
|
+
"""🔊 True if the sound is currently paused."""
|
|
3079
|
+
...
|
|
3080
|
+
|
|
3081
|
+
def ended(self) -> None:
|
|
3082
|
+
"""🔊 True if the sound has finished playing."""
|
|
3083
|
+
...
|
|
3084
|
+
|
|
3085
|
+
def play(self) -> object:
|
|
3086
|
+
"""🔊 Plays the sound.
|
|
3087
|
+
|
|
3088
|
+
If this function is run when the sound is already playing,
|
|
3089
|
+
a new playback will start, causing a layering effect.
|
|
3090
|
+
|
|
3091
|
+
If this function is run when the sound is paused,
|
|
3092
|
+
all playback instances will be resumed.
|
|
3093
|
+
|
|
3094
|
+
Use await to wait for the sound to finish playing."""
|
|
3095
|
+
...
|
|
3096
|
+
|
|
3097
|
+
def pause(self) -> None:
|
|
3098
|
+
"""🔊 Pauses the sound, allowing it to be resumed."""
|
|
3099
|
+
...
|
|
3100
|
+
|
|
3101
|
+
def stop(self) -> None:
|
|
3102
|
+
"""🔊 Stops the sound, resetting its playback position
|
|
3103
|
+
to the beginning.
|
|
3104
|
+
|
|
3105
|
+
Removes all playback instances."""
|
|
3106
|
+
...
|
|
3107
|
+
|
|
3108
|
+
# 📑 dom
|
|
3109
|
+
|
|
3110
|
+
"""📑
|
|
3111
|
+
The Document Object Model (DOM) is an interface for
|
|
3112
|
+
creating and editing web pages with JavaScript.
|
|
3113
|
+
"""
|
|
3114
|
+
|
|
3115
|
+
def createEl(tag: str, content: str = ...) -> object:
|
|
3116
|
+
"""📑
|
|
3117
|
+
Creates a new HTML element and adds it to the page. createElement is
|
|
3118
|
+
an alias.
|
|
3119
|
+
|
|
3120
|
+
Modify the element's CSS style to change its appearance.
|
|
3121
|
+
|
|
3122
|
+
Use addEventListener to respond to events such as:
|
|
3123
|
+
|
|
3124
|
+
- "click": when the element is clicked
|
|
3125
|
+
- "mouseover": when the mouse hovers over the element
|
|
3126
|
+
- "mouseout": when the mouse stops hovering over the element
|
|
3127
|
+
- "input": when a form element's value changes
|
|
3128
|
+
|
|
3129
|
+
q5 adds some extra functionality to the elements it creates:
|
|
3130
|
+
|
|
3131
|
+
- the position function makes it easy to place the element
|
|
3132
|
+
relative to the canvas
|
|
3133
|
+
- the size function sets the width and height of the element
|
|
3134
|
+
- alternatively, use the element's x, y, width, and height properties
|
|
3135
|
+
|
|
3136
|
+
:param tag: tag name of the element
|
|
3137
|
+
:param content: content of the element
|
|
3138
|
+
:returns: element
|
|
3139
|
+
|
|
3140
|
+
Example::
|
|
3141
|
+
|
|
3142
|
+
Canvas(200)
|
|
3143
|
+
|
|
3144
|
+
el = createEl('div', '*')
|
|
3145
|
+
el.position(50, 50)
|
|
3146
|
+
el.size(100, 100)
|
|
3147
|
+
el.style.fontSize = '136px'
|
|
3148
|
+
el.style.textAlign = 'center'
|
|
3149
|
+
el.style.backgroundColor = 'blue'
|
|
3150
|
+
el.style.color = 'white'
|
|
3151
|
+
"""
|
|
3152
|
+
...
|
|
3153
|
+
|
|
3154
|
+
def createA(href: str, text: str = ..., newTab: bool = ...) -> None:
|
|
3155
|
+
"""📑
|
|
3156
|
+
Creates a link element.
|
|
3157
|
+
|
|
3158
|
+
:param href: url
|
|
3159
|
+
:param text: text content
|
|
3160
|
+
:param newTab: whether to open the link in a new tab
|
|
3161
|
+
|
|
3162
|
+
Example::
|
|
3163
|
+
|
|
3164
|
+
Canvas(200)
|
|
3165
|
+
|
|
3166
|
+
link = createA('https://q5js.org', 'q5.js')
|
|
3167
|
+
link.position(16, 42)
|
|
3168
|
+
link.style.fontSize = '80px'
|
|
3169
|
+
|
|
3170
|
+
link.addEventListener('mouseover', () =>
|
|
3171
|
+
background('cyan')
|
|
3172
|
+
})
|
|
3173
|
+
"""
|
|
3174
|
+
...
|
|
3175
|
+
|
|
3176
|
+
def createButton(content: str = ...) -> None:
|
|
3177
|
+
"""📑
|
|
3178
|
+
Creates a button element.
|
|
3179
|
+
|
|
3180
|
+
:param content: text content
|
|
3181
|
+
|
|
3182
|
+
Example::
|
|
3183
|
+
|
|
3184
|
+
Canvas(200, 100)
|
|
3185
|
+
|
|
3186
|
+
btn = createButton('Click me!')
|
|
3187
|
+
|
|
3188
|
+
btn.addEventListener('click', () =>
|
|
3189
|
+
background(random(0.4, 1))
|
|
3190
|
+
})
|
|
3191
|
+
"""
|
|
3192
|
+
...
|
|
3193
|
+
|
|
3194
|
+
def createCheckbox(label: str = ..., checked: bool = ...) -> None:
|
|
3195
|
+
"""📑
|
|
3196
|
+
Creates a checkbox element.
|
|
3197
|
+
|
|
3198
|
+
Use the checked property to get or set the checkbox's state.
|
|
3199
|
+
|
|
3200
|
+
The label property is the text label element next to the checkbox.
|
|
3201
|
+
|
|
3202
|
+
:param label: text label placed next to the checkbox
|
|
3203
|
+
:param checked: initial state
|
|
3204
|
+
|
|
3205
|
+
Example::
|
|
3206
|
+
|
|
3207
|
+
Canvas(200, 100)
|
|
3208
|
+
|
|
3209
|
+
box = createCheckbox('Check me!')
|
|
3210
|
+
box.label.style.color = 'lime'
|
|
3211
|
+
|
|
3212
|
+
box.addEventListener('input', () =>
|
|
3213
|
+
if box.checked: background('lime')
|
|
3214
|
+
else: background('black')
|
|
3215
|
+
})
|
|
3216
|
+
"""
|
|
3217
|
+
...
|
|
3218
|
+
|
|
3219
|
+
def createColorPicker(value: str = ...) -> None:
|
|
3220
|
+
"""📑
|
|
3221
|
+
Creates a color input element.
|
|
3222
|
+
|
|
3223
|
+
Use the value property to get or set the color value.
|
|
3224
|
+
|
|
3225
|
+
:param value: initial color value
|
|
3226
|
+
|
|
3227
|
+
Example::
|
|
3228
|
+
|
|
3229
|
+
Canvas(200, 100)
|
|
3230
|
+
|
|
3231
|
+
picker = createColorPicker()
|
|
3232
|
+
picker.value = '#fd7575'
|
|
3233
|
+
|
|
3234
|
+
def draw():
|
|
3235
|
+
background(picker.value)
|
|
3236
|
+
"""
|
|
3237
|
+
...
|
|
3238
|
+
|
|
3239
|
+
def createImg(src: str) -> None:
|
|
3240
|
+
"""📑
|
|
3241
|
+
Creates an image element.
|
|
3242
|
+
|
|
3243
|
+
:param src: url of the image
|
|
3244
|
+
|
|
3245
|
+
Example::
|
|
3246
|
+
|
|
3247
|
+
Canvas(200, 100)
|
|
3248
|
+
|
|
3249
|
+
img = createImg('/assets/q5play_logo.avif')
|
|
3250
|
+
img.position(0, 0).size(100, 100)
|
|
3251
|
+
"""
|
|
3252
|
+
...
|
|
3253
|
+
|
|
3254
|
+
def createInput(value: str = ..., type: str = ...) -> None:
|
|
3255
|
+
"""📑
|
|
3256
|
+
Creates an input element.
|
|
3257
|
+
|
|
3258
|
+
Use the value property to get or set the input's value.
|
|
3259
|
+
|
|
3260
|
+
Use the placeholder property to set label text that appears
|
|
3261
|
+
inside the input when it's empty.
|
|
3262
|
+
|
|
3263
|
+
See MDN's input documentation for the full list of input types.
|
|
3264
|
+
|
|
3265
|
+
:param value: initial value
|
|
3266
|
+
:param type: text input type, can be 'text', 'password', 'email', 'number', 'range', 'search', 'tel', 'url'
|
|
3267
|
+
|
|
3268
|
+
Example::
|
|
3269
|
+
|
|
3270
|
+
Canvas(200, 100)
|
|
3271
|
+
textSize(64)
|
|
3272
|
+
|
|
3273
|
+
input = createInput()
|
|
3274
|
+
input.placeholder = 'Type here!'
|
|
3275
|
+
input.size(200, 32)
|
|
3276
|
+
|
|
3277
|
+
input.addEventListener('input', () =>
|
|
3278
|
+
background('orange')
|
|
3279
|
+
text(input.value, -90, 30)
|
|
3280
|
+
})
|
|
3281
|
+
"""
|
|
3282
|
+
...
|
|
3283
|
+
|
|
3284
|
+
def createP(content: str = ...) -> None:
|
|
3285
|
+
"""📑
|
|
3286
|
+
Creates a paragraph element.
|
|
3287
|
+
|
|
3288
|
+
:param content: text content
|
|
3289
|
+
|
|
3290
|
+
Example::
|
|
3291
|
+
|
|
3292
|
+
Canvas(200, 50)
|
|
3293
|
+
background('coral')
|
|
3294
|
+
|
|
3295
|
+
p = createP('Hello, world!')
|
|
3296
|
+
p.style.color = 'pink'
|
|
3297
|
+
"""
|
|
3298
|
+
...
|
|
3299
|
+
|
|
3300
|
+
def createRadio(groupName: str = ...) -> None:
|
|
3301
|
+
"""📑
|
|
3302
|
+
Creates a radio button group.
|
|
3303
|
+
|
|
3304
|
+
Use the option(label, value) function to add new radio buttons
|
|
3305
|
+
to the group.
|
|
3306
|
+
|
|
3307
|
+
Use the value property to get or set the value of the selected radio button.
|
|
3308
|
+
|
|
3309
|
+
:param groupName:
|
|
3310
|
+
|
|
3311
|
+
Example::
|
|
3312
|
+
|
|
3313
|
+
Canvas(200, 160)
|
|
3314
|
+
|
|
3315
|
+
radio = createRadio()
|
|
3316
|
+
radio.option('square', '1')
|
|
3317
|
+
radio.option('circle', '2')
|
|
3318
|
+
|
|
3319
|
+
def draw():
|
|
3320
|
+
background(0.8)
|
|
3321
|
+
if radio.value == '1': square(-40, -40, 80)
|
|
3322
|
+
if radio.value == '2': circle(0, 0, 80)
|
|
3323
|
+
"""
|
|
3324
|
+
...
|
|
3325
|
+
|
|
3326
|
+
def createSelect(placeholder: str = ...) -> None:
|
|
3327
|
+
"""📑
|
|
3328
|
+
Creates a select element.
|
|
3329
|
+
|
|
3330
|
+
Use the option(label, value) function to add new options to
|
|
3331
|
+
the select element.
|
|
3332
|
+
|
|
3333
|
+
Set multiple to true to allow multiple options to be selected.
|
|
3334
|
+
|
|
3335
|
+
Use the value property to get or set the selected option value.
|
|
3336
|
+
|
|
3337
|
+
Use the selected property get the labels of the selected
|
|
3338
|
+
options or set the selected options by label. Can be a single
|
|
3339
|
+
string or an array of strings.
|
|
3340
|
+
|
|
3341
|
+
:param placeholder: optional placeholder text that appears before an option is selected
|
|
3342
|
+
|
|
3343
|
+
Example::
|
|
3344
|
+
|
|
3345
|
+
Canvas(200, 100)
|
|
3346
|
+
|
|
3347
|
+
sel = createSelect('Select an option')
|
|
3348
|
+
sel.option('Red', '#f55')
|
|
3349
|
+
sel.option('Green', '#5f5')
|
|
3350
|
+
|
|
3351
|
+
sel.addEventListener('change', () =>
|
|
3352
|
+
background(sel.value)
|
|
3353
|
+
})
|
|
3354
|
+
"""
|
|
3355
|
+
...
|
|
3356
|
+
|
|
3357
|
+
def createSlider(min: float, max: float, value: float = ..., step: float = ...) -> None:
|
|
3358
|
+
"""📑
|
|
3359
|
+
Creates a slider element.
|
|
3360
|
+
|
|
3361
|
+
Use the value property to get or set the slider's value.
|
|
3362
|
+
|
|
3363
|
+
Use the val function to get the slider's value as a number.
|
|
3364
|
+
|
|
3365
|
+
:param min: minimum value
|
|
3366
|
+
:param max: maximum value
|
|
3367
|
+
:param value: initial value
|
|
3368
|
+
:param step: step size
|
|
3369
|
+
|
|
3370
|
+
Example::
|
|
3371
|
+
|
|
3372
|
+
Canvas(200)
|
|
3373
|
+
|
|
3374
|
+
slider = createSlider(0, 1, 0.5, 0.1)
|
|
3375
|
+
slider.position(10, 10).size(180)
|
|
3376
|
+
|
|
3377
|
+
def draw():
|
|
3378
|
+
background(slider.val())
|
|
3379
|
+
"""
|
|
3380
|
+
...
|
|
3381
|
+
|
|
3382
|
+
def createVideo(src: str) -> object:
|
|
3383
|
+
"""📑
|
|
3384
|
+
Creates a video element.
|
|
3385
|
+
|
|
3386
|
+
Note that videos must be muted to autoplay and the play and
|
|
3387
|
+
pause functions can only be run after a user interaction.
|
|
3388
|
+
|
|
3389
|
+
The video element can be hidden and its content can be
|
|
3390
|
+
displayed on the canvas using the image function.
|
|
3391
|
+
|
|
3392
|
+
:param src: url of the video
|
|
3393
|
+
:returns: a new video element
|
|
3394
|
+
|
|
3395
|
+
Example::
|
|
3396
|
+
|
|
3397
|
+
Canvas(1)
|
|
3398
|
+
|
|
3399
|
+
vid = createVideo('/assets/apollo4.mp4')
|
|
3400
|
+
vid.size(200, 150)
|
|
3401
|
+
vid.autoplay = vid.muted = vid.loop = True
|
|
3402
|
+
vid.controls = True
|
|
3403
|
+
"""
|
|
3404
|
+
...
|
|
3405
|
+
|
|
3406
|
+
def createCapture(type: str = ..., flipped: bool = ...) -> object:
|
|
3407
|
+
"""📑
|
|
3408
|
+
Creates a capture from a connected camera, such as a webcam.
|
|
3409
|
+
|
|
3410
|
+
The capture video element can be hidden and its content can be
|
|
3411
|
+
displayed on the canvas using the image function.
|
|
3412
|
+
|
|
3413
|
+
Can preload to ensure the capture is ready to use when your
|
|
3414
|
+
sketch starts.
|
|
3415
|
+
|
|
3416
|
+
Requests the highest video resolution from the user facing camera
|
|
3417
|
+
by default. The first parameter to this function can be used to
|
|
3418
|
+
specify the constraints for the capture. See getUserMedia
|
|
3419
|
+
for more info.
|
|
3420
|
+
|
|
3421
|
+
:param type: type of capture, can be only VIDEO or only AUDIO, the default is to use both video and audio
|
|
3422
|
+
:param flipped: whether to mirror the video vertically, true by default
|
|
3423
|
+
:returns: a new video element
|
|
3424
|
+
|
|
3425
|
+
Example::
|
|
3426
|
+
|
|
3427
|
+
def mousePressed():
|
|
3428
|
+
cap = createCapture(VIDEO)
|
|
3429
|
+
cap.size(200, 112.5)
|
|
3430
|
+
canvas.remove()
|
|
3431
|
+
"""
|
|
3432
|
+
...
|
|
3433
|
+
|
|
3434
|
+
def findEl(selector: str) -> object:
|
|
3435
|
+
"""📑
|
|
3436
|
+
Finds the first element in the DOM that matches the given CSS selector.
|
|
3437
|
+
|
|
3438
|
+
Alias for document.querySelector.
|
|
3439
|
+
|
|
3440
|
+
:param selector:
|
|
3441
|
+
:returns: element
|
|
3442
|
+
"""
|
|
3443
|
+
...
|
|
3444
|
+
|
|
3445
|
+
def findEls(selector: str) -> list[object]:
|
|
3446
|
+
"""📑
|
|
3447
|
+
Finds all elements in the DOM that match the given CSS selector.
|
|
3448
|
+
|
|
3449
|
+
Alias for document.querySelectorAll.
|
|
3450
|
+
|
|
3451
|
+
:param selector:
|
|
3452
|
+
:returns: elements
|
|
3453
|
+
"""
|
|
3454
|
+
...
|
|
3455
|
+
|
|
3456
|
+
# 🎞 record
|
|
3457
|
+
|
|
3458
|
+
def createRecorder() -> object:
|
|
3459
|
+
"""🎞
|
|
3460
|
+
Creates a recorder. Simply hit record to start recording!
|
|
3461
|
+
|
|
3462
|
+
The following properties can be set via the recorder UI or
|
|
3463
|
+
programmatically.
|
|
3464
|
+
|
|
3465
|
+
- format is set to "H.264" by default.
|
|
3466
|
+
- bitrate is a number in megabits per second (mbps). Its default
|
|
3467
|
+
value is determined by the height of the canvas. Increasing the
|
|
3468
|
+
bitrate will increase the quality and file size of the recording.
|
|
3469
|
+
- captureAudio is set to true by default. Set to false to disable
|
|
3470
|
+
audio recording.
|
|
3471
|
+
|
|
3472
|
+
Note that recordings are done at a variable frame rate (VFR), which
|
|
3473
|
+
makes the output video incompatible with some editing software.
|
|
3474
|
+
For more info, see the
|
|
3475
|
+
"Recording the Canvas".
|
|
3476
|
+
wiki page.
|
|
3477
|
+
:returns: a recorder, q5 DOM element
|
|
3478
|
+
|
|
3479
|
+
Example::
|
|
3480
|
+
|
|
3481
|
+
Canvas(200)
|
|
3482
|
+
|
|
3483
|
+
rec = createRecorder()
|
|
3484
|
+
rec.bitrate = 10
|
|
3485
|
+
|
|
3486
|
+
def draw():
|
|
3487
|
+
circle(mouseX, jit(halfHeight), 10)
|
|
3488
|
+
"""
|
|
3489
|
+
...
|
|
3490
|
+
|
|
3491
|
+
def record() -> None:
|
|
3492
|
+
"""🎞
|
|
3493
|
+
Starts recording the canvas or resumes recording if it was paused.
|
|
3494
|
+
|
|
3495
|
+
If no recorder exists, one is created but not displayed.
|
|
3496
|
+
"""
|
|
3497
|
+
...
|
|
3498
|
+
|
|
3499
|
+
def pauseRecording() -> None:
|
|
3500
|
+
"""🎞
|
|
3501
|
+
Pauses the canvas recording, if one is in progress.
|
|
3502
|
+
"""
|
|
3503
|
+
...
|
|
3504
|
+
|
|
3505
|
+
def deleteRecording() -> None:
|
|
3506
|
+
"""🎞
|
|
3507
|
+
Discards the current recording.
|
|
3508
|
+
"""
|
|
3509
|
+
...
|
|
3510
|
+
|
|
3511
|
+
def saveRecording(fileName: str) -> None:
|
|
3512
|
+
"""🎞
|
|
3513
|
+
Saves the current recording as a video file.
|
|
3514
|
+
|
|
3515
|
+
:param fileName:
|
|
3516
|
+
|
|
3517
|
+
Example::
|
|
3518
|
+
|
|
3519
|
+
def draw():
|
|
3520
|
+
square(mouseX, jit(100), 10)
|
|
3521
|
+
|
|
3522
|
+
def mousePressed():
|
|
3523
|
+
if !recording: record()
|
|
3524
|
+
else: saveRecording('squares')
|
|
3525
|
+
"""
|
|
3526
|
+
...
|
|
3527
|
+
|
|
3528
|
+
recording: bool
|
|
3529
|
+
"""🎞
|
|
3530
|
+
True if the canvas is currently being recorded.
|
|
3531
|
+
"""
|
|
3532
|
+
|
|
3533
|
+
# 🛠 utilities
|
|
3534
|
+
|
|
3535
|
+
async def load(*urls: str) -> object:
|
|
3536
|
+
"""🛠
|
|
3537
|
+
Loads a file or multiple files.
|
|
3538
|
+
|
|
3539
|
+
File type is determined by file extension. q5 supports loading
|
|
3540
|
+
text, json, csv, font, audio, and image files.
|
|
3541
|
+
|
|
3542
|
+
By default, assets are loaded in parallel before q5 runs draw. Use await to wait for assets to load.
|
|
3543
|
+
|
|
3544
|
+
:param urls:
|
|
3545
|
+
:returns: a promise that resolves with objects
|
|
3546
|
+
|
|
3547
|
+
Example::
|
|
3548
|
+
|
|
3549
|
+
Canvas(200)
|
|
3550
|
+
|
|
3551
|
+
logo = load('/q5js_logo.avif')
|
|
3552
|
+
|
|
3553
|
+
def draw():
|
|
3554
|
+
image(logo, -100, -100, 200, 200)
|
|
3555
|
+
"""
|
|
3556
|
+
...
|
|
3557
|
+
|
|
3558
|
+
def save(data: dict = ..., fileName: str = ...) -> None:
|
|
3559
|
+
"""🛠
|
|
3560
|
+
Saves data to a file.
|
|
3561
|
+
|
|
3562
|
+
If data is not specified, the canvas will be saved.
|
|
3563
|
+
|
|
3564
|
+
If no arguments are provided, the canvas will be saved as
|
|
3565
|
+
an image file named "untitled.png".
|
|
3566
|
+
|
|
3567
|
+
:param data: canvas, image, or JS object
|
|
3568
|
+
:param fileName: filename to save as
|
|
3569
|
+
|
|
3570
|
+
Example::
|
|
3571
|
+
|
|
3572
|
+
Canvas(200)
|
|
3573
|
+
background(0.8)
|
|
3574
|
+
circle(0, 0, 50)
|
|
3575
|
+
|
|
3576
|
+
def mousePressed():
|
|
3577
|
+
save('circle.png')
|
|
3578
|
+
"""
|
|
3579
|
+
...
|
|
3580
|
+
|
|
3581
|
+
def millis() -> float:
|
|
3582
|
+
"""🛠
|
|
3583
|
+
Returns the number of milliseconds since the program started.
|
|
3584
|
+
:returns: milliseconds since the program started
|
|
3585
|
+
|
|
3586
|
+
Example::
|
|
3587
|
+
|
|
3588
|
+
Canvas(200)
|
|
3589
|
+
|
|
3590
|
+
def draw():
|
|
3591
|
+
background(0.8)
|
|
3592
|
+
|
|
3593
|
+
if millis(: > 2000)
|
|
3594
|
+
text('Hello, world!', -90, 0)
|
|
3595
|
+
"""
|
|
3596
|
+
...
|
|
3597
|
+
|
|
3598
|
+
def loadText(url: str) -> dict:
|
|
3599
|
+
"""🛠
|
|
3600
|
+
Loads a text file from the specified url.
|
|
3601
|
+
|
|
3602
|
+
Using await to get the loaded text as a string is recommended.
|
|
3603
|
+
|
|
3604
|
+
:param url: text file
|
|
3605
|
+
:returns: an object containing the loaded text in the property obj.text or use await to get the text string directly
|
|
3606
|
+
"""
|
|
3607
|
+
...
|
|
3608
|
+
|
|
3609
|
+
def loadJSON(url: str) -> Any:
|
|
3610
|
+
"""🛠
|
|
3611
|
+
Loads a JSON file from the specified url.
|
|
3612
|
+
|
|
3613
|
+
Using await to get the loaded JSON object or array is recommended.
|
|
3614
|
+
|
|
3615
|
+
:param url: JSON file
|
|
3616
|
+
:returns: an object or array containing the loaded JSON
|
|
3617
|
+
"""
|
|
3618
|
+
...
|
|
3619
|
+
|
|
3620
|
+
def loadCSV(url: str) -> list[dict]:
|
|
3621
|
+
"""🛠
|
|
3622
|
+
Loads a CSV file from the specified url.
|
|
3623
|
+
|
|
3624
|
+
Using await to get the loaded CSV as an array of objects is recommended.
|
|
3625
|
+
|
|
3626
|
+
:param url: CSV file
|
|
3627
|
+
:returns: an array of objects containing the loaded CSV
|
|
3628
|
+
"""
|
|
3629
|
+
...
|
|
3630
|
+
|
|
3631
|
+
def loadXML(url: str) -> Any:
|
|
3632
|
+
"""🛠
|
|
3633
|
+
Loads an xml file from the specified url.
|
|
3634
|
+
|
|
3635
|
+
Using await to get the loaded XML Element is recommended.
|
|
3636
|
+
|
|
3637
|
+
:param url: xml file
|
|
3638
|
+
:returns: an object containing the loaded XML Element in a property called obj.DOM or use await to get the XML Element directly
|
|
3639
|
+
|
|
3640
|
+
Example::
|
|
3641
|
+
|
|
3642
|
+
Canvas(200)
|
|
3643
|
+
background(0.8)
|
|
3644
|
+
textSize(32)
|
|
3645
|
+
|
|
3646
|
+
xml = await load('/assets/animals.xml')
|
|
3647
|
+
mammals = xml.querySelectorAll('mammal')
|
|
3648
|
+
y = -90
|
|
3649
|
+
for (let mammal of mammals)
|
|
3650
|
+
text(mammal.textContent, -90, (y += 32))
|
|
3651
|
+
"""
|
|
3652
|
+
...
|
|
3653
|
+
|
|
3654
|
+
def loadAll() -> object:
|
|
3655
|
+
"""🛠
|
|
3656
|
+
Wait for any assets that started loading to finish loading. By default q5 runs this before looping draw (which is called preloading), but it can be used even after draw starts looping.
|
|
3657
|
+
:returns: a promise that resolves with loaded objects
|
|
3658
|
+
"""
|
|
3659
|
+
...
|
|
3660
|
+
|
|
3661
|
+
def disablePreload() -> None:
|
|
3662
|
+
"""🛠
|
|
3663
|
+
Disables the automatic preloading of assets before draw starts looping. This allows draw to start immediately, and assets can be lazy loaded or loadAll() can be used to wait for assets to finish loading later.
|
|
3664
|
+
"""
|
|
3665
|
+
...
|
|
3666
|
+
|
|
3667
|
+
def nf(num: float, digits: float) -> str:
|
|
3668
|
+
"""🛠
|
|
3669
|
+
nf is short for number format. It formats a number
|
|
3670
|
+
to a string with a specified number of digits.
|
|
3671
|
+
|
|
3672
|
+
:param num: number to format
|
|
3673
|
+
:param digits: number of digits to format to
|
|
3674
|
+
:returns: formatted number
|
|
3675
|
+
|
|
3676
|
+
Example::
|
|
3677
|
+
|
|
3678
|
+
Canvas(200, 100)
|
|
3679
|
+
background(0.8)
|
|
3680
|
+
|
|
3681
|
+
textSize(32)
|
|
3682
|
+
text(nf(PI, 4, 5), -90, 10)
|
|
3683
|
+
"""
|
|
3684
|
+
...
|
|
3685
|
+
|
|
3686
|
+
def shuffle(arr: list[Any], modify: bool = ...) -> list[Any]:
|
|
3687
|
+
"""🛠
|
|
3688
|
+
Shuffles the elements of an array.
|
|
3689
|
+
|
|
3690
|
+
:param arr: array to shuffle
|
|
3691
|
+
:param modify: whether to modify the original array, false by default which copies the array before shuffling
|
|
3692
|
+
:returns: shuffled array
|
|
3693
|
+
"""
|
|
3694
|
+
...
|
|
3695
|
+
|
|
3696
|
+
def storeItem(key: str, val: str) -> None:
|
|
3697
|
+
"""🛠
|
|
3698
|
+
Stores an item in localStorage.
|
|
3699
|
+
|
|
3700
|
+
:param key: key under which to store the item
|
|
3701
|
+
:param val: value to store
|
|
3702
|
+
"""
|
|
3703
|
+
...
|
|
3704
|
+
|
|
3705
|
+
def getItem(key: str) -> str:
|
|
3706
|
+
"""🛠
|
|
3707
|
+
Retrieves an item from localStorage.
|
|
3708
|
+
|
|
3709
|
+
:param key: key of the item to retrieve
|
|
3710
|
+
:returns: value of the retrieved item
|
|
3711
|
+
"""
|
|
3712
|
+
...
|
|
3713
|
+
|
|
3714
|
+
def removeItem(key: str) -> None:
|
|
3715
|
+
"""🛠
|
|
3716
|
+
Removes an item from localStorage.
|
|
3717
|
+
|
|
3718
|
+
:param key: key of the item to remove
|
|
3719
|
+
"""
|
|
3720
|
+
...
|
|
3721
|
+
|
|
3722
|
+
def clearStorage() -> None:
|
|
3723
|
+
"""🛠
|
|
3724
|
+
Clears all items from localStorage.
|
|
3725
|
+
"""
|
|
3726
|
+
...
|
|
3727
|
+
|
|
3728
|
+
def year() -> float:
|
|
3729
|
+
"""🛠
|
|
3730
|
+
Returns the current year.
|
|
3731
|
+
:returns: current year
|
|
3732
|
+
"""
|
|
3733
|
+
...
|
|
3734
|
+
|
|
3735
|
+
def day() -> float:
|
|
3736
|
+
"""🛠
|
|
3737
|
+
Returns the current day of the month.
|
|
3738
|
+
:returns: current day
|
|
3739
|
+
"""
|
|
3740
|
+
...
|
|
3741
|
+
|
|
3742
|
+
def hour() -> float:
|
|
3743
|
+
"""🛠
|
|
3744
|
+
Returns the current hour.
|
|
3745
|
+
:returns: current hour
|
|
3746
|
+
"""
|
|
3747
|
+
...
|
|
3748
|
+
|
|
3749
|
+
def minute() -> float:
|
|
3750
|
+
"""🛠
|
|
3751
|
+
Returns the current minute.
|
|
3752
|
+
:returns: current minute
|
|
3753
|
+
"""
|
|
3754
|
+
...
|
|
3755
|
+
|
|
3756
|
+
def second() -> float:
|
|
3757
|
+
"""🛠
|
|
3758
|
+
Returns the current second.
|
|
3759
|
+
:returns: current second
|
|
3760
|
+
"""
|
|
3761
|
+
...
|
|
3762
|
+
|
|
3763
|
+
# ↗ vector
|
|
3764
|
+
|
|
3765
|
+
class Vector:
|
|
3766
|
+
"""↗
|
|
3767
|
+
Constructs a new Vector object.
|
|
3768
|
+
"""
|
|
3769
|
+
|
|
3770
|
+
|
|
3771
|
+
def x(self) -> None:
|
|
3772
|
+
"""↗ The x component of the vector."""
|
|
3773
|
+
...
|
|
3774
|
+
|
|
3775
|
+
def y(self) -> None:
|
|
3776
|
+
"""↗ The y component of the vector."""
|
|
3777
|
+
...
|
|
3778
|
+
|
|
3779
|
+
def z(self) -> None:
|
|
3780
|
+
"""↗ The z component of the vector, if applicable."""
|
|
3781
|
+
...
|
|
3782
|
+
|
|
3783
|
+
def add(self, v: Vector) -> Vector:
|
|
3784
|
+
"""↗ Adds a vector to this vector."""
|
|
3785
|
+
...
|
|
3786
|
+
|
|
3787
|
+
def sub(self, v: Vector) -> Vector:
|
|
3788
|
+
"""↗ Subtracts a vector from this vector."""
|
|
3789
|
+
...
|
|
3790
|
+
|
|
3791
|
+
def mult(self, n: float | Vector) -> Vector:
|
|
3792
|
+
"""↗ Multiplies this vector by a scalar or element-wise by another vector."""
|
|
3793
|
+
...
|
|
3794
|
+
|
|
3795
|
+
def div(self, n: float | Vector) -> Vector:
|
|
3796
|
+
"""↗ Divides this vector by a scalar or element-wise by another vector."""
|
|
3797
|
+
...
|
|
3798
|
+
|
|
3799
|
+
def mag(self) -> float:
|
|
3800
|
+
"""↗ Calculates the magnitude (length) of the vector."""
|
|
3801
|
+
...
|
|
3802
|
+
|
|
3803
|
+
def normalize(self) -> Vector:
|
|
3804
|
+
"""↗ Normalizes the vector to a length of 1 (making it a unit vector)."""
|
|
3805
|
+
...
|
|
3806
|
+
|
|
3807
|
+
def setMag(self, len: float) -> Vector:
|
|
3808
|
+
"""↗ Sets the magnitude of the vector to the specified length."""
|
|
3809
|
+
...
|
|
3810
|
+
|
|
3811
|
+
def dot(self, v: Vector) -> float:
|
|
3812
|
+
"""↗ Calculates the dot product of this vector and another vector."""
|
|
3813
|
+
...
|
|
3814
|
+
|
|
3815
|
+
def cross(self, v: Vector) -> Vector:
|
|
3816
|
+
"""↗ Calculates the cross product of this vector and another vector."""
|
|
3817
|
+
...
|
|
3818
|
+
|
|
3819
|
+
def dist(self, v: Vector) -> float:
|
|
3820
|
+
"""↗ Calculates the distance between this vector and another vector."""
|
|
3821
|
+
...
|
|
3822
|
+
|
|
3823
|
+
def copy(self) -> Vector:
|
|
3824
|
+
"""↗ Copies this vector."""
|
|
3825
|
+
...
|
|
3826
|
+
|
|
3827
|
+
def set(self, x: float, y: float, z: float = ...) -> None:
|
|
3828
|
+
"""↗ Sets the components of the vector."""
|
|
3829
|
+
...
|
|
3830
|
+
|
|
3831
|
+
def limit(self, max: float) -> Vector:
|
|
3832
|
+
"""↗ Limits the magnitude of the vector to the value used for the max parameter."""
|
|
3833
|
+
...
|
|
3834
|
+
|
|
3835
|
+
def heading(self) -> float:
|
|
3836
|
+
"""↗ Calculates the angle of rotation for this vector (only 2D vectors)."""
|
|
3837
|
+
...
|
|
3838
|
+
|
|
3839
|
+
def setHeading(self, angle: float) -> Vector:
|
|
3840
|
+
"""↗ Rotates the vector to a specific angle without changing its magnitude."""
|
|
3841
|
+
...
|
|
3842
|
+
|
|
3843
|
+
def rotate(self, angle: float) -> Vector:
|
|
3844
|
+
"""↗ Rotates the vector by the given angle (only 2D vectors)."""
|
|
3845
|
+
...
|
|
3846
|
+
|
|
3847
|
+
def lerp(self, v: Vector, amt: float) -> Vector:
|
|
3848
|
+
"""↗ Linearly interpolates between this vector and another vector."""
|
|
3849
|
+
...
|
|
3850
|
+
|
|
3851
|
+
def slerp(self, v: Vector, amt: float) -> Vector:
|
|
3852
|
+
"""↗ Linearly interpolates between this vector and another vector, including the magnitude."""
|
|
3853
|
+
...
|
|
3854
|
+
|
|
3855
|
+
# 🖌 shaping
|
|
3856
|
+
|
|
3857
|
+
def arc(x: float, y: float, w: float, h: float, start: float, stop: float, mode: float = ...) -> None:
|
|
3858
|
+
"""🖌
|
|
3859
|
+
Draws an arc, which is a section of an ellipse.
|
|
3860
|
+
|
|
3861
|
+
ellipseMode affects how the arc is drawn.
|
|
3862
|
+
|
|
3863
|
+
q5 WebGPU only supports the default PIE_OPEN mode.
|
|
3864
|
+
|
|
3865
|
+
:param x: x-coordinate
|
|
3866
|
+
:param y: y-coordinate
|
|
3867
|
+
:param w: width of the ellipse
|
|
3868
|
+
:param h: height of the ellipse
|
|
3869
|
+
:param start: angle to start the arc
|
|
3870
|
+
:param stop: angle to stop the arc
|
|
3871
|
+
:param mode: shape and stroke style setting, default is PIE_OPEN for a pie shape with an unclosed stroke, can be PIE, CHORD, or CHORD_OPEN
|
|
3872
|
+
|
|
3873
|
+
Example::
|
|
3874
|
+
|
|
3875
|
+
Canvas(200)
|
|
3876
|
+
background(0.8)
|
|
3877
|
+
|
|
3878
|
+
arc(0, 0, 160, 160, 0.8, -0.8)
|
|
3879
|
+
"""
|
|
3880
|
+
...
|
|
3881
|
+
|
|
3882
|
+
def curve() -> None:
|
|
3883
|
+
"""🖌
|
|
3884
|
+
Draws a curve.
|
|
3885
|
+
|
|
3886
|
+
Example::
|
|
3887
|
+
|
|
3888
|
+
Canvas(200, 100)
|
|
3889
|
+
background(0.8)
|
|
3890
|
+
|
|
3891
|
+
curve(-100, -200, -50, 0, 50, 0, 100, -200)
|
|
3892
|
+
"""
|
|
3893
|
+
...
|
|
3894
|
+
|
|
3895
|
+
def curveDetail(val: float) -> None:
|
|
3896
|
+
"""🖌
|
|
3897
|
+
Sets the amount of straight line segments used to make a curve.
|
|
3898
|
+
|
|
3899
|
+
Only takes effect in q5 WebGPU.
|
|
3900
|
+
|
|
3901
|
+
:param val: curve detail level, default is 20
|
|
3902
|
+
|
|
3903
|
+
Example::
|
|
3904
|
+
|
|
3905
|
+
Canvas(200)
|
|
3906
|
+
|
|
3907
|
+
curveDetail(4)
|
|
3908
|
+
|
|
3909
|
+
strokeWeight(10)
|
|
3910
|
+
stroke(0, 1, 1)
|
|
3911
|
+
curve(-100, -200, -50, 0, 50, 0, 100, -200)
|
|
3912
|
+
"""
|
|
3913
|
+
...
|
|
3914
|
+
|
|
3915
|
+
def beginShape() -> None:
|
|
3916
|
+
"""🖌
|
|
3917
|
+
Starts storing vertices for a shape.
|
|
3918
|
+
"""
|
|
3919
|
+
...
|
|
3920
|
+
|
|
3921
|
+
def endShape(close: bool = ...) -> None:
|
|
3922
|
+
"""🖌
|
|
3923
|
+
Ends storing vertices for a shape.
|
|
3924
|
+
|
|
3925
|
+
:param close: whether to close the shape by connecting the last vertex to the first vertex, default is false
|
|
3926
|
+
"""
|
|
3927
|
+
...
|
|
3928
|
+
|
|
3929
|
+
def vertex(x: float, y: float) -> None:
|
|
3930
|
+
"""🖌
|
|
3931
|
+
Specifies a vertex in a shape.
|
|
3932
|
+
|
|
3933
|
+
:param x: x-coordinate
|
|
3934
|
+
:param y: y-coordinate
|
|
3935
|
+
|
|
3936
|
+
Example::
|
|
3937
|
+
|
|
3938
|
+
Canvas(200)
|
|
3939
|
+
|
|
3940
|
+
stroke(1, 0.5)
|
|
3941
|
+
strokeWeight(20)
|
|
3942
|
+
|
|
3943
|
+
beginShape()
|
|
3944
|
+
fill(1, 0, 0)
|
|
3945
|
+
vertex(-80, -80)
|
|
3946
|
+
vertex(40, -60)
|
|
3947
|
+
fill(0, 0, 1)
|
|
3948
|
+
vertex(80, 60)
|
|
3949
|
+
vertex(-60, 80)
|
|
3950
|
+
endShape(True)
|
|
3951
|
+
"""
|
|
3952
|
+
...
|
|
3953
|
+
|
|
3954
|
+
def bezierVertex(cp1x: float, cp1y: float, cp2x: float, cp2y: float, x: float, y: float) -> None:
|
|
3955
|
+
"""🖌
|
|
3956
|
+
Specifies a Bezier vertex in a shape.
|
|
3957
|
+
|
|
3958
|
+
:param cp1x: x-coordinate of the first control point
|
|
3959
|
+
:param cp1y: y-coordinate of the first control point
|
|
3960
|
+
:param cp2x: x-coordinate of the second control point
|
|
3961
|
+
:param cp2y: y-coordinate of the second control point
|
|
3962
|
+
:param x: x-coordinate of the anchor point
|
|
3963
|
+
:param y: y-coordinate of the anchor point
|
|
3964
|
+
"""
|
|
3965
|
+
...
|
|
3966
|
+
|
|
3967
|
+
def quadraticVertex(cp1x: float, cp1y: float, x: float, y: float) -> None:
|
|
3968
|
+
"""🖌
|
|
3969
|
+
Specifies a quadratic Bezier vertex in a shape.
|
|
3970
|
+
|
|
3971
|
+
:param cp1x: x-coordinate of the control point
|
|
3972
|
+
:param cp1y: y-coordinate of the control point
|
|
3973
|
+
:param x: x-coordinate of the anchor point
|
|
3974
|
+
:param y: y-coordinate of the anchor point
|
|
3975
|
+
"""
|
|
3976
|
+
...
|
|
3977
|
+
|
|
3978
|
+
def bezier(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, x4: float, y4: float) -> None:
|
|
3979
|
+
"""🖌
|
|
3980
|
+
Draws a Bezier curve.
|
|
3981
|
+
|
|
3982
|
+
:param x1: x-coordinate of the first anchor point
|
|
3983
|
+
:param y1: y-coordinate of the first anchor point
|
|
3984
|
+
:param x2: x-coordinate of the first control point
|
|
3985
|
+
:param y2: y-coordinate of the first control point
|
|
3986
|
+
:param x3: x-coordinate of the second control point
|
|
3987
|
+
:param y3: y-coordinate of the second control point
|
|
3988
|
+
:param x4: x-coordinate of the second anchor point
|
|
3989
|
+
:param y4: y-coordinate of the second anchor point
|
|
3990
|
+
"""
|
|
3991
|
+
...
|
|
3992
|
+
|
|
3993
|
+
def triangle(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float) -> None:
|
|
3994
|
+
"""🖌
|
|
3995
|
+
Draws a triangle.
|
|
3996
|
+
|
|
3997
|
+
:param x1: x-coordinate of the first vertex
|
|
3998
|
+
:param y1: y-coordinate of the first vertex
|
|
3999
|
+
:param x2: x-coordinate of the second vertex
|
|
4000
|
+
:param y2: y-coordinate of the second vertex
|
|
4001
|
+
:param x3: x-coordinate of the third vertex
|
|
4002
|
+
:param y3: y-coordinate of the third vertex
|
|
4003
|
+
"""
|
|
4004
|
+
...
|
|
4005
|
+
|
|
4006
|
+
def quad(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, x4: float, y4: float) -> None:
|
|
4007
|
+
"""🖌
|
|
4008
|
+
Draws a quadrilateral.
|
|
4009
|
+
|
|
4010
|
+
:param x1: x-coordinate of the first vertex
|
|
4011
|
+
:param y1: y-coordinate of the first vertex
|
|
4012
|
+
:param x2: x-coordinate of the second vertex
|
|
4013
|
+
:param y2: y-coordinate of the second vertex
|
|
4014
|
+
:param x3: x-coordinate of the third vertex
|
|
4015
|
+
:param y3: y-coordinate of the third vertex
|
|
4016
|
+
:param x4: x-coordinate of the fourth vertex
|
|
4017
|
+
:param y4: y-coordinate of the fourth vertex
|
|
4018
|
+
"""
|
|
4019
|
+
...
|
|
4020
|
+
|
|
4021
|
+
def beginContour() -> None:
|
|
4022
|
+
"""🖌
|
|
4023
|
+
Starts storing vertices for a contour.
|
|
4024
|
+
|
|
4025
|
+
Not available in q5 WebGPU.
|
|
4026
|
+
"""
|
|
4027
|
+
...
|
|
4028
|
+
|
|
4029
|
+
def endContour() -> None:
|
|
4030
|
+
"""🖌
|
|
4031
|
+
Ends storing vertices for a contour.
|
|
4032
|
+
|
|
4033
|
+
Not available in q5 WebGPU.
|
|
4034
|
+
"""
|
|
4035
|
+
...
|
|
4036
|
+
|
|
4037
|
+
# ⚡ shaders
|
|
4038
|
+
|
|
4039
|
+
"""⚡
|
|
4040
|
+
Custom shaders written in WGSL (WebGPU Shading Language) can be
|
|
4041
|
+
used to create advanced visual effects in q5!
|
|
4042
|
+
"""
|
|
4043
|
+
|
|
4044
|
+
def createShader(code: str, type: str = ..., data: list[float] = ...) -> Any:
|
|
4045
|
+
"""⚡
|
|
4046
|
+
Creates a shader that q5's WebGPU renderer can use.
|
|
4047
|
+
|
|
4048
|
+
If type is not specified, this function customizes a copy of the default shapes shader, which affects these functions: plane, line, and endShape.
|
|
4049
|
+
|
|
4050
|
+
For more information on the vertex and fragment function
|
|
4051
|
+
input parameters, data, and helper functions made available for use
|
|
4052
|
+
in your custom shader code, read the
|
|
4053
|
+
"Custom Shaders in q5 WebGPU"
|
|
4054
|
+
wiki page.
|
|
4055
|
+
|
|
4056
|
+
:param code: WGSL shader code excerpt
|
|
4057
|
+
:param type: defaults to "shapes"
|
|
4058
|
+
:param data: only for use with fully custom shaders
|
|
4059
|
+
:returns: a shader program
|
|
4060
|
+
|
|
4061
|
+
Example::
|
|
4062
|
+
|
|
4063
|
+
Canvas(200)
|
|
4064
|
+
|
|
4065
|
+
wobble = createShader(`
|
|
4066
|
+
@vertex
|
|
4067
|
+
fn vertexMain(v: VertexParams) -> FragParams
|
|
4068
|
+
vert = transformVertex(v.pos, v.matrixIndex)
|
|
4069
|
+
|
|
4070
|
+
i = f32(v.vertexIndex) % 4 * 100
|
|
4071
|
+
vert.x += cos((q.time + i) * 0.01) * 0.1
|
|
4072
|
+
|
|
4073
|
+
f: FragParams
|
|
4074
|
+
f.position = vert
|
|
4075
|
+
f.color = vec4f(1, 0, 0, 1)
|
|
4076
|
+
return f
|
|
4077
|
+
}`)
|
|
4078
|
+
|
|
4079
|
+
def draw():
|
|
4080
|
+
clear()
|
|
4081
|
+
shader(wobble)
|
|
4082
|
+
plane(0, 0, 100)
|
|
4083
|
+
"""
|
|
4084
|
+
...
|
|
4085
|
+
|
|
4086
|
+
def plane(x: float, y: float, w: float, h: float = ...) -> None:
|
|
4087
|
+
"""⚡
|
|
4088
|
+
A plane is a centered rectangle with no stroke.
|
|
4089
|
+
|
|
4090
|
+
:param x: center x
|
|
4091
|
+
:param y: center y
|
|
4092
|
+
:param w: width or side length
|
|
4093
|
+
:param h: height
|
|
4094
|
+
|
|
4095
|
+
Example::
|
|
4096
|
+
|
|
4097
|
+
Canvas(200)
|
|
4098
|
+
plane(0, 0, 100)
|
|
4099
|
+
"""
|
|
4100
|
+
...
|
|
4101
|
+
|
|
4102
|
+
def shader(shaderModule: Any) -> None:
|
|
4103
|
+
"""⚡
|
|
4104
|
+
Applies a shader.
|
|
4105
|
+
|
|
4106
|
+
:param shaderModule: a shader program
|
|
4107
|
+
"""
|
|
4108
|
+
...
|
|
4109
|
+
|
|
4110
|
+
def resetShader() -> None:
|
|
4111
|
+
"""⚡
|
|
4112
|
+
Make q5 use the default shapes shader.
|
|
4113
|
+
|
|
4114
|
+
Example::
|
|
4115
|
+
|
|
4116
|
+
Canvas(200)
|
|
4117
|
+
|
|
4118
|
+
stripes = createShader(`
|
|
4119
|
+
@fragment
|
|
4120
|
+
fn fragMain(f: FragParams) -> @location(0) vec4f
|
|
4121
|
+
g = cos((q.mouseY + f.position.y) * 0.05)
|
|
4122
|
+
return vec4(1, g, 0, 1)
|
|
4123
|
+
}`)
|
|
4124
|
+
|
|
4125
|
+
def draw():
|
|
4126
|
+
shader(stripes)
|
|
4127
|
+
plane(0, 0, width, height)
|
|
4128
|
+
|
|
4129
|
+
resetShader()
|
|
4130
|
+
triangle(-50, -50, 0, 50, 50, -50)
|
|
4131
|
+
"""
|
|
4132
|
+
...
|
|
4133
|
+
|
|
4134
|
+
def resetFrameShader() -> None:
|
|
4135
|
+
"""⚡
|
|
4136
|
+
Make q5 use the default frame shader.
|
|
4137
|
+
"""
|
|
4138
|
+
...
|
|
4139
|
+
|
|
4140
|
+
def resetImageShader() -> None:
|
|
4141
|
+
"""⚡
|
|
4142
|
+
Make q5 use the default image shader.
|
|
4143
|
+
"""
|
|
4144
|
+
...
|
|
4145
|
+
|
|
4146
|
+
def resetVideoShader() -> None:
|
|
4147
|
+
"""⚡
|
|
4148
|
+
Make q5 use the default video shader.
|
|
4149
|
+
"""
|
|
4150
|
+
...
|
|
4151
|
+
|
|
4152
|
+
def resetTextShader() -> None:
|
|
4153
|
+
"""⚡
|
|
4154
|
+
Make q5 use the default text shader.
|
|
4155
|
+
"""
|
|
4156
|
+
...
|
|
4157
|
+
|
|
4158
|
+
def resetShaders() -> None:
|
|
4159
|
+
"""⚡
|
|
4160
|
+
Make q5 use all default shaders.
|
|
4161
|
+
"""
|
|
4162
|
+
...
|
|
4163
|
+
|
|
4164
|
+
def createFrameShader() -> None:
|
|
4165
|
+
"""⚡
|
|
4166
|
+
Creates a shader that q5 can use to draw frames.
|
|
4167
|
+
|
|
4168
|
+
Use this function to customize a copy of the
|
|
4169
|
+
default frame shader.
|
|
4170
|
+
|
|
4171
|
+
Example::
|
|
4172
|
+
|
|
4173
|
+
Canvas(200, 400)
|
|
4174
|
+
stroke(1)
|
|
4175
|
+
strokeWeight(8)
|
|
4176
|
+
strokeCap(PROJECT)
|
|
4177
|
+
|
|
4178
|
+
boxy = createFrameShader(`
|
|
4179
|
+
@fragment
|
|
4180
|
+
fn fragMain(f: FragParams) -> @location(0) vec4f
|
|
4181
|
+
x = sin(f.texCoord.y * 4 + q.time * 0.002)
|
|
4182
|
+
y = cos(f.texCoord.x * 4 + q.time * 0.002)
|
|
4183
|
+
uv = f.texCoord + vec2f(x, y)
|
|
4184
|
+
return textureSample(tex, samp, uv)
|
|
4185
|
+
}`)
|
|
4186
|
+
|
|
4187
|
+
def draw():
|
|
4188
|
+
line(mouseX, mouseY, pmouseX, pmouseY)
|
|
4189
|
+
mouseIsPressed ? resetShaders() : shader(boxy)
|
|
4190
|
+
"""
|
|
4191
|
+
...
|
|
4192
|
+
|
|
4193
|
+
def createImageShader(code: str) -> Any:
|
|
4194
|
+
"""⚡
|
|
4195
|
+
Creates a shader that q5 can use to draw images.
|
|
4196
|
+
|
|
4197
|
+
Use this function to customize a copy of the
|
|
4198
|
+
default image shader.
|
|
4199
|
+
|
|
4200
|
+
:param code: WGSL shader code excerpt
|
|
4201
|
+
:returns: a shader program
|
|
4202
|
+
|
|
4203
|
+
Example::
|
|
4204
|
+
|
|
4205
|
+
Canvas(200, 400)
|
|
4206
|
+
imageMode(CENTER)
|
|
4207
|
+
|
|
4208
|
+
logo = loadImage('/q5js_logo.avif')
|
|
4209
|
+
|
|
4210
|
+
grate = createImageShader(`
|
|
4211
|
+
@fragment
|
|
4212
|
+
fn fragMain(f: FragParams) -> @location(0) vec4f
|
|
4213
|
+
texColor = textureSample(tex, samp, f.texCoord)
|
|
4214
|
+
texColor.b += (q.mouseX + f.position.x) % 20 / 10
|
|
4215
|
+
return texColor
|
|
4216
|
+
}`)
|
|
4217
|
+
|
|
4218
|
+
def draw():
|
|
4219
|
+
background(0.7)
|
|
4220
|
+
shader(grate)
|
|
4221
|
+
image(logo, 0, 0, 180, 180)
|
|
4222
|
+
"""
|
|
4223
|
+
...
|
|
4224
|
+
|
|
4225
|
+
def createVideoShader(code: str) -> Any:
|
|
4226
|
+
"""⚡
|
|
4227
|
+
Creates a shader that q5 can use to draw video frames.
|
|
4228
|
+
|
|
4229
|
+
Use this function to customize a copy of the
|
|
4230
|
+
default video shader.
|
|
4231
|
+
|
|
4232
|
+
:param code: WGSL shader code excerpt
|
|
4233
|
+
:returns: a shader program
|
|
4234
|
+
|
|
4235
|
+
Example::
|
|
4236
|
+
|
|
4237
|
+
Canvas(200, 600)
|
|
4238
|
+
|
|
4239
|
+
vid = createVideo('/assets/apollo4.mp4')
|
|
4240
|
+
vid.hide()
|
|
4241
|
+
|
|
4242
|
+
flipper = createVideoShader(`
|
|
4243
|
+
@vertex
|
|
4244
|
+
fn vertexMain(v: VertexParams) -> FragParams
|
|
4245
|
+
vert = transformVertex(v.pos, v.matrixIndex)
|
|
4246
|
+
|
|
4247
|
+
vi = f32(v.vertexIndex)
|
|
4248
|
+
vert.y *= cos((q.frameCount + vi * 10) * 0.03)
|
|
4249
|
+
|
|
4250
|
+
f: FragParams
|
|
4251
|
+
f.position = vert
|
|
4252
|
+
f.texCoord = v.texCoord
|
|
4253
|
+
return f
|
|
4254
|
+
|
|
4255
|
+
@fragment
|
|
4256
|
+
fn fragMain(f: FragParams) -> @location(0) vec4f
|
|
4257
|
+
texColor = textureSampleBaseClampToEdge(tex, samp, f.texCoord)
|
|
4258
|
+
texColor.r = 0
|
|
4259
|
+
texColor.b *= 2
|
|
4260
|
+
return texColor
|
|
4261
|
+
}`)
|
|
4262
|
+
|
|
4263
|
+
def draw():
|
|
4264
|
+
clear()
|
|
4265
|
+
if mouseIsPressed: vid.play()
|
|
4266
|
+
shader(flipper)
|
|
4267
|
+
image(vid, -100, 150, 200, 150)
|
|
4268
|
+
"""
|
|
4269
|
+
...
|
|
4270
|
+
|
|
4271
|
+
def createTextShader(code: str) -> Any:
|
|
4272
|
+
"""⚡
|
|
4273
|
+
Creates a shader that q5 can use to draw text.
|
|
4274
|
+
|
|
4275
|
+
Use this function to customize a copy of the
|
|
4276
|
+
default text shader.
|
|
4277
|
+
|
|
4278
|
+
:param code: WGSL shader code excerpt
|
|
4279
|
+
:returns: a shader program
|
|
4280
|
+
|
|
4281
|
+
Example::
|
|
4282
|
+
|
|
4283
|
+
Canvas(200)
|
|
4284
|
+
textAlign(CENTER, CENTER)
|
|
4285
|
+
|
|
4286
|
+
spin = createTextShader(`
|
|
4287
|
+
@vertex
|
|
4288
|
+
fn vertexMain(v : VertexParams) -> FragParams
|
|
4289
|
+
char = textChars[v.instanceIndex]
|
|
4290
|
+
text = textMetadata[i32(char.w)]
|
|
4291
|
+
fontChar = fontChars[i32(char.z)]
|
|
4292
|
+
pos = calcPos(v.vertexIndex, char, fontChar, text)
|
|
4293
|
+
|
|
4294
|
+
vert = transformVertex(pos, text.matrixIndex)
|
|
4295
|
+
|
|
4296
|
+
i = f32(v.instanceIndex + 1)
|
|
4297
|
+
vert.y *= cos((q.frameCount - 5 * i) * 0.05)
|
|
4298
|
+
|
|
4299
|
+
f : FragParams
|
|
4300
|
+
f.position = vert
|
|
4301
|
+
f.texCoord = calcUV(v.vertexIndex, fontChar)
|
|
4302
|
+
f.fillColor = colors[i32(text.fillIndex)]
|
|
4303
|
+
f.strokeColor = colors[i32(text.strokeIndex)]
|
|
4304
|
+
f.strokeWeight = text.strokeWeight
|
|
4305
|
+
f.edge = text.edge
|
|
4306
|
+
return f
|
|
4307
|
+
}`)
|
|
4308
|
+
|
|
4309
|
+
def draw():
|
|
4310
|
+
clear()
|
|
4311
|
+
shader(spin)
|
|
4312
|
+
fill(1, 0, 1)
|
|
4313
|
+
textSize(32)
|
|
4314
|
+
text('Hello, World!', 0, 0)
|
|
4315
|
+
"""
|
|
4316
|
+
...
|
|
4317
|
+
|
|
4318
|
+
# ⚙ advanced
|
|
4319
|
+
|
|
4320
|
+
q5: type[Q5]
|
|
4321
|
+
"""⚙
|
|
4322
|
+
Alias for Q5.
|
|
4323
|
+
"""
|
|
4324
|
+
|
|
4325
|
+
class Q5:
|
|
4326
|
+
"""⚙
|
|
4327
|
+
Creates an instance of Q5.
|
|
4328
|
+
|
|
4329
|
+
Used by the global Canvas function.
|
|
4330
|
+
"""
|
|
4331
|
+
|
|
4332
|
+
|
|
4333
|
+
def version(self) -> str:
|
|
4334
|
+
"""⚙ The current minor version of q5."""
|
|
4335
|
+
...
|
|
4336
|
+
|
|
4337
|
+
def lang(self) -> None:
|
|
4338
|
+
"""⚙ Set to a language code other than 'en' (English) to use q5 in an additional language.
|
|
4339
|
+
|
|
4340
|
+
Currently supported languages:
|
|
4341
|
+
|
|
4342
|
+
- 'es' (Spanish)"""
|
|
4343
|
+
...
|
|
4344
|
+
|
|
4345
|
+
def disableFriendlyErrors(self) -> None:
|
|
4346
|
+
"""⚙ Turn off q5's friendly error messages."""
|
|
4347
|
+
...
|
|
4348
|
+
|
|
4349
|
+
def errorTolerant(self) -> None:
|
|
4350
|
+
"""⚙ Set to true to keep draw looping after an error."""
|
|
4351
|
+
...
|
|
4352
|
+
|
|
4353
|
+
def supportsHDR(self) -> None:
|
|
4354
|
+
"""⚙ True if the device supports HDR (the display-p3 colorspace)."""
|
|
4355
|
+
...
|
|
4356
|
+
|
|
4357
|
+
def canvasOptions(self) -> None:
|
|
4358
|
+
"""⚙ Sets the default canvas context attributes used for newly created
|
|
4359
|
+
canvases and internal graphics. These options are overwritten by any
|
|
4360
|
+
per-canvas options you pass to Canvas."""
|
|
4361
|
+
...
|
|
4362
|
+
|
|
4363
|
+
def MAX_TRANSFORMS(self) -> None:
|
|
4364
|
+
"""⚙ A WebGPU memory allocation limit.
|
|
4365
|
+
|
|
4366
|
+
The maximum number of transformation matrixes
|
|
4367
|
+
that can be used per frame."""
|
|
4368
|
+
...
|
|
4369
|
+
|
|
4370
|
+
def MAX_RECTS(self) -> None:
|
|
4371
|
+
"""⚙ A WebGPU memory allocation limit.
|
|
4372
|
+
|
|
4373
|
+
The maximum number of rectangles
|
|
4374
|
+
(calls to rect, square, capsule)
|
|
4375
|
+
that can be drawn per frame."""
|
|
4376
|
+
...
|
|
4377
|
+
|
|
4378
|
+
def MAX_ELLIPSES(self) -> None:
|
|
4379
|
+
"""⚙ A WebGPU memory allocation limit.
|
|
4380
|
+
|
|
4381
|
+
The maximum number of ellipses
|
|
4382
|
+
(calls to ellipse, circle, and arc)
|
|
4383
|
+
that can be drawn per frame."""
|
|
4384
|
+
...
|
|
4385
|
+
|
|
4386
|
+
def MAX_CHARS(self) -> None:
|
|
4387
|
+
"""⚙ A WebGPU memory allocation limit.
|
|
4388
|
+
|
|
4389
|
+
The maximum number of text characters
|
|
4390
|
+
that can be drawn per frame."""
|
|
4391
|
+
...
|
|
4392
|
+
|
|
4393
|
+
def MAX_TEXTS(self) -> None:
|
|
4394
|
+
"""⚙ A WebGPU memory allocation limit.
|
|
4395
|
+
|
|
4396
|
+
The maximum number of separate calls to text
|
|
4397
|
+
that can be drawn per frame."""
|
|
4398
|
+
...
|
|
4399
|
+
|
|
4400
|
+
def WebGPU(self) -> None:
|
|
4401
|
+
"""⚙ Creates a new Q5 instance that uses q5's WebGPU renderer."""
|
|
4402
|
+
...
|
|
4403
|
+
|
|
4404
|
+
def addHook(self, lifecycle: str, fn: Callable[..., Any]) -> None:
|
|
4405
|
+
"""⚙ Addons can augment q5 with new functionality by adding hooks,
|
|
4406
|
+
functions to be run at specific phases in the q5 lifecycle.
|
|
4407
|
+
|
|
4408
|
+
Inside the function, this refers to the Q5 instance."""
|
|
4409
|
+
...
|
|
4410
|
+
|
|
4411
|
+
def registerAddon(self, addon: Callable[..., Any]) -> None:
|
|
4412
|
+
"""⚙ p5.js v2 compatible way to register an addon with q5."""
|
|
4413
|
+
...
|
|
4414
|
+
|
|
4415
|
+
def modules(self) -> None:
|
|
4416
|
+
"""⚙ An object containing q5's modules, functions that run when q5 loads.
|
|
4417
|
+
|
|
4418
|
+
Each function receives two input parameters:
|
|
4419
|
+
|
|
4420
|
+
- the q5 instance
|
|
4421
|
+
- a proxy for editing the q5 instance and corresponding properties of the global scope"""
|
|
4422
|
+
...
|
|
4423
|
+
|
|
4424
|
+
def draw(self) -> None:
|
|
4425
|
+
"""⚙ The q5 draw function is run 60 times per second by default."""
|
|
4426
|
+
...
|
|
4427
|
+
|
|
4428
|
+
def postProcess(self) -> None:
|
|
4429
|
+
"""⚙ Runs after each draw function call and post-draw q5 addon processes, if any.
|
|
4430
|
+
|
|
4431
|
+
Useful for adding post-processing effects when it's not possible
|
|
4432
|
+
to do so at the end of the draw function, such as when using
|
|
4433
|
+
addons like q5play that auto-draw to the canvas after the draw
|
|
4434
|
+
function is run."""
|
|
4435
|
+
...
|