p5 2.0.0 → 2.0.1
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/{src → dist}/accessibility/color_namer.js +48 -3
- package/{src → dist}/accessibility/describe.js +2 -2
- package/{src → dist}/accessibility/gridOutput.js +2 -2
- package/dist/accessibility/index.js +60 -0
- package/{src → dist}/accessibility/outputs.js +2 -2
- package/{src → dist}/accessibility/textOutput.js +2 -2
- package/dist/app.js +120 -0
- package/{src → dist}/color/color_conversion.js +48 -10
- package/{src → dist}/color/color_spaces/hsb.js +3 -1
- package/dist/color/creating_reading.js +3 -0
- package/dist/color/index.js +13 -0
- package/dist/color/p5.Color.culori.js +1 -0
- package/dist/color/p5.Color.js +3 -0
- package/{src → dist}/color/setting.js +9 -6
- package/{src/core/constants.js → dist/constants-C-g_eAdC.js} +266 -130
- package/{src → dist}/core/States.js +3 -1
- package/dist/core/constants.js +1 -0
- package/{src → dist}/core/environment.js +7 -6
- package/{src → dist}/core/friendly_errors/browser_errors.js +1 -1
- package/{src → dist}/core/friendly_errors/fes_core.js +14 -44
- package/{src → dist}/core/friendly_errors/file_errors.js +6 -3
- package/dist/core/friendly_errors/index.js +23 -0
- package/dist/core/friendly_errors/param_validator.js +5455 -0
- package/{src → dist}/core/friendly_errors/sketch_reader.js +50 -4
- package/{src → dist}/core/friendly_errors/sketch_verifier.js +6 -6
- package/{src → dist}/core/friendly_errors/stacktrace.js +3 -5
- package/{src → dist}/core/friendly_errors/validate_params.js +50 -41
- package/{src → dist}/core/helpers.js +9 -6
- package/dist/core/init.js +105 -0
- package/dist/core/internationalization.js +302 -0
- package/dist/core/legacy.js +73 -0
- package/dist/core/main.js +44 -0
- package/dist/core/noop.js +3 -0
- package/dist/core/p5.Graphics.js +40 -0
- package/dist/core/p5.Renderer.js +11 -0
- package/dist/core/p5.Renderer2D.js +44 -0
- package/dist/core/reference.js +1 -0
- package/dist/core/rendering.js +40 -0
- package/{src → dist}/core/structure.js +3 -3
- package/{src → dist}/core/transform.js +2 -2
- package/{src/color/creating_reading.js → dist/creating_reading-D4AAKRbx.js} +841 -13
- package/{src → dist}/data/index.js +3 -1
- package/{src → dist}/data/local_storage.js +2 -8
- package/{src → dist}/dom/dom.js +11 -5
- package/dist/dom/index.js +18 -0
- package/{src → dist}/dom/p5.Element.js +14 -12
- package/{src → dist}/dom/p5.File.js +4 -4
- package/{src → dist}/dom/p5.MediaElement.js +10 -4
- package/{src → dist}/events/acceleration.js +2 -2
- package/{src → dist}/events/index.js +3 -1
- package/{src → dist}/events/keyboard.js +14 -11
- package/{src → dist}/events/pointer.js +16 -17
- package/dist/image/const.js +9 -0
- package/{src → dist}/image/filterRenderer2D.js +57 -37
- package/{src → dist}/image/filters.js +1 -3
- package/dist/image/image.js +40 -0
- package/dist/image/index.js +51 -0
- package/dist/image/loading_displaying.js +40 -0
- package/dist/image/p5.Image.js +11 -0
- package/{src → dist}/image/pixels.js +4 -3
- package/{src → dist}/io/csv.js +72 -70
- package/dist/io/files.js +40 -0
- package/dist/io/index.js +51 -0
- package/{src → dist}/io/p5.Table.js +6 -6
- package/{src → dist}/io/p5.TableRow.js +3 -4
- package/{src → dist}/io/p5.XML.js +2 -5
- package/{src → dist}/io/utilities.js +1 -1
- package/{src/core/p5.Renderer2D.js → dist/main-s72KWcUy.js} +735 -57
- package/{src → dist}/math/Matrices/Matrix.js +10 -8
- package/{src → dist}/math/Matrices/MatrixInterface.js +5 -3
- package/{src → dist}/math/Matrices/MatrixNumjs.js +12 -26
- package/{src → dist}/math/calculation.js +2 -2
- package/{src → dist}/math/index.js +6 -3
- package/{src → dist}/math/math.js +2 -2
- package/{src → dist}/math/noise.js +2 -2
- package/{src → dist}/math/p5.Matrix.js +7 -4
- package/{src → dist}/math/p5.Vector.js +6 -6
- package/{src → dist}/math/random.js +2 -2
- package/{src → dist}/math/trigonometry.js +16 -15
- package/{src/image/p5.Image.js → dist/p5.Renderer-CwAYZOC2.js} +390 -19
- package/dist/rendering--aAe5aq3.js +24925 -0
- package/{src → dist}/shape/2d_primitives.js +18 -17
- package/{src → dist}/shape/attributes.js +18 -17
- package/{src → dist}/shape/curves.js +2 -2
- package/{src → dist}/shape/custom_shapes.js +44 -64
- package/{src → dist}/shape/index.js +10 -2
- package/{src → dist}/shape/vertex.js +2 -3
- package/dist/type/index.js +25 -0
- package/{src → dist}/type/lib/Typr.js +76 -94
- package/{src → dist}/type/p5.Font.js +37 -61
- package/{src → dist}/type/textCore.js +34 -57
- package/{src → dist}/type/unicodeRanges.js +3 -1
- package/{src → dist}/utilities/conversion.js +2 -2
- package/{src → dist}/utilities/index.js +3 -1
- package/{src → dist}/utilities/time_date.js +6 -7
- package/{src → dist}/utilities/utility_functions.js +2 -2
- package/dist/webgl/3d_primitives.js +40 -0
- package/{src → dist}/webgl/GeometryBufferCache.js +3 -1
- package/{src → dist}/webgl/GeometryBuilder.js +12 -8
- package/{src → dist}/webgl/ShaderGenerator.js +79 -82
- package/{src → dist}/webgl/ShapeBuilder.js +26 -23
- package/dist/webgl/index.js +76 -0
- package/{src → dist}/webgl/interaction.js +7 -6
- package/dist/webgl/light.js +40 -0
- package/{src → dist}/webgl/loading.js +45 -12
- package/dist/webgl/material.js +40 -0
- package/dist/webgl/p5.Camera.js +40 -0
- package/{src → dist}/webgl/p5.DataArray.js +3 -5
- package/dist/webgl/p5.Framebuffer.js +40 -0
- package/{src → dist}/webgl/p5.Geometry.js +12 -15
- package/{src → dist}/webgl/p5.Quat.js +5 -4
- package/{src → dist}/webgl/p5.RenderBuffer.js +2 -3
- package/dist/webgl/p5.RendererGL.js +40 -0
- package/dist/webgl/p5.Shader.js +40 -0
- package/dist/webgl/p5.Texture.js +40 -0
- package/{src → dist}/webgl/text.js +51 -9
- package/lib/p5.esm.js +102 -48
- package/lib/p5.js +102 -48
- package/lib/p5.min.js +1 -1
- package/package.json +17 -16
- package/translations/dev.js +6 -6
- package/translations/index.js +1 -1
- package/src/README.md +0 -27
- package/src/accessibility/index.js +0 -13
- package/src/app.js +0 -61
- package/src/color/index.js +0 -9
- package/src/color/p5.Color.culori.js +0 -66
- package/src/color/p5.Color.js +0 -851
- package/src/core/README.md +0 -91
- package/src/core/friendly_errors/index.js +0 -13
- package/src/core/friendly_errors/param_validator.js +0 -561
- package/src/core/init.js +0 -58
- package/src/core/internationalization.js +0 -195
- package/src/core/legacy.js +0 -29
- package/src/core/main.js +0 -689
- package/src/core/noop.js +0 -1
- package/src/core/p5.Graphics.js +0 -696
- package/src/core/p5.Renderer.js +0 -408
- package/src/core/reference.js +0 -2060
- package/src/core/rendering.js +0 -697
- package/src/dom/index.js +0 -11
- package/src/image/const.js +0 -6
- package/src/image/image.js +0 -731
- package/src/image/index.js +0 -15
- package/src/image/loading_displaying.js +0 -1431
- package/src/io/files.js +0 -2210
- package/src/io/index.js +0 -11
- package/src/math/README.md +0 -40
- package/src/type/index.js +0 -9
- package/src/webgl/3d_primitives.js +0 -2741
- package/src/webgl/index.js +0 -37
- package/src/webgl/light.js +0 -1851
- package/src/webgl/material.js +0 -3854
- package/src/webgl/p5.Camera.js +0 -4010
- package/src/webgl/p5.Framebuffer.js +0 -1865
- package/src/webgl/p5.RendererGL.js +0 -2867
- package/src/webgl/p5.Shader.js +0 -1505
- package/src/webgl/p5.Texture.js +0 -541
- package/src/webgl/shaders/basic.frag +0 -6
- package/src/webgl/shaders/filters/base.frag +0 -22
- package/src/webgl/shaders/filters/base.vert +0 -19
- package/src/webgl/shaders/filters/blur.frag +0 -60
- package/src/webgl/shaders/filters/default.vert +0 -18
- package/src/webgl/shaders/filters/dilate.frag +0 -39
- package/src/webgl/shaders/filters/erode.frag +0 -39
- package/src/webgl/shaders/filters/gray.frag +0 -16
- package/src/webgl/shaders/filters/invert.frag +0 -15
- package/src/webgl/shaders/filters/opaque.frag +0 -12
- package/src/webgl/shaders/filters/posterize.frag +0 -29
- package/src/webgl/shaders/filters/threshold.frag +0 -23
- package/src/webgl/shaders/font.frag +0 -216
- package/src/webgl/shaders/font.vert +0 -44
- package/src/webgl/shaders/imageLight.vert +0 -33
- package/src/webgl/shaders/imageLightDiffused.frag +0 -82
- package/src/webgl/shaders/imageLightSpecular.frag +0 -134
- package/src/webgl/shaders/light.vert +0 -37
- package/src/webgl/shaders/light_texture.frag +0 -26
- package/src/webgl/shaders/lighting.glsl +0 -227
- package/src/webgl/shaders/line.frag +0 -74
- package/src/webgl/shaders/line.vert +0 -294
- package/src/webgl/shaders/normal.frag +0 -6
- package/src/webgl/shaders/normal.vert +0 -72
- package/src/webgl/shaders/phong.frag +0 -84
- package/src/webgl/shaders/phong.vert +0 -87
- package/src/webgl/shaders/point.frag +0 -29
- package/src/webgl/shaders/point.vert +0 -19
- package/src/webgl/shaders/sphereMapping.frag +0 -26
- package/src/webgl/shaders/webgl2Compatibility.glsl +0 -34
package/src/core/main.js
DELETED
|
@@ -1,689 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Structure
|
|
3
|
-
* @submodule Structure
|
|
4
|
-
* @for p5
|
|
5
|
-
* @requires constants
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import * as constants from './constants';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* This is the p5 instance constructor.
|
|
12
|
-
*
|
|
13
|
-
* A p5 instance holds all the properties and methods related to
|
|
14
|
-
* a p5 sketch. It expects an incoming sketch closure and it can also
|
|
15
|
-
* take an optional node parameter for attaching the generated p5 canvas
|
|
16
|
-
* to a node. The sketch closure takes the newly created p5 instance as
|
|
17
|
-
* its sole argument and may optionally set <a href="#/p5/preload">preload()</a>,
|
|
18
|
-
* <a href="#/p5/setup">setup()</a>, and/or
|
|
19
|
-
* <a href="#/p5/draw">draw()</a> properties on it for running a sketch.
|
|
20
|
-
*
|
|
21
|
-
* A p5 sketch can run in "global" or "instance" mode:
|
|
22
|
-
* "global" - all properties and methods are attached to the window
|
|
23
|
-
* "instance" - all properties and methods are bound to this p5 object
|
|
24
|
-
*
|
|
25
|
-
* @class p5
|
|
26
|
-
* @param {function(p5)} sketch a closure that can set optional <a href="#/p5/preload">preload()</a>,
|
|
27
|
-
* <a href="#/p5/setup">setup()</a>, and/or <a href="#/p5/draw">draw()</a> properties on the
|
|
28
|
-
* given p5 instance
|
|
29
|
-
* @param {HTMLElement} [node] element to attach canvas to
|
|
30
|
-
* @return {p5} a p5 instance
|
|
31
|
-
*/
|
|
32
|
-
class p5 {
|
|
33
|
-
static VERSION = constants.VERSION;
|
|
34
|
-
// This is a pointer to our global mode p5 instance, if we're in
|
|
35
|
-
// global mode.
|
|
36
|
-
static instance = null;
|
|
37
|
-
static lifecycleHooks = {
|
|
38
|
-
presetup: [],
|
|
39
|
-
postsetup: [],
|
|
40
|
-
predraw: [],
|
|
41
|
-
postdraw: [],
|
|
42
|
-
remove: []
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// FES stub
|
|
46
|
-
static _checkForUserDefinedFunctions = () => {};
|
|
47
|
-
static _friendlyFileLoadError = () => {};
|
|
48
|
-
|
|
49
|
-
constructor(sketch, node) {
|
|
50
|
-
//////////////////////////////////////////////
|
|
51
|
-
// PRIVATE p5 PROPERTIES AND METHODS
|
|
52
|
-
//////////////////////////////////////////////
|
|
53
|
-
|
|
54
|
-
this.hitCriticalError = false;
|
|
55
|
-
this._setupDone = false;
|
|
56
|
-
this._userNode = node;
|
|
57
|
-
this._curElement = null;
|
|
58
|
-
this._elements = [];
|
|
59
|
-
this._glAttributes = null;
|
|
60
|
-
this._requestAnimId = 0;
|
|
61
|
-
this._isGlobal = false;
|
|
62
|
-
this._loop = true;
|
|
63
|
-
this._startListener = null;
|
|
64
|
-
this._initializeInstanceVariables();
|
|
65
|
-
this._events = {
|
|
66
|
-
// keep track of user-events for unregistering later
|
|
67
|
-
pointerdown: null,
|
|
68
|
-
pointerup: null,
|
|
69
|
-
pointermove: null,
|
|
70
|
-
dragend: null,
|
|
71
|
-
dragover: null,
|
|
72
|
-
click: null,
|
|
73
|
-
dblclick: null,
|
|
74
|
-
mouseover: null,
|
|
75
|
-
mouseout: null,
|
|
76
|
-
keydown: null,
|
|
77
|
-
keyup: null,
|
|
78
|
-
keypress: null,
|
|
79
|
-
wheel: null,
|
|
80
|
-
resize: null,
|
|
81
|
-
blur: null
|
|
82
|
-
};
|
|
83
|
-
this._millisStart = -1;
|
|
84
|
-
this._recording = false;
|
|
85
|
-
|
|
86
|
-
// States used in the custom random generators
|
|
87
|
-
this._lcg_random_state = null; // NOTE: move to random.js
|
|
88
|
-
this._gaussian_previous = false; // NOTE: move to random.js
|
|
89
|
-
|
|
90
|
-
if (window.DeviceOrientationEvent) {
|
|
91
|
-
this._events.deviceorientation = null;
|
|
92
|
-
}
|
|
93
|
-
if (window.DeviceMotionEvent && !window._isNodeWebkit) {
|
|
94
|
-
this._events.devicemotion = null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ensure correct reporting of window dimensions
|
|
98
|
-
this._updateWindowSize();
|
|
99
|
-
|
|
100
|
-
const bindGlobal = (property) => {
|
|
101
|
-
Object.defineProperty(window, property, {
|
|
102
|
-
configurable: true,
|
|
103
|
-
enumerable: true,
|
|
104
|
-
get: () => {
|
|
105
|
-
if(typeof this[property] === 'function'){
|
|
106
|
-
return this[property].bind(this);
|
|
107
|
-
}else{
|
|
108
|
-
return this[property];
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
set: (newValue) => {
|
|
112
|
-
Object.defineProperty(window, property, {
|
|
113
|
-
configurable: true,
|
|
114
|
-
enumerable: true,
|
|
115
|
-
value: newValue,
|
|
116
|
-
writable: true
|
|
117
|
-
});
|
|
118
|
-
if (!p5.disableFriendlyErrors) {
|
|
119
|
-
console.log(`You just changed the value of "${property}", which was a p5 global value. This could cause problems later if you're not careful.`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
};
|
|
124
|
-
// If the user has created a global setup or draw function,
|
|
125
|
-
// assume "global" mode and make everything global (i.e. on the window)
|
|
126
|
-
if (!sketch) {
|
|
127
|
-
this._isGlobal = true;
|
|
128
|
-
if (window.hitCriticalError) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
p5.instance = this;
|
|
132
|
-
|
|
133
|
-
// Loop through methods on the prototype and attach them to the window
|
|
134
|
-
// All methods and properties with name starting with '_' will be skipped
|
|
135
|
-
for (const p of Object.getOwnPropertyNames(p5.prototype)) {
|
|
136
|
-
if(p[0] === '_') continue;
|
|
137
|
-
bindGlobal(p);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Attach its properties to the window
|
|
141
|
-
for (const p in this) {
|
|
142
|
-
if (this.hasOwnProperty(p)) {
|
|
143
|
-
if(p[0] === '_') continue;
|
|
144
|
-
bindGlobal(p);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
// Else, the user has passed in a sketch closure that may set
|
|
149
|
-
// user-provided 'setup', 'draw', etc. properties on this instance of p5
|
|
150
|
-
sketch(this);
|
|
151
|
-
|
|
152
|
-
// Run a check to see if the user has misspelled 'setup', 'draw', etc
|
|
153
|
-
// detects capitalization mistakes only ( Setup, SETUP, MouseClicked, etc)
|
|
154
|
-
p5._checkForUserDefinedFunctions(this);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Bind events to window (not using container div bc key events don't work)
|
|
158
|
-
for (const e in this._events) {
|
|
159
|
-
const f = this[`_on${e}`];
|
|
160
|
-
if (f) {
|
|
161
|
-
const m = f.bind(this);
|
|
162
|
-
window.addEventListener(e, m, { passive: false });
|
|
163
|
-
this._events[e] = m;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const focusHandler = () => {
|
|
168
|
-
this.focused = true;
|
|
169
|
-
};
|
|
170
|
-
const blurHandler = () => {
|
|
171
|
-
this.focused = false;
|
|
172
|
-
};
|
|
173
|
-
window.addEventListener('focus', focusHandler);
|
|
174
|
-
window.addEventListener('blur', blurHandler);
|
|
175
|
-
p5.lifecycleHooks.remove.push(function() {
|
|
176
|
-
window.removeEventListener('focus', focusHandler);
|
|
177
|
-
window.removeEventListener('blur', blurHandler);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
// Initialization complete, start runtime
|
|
181
|
-
if (document.readyState === 'complete') {
|
|
182
|
-
this.#_start();
|
|
183
|
-
} else {
|
|
184
|
-
this._startListener = this.#_start.bind(this);
|
|
185
|
-
window.addEventListener('load', this._startListener, false);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
get pixels(){
|
|
190
|
-
return this._renderer.pixels;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
get drawingContext(){
|
|
194
|
-
return this._renderer.drawingContext;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
static registerAddon(addon) {
|
|
198
|
-
const lifecycles = {};
|
|
199
|
-
addon(p5, p5.prototype, lifecycles);
|
|
200
|
-
|
|
201
|
-
const validLifecycles = Object.keys(p5.lifecycleHooks);
|
|
202
|
-
for(const name of validLifecycles){
|
|
203
|
-
if(typeof lifecycles[name] === 'function'){
|
|
204
|
-
p5.lifecycleHooks[name].push(lifecycles[name]);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async #_start() {
|
|
210
|
-
if (this.hitCriticalError) return;
|
|
211
|
-
// Find node if id given
|
|
212
|
-
if (this._userNode) {
|
|
213
|
-
if (typeof this._userNode === 'string') {
|
|
214
|
-
this._userNode = document.getElementById(this._userNode);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
await this.#_setup();
|
|
219
|
-
if (this.hitCriticalError) return;
|
|
220
|
-
if (!this._recording) {
|
|
221
|
-
this._draw();
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async #_setup() {
|
|
226
|
-
// Run `presetup` hooks
|
|
227
|
-
await this._runLifecycleHook('presetup');
|
|
228
|
-
if (this.hitCriticalError) return;
|
|
229
|
-
|
|
230
|
-
// Always create a default canvas.
|
|
231
|
-
// Later on if the user calls createCanvas, this default one
|
|
232
|
-
// will be replaced
|
|
233
|
-
this.createCanvas(
|
|
234
|
-
100,
|
|
235
|
-
100,
|
|
236
|
-
constants.P2D
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
// Record the time when sketch starts
|
|
240
|
-
this._millisStart = window.performance.now();
|
|
241
|
-
|
|
242
|
-
const context = this._isGlobal ? window : this;
|
|
243
|
-
if (typeof context.setup === 'function') {
|
|
244
|
-
await context.setup();
|
|
245
|
-
}
|
|
246
|
-
if (this.hitCriticalError) return;
|
|
247
|
-
|
|
248
|
-
// unhide any hidden canvases that were created
|
|
249
|
-
const canvases = document.getElementsByTagName('canvas');
|
|
250
|
-
|
|
251
|
-
// Apply touchAction = 'none' to canvases if pointer events exist
|
|
252
|
-
if (Object.keys(this._events).some(event => event.startsWith('pointer'))) {
|
|
253
|
-
for (const k of canvases) {
|
|
254
|
-
k.style.touchAction = 'none';
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
for (const k of canvases) {
|
|
260
|
-
if (k.dataset.hidden === 'true') {
|
|
261
|
-
k.style.visibility = '';
|
|
262
|
-
delete k.dataset.hidden;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
this._lastTargetFrameTime = window.performance.now();
|
|
267
|
-
this._lastRealFrameTime = window.performance.now();
|
|
268
|
-
this._setupDone = true;
|
|
269
|
-
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
|
|
270
|
-
this._updateAccsOutput();
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Run `postsetup` hooks
|
|
274
|
-
await this._runLifecycleHook('postsetup');
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// While '#_draw' here is async, it is not awaited as 'requestAnimationFrame'
|
|
278
|
-
// does not await its callback. Thus it is not recommended for 'draw()` to be
|
|
279
|
-
// async and use await within as the next frame may start rendering before the
|
|
280
|
-
// current frame finish awaiting. The same goes for lifecycle hooks 'predraw'
|
|
281
|
-
// and 'postdraw'.
|
|
282
|
-
async _draw(requestAnimationFrameTimestamp) {
|
|
283
|
-
if (this.hitCriticalError) return;
|
|
284
|
-
const now = requestAnimationFrameTimestamp || window.performance.now();
|
|
285
|
-
const timeSinceLastFrame = now - this._lastTargetFrameTime;
|
|
286
|
-
const targetTimeBetweenFrames = 1000 / this._targetFrameRate;
|
|
287
|
-
|
|
288
|
-
// only draw if we really need to; don't overextend the browser.
|
|
289
|
-
// draw if we're within 5ms of when our next frame should paint
|
|
290
|
-
// (this will prevent us from giving up opportunities to draw
|
|
291
|
-
// again when it's really about time for us to do so). fixes an
|
|
292
|
-
// issue where the frameRate is too low if our refresh loop isn't
|
|
293
|
-
// in sync with the browser. note that we have to draw once even
|
|
294
|
-
// if looping is off, so we bypass the time delay if that
|
|
295
|
-
// is the case.
|
|
296
|
-
const epsilon = 5;
|
|
297
|
-
if (
|
|
298
|
-
!this._loop ||
|
|
299
|
-
timeSinceLastFrame >= targetTimeBetweenFrames - epsilon
|
|
300
|
-
) {
|
|
301
|
-
//mandatory update values(matrixes and stack)
|
|
302
|
-
this.deltaTime = now - this._lastRealFrameTime;
|
|
303
|
-
this._frameRate = 1000.0 / this.deltaTime;
|
|
304
|
-
await this.redraw();
|
|
305
|
-
this._lastTargetFrameTime = Math.max(this._lastTargetFrameTime
|
|
306
|
-
+ targetTimeBetweenFrames, now);
|
|
307
|
-
this._lastRealFrameTime = now;
|
|
308
|
-
|
|
309
|
-
// If the user is actually using mouse module, then update
|
|
310
|
-
// coordinates, otherwise skip. We can test this by simply
|
|
311
|
-
// checking if any of the mouse functions are available or not.
|
|
312
|
-
// NOTE : This reflects only in complete build or modular build.
|
|
313
|
-
if (typeof this._updateMouseCoords !== 'undefined') {
|
|
314
|
-
this._updateMouseCoords();
|
|
315
|
-
|
|
316
|
-
//reset delta values so they reset even if there is no mouse event to set them
|
|
317
|
-
// for example if the mouse is outside the screen
|
|
318
|
-
this.movedX = 0;
|
|
319
|
-
this.movedY = 0;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// get notified the next time the browser gives us
|
|
324
|
-
// an opportunity to draw.
|
|
325
|
-
if (this._loop) {
|
|
326
|
-
this._requestAnimId = window.requestAnimationFrame(
|
|
327
|
-
this._draw.bind(this)
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Removes the sketch from the web page.
|
|
334
|
-
*
|
|
335
|
-
* Calling `remove()` stops the draw loop and removes any HTML elements
|
|
336
|
-
* created by the sketch, including the canvas. A new sketch can be
|
|
337
|
-
* created by using the <a href="#/p5/p5">p5()</a> constructor, as in
|
|
338
|
-
* `new p5()`.
|
|
339
|
-
*
|
|
340
|
-
* @example
|
|
341
|
-
* <div>
|
|
342
|
-
* <code>
|
|
343
|
-
* // Double-click to remove the canvas.
|
|
344
|
-
*
|
|
345
|
-
* function setup() {
|
|
346
|
-
* createCanvas(100, 100);
|
|
347
|
-
*
|
|
348
|
-
* describe(
|
|
349
|
-
* 'A white circle on a gray background. The circle follows the mouse as the user moves. The sketch disappears when the user double-clicks.'
|
|
350
|
-
* );
|
|
351
|
-
* }
|
|
352
|
-
*
|
|
353
|
-
* function draw() {
|
|
354
|
-
* // Paint the background repeatedly.
|
|
355
|
-
* background(200);
|
|
356
|
-
*
|
|
357
|
-
* // Draw circles repeatedly.
|
|
358
|
-
* circle(mouseX, mouseY, 40);
|
|
359
|
-
* }
|
|
360
|
-
*
|
|
361
|
-
* // Remove the sketch when the user double-clicks.
|
|
362
|
-
* function doubleClicked() {
|
|
363
|
-
* remove();
|
|
364
|
-
* }
|
|
365
|
-
* </code>
|
|
366
|
-
* </div>
|
|
367
|
-
*/
|
|
368
|
-
async remove() {
|
|
369
|
-
// Remove start listener to prevent orphan canvas being created
|
|
370
|
-
if(this._startListener){
|
|
371
|
-
window.removeEventListener('load', this._startListener, false);
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
if (this._curElement) {
|
|
375
|
-
// stop draw
|
|
376
|
-
this._loop = false;
|
|
377
|
-
if (this._requestAnimId) {
|
|
378
|
-
window.cancelAnimationFrame(this._requestAnimId);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
// unregister events sketch-wide
|
|
382
|
-
for (const ev in this._events) {
|
|
383
|
-
window.removeEventListener(ev, this._events[ev]);
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// remove DOM elements created by p5, and listeners
|
|
387
|
-
for (const e of this._elements) {
|
|
388
|
-
if (e.elt && e.elt.parentNode) {
|
|
389
|
-
e.elt.parentNode.removeChild(e.elt);
|
|
390
|
-
}
|
|
391
|
-
for (const elt_ev in e._events) {
|
|
392
|
-
e.elt.removeEventListener(elt_ev, e._events[elt_ev]);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// Run `remove` hooks
|
|
397
|
-
await this._runLifecycleHook('remove');
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// remove window bound properties and methods
|
|
401
|
-
if (this._isGlobal) {
|
|
402
|
-
for (const p in p5.prototype) {
|
|
403
|
-
try {
|
|
404
|
-
delete window[p];
|
|
405
|
-
} catch (x) {
|
|
406
|
-
window[p] = undefined;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
for (const p2 in this) {
|
|
410
|
-
if (this.hasOwnProperty(p2)) {
|
|
411
|
-
try {
|
|
412
|
-
delete window[p2];
|
|
413
|
-
} catch (x) {
|
|
414
|
-
window[p2] = undefined;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
p5.instance = null;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
async _runLifecycleHook(hookName) {
|
|
423
|
-
for(const hook of p5.lifecycleHooks[hookName]){
|
|
424
|
-
await hook.call(this);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
_initializeInstanceVariables() {
|
|
429
|
-
this._accessibleOutputs = {
|
|
430
|
-
text: false,
|
|
431
|
-
grid: false,
|
|
432
|
-
textLabel: false,
|
|
433
|
-
gridLabel: false
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
this._styles = [];
|
|
437
|
-
this._downKeys = {}; //Holds the key codes of currently pressed keys
|
|
438
|
-
this._downKeyCodes = {};
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
// Attach constants to p5 prototype
|
|
443
|
-
for (const k in constants) {
|
|
444
|
-
p5.prototype[k] = constants[k];
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
//////////////////////////////////////////////
|
|
448
|
-
// PUBLIC p5 PROPERTIES AND METHODS
|
|
449
|
-
//////////////////////////////////////////////
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* A function that's called once when the sketch begins running.
|
|
453
|
-
*
|
|
454
|
-
* Declaring the function `setup()` sets a code block to run once
|
|
455
|
-
* automatically when the sketch starts running. It's used to perform
|
|
456
|
-
* setup tasks such as creating the canvas and initializing variables:
|
|
457
|
-
*
|
|
458
|
-
* ```js
|
|
459
|
-
* function setup() {
|
|
460
|
-
* // Code to run once at the start of the sketch.
|
|
461
|
-
* }
|
|
462
|
-
* ```
|
|
463
|
-
*
|
|
464
|
-
* Code placed in `setup()` will run once before code placed in
|
|
465
|
-
* <a href="#/p5/draw">draw()</a> begins looping. If the
|
|
466
|
-
* <a href="#/p5/preload">preload()</a> is declared, then `setup()` will
|
|
467
|
-
* run immediately after <a href="#/p5/preload">preload()</a> finishes
|
|
468
|
-
* loading assets.
|
|
469
|
-
*
|
|
470
|
-
* Note: `setup()` doesn’t have to be declared, but it’s common practice to do so.
|
|
471
|
-
*
|
|
472
|
-
* @method setup
|
|
473
|
-
* @for p5
|
|
474
|
-
*
|
|
475
|
-
* @example
|
|
476
|
-
* <div>
|
|
477
|
-
* <code>
|
|
478
|
-
* function setup() {
|
|
479
|
-
* createCanvas(100, 100);
|
|
480
|
-
*
|
|
481
|
-
* background(200);
|
|
482
|
-
*
|
|
483
|
-
* // Draw the circle.
|
|
484
|
-
* circle(50, 50, 40);
|
|
485
|
-
*
|
|
486
|
-
* describe('A white circle on a gray background.');
|
|
487
|
-
* }
|
|
488
|
-
* </code>
|
|
489
|
-
* </div>
|
|
490
|
-
*
|
|
491
|
-
* <div>
|
|
492
|
-
* <code>
|
|
493
|
-
* function setup() {
|
|
494
|
-
* createCanvas(100, 100);
|
|
495
|
-
*
|
|
496
|
-
* // Paint the background once.
|
|
497
|
-
* background(200);
|
|
498
|
-
*
|
|
499
|
-
* describe(
|
|
500
|
-
* 'A white circle on a gray background. The circle follows the mouse as the user moves, leaving a trail.'
|
|
501
|
-
* );
|
|
502
|
-
* }
|
|
503
|
-
*
|
|
504
|
-
* function draw() {
|
|
505
|
-
* // Draw circles repeatedly.
|
|
506
|
-
* circle(mouseX, mouseY, 40);
|
|
507
|
-
* }
|
|
508
|
-
* </code>
|
|
509
|
-
* </div>
|
|
510
|
-
*
|
|
511
|
-
* <div>
|
|
512
|
-
* <code>
|
|
513
|
-
* let img;
|
|
514
|
-
*
|
|
515
|
-
* async function setup() {
|
|
516
|
-
* img = await loadImage('assets/bricks.jpg');
|
|
517
|
-
*
|
|
518
|
-
* createCanvas(100, 100);
|
|
519
|
-
*
|
|
520
|
-
* // Draw the image.
|
|
521
|
-
* image(img, 0, 0);
|
|
522
|
-
*
|
|
523
|
-
* describe(
|
|
524
|
-
* 'A white circle on a brick wall. The circle follows the mouse as the user moves, leaving a trail.'
|
|
525
|
-
* );
|
|
526
|
-
* }
|
|
527
|
-
*
|
|
528
|
-
* function draw() {
|
|
529
|
-
* // Style the circle.
|
|
530
|
-
* noStroke();
|
|
531
|
-
*
|
|
532
|
-
* // Draw the circle.
|
|
533
|
-
* circle(mouseX, mouseY, 10);
|
|
534
|
-
* }
|
|
535
|
-
* </code>
|
|
536
|
-
* </div>
|
|
537
|
-
*/
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* A function that's called repeatedly while the sketch runs.
|
|
541
|
-
*
|
|
542
|
-
* Declaring the function `draw()` sets a code block to run repeatedly
|
|
543
|
-
* once the sketch starts. It’s used to create animations and respond to
|
|
544
|
-
* user inputs:
|
|
545
|
-
*
|
|
546
|
-
* ```js
|
|
547
|
-
* function draw() {
|
|
548
|
-
* // Code to run repeatedly.
|
|
549
|
-
* }
|
|
550
|
-
* ```
|
|
551
|
-
*
|
|
552
|
-
* This is often called the "draw loop" because p5.js calls the code in
|
|
553
|
-
* `draw()` in a loop behind the scenes. By default, `draw()` tries to run
|
|
554
|
-
* 60 times per second. The actual rate depends on many factors. The
|
|
555
|
-
* drawing rate, called the "frame rate", can be controlled by calling
|
|
556
|
-
* <a href="#/p5/frameRate">frameRate()</a>. The number of times `draw()`
|
|
557
|
-
* has run is stored in the system variable
|
|
558
|
-
* <a href="#/p5/frameCount">frameCount()</a>.
|
|
559
|
-
*
|
|
560
|
-
* Code placed within `draw()` begins looping after
|
|
561
|
-
* <a href="#/p5/setup">setup()</a> runs. `draw()` will run until the user
|
|
562
|
-
* closes the sketch. `draw()` can be stopped by calling the
|
|
563
|
-
* <a href="#/p5/noLoop">noLoop()</a> function. `draw()` can be resumed by
|
|
564
|
-
* calling the <a href="#/p5/loop">loop()</a> function.
|
|
565
|
-
*
|
|
566
|
-
* @method draw
|
|
567
|
-
* @for p5
|
|
568
|
-
*
|
|
569
|
-
* @example
|
|
570
|
-
* <div>
|
|
571
|
-
* <code>
|
|
572
|
-
* function setup() {
|
|
573
|
-
* createCanvas(100, 100);
|
|
574
|
-
*
|
|
575
|
-
* // Paint the background once.
|
|
576
|
-
* background(200);
|
|
577
|
-
*
|
|
578
|
-
* describe(
|
|
579
|
-
* 'A white circle on a gray background. The circle follows the mouse as the user moves, leaving a trail.'
|
|
580
|
-
* );
|
|
581
|
-
* }
|
|
582
|
-
*
|
|
583
|
-
* function draw() {
|
|
584
|
-
* // Draw circles repeatedly.
|
|
585
|
-
* circle(mouseX, mouseY, 40);
|
|
586
|
-
* }
|
|
587
|
-
* </code>
|
|
588
|
-
* </div>
|
|
589
|
-
*
|
|
590
|
-
* <div>
|
|
591
|
-
* <code>
|
|
592
|
-
* function setup() {
|
|
593
|
-
* createCanvas(100, 100);
|
|
594
|
-
*
|
|
595
|
-
* describe(
|
|
596
|
-
* 'A white circle on a gray background. The circle follows the mouse as the user moves.'
|
|
597
|
-
* );
|
|
598
|
-
* }
|
|
599
|
-
*
|
|
600
|
-
* function draw() {
|
|
601
|
-
* // Paint the background repeatedly.
|
|
602
|
-
* background(200);
|
|
603
|
-
*
|
|
604
|
-
* // Draw circles repeatedly.
|
|
605
|
-
* circle(mouseX, mouseY, 40);
|
|
606
|
-
* }
|
|
607
|
-
* </code>
|
|
608
|
-
* </div>
|
|
609
|
-
*
|
|
610
|
-
* <div>
|
|
611
|
-
* <code>
|
|
612
|
-
* // Double-click the canvas to change the circle's color.
|
|
613
|
-
*
|
|
614
|
-
* function setup() {
|
|
615
|
-
* createCanvas(100, 100);
|
|
616
|
-
*
|
|
617
|
-
* describe(
|
|
618
|
-
* 'A white circle on a gray background. The circle follows the mouse as the user moves. The circle changes color to pink when the user double-clicks.'
|
|
619
|
-
* );
|
|
620
|
-
* }
|
|
621
|
-
*
|
|
622
|
-
* function draw() {
|
|
623
|
-
* // Paint the background repeatedly.
|
|
624
|
-
* background(200);
|
|
625
|
-
*
|
|
626
|
-
* // Draw circles repeatedly.
|
|
627
|
-
* circle(mouseX, mouseY, 40);
|
|
628
|
-
* }
|
|
629
|
-
*
|
|
630
|
-
* // Change the fill color when the user double-clicks.
|
|
631
|
-
* function doubleClicked() {
|
|
632
|
-
* fill('deeppink');
|
|
633
|
-
* }
|
|
634
|
-
* </code>
|
|
635
|
-
* </div>
|
|
636
|
-
*/
|
|
637
|
-
|
|
638
|
-
/**
|
|
639
|
-
* Turns off the parts of the Friendly Error System (FES) that impact performance.
|
|
640
|
-
*
|
|
641
|
-
* The <a href="https://github.com/processing/p5.js/blob/main/contributor_docs/friendly_error_system.md" target="_blank">FES</a>
|
|
642
|
-
* can cause sketches to draw slowly because it does extra work behind the
|
|
643
|
-
* scenes. For example, the FES checks the arguments passed to functions,
|
|
644
|
-
* which takes time to process. Disabling the FES can significantly improve
|
|
645
|
-
* performance by turning off these checks.
|
|
646
|
-
*
|
|
647
|
-
* @property {Boolean} disableFriendlyErrors
|
|
648
|
-
*
|
|
649
|
-
* @example
|
|
650
|
-
* <div>
|
|
651
|
-
* <code>
|
|
652
|
-
* // Disable the FES.
|
|
653
|
-
* p5.disableFriendlyErrors = true;
|
|
654
|
-
*
|
|
655
|
-
* function setup() {
|
|
656
|
-
* createCanvas(100, 100);
|
|
657
|
-
*
|
|
658
|
-
* background(200);
|
|
659
|
-
*
|
|
660
|
-
* // The circle() function requires three arguments. The
|
|
661
|
-
* // next line would normally display a friendly error that
|
|
662
|
-
* // points this out. Instead, nothing happens and it fails
|
|
663
|
-
* // silently.
|
|
664
|
-
* circle(50, 50);
|
|
665
|
-
*
|
|
666
|
-
* describe('A gray square.');
|
|
667
|
-
* }
|
|
668
|
-
* </code>
|
|
669
|
-
* </div>
|
|
670
|
-
*/
|
|
671
|
-
p5.disableFriendlyErrors = false;
|
|
672
|
-
|
|
673
|
-
import transform from './transform';
|
|
674
|
-
import structure from './structure';
|
|
675
|
-
import environment from './environment';
|
|
676
|
-
import rendering from './rendering';
|
|
677
|
-
import renderer from './p5.Renderer';
|
|
678
|
-
import renderer2D from './p5.Renderer2D';
|
|
679
|
-
import graphics from './p5.Graphics';
|
|
680
|
-
|
|
681
|
-
p5.registerAddon(transform);
|
|
682
|
-
p5.registerAddon(structure);
|
|
683
|
-
p5.registerAddon(environment);
|
|
684
|
-
p5.registerAddon(rendering);
|
|
685
|
-
p5.registerAddon(renderer);
|
|
686
|
-
p5.registerAddon(renderer2D);
|
|
687
|
-
p5.registerAddon(graphics);
|
|
688
|
-
|
|
689
|
-
export default p5;
|
package/src/core/noop.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function(){}
|