Skip to content

Commit c12916d

Browse files
2.23.14
1 parent fec0b68 commit c12916d

File tree

8 files changed

+45
-66
lines changed

8 files changed

+45
-66
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "q5",
3-
"version": "2.23.13",
3+
"version": "2.23.14",
44
"description": "Beginner friendly graphics powered by WebGPU and optimized for interactive art!",
55
"author": "quinton-ashley",
66
"contributors": [

q5.d.ts

+27-42
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ createCanvas(200);
17161716
let c = color('lime');
17171717
17181718
function draw() {
1719-
set(randomX(), randomY(), c);
1719+
set(random(200), random(200), c);
17201720
updatePixels();
17211721
}
17221722
*/
@@ -2376,11 +2376,13 @@ function mouseWheel(e) {
23762376

23772377
// 🧮 math
23782378

2379-
/** 🧮
2380-
* Generates random numbers. If no arguments are provided, returns a random number between 0 and 1.
2381-
* If one number argument is provided, returns a random number between 0 and the provided value.
2382-
* If two number arguments are provided, returns a random number between the two values.
2383-
* If an array is provided, returns a random element from the array.
2379+
/** 🧮
2380+
* Generates a random value.
2381+
*
2382+
* - If no inputs are provided, returns a number between 0 and 1.
2383+
* - If one numerical input is provided, returns a number between 0 and the provided value.
2384+
* - If two numerical inputs are provided, returns a number between the two values.
2385+
* - If an array is provided, returns a random element from the array.
23842386
* @param {number | any[]} [low] lower bound (inclusive) or an array
23852387
* @param {number} [high] upper bound (exclusive)
23862388
* @returns {number | any} a random number or element
@@ -2390,52 +2392,35 @@ background(200);
23902392
frameRate(5);
23912393
23922394
function draw() {
2393-
circle(100, 100, random(200));
2395+
circle(100, 100, random(20, 200));
23942396
}
2395-
*/
2396-
function random(low?: number | any[], high?: number): number | any;
2397-
2398-
/** 🧮
2399-
* Generates a random number within the range of the canvas width.
2400-
* @param {number} [margin] distance to extend (positive) or contract (negative) the range from canvas edges
2401-
* @returns {number} random x value
24022397
* @example
2403-
createCanvas(200, 100);
2404-
background(200);
2405-
2406-
function draw() {
2407-
circle(randomX(), 50, random(50));
2408-
}
2409-
* @example
2410-
createCanvas(200, 100);
2411-
background(200);
2412-
24132398
function draw() {
2414-
circle(randomX(-60), 50, random(50));
2399+
circle(random(200), random(200), 10);
24152400
}
24162401
*/
2417-
function randomX(margin?: number): number;
2402+
function random(low?: number | any[], high?: number): number | any;
24182403

2419-
/** 🧮
2420-
* Generates a random number within the range of the canvas height.
2421-
* @param {number} [margin] distance to extend (positive) or contract (negative) the range from canvas edges
2422-
* @returns {number} random y value
2404+
/**
2405+
* Generates a random number within a symmetric range around zero.
2406+
*
2407+
* Equivalent to `random(-val, val)`.
2408+
* @param {number} val absolute maximum value, default is 1
2409+
* @returns {number} random number between -val and val
24232410
* @example
2424-
createCanvas(200);
2425-
background(200);
2426-
24272411
function draw() {
2428-
circle(100, randomY(), random(50));
2412+
translate(mouseX, mouseY);
2413+
circle(randSym(5), randSym(5), 5);
24292414
}
24302415
* @example
2431-
createCanvas(200);
2432-
background(200);
2416+
let q = await Q5.WebGPU();
2417+
createCanvas(200, 100);
24332418
2434-
function draw() {
2435-
circle(randomX(), randomY(-60), 10);
2436-
}
2419+
q.draw = () => {
2420+
circle(randSym(100), 0, random(50));
2421+
};
24372422
*/
2438-
function randomY(margin?: number): number;
2423+
function randSym(val: number): number;
24392424

24402425
/** 🧮
24412426
* Calculates the distance between two points.
@@ -3131,7 +3116,7 @@ let rec = createRecorder();
31313116
rec.bitrate = 10;
31323117
31333118
function draw() {
3134-
circle(mouseX, randomY(), 10);
3119+
circle(mouseX, random(height), 10);
31353120
}
31363121
*/
31373122
function createRecorder(): HTMLElement;
@@ -3158,7 +3143,7 @@ function draw() {
31583143
* @param {string} fileName
31593144
* @example
31603145
function draw() {
3161-
square(mouseX, randomY(), 10);
3146+
square(mouseX, random(200), 10);
31623147
}
31633148
31643149
function mousePressed() {

q5.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function Q5(scope, parent, renderer) {
1616
$._renderer = renderer || Q5.render;
1717
$['_' + $._renderer] = true;
1818
}
19-
$._preloadCount = 0;
2019

2120
let autoLoaded = scope == 'auto';
2221
scope ??= 'global';
@@ -72,11 +71,13 @@ function Q5(scope, parent, renderer) {
7271
$.deviceOrientation = window.screen?.orientation?.type;
7372
}
7473

74+
$._preloadCount = 0;
7575
$._incrementPreload = () => q._preloadCount++;
7676
$._decrementPreload = () => q._preloadCount--;
7777

7878
$._usePreload = true;
7979
$.usePreloadSystem = (v) => ($._usePreload = v);
80+
$.isPreloadSupported = () => $._usePreload;
8081

8182
$._draw = (timestamp) => {
8283
let ts = timestamp || performance.now();
@@ -2743,6 +2744,8 @@ Q5.modules.dom = ($, q) => {
27432744
if ($.canvas) $.canvas.parentElement.append(el);
27442745
else document.body.append(el);
27452746

2747+
el.elt = el; // p5 compat
2748+
27462749
return el;
27472750
};
27482751
$.createEl = $.createElement;
@@ -3391,13 +3394,7 @@ Q5.modules.math = ($, q) => {
33913394
}
33923395
};
33933396

3394-
if ($._c2d) {
3395-
$.randomX = (v = 0) => $.random(-v, $.canvas.w + v);
3396-
$.randomY = (v = 0) => $.random(-v, $.canvas.h + v);
3397-
} else {
3398-
$.randomX = (v = 0) => $.random(-$.canvas.hw - v, $.canvas.hw + v);
3399-
$.randomY = (v = 0) => $.random(-$.canvas.hh - v, $.canvas.hh + v);
3400-
}
3397+
$.randSym = (v = 1) => $.random(-v, v);
34013398

34023399
$.randomGenerator = (method) => {
34033400
if (method == $.LCG) rng1 = lcg();
@@ -6095,9 +6092,9 @@ fn fragMain(f: FragParams) -> @location(0) vec4f {
60956092
}
60966093
}
60976094

6098-
if (shapeVertCount < 3) {
6099-
throw new Error('A shape must have at least 3 vertices.');
6100-
}
6095+
if (!shapeVertCount) return;
6096+
if (shapeVertCount == 1) return $.point(sv[0], -sv[1]);
6097+
if (shapeVertCount == 2) return $.line(sv[0], -sv[1], sv[4], -sv[5]);
61016098

61026099
// close the shape if requested
61036100
if (close) {

q5.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/q5-core.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function Q5(scope, parent, renderer) {
1616
$._renderer = renderer || Q5.render;
1717
$['_' + $._renderer] = true;
1818
}
19-
$._preloadCount = 0;
2019

2120
let autoLoaded = scope == 'auto';
2221
scope ??= 'global';
@@ -72,11 +71,13 @@ function Q5(scope, parent, renderer) {
7271
$.deviceOrientation = window.screen?.orientation?.type;
7372
}
7473

74+
$._preloadCount = 0;
7575
$._incrementPreload = () => q._preloadCount++;
7676
$._decrementPreload = () => q._preloadCount--;
7777

7878
$._usePreload = true;
7979
$.usePreloadSystem = (v) => ($._usePreload = v);
80+
$.isPreloadSupported = () => $._usePreload;
8081

8182
$._draw = (timestamp) => {
8283
let ts = timestamp || performance.now();

src/q5-dom.js

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Q5.modules.dom = ($, q) => {
9292
if ($.canvas) $.canvas.parentElement.append(el);
9393
else document.body.append(el);
9494

95+
el.elt = el; // p5 compat
96+
9597
return el;
9698
};
9799
$.createEl = $.createElement;

src/q5-math.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,7 @@ Q5.modules.math = ($, q) => {
138138
}
139139
};
140140

141-
if ($._c2d) {
142-
$.randomX = (v = 0) => $.random(-v, $.canvas.w + v);
143-
$.randomY = (v = 0) => $.random(-v, $.canvas.h + v);
144-
} else {
145-
$.randomX = (v = 0) => $.random(-$.canvas.hw - v, $.canvas.hw + v);
146-
$.randomY = (v = 0) => $.random(-$.canvas.hh - v, $.canvas.hh + v);
147-
}
141+
$.randSym = (v = 1) => $.random(-v, v);
148142

149143
$.randomGenerator = (method) => {
150144
if (method == $.LCG) rng1 = lcg();

src/q5-webgpu-shapes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,9 @@ fn fragMain(f: FragParams) -> @location(0) vec4f {
574574
}
575575
}
576576

577-
if (shapeVertCount < 3) {
578-
throw new Error('A shape must have at least 3 vertices.');
579-
}
577+
if (!shapeVertCount) return;
578+
if (shapeVertCount == 1) return $.point(sv[0], -sv[1]);
579+
if (shapeVertCount == 2) return $.line(sv[0], -sv[1], sv[4], -sv[5]);
580580

581581
// close the shape if requested
582582
if (close) {

0 commit comments

Comments
 (0)