q5 2.9.21 → 2.9.23

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.
Files changed (77) hide show
  1. package/.vscode/launch.json +26 -0
  2. package/bun.lockb +0 -0
  3. package/p5-tests/js/chai_helpers.js +20 -0
  4. package/p5-tests/js/mocha_setup.js +2 -0
  5. package/p5-tests/js/modernizr.js +5 -0
  6. package/p5-tests/js/p5_helpers.js +135 -0
  7. package/p5-tests/js/sinon.js +5949 -0
  8. package/p5-tests/mocha.css +289 -0
  9. package/p5-tests/test.html +71 -0
  10. package/p5-tests/unit/color/color_conversion.js +68 -0
  11. package/p5-tests/unit/color/creating_reading.js +217 -0
  12. package/p5-tests/unit/color/p5.Color.js +1000 -0
  13. package/p5-tests/unit/color/setting.js +289 -0
  14. package/p5-tests/unit/core/2d_primitives.js +490 -0
  15. package/p5-tests/unit/core/attributes.js +115 -0
  16. package/p5-tests/unit/core/curves.js +139 -0
  17. package/p5-tests/unit/core/environment.js +248 -0
  18. package/p5-tests/unit/core/error_helpers.js +1158 -0
  19. package/p5-tests/unit/core/main.js +340 -0
  20. package/p5-tests/unit/core/p5.Element.js +773 -0
  21. package/p5-tests/unit/core/p5.Graphics.js +179 -0
  22. package/p5-tests/unit/core/preload.js +285 -0
  23. package/p5-tests/unit/core/rendering.js +116 -0
  24. package/p5-tests/unit/core/structure.js +293 -0
  25. package/p5-tests/unit/core/transform.js +144 -0
  26. package/p5-tests/unit/core/version.js +28 -0
  27. package/p5-tests/unit/core/vertex.js +137 -0
  28. package/p5-tests/unit/dom/dom.js +2146 -0
  29. package/p5-tests/unit/events/acceleration.js +213 -0
  30. package/p5-tests/unit/events/keyboard.js +179 -0
  31. package/p5-tests/unit/events/mouse.js +487 -0
  32. package/p5-tests/unit/events/touch.js +180 -0
  33. package/p5-tests/unit/image/downloading.js +379 -0
  34. package/p5-tests/unit/image/filters.js +92 -0
  35. package/p5-tests/unit/image/loading.js +413 -0
  36. package/p5-tests/unit/image/p5.Image.js +201 -0
  37. package/p5-tests/unit/image/pixels.js +234 -0
  38. package/p5-tests/unit/io/files.js +378 -0
  39. package/p5-tests/unit/io/loadBytes.js +149 -0
  40. package/p5-tests/unit/io/loadImage.js +123 -0
  41. package/p5-tests/unit/io/loadJSON.js +185 -0
  42. package/p5-tests/unit/io/loadModel.js +215 -0
  43. package/p5-tests/unit/io/loadShader.js +176 -0
  44. package/p5-tests/unit/io/loadStrings.js +140 -0
  45. package/p5-tests/unit/io/loadTable.js +183 -0
  46. package/p5-tests/unit/io/loadXML.js +127 -0
  47. package/p5-tests/unit/io/saveModel.js +113 -0
  48. package/p5-tests/unit/io/saveTable.js +142 -0
  49. package/p5-tests/unit/math/calculation.js +452 -0
  50. package/p5-tests/unit/math/noise.js +66 -0
  51. package/p5-tests/unit/math/p5.Vector.js +1886 -0
  52. package/p5-tests/unit/math/random.js +177 -0
  53. package/p5-tests/unit/math/trigonometry.js +144 -0
  54. package/p5-tests/unit/spec.js +50 -0
  55. package/p5-tests/unit/typography/attributes.js +120 -0
  56. package/p5-tests/unit/typography/loadFont.js +162 -0
  57. package/p5-tests/unit/typography/p5.Font.js +63 -0
  58. package/p5-tests/unit/utilities/conversion.js +329 -0
  59. package/p5-tests/unit/utilities/time_date.js +133 -0
  60. package/package.json +1 -1
  61. package/q5.js +158 -54
  62. package/q5.min.js +1 -1
  63. package/src/q5-2d-canvas.js +8 -2
  64. package/src/q5-2d-drawing.js +20 -7
  65. package/src/q5-2d-image.js +4 -1
  66. package/src/q5-2d-text.js +7 -0
  67. package/src/q5-canvas.js +6 -5
  68. package/src/q5-color.js +5 -0
  69. package/src/q5-core.js +3 -1
  70. package/src/q5-input.js +12 -0
  71. package/src/q5-math.js +11 -3
  72. package/src/q5-record.js +2 -0
  73. package/src/q5-vector.js +33 -0
  74. package/src/q5-webgpu-canvas.js +11 -7
  75. package/src/q5-webgpu-drawing.js +15 -12
  76. package/src/q5-webgpu-image.js +1 -1
  77. package/src/q5-webgpu-text.js +22 -15
@@ -0,0 +1,340 @@
1
+ suite('Core', function () {
2
+ suite('p5.prototype.registerMethod', function () {
3
+ teardown(function() {
4
+ p5.prototype._registeredMethods = Q5.methods;
5
+
6
+ p5.prototype._registeredMethods.init = [];
7
+ p5.prototype._registeredMethods.beforePreload = [];
8
+ p5.prototype._registeredMethods.preload = [];
9
+ p5.prototype._registeredMethods.afterPreload = [];
10
+ p5.prototype._registeredMethods.beforeSetup = [];
11
+ p5.prototype._registeredMethods.setup = [];
12
+ p5.prototype._registeredMethods.afterSetup = [];
13
+ p5.prototype._registeredMethods.pre = [];
14
+ p5.prototype._registeredMethods.draw = [];
15
+ p5.prototype._registeredMethods.post = [];
16
+ });
17
+ test('should register and call "init" methods', function () {
18
+ var myp5, myInitCalled;
19
+ p5.prototype.registerMethod('init', function myInit() {
20
+ assert(
21
+ !myInitCalled,
22
+ 'myInit should only be called once during test suite'
23
+ );
24
+ myInitCalled = true;
25
+
26
+ this.myInitCalled = true;
27
+ });
28
+
29
+ myp5 = new p5(function (sketch) {
30
+ assert(sketch.hasOwnProperty('myInitCalled'));
31
+ assert(sketch.myInitCalled);
32
+
33
+ sketch.sketchFunctionCalled = true;
34
+ });
35
+
36
+ assert(myp5.sketchFunctionCalled);
37
+ });
38
+ test('should register and call before and after "preload" hooks', function () {
39
+ return new Promise(resolve => {
40
+ let beforePreloadCalled = false;
41
+ let preloadCalled = false;
42
+ let afterPreloadCalled = false;
43
+
44
+ p5.prototype.registerMethod('beforePreload', () => {
45
+ beforePreloadCalled = true;
46
+ });
47
+
48
+ p5.prototype.registerMethod('preload', () => {
49
+ assert.equal(beforePreloadCalled, true);
50
+ preloadCalled = true;
51
+ });
52
+
53
+ p5.prototype.registerMethod('afterPreload', () => {
54
+ if (beforePreloadCalled && preloadCalled) afterPreloadCalled = true;
55
+ });
56
+
57
+ myp5 = new p5(function (sketch) {
58
+ sketch.preload = () => {};
59
+ sketch.setup = () => {
60
+ assert.equal(afterPreloadCalled, true);
61
+ resolve();
62
+ };
63
+ });
64
+ });
65
+ });
66
+ test('should register and call before and after "setup" hooks', function () {
67
+ return new Promise(resolve => {
68
+ let beforeSetupCalled = false;
69
+ let setupCalled = false;
70
+ let afterSetupCalled = false;
71
+
72
+ p5.prototype.registerMethod('beforeSetup', () => {
73
+ beforeSetupCalled = true;
74
+ });
75
+
76
+ p5.prototype.registerMethod('setup', () => {
77
+ assert.equal(beforeSetupCalled, true);
78
+ setupCalled = true;
79
+ });
80
+
81
+ p5.prototype.registerMethod('afterSetup', () => {
82
+ if (beforeSetupCalled && setupCalled) afterSetupCalled = true;
83
+ });
84
+
85
+ myp5 = new p5(function (sketch) {
86
+ sketch.setup = () => {};
87
+ sketch.draw = () => {
88
+ assert.equal(afterSetupCalled, true);
89
+ resolve();
90
+ };
91
+ });
92
+ });
93
+ });
94
+ test('should register and call pre and post "draw" hooks', function () {
95
+ return new Promise(resolve => {
96
+ let preDrawCalled = false;
97
+ let drawCalled = false;
98
+ let postDrawCalled = false;
99
+
100
+ p5.prototype.registerMethod('pre', () => {
101
+ preDrawCalled = true;
102
+ });
103
+
104
+ p5.prototype.registerMethod('draw', () => {
105
+ assert.equal(preDrawCalled, true);
106
+ drawCalled = true;
107
+ });
108
+
109
+ p5.prototype.registerMethod('post', () => {
110
+ if (preDrawCalled && drawCalled) postDrawCalled = true;
111
+ });
112
+
113
+ myp5 = new p5(function (sketch) {
114
+ sketch.draw = () => {
115
+ if (sketch.frameCount === 2) {
116
+ assert.equal(postDrawCalled, true);
117
+ resolve();
118
+ }
119
+ };
120
+ });
121
+ });
122
+ });
123
+ });
124
+
125
+ suite('new p5() / global mode', function () {
126
+ var iframe;
127
+
128
+ teardown(function () {
129
+ if (iframe) {
130
+ iframe.teardown();
131
+ iframe = null;
132
+ }
133
+ });
134
+
135
+ test('is triggered when "setup" is in window', function () {
136
+ return new Promise(function (resolve, reject) {
137
+ iframe = createP5Iframe();
138
+ iframe.elt.contentWindow.setup = function () {
139
+ resolve();
140
+ };
141
+ });
142
+ });
143
+
144
+ test('is triggered when "draw" is in window', function () {
145
+ return new Promise(function (resolve, reject) {
146
+ iframe = createP5Iframe();
147
+ iframe.elt.contentWindow.draw = function () {
148
+ resolve();
149
+ };
150
+ });
151
+ });
152
+
153
+ test('works when p5.js is loaded asynchronously', function () {
154
+ return new Promise(function (resolve, reject) {
155
+ iframe = createP5Iframe(`
156
+ <script>
157
+ window.onload = function() {
158
+ var script = document.createElement('script');
159
+ script.setAttribute('src', '${P5_SCRIPT_URL}');
160
+
161
+ document.body.appendChild(script);
162
+ }
163
+ </script>`);
164
+
165
+ iframe.elt.contentWindow.setup = resolve;
166
+ });
167
+ });
168
+
169
+ test('works on-demand', function () {
170
+ return new Promise(function (resolve, reject) {
171
+ iframe = createP5Iframe(
172
+ [
173
+ P5_SCRIPT_TAG,
174
+ '<script>',
175
+ 'new p5();',
176
+ 'originalP5Instance = p5.instance',
177
+ 'myURL = p5.prototype.getURL();',
178
+ 'function setup() { setupCalled = true; }',
179
+ 'window.addEventListener("load", onDoneLoading, false);',
180
+ '</script>'
181
+ ].join('\n')
182
+ );
183
+ iframe.elt.contentWindow.onDoneLoading = resolve;
184
+ }).then(function () {
185
+ var win = iframe.elt.contentWindow;
186
+ assert.equal(typeof win.myURL, 'string');
187
+ assert.strictEqual(win.setupCalled, true);
188
+ assert.strictEqual(win.originalP5Instance, win.p5.instance);
189
+ });
190
+ });
191
+ });
192
+
193
+ suite('p5.prototype._createFriendlyGlobalFunctionBinder', function () {
194
+ var noop = function () {};
195
+ var createBinder = p5.prototype._createFriendlyGlobalFunctionBinder;
196
+ var logMsg, globalObject, bind, iframe;
197
+
198
+ teardown(function () {
199
+ if (iframe) {
200
+ iframe.teardown();
201
+ iframe = null;
202
+ }
203
+ });
204
+
205
+ setup(function () {
206
+ globalObject = {};
207
+ logMsg = undefined;
208
+ bind = createBinder({
209
+ globalObject: globalObject,
210
+ log: function (msg) {
211
+ if (logMsg !== undefined) {
212
+ // For simplicity, we'll write each test so it's expected to
213
+ // log a message at most once.
214
+ throw new Error('log() was called more than once');
215
+ }
216
+ logMsg = msg;
217
+ }
218
+ });
219
+ });
220
+ if (!window.IS_TESTING_MINIFIED_VERSION) {
221
+ test('should warn when globals already exist', function () {
222
+ const _friendlyErrorStub = sinon.stub(p5, '_friendlyError');
223
+ try {
224
+ globalObject.text = 'hi';
225
+ bind('text', noop);
226
+ expect(
227
+ _friendlyErrorStub.calledOnce,
228
+ 'p5._friendlyError was not called'
229
+ ).to.be.true;
230
+ } finally {
231
+ _friendlyErrorStub.restore();
232
+ }
233
+ });
234
+
235
+ test('should warn when globals are overwritten', function () {
236
+ bind('text', noop);
237
+ globalObject.text = 'boop';
238
+
239
+ assert.match(logMsg, /You just changed the value of "text"/);
240
+ assert.equal(globalObject.text, 'boop');
241
+ assert.deepEqual(Object.keys(globalObject), ['text']);
242
+ });
243
+ } else {
244
+ test('should NOT warn when globals already exist', function () {
245
+ const _friendlyErrorStub = sinon.stub(p5, '_friendlyError');
246
+ try {
247
+ globalObject.text = 'hi';
248
+ bind('text', noop);
249
+ expect(
250
+ _friendlyErrorStub.calledOnce,
251
+ 'p5._friendlyError was called in minified p5.js'
252
+ ).to.be.false;
253
+ } finally {
254
+ _friendlyErrorStub.restore();
255
+ }
256
+ });
257
+
258
+ test('should NOT warn when globals are overwritten', function () {
259
+ bind('text', noop);
260
+ globalObject.text = 'boop';
261
+
262
+ assert.isUndefined(logMsg);
263
+ assert.equal(globalObject.text, 'boop');
264
+ assert.deepEqual(Object.keys(globalObject), ['text']);
265
+ });
266
+ }
267
+
268
+ test('should allow overwritten globals to be overwritten', function () {
269
+ bind('text', noop);
270
+ globalObject.text = 'boop';
271
+ globalObject.text += 'blap';
272
+ assert.equal(globalObject.text, 'boopblap');
273
+ });
274
+
275
+ test('should allow globals to be deleted', function () {
276
+ bind('text', noop);
277
+ delete globalObject.text;
278
+ assert.isUndefined(globalObject.text);
279
+ assert.isUndefined(logMsg);
280
+ });
281
+
282
+ test('should create enumerable globals', function () {
283
+ bind('text', noop);
284
+ assert.deepEqual(Object.keys(globalObject), ['text']);
285
+ });
286
+
287
+ test('should not warn about overwriting print()', function () {
288
+ globalObject.print = window.print;
289
+ bind('print', noop);
290
+ assert.equal(globalObject.print, noop);
291
+ assert.isUndefined(logMsg);
292
+ });
293
+
294
+ // This is a regression test for
295
+ // https://github.com/processing/p5.js/issues/1350.
296
+ test('should not warn about overwriting preload methods', function () {
297
+ globalObject.loadJSON = function () {
298
+ throw new Error();
299
+ };
300
+ bind('loadJSON', noop);
301
+ assert.equal(globalObject.loadJSON, noop);
302
+ assert.isUndefined(logMsg);
303
+ });
304
+
305
+ test('should not warn about overwriting non-functions', function () {
306
+ bind('mouseX', 5);
307
+ globalObject.mouseX = 50;
308
+ assert.equal(globalObject.mouseX, 50);
309
+ assert.isUndefined(logMsg);
310
+ });
311
+
312
+ test('instance preload is independent of window', function () {
313
+ // callback for p5 instance mode.
314
+ // It does not define a preload.
315
+ // This tests that we don't call the global preload accidentally.
316
+ function cb(s) {
317
+ s.setup = function () {
318
+ window.afterSetup();
319
+ };
320
+ }
321
+ return new Promise(function (resolve) {
322
+ iframe = createP5Iframe(
323
+ [
324
+ P5_SCRIPT_TAG,
325
+ '<script>',
326
+ 'globalPreloads = 0;',
327
+ 'function setup() { }',
328
+ 'function preload() { window.globalPreloads++; }',
329
+ 'new p5(' + cb.toString() + ');',
330
+ '</script>'
331
+ ].join('\n')
332
+ );
333
+ iframe.elt.contentWindow.afterSetup = resolve;
334
+ }).then(function () {
335
+ var win = iframe.elt.contentWindow;
336
+ assert.strictEqual(win.globalPreloads, 1);
337
+ });
338
+ });
339
+ });
340
+ });