q5 1.9.2 → 1.9.3
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/README.md +114 -79
- package/package.json +1 -1
- package/q5.js +304 -437
- package/q5.min.js +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
# <img src="q5js_logo.png" height="64"> <img src="q5js_brand.png" height="64">
|
|
2
2
|
|
|
3
|
-
q5.js
|
|
3
|
+
**q5.js** implements all of [p5][]'s 2D drawing, math, and user input functionality.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
It's a drop-in replacement that's performance optimized and 23x smaller than p5. q5 even has a few exclusive features: top-level global mode, HDR color support, namespace mode, and text image caching.
|
|
6
6
|
|
|
7
|
-
q5 doesn't include any friendly error messages
|
|
7
|
+
But q5 doesn't include any friendly error messages, so its mainly for people who are already familiar with p5.js or JS programming in general. If you're a beginner, stick with p5 while developing a sketch, then use q5 to share your work.
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
10
|
|
|
11
|
-
q5 should work with your existing p5.js sketches, no modifications required! If you have any problems though, please [make an issue report
|
|
11
|
+
q5 should work with your existing p5.js sketches, no modifications required! If you have any problems though, please [make an issue report][].
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Or you can use q5.js in your own project by adding this line to your HTML file:
|
|
13
|
+
Use q5.js in your own project by adding this line to your HTML file:
|
|
16
14
|
|
|
17
15
|
```html
|
|
18
16
|
<script src="https://quinton-ashley.github.io/q5.js/q5.js"></script>
|
|
@@ -24,6 +22,8 @@ q5 is also available on [npm](https://www.npmjs.com/package/q5)!
|
|
|
24
22
|
npm install q5
|
|
25
23
|
```
|
|
26
24
|
|
|
25
|
+
Or try out the [q5.js template sketch](https://editor.p5js.org/quinton-ashley/sketches/8SEtLEDl9) for the online p5.js Web Editor.
|
|
26
|
+
|
|
27
27
|
## Support this project 🤝
|
|
28
28
|
|
|
29
29
|
q5 is open source and [multi-licensed](https://github.com/quinton-ashley/p5play-web/blob/main/LICENSING.md). Anyone can use q5 for free under the terms of the AGPLv3. 🎉
|
|
@@ -34,7 +34,7 @@ If you can't afford to pay, you can apply for the free [p5play Novice License](h
|
|
|
34
34
|
|
|
35
35
|
## Using p5 Addon Libraries
|
|
36
36
|
|
|
37
|
-
q5.js is compatible with popular p5 addons and projects that use p5, such as p5play, because it aliases `Q5` to `p5`.
|
|
37
|
+
q5.js is compatible with popular p5 addons and projects that use p5, such as [p5play][], because it aliases `Q5` to `p5`.
|
|
38
38
|
|
|
39
39
|
To use addons, simply load them after q5.js:
|
|
40
40
|
|
|
@@ -47,11 +47,11 @@ To use addons, simply load them after q5.js:
|
|
|
47
47
|
|
|
48
48
|
## New Features: Top-Level Global Mode
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
> q5.js includes some exclusive features that aren't available in p5.js. Using them is optional!
|
|
51
51
|
|
|
52
|
-
**
|
|
52
|
+
In **p5**, p5 functions can't be used on the file level. Also you must declare a `setup` or `draw` function on the file level for p5 to start running in global mode.
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
**q5** can automatically run in global mode as well, so existing sketches don't require any modification. But if you initialize Q5 at the top of your sketch, the `preload` and `setup` functions become optional.
|
|
55
55
|
|
|
56
56
|
```js
|
|
57
57
|
new Q5();
|
|
@@ -62,82 +62,86 @@ fill(c);
|
|
|
62
62
|
rect(15, 15, 35, 70);
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
```js
|
|
68
|
-
new Q5();
|
|
69
|
-
|
|
70
|
-
fill(255, 0, 0);
|
|
65
|
+
This is great because you don't have to declare variables on the file level and then define them in `preload` or `setup`. You can declare and define them at the same time!
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
requestAnimationFrame(myLoop);
|
|
74
|
-
rect(15, 15, 35, 70);
|
|
75
|
-
}
|
|
76
|
-
myLoop();
|
|
77
|
-
```
|
|
67
|
+
## New Features: HDR Color Support
|
|
78
68
|
|
|
79
|
-
|
|
69
|
+
Most modern devices support the "display-p3" HDR color space. If a device doesn't support it, q5 will fall back to "srgb".
|
|
80
70
|
|
|
81
|
-
|
|
71
|
+
**q5** now supports the [oklch](https://oklch.com/#63.65,0.2872,16.57,100) color format which is capable of representing HDR colors.
|
|
82
72
|
|
|
83
73
|
```js
|
|
84
|
-
|
|
85
|
-
p.setup = function () {
|
|
86
|
-
p.createCanvas(100, 100);
|
|
87
|
-
};
|
|
88
|
-
p.draw = function () {
|
|
89
|
-
p.background(0);
|
|
90
|
-
};
|
|
91
|
-
};
|
|
74
|
+
colorMode('oklch');
|
|
92
75
|
|
|
93
|
-
|
|
76
|
+
// (lightness, chroma, hue, alpha)
|
|
77
|
+
let c = color(0.637, 0.287, 16.57, 1);
|
|
94
78
|
```
|
|
95
79
|
|
|
96
|
-
|
|
80
|
+
Support for the HSV color format was removed in q5 v1.9.3 because color experts thought HSV was flawed, outdated, and ought to be abandoned way back in 1997!
|
|
97
81
|
|
|
98
|
-
|
|
99
|
-
- Variables inside `sketch` can no longer be accessed via browser console, which makes it less convenient for debugging.
|
|
82
|
+
The `color` function does accept strings but only hex strings in "#RRGGBB" or "#RRGGBBAA" format. It also does not accept percentages so you'll have to convert those to decimal values.
|
|
100
83
|
|
|
101
|
-
|
|
84
|
+
`colorMode` accepts 'rgb', 'oklch', or 'srgb'. The default mode is 'rgb', which upgrades rgb colors to HDR on supported displays. Specifying 'srgb' enables sRGB gamut correction for rgb colors on HDR displays.
|
|
102
85
|
|
|
103
|
-
|
|
104
|
-
let q5 = new Q5('namespace');
|
|
86
|
+
## New Features: Customize Canvas Context Attributes
|
|
105
87
|
|
|
106
|
-
|
|
107
|
-
q5.createCanvas(100, 100);
|
|
108
|
-
};
|
|
88
|
+
In **p5**, you're stuck with the default [canvas context attributes][], which can't be changed. So the canvas must have an alpha layer, even if you don't need one. p5 also doesn't support HDR color spaces or desynchronized rendering.
|
|
109
89
|
|
|
110
|
-
q5
|
|
111
|
-
|
|
90
|
+
But **q5** has its own defaults:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
Q5.canvasOptions = {
|
|
94
|
+
alpha: false,
|
|
95
|
+
desynchronized: true,
|
|
96
|
+
colorSpace: 'display-p3'
|
|
112
97
|
};
|
|
113
98
|
```
|
|
114
99
|
|
|
115
|
-
|
|
100
|
+
The `Q5.canvasOptions` object can be overridden, which will effect all q5 instances.You can also override any of these defaults by passing an options object as the fourth parameter to the `createCanvas()` function:
|
|
116
101
|
|
|
117
102
|
```js
|
|
118
|
-
|
|
119
|
-
|
|
103
|
+
createCanvas(400, 400, '2d', {
|
|
104
|
+
alpha: true
|
|
105
|
+
});
|
|
106
|
+
```
|
|
120
107
|
|
|
121
|
-
|
|
122
|
-
q5.createCanvas(400, 400);
|
|
123
|
-
};
|
|
108
|
+
## New Features: Namespace Mode
|
|
124
109
|
|
|
125
|
-
|
|
126
|
-
q5.background(100);
|
|
127
|
-
};
|
|
110
|
+
**p5**'s [instance mode][] enables multiple sketches to run on one page. To avoid needing to preface every p5 function with `p.` you can use a JS [with statement][].
|
|
128
111
|
|
|
129
|
-
|
|
130
|
-
|
|
112
|
+
```js
|
|
113
|
+
let sketch = (p) => {
|
|
114
|
+
with (p) {
|
|
115
|
+
p.setup = () => {
|
|
116
|
+
createCanvas(400, 400);
|
|
117
|
+
};
|
|
118
|
+
p.draw = () => {
|
|
119
|
+
background(100);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
131
122
|
};
|
|
132
123
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
124
|
+
let myp5 = new p5(sketch);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**q5** introduces "namespace" mode, in addition to the global and instance modes. You can call the namespace variable whatever you like.
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
let q = new Q5('namespace');
|
|
131
|
+
|
|
132
|
+
with (q) {
|
|
133
|
+
q.setup = () => {
|
|
134
|
+
createCanvas(400, 400);
|
|
135
|
+
};
|
|
136
|
+
q.draw = () => {
|
|
137
|
+
background(100);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
136
140
|
```
|
|
137
141
|
|
|
138
142
|
## Motivation: Part 1
|
|
139
143
|
|
|
140
|
-
|
|
144
|
+
> This section was written by @LingDong-, co-creator of q5.
|
|
141
145
|
|
|
142
146
|
After having used many graphics libraries across many different languages, I have found that the Processing/p5.js/Openframeworks system has one huge advantage over others:
|
|
143
147
|
|
|
@@ -153,55 +157,66 @@ In fact, its not uncommon for successful software systems to have multiple imple
|
|
|
153
157
|
|
|
154
158
|
## Motivation: Part 2
|
|
155
159
|
|
|
156
|
-
|
|
160
|
+
> This section was written by @quinton-ashley, co-creator of q5.
|
|
157
161
|
|
|
158
162
|
I thought @LingDong-'s work on q5 and the idea itself had great potential. So I decided to upgrade its compatibility with p5.js. My main goal was to make it work with [p5play](https://p5play.org)!
|
|
159
163
|
|
|
160
164
|
An increase in performance of even a few frames per second can make a significant difference in the user experience of a work of interactive art or a game, especially on mobile devices.
|
|
161
165
|
|
|
162
|
-
I was also interested in working on q5 because for a lot of p5.js users, the library itself is a black box. Even as an expert JS programmer and someone who teaches CS for a living, I still find myself scratching my head when I look at the p5.js source code. p5 was initially released 10 years ago and I think some bad design choices were made due to JS limitations at the time. It's also become an absolutely massive library, with literally over 100,000 lines of code and documentation! p5.js is 4.3 MB un-minified, q5.js is
|
|
166
|
+
I was also interested in working on q5 because for a lot of p5.js users, the library itself is a black box. Even as an expert JS programmer and someone who teaches CS for a living, I still find myself scratching my head when I look at the p5.js source code. p5 was initially released 10 years ago and I think some bad design choices were made due to JS limitations at the time. It's also become an absolutely massive library, with literally over 100,000 lines of code and documentation! p5.js is 4.3 MB un-minified, q5.js is under 70kb.
|
|
163
167
|
|
|
164
168
|
I think it'd be better if the canvas mode, webgl mode, Friendly Error System, and accessibility features of p5 were offered in separate files. Yet, the powers that be at the Processing Foundation have made it clear that they don't want to do that. Instead they insist on adding more accessibility features to the base library, which the majority of people just don't need. So q5 is a good alternative that trims out the fat.
|
|
165
169
|
|
|
166
170
|
Thanks in large part to @LingDong-'s design, q5 is well organized, concise, and utilizes many modern JS features! I think even without documentation, the source code is easier for experienced JS programmers to comprehend.
|
|
167
171
|
|
|
168
|
-
## More
|
|
172
|
+
## More exclusive features
|
|
169
173
|
|
|
170
174
|
q5.js provides some other features that are not in p5.js:
|
|
171
175
|
|
|
172
176
|
- `textCache(true)`: Text image caching is enabled by default. Rotated text is only rendered once, and then cached as an image. This can result in ridiculously high 90x performance boosts for text-heavy sketches. Users don't need to change their code, the `text` function can be used as normal, q5 takes care of everything behind the scenes.
|
|
173
177
|
- `loadSound()`: Basic sound support in q5.js, returns a Web Audio object. Not as powerful as p5.sound, but it's good enough for simple sketches. Includes `sound.setVolume()`.
|
|
178
|
+
- `ctx`: an alias for `drawingContext`
|
|
174
179
|
- `randomExponential()` in addition to `randomGaussian()`: a random distribution that resembles exponential decay.
|
|
175
180
|
- `curveAlpha()`: manipulate the `α` parameter of Catmull-Rom curves.
|
|
176
181
|
- `relRotationX`, `relRotationY` and `relRotationZ`: Similar to `rotationX/Y/Z`, but are relative to the orientation of the mobile device.
|
|
177
182
|
|
|
178
183
|
## Cutting room floor
|
|
179
184
|
|
|
180
|
-
**p5.js** has some pretty extensive parsing capabilities. For example, it can parse out a color from strings like `color('hsl(160, 100%, 50%)')
|
|
185
|
+
**p5.js** has some pretty extensive parsing capabilities. For example, it can parse out a color from strings like `color('hsl(160, 100%, 50%)')`. Functions behave sightly differently when under different "modes" and some have secret default settings, such as `arc` and `text`.
|
|
181
186
|
|
|
182
|
-
**q5.js** will only do things when you communicate the command in the simplest way. This means that functions mainly just take numeric inputs.
|
|
187
|
+
**q5.js** will only do things when you communicate the command in the simplest way. This means that functions mainly just take numeric inputs. q5 has almost no overhead between digesting your parameters and putting them into use.
|
|
183
188
|
|
|
184
|
-
##
|
|
189
|
+
## Size Comparison
|
|
185
190
|
|
|
186
|
-
|
|
191
|
+
Unminified:
|
|
187
192
|
|
|
188
|
-
|
|
193
|
+
- p5.js **4300kb** ⚠️
|
|
194
|
+
- p5.sound.js 488kb
|
|
195
|
+
- q5.js 66kb
|
|
189
196
|
|
|
190
|
-
|
|
197
|
+
Minified:
|
|
198
|
+
|
|
199
|
+
- p5.min.js 1000kb
|
|
191
200
|
- p5.sound.min.js 200kb
|
|
201
|
+
- q5.min.js **42kb** 🎉
|
|
192
202
|
|
|
193
|
-
|
|
203
|
+
## Benchmarks
|
|
194
204
|
|
|
195
|
-
|
|
196
|
-
- p5play.min.js 93kb
|
|
205
|
+
q5.js has a significant speed advantage in imaging operations because it uses hardware accelerated Canvas APIs whenever possible, instead of going pixel by pixel. Most other functionalities have very marginal speed improvements (or none at all when parameter validation overhead is negligible). The operations with important performance differences are listed below.
|
|
197
206
|
|
|
198
|
-
|
|
207
|
+
The following benchmarks are generated with Google Chrome 120, on a MacBook Air M1 2020. q5.js v1.9.3 vs p5.js v1.9.0.
|
|
199
208
|
|
|
200
|
-
|
|
209
|
+
Less time (milliseconds) is better.
|
|
201
210
|
|
|
202
|
-
|
|
211
|
+
| Task | p5.js | q5.js |
|
|
212
|
+
| -------------------------------------------------- | ----- | ----- |
|
|
213
|
+
| Generate 100,000 random colors with `color(r,g,b)` | 168ms | 12ms |
|
|
203
214
|
|
|
204
|
-
|
|
215
|
+
## Older Benchmarks
|
|
216
|
+
|
|
217
|
+
The following benchmarks are generated with Google Chrome 84, on an old-ish MacBook Pro 2015 (with lots of apps and tabs running); Performance varies depending on software and hardware. p5.js version used is v1.1.9.
|
|
218
|
+
|
|
219
|
+
Higher FPS (frames per second) is better.
|
|
205
220
|
|
|
206
221
|
| Operation on 1024x1024 image | p5.js | q5.js |
|
|
207
222
|
| ---------------------------- | ----- | -------- |
|
|
@@ -213,7 +228,7 @@ p5.js version used is **1.1.9**.
|
|
|
213
228
|
| opaque | 20FPS | 60FPS |
|
|
214
229
|
| erode/dilate | 5FPS | 9FPS |
|
|
215
230
|
|
|
216
|
-
|
|
|
231
|
+
| Task | p5.js | q5.js |
|
|
217
232
|
| --------------------------------------------------- | ----- | ----- |
|
|
218
233
|
| Generating 10,000 `randomGaussian()` sample | 10FPS | 20FPS |
|
|
219
234
|
| Calling `noiseSeed()` 1,000 times | 10FPS | 60FPS |
|
|
@@ -222,7 +237,27 @@ p5.js version used is **1.1.9**.
|
|
|
222
237
|
|
|
223
238
|
<sub>\* Only for browsers that support CanvasRenderingContext2D.filter ([75% of all](https://caniuse.com/#feat=mdn-api_canvasrenderingcontext2d_filter) as of Aug 2020, including Chrome, Firefox and Edge). For those that don't, performance is similar to p5.js, as identical implementations are usually used as fallbacks.</sub>
|
|
224
239
|
|
|
240
|
+
## Contributing
|
|
241
|
+
|
|
225
242
|
Speed is a goal for q5.js, and we would very much like to see the above list grow. If you know how to make something faster, advice/pull requests are very welcome!
|
|
226
243
|
|
|
227
|
-
|
|
228
|
-
|
|
244
|
+
## Credits
|
|
245
|
+
|
|
246
|
+
catmullRomSpline:
|
|
247
|
+
https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
|
|
248
|
+
|
|
249
|
+
ziggurat:
|
|
250
|
+
http://ziggurat.glitch.me/
|
|
251
|
+
|
|
252
|
+
random:
|
|
253
|
+
https://github.com/processing/p5.js/blob/1.1.9/src/math/noise.js
|
|
254
|
+
|
|
255
|
+
Curve query:
|
|
256
|
+
https://github.com/processing/p5.js/blob/1.1.9/src/core/shape/curves.js
|
|
257
|
+
|
|
258
|
+
[p5]: https://p5js.org
|
|
259
|
+
[p5play]: https://p5play.org
|
|
260
|
+
[instance mode]: https://p5js.org/examples/instance-mode-instantiation.html
|
|
261
|
+
[with statement]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with
|
|
262
|
+
[make an issue report]: https://github.com/quinton-ashley/q5.js/issues
|
|
263
|
+
[context attributes]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext#contextattributes
|