Skip to content
Open
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"types": "./types/global.d.ts",
"default": "./dist/app.js"
},
"./node": {
"types": "./types/p5.d.ts",
"default": "./dist/app.node.js"
},
"./core": {
"default": "./dist/core/main.js"
},
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default [
format: 'es',
dir: 'dist'
},
external: /node_modules/,
external: /node_modules\/(?!gifenc)/,
plugins
},
...generateModuleBuild()
Expand Down
60 changes: 60 additions & 0 deletions src/app.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// core
import p5 from './core/main';

// shape
import shape from './shape';
shape(p5);

//accessibility
import accessibility from './accessibility';
accessibility(p5);

// color
import color from './color';
color(p5);

// core
// currently, it only contains the test for parameter validation
// import friendlyErrors from './core/friendly_errors';
// friendlyErrors(p5);

// data
import data from './data';
data(p5);

// DOM
import dom from './dom';
dom(p5);

// image
import image from './image';
image(p5);

// io
import io from './io';
io(p5);

// math
import math from './math';
math(p5);

// utilities
import utilities from './utilities';
utilities(p5);

// webgl
import webgl from './webgl';
webgl(p5);

// typography
import type from './type';
type(p5);

// Shaders + filters
import shader from './webgl/p5.Shader';
p5.registerAddon(shader);
import strands from './strands/p5.strands';
p5.registerAddon(strands);

export default p5;

43 changes: 17 additions & 26 deletions src/core/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ import * as C from './constants';

function environment(p5, fn, lifecycles){
const standardCursors = [C.ARROW, C.CROSS, C.HAND, C.MOVE, C.TEXT, C.WAIT];
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';

fn._frameRate = 0;
fn._lastFrameTime = window.performance.now();
fn._lastFrameTime = globalThis.performance.now();
fn._targetFrameRate = 60;

const _windowPrint = window.print;
const windowPrint = isBrowser ? window.print : null;
let windowPrintDisabled = false;

lifecycles.presetup = function(){
const events = [
'resize'
];

for(const event of events){
window.addEventListener(event, this[`_on${event}`].bind(this), {
passive: false,
signal: this._removeSignal
});
if(isBrowser){
for(const event of events){
window.addEventListener(event, this[`_on${event}`].bind(this), {
passive: false,
signal: this._removeSignal
});
}
}
};

Expand Down Expand Up @@ -59,9 +62,9 @@ function environment(p5, fn, lifecycles){
* }
*/
fn.print = function(...args) {
if (!args.length) {
if (!args.length && windowPrint !== null) {
if (!windowPrintDisabled) {
_windowPrint();
windowPrint();
if (
window.confirm(
'You just tried to print the webpage. Do you want to prevent this from running again?'
Expand Down Expand Up @@ -198,7 +201,7 @@ function environment(p5, fn, lifecycles){
* }
* }
*/
fn.focused = document.hasFocus();
fn.focused = isBrowser ? document.hasFocus() : true;

/**
* Changes the cursor's appearance.
Expand Down Expand Up @@ -580,7 +583,7 @@ function environment(p5, fn, lifecycles){
* @alt
* This example does not render anything.
*/
fn.displayWidth = screen.width;
fn.displayWidth = isBrowser ? window.screen.width : 0;

/**
* A `Number` variable that stores the height of the screen display.
Expand Down Expand Up @@ -608,7 +611,7 @@ function environment(p5, fn, lifecycles){
* @alt
* This example does not render anything.
*/
fn.displayHeight = screen.height;
fn.displayHeight = isBrowser ? window.screen.height : 0;

/**
* A `Number` variable that stores the width of the browser's viewport.
Expand Down Expand Up @@ -734,21 +737,11 @@ function environment(p5, fn, lifecycles){
};

function getWindowWidth() {
return (
window.innerWidth ||
(document.documentElement && document.documentElement.clientWidth) ||
(document.body && document.body.clientWidth) ||
0
);
return isBrowser ? document.documentElement.clientWidth : 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any implications to using document.documentElement.clientWidth rather than window.innerWidth?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.innerWidth includes scrollbar while document.documentElement.clientWidth does not, which means the former will include areas that will be covered up by the scrollbar while the later will only be the visible area.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is kind of a different behavior although the previous behavior feels a bit undefined because it fallback on these different definition, at the same time I think document.documentElement.clientWidth is more likely to be the desired value. Can change it back though if there are edge cases that will be messed up by this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense then, we can just make sure we test this a bit when it's in an RC to see if there are any edge cases it breaks

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I can think of is that if the sketch is created when the page does not have scroll bars but later on in the page lifetime it gained a scroll bar, that may be a bit unexpected since the value of document.documentElement.clientWidth changed in this case while window.innerWidth would stay the same. Not sure if it would be a problem though.

}

function getWindowHeight() {
return (
window.innerHeight ||
(document.documentElement && document.documentElement.clientHeight) ||
(document.body && document.body.clientHeight) ||
0
);
return isBrowser ? document.documentElement.clientHeight : 0;
}

/**
Expand Down Expand Up @@ -808,7 +801,6 @@ function environment(p5, fn, lifecycles){
* }
*/
fn.fullscreen = function(val) {
// p5._validateParameters('fullscreen', arguments);
// no arguments, return fullscreen or not
if (typeof val === 'undefined') {
return (
Expand Down Expand Up @@ -879,7 +871,6 @@ function environment(p5, fn, lifecycles){
* @returns {Number} current pixel density of the sketch.
*/
fn.pixelDensity = function(val) {
// p5._validateParameters('pixelDensity', arguments);
let returnValue;
if (typeof val === 'number') {
if (val !== this._renderer._pixelDensity) {
Expand Down
14 changes: 8 additions & 6 deletions src/core/friendly_errors/fes_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { translator } from '../internationalization';
import errorTable from './browser_errors';
import * as contants from '../constants';

function fesCore(p5, fn){
function fesCore(p5, fn, lifecycles){
// p5.js blue, p5.js orange, auto dark green; fallback p5.js darkened magenta
// See testColors below for all the color codes and names
const typeColors = ['#2D7BB6', '#EE9900', '#4DB200', '#C83C00'];
Expand Down Expand Up @@ -972,9 +972,11 @@ function fesCore(p5, fn){
p5._fesLogger = null;
p5._fesLogCache = {};

window.addEventListener('load', checkForUserDefinedFunctions, false);
window.addEventListener('error', p5._fesErrorMonitor, false);
window.addEventListener('unhandledrejection', p5._fesErrorMonitor, false);
lifecycles.presetup = function () {
window.addEventListener('load', checkForUserDefinedFunctions, false);
window.addEventListener('error', p5._fesErrorMonitor, false);
window.addEventListener('unhandledrejection', p5._fesErrorMonitor, false);
};

/**
* Prints out all the colors in the color pallete with white text.
Expand Down Expand Up @@ -1134,7 +1136,7 @@ function fesCore(p5, fn){
// Exposing this primarily for unit testing.
fn._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode;

if (document.readyState !== 'complete') {
if (typeof document !== 'undefined' && document.readyState !== 'complete') {
window.addEventListener('error', helpForMisusedAtTopLevelCode, false);

// Our job is only to catch ReferenceErrors that are thrown when
Expand All @@ -1150,5 +1152,5 @@ function fesCore(p5, fn){
export default fesCore;

if (typeof p5 !== 'undefined') {
fesCore(p5, p5.prototype);
p5.registerAddon(fesCore);
}
2 changes: 1 addition & 1 deletion src/core/friendly_errors/param_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,5 +618,5 @@ function validateParams(p5, fn, lifecycles) {
export default validateParams;

if (typeof p5 !== 'undefined') {
validateParams(p5, p5.prototype);
p5.registerAddon(validateParams);
}
26 changes: 15 additions & 11 deletions src/core/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { initialize as initTranslator } from './internationalization';
* @return {Undefined}
*/
export const _globalInit = () => {
if(typeof window === 'undefined') return;
// Could have been any property defined within the p5 constructor.
// If that property is already a part of the global object,
// this code has already run before, likely due to a duplicate import
Expand Down Expand Up @@ -40,17 +41,20 @@ export const _globalInit = () => {
};

// make a promise that resolves when the document is ready
export const waitForDocumentReady = () =>
new Promise((resolve, reject) => {
// if the page is ready, initialize p5 immediately
if (document.readyState === 'complete') {
resolve();
// if the page is still loading, add an event listener
// and initialize p5 as soon as it finishes loading
} else {
window.addEventListener('load', resolve, false);
}
});
export const waitForDocumentReady = () =>{
if(typeof document !== 'undefined'){
return new Promise((resolve, reject) => {
// if the page is ready, initialize p5 immediately
if (document.readyState === 'complete') {
resolve();
// if the page is still loading, add an event listener
// and initialize p5 as soon as it finishes loading
} else {
window.addEventListener('load', resolve, false);
}
});
}
};

// only load translations if we're using the full, un-minified library
export const waitingForTranslator =
Expand Down
Loading
Loading