Skip to content

Commit 38d522b

Browse files
authored
Merge pull request #8357 from limzykenneth/server-usage
Fixes for server side usage
2 parents e83e9d4 + 75d3364 commit 38d522b

12 files changed

Lines changed: 163 additions & 89 deletions

File tree

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
"types": "./types/global.d.ts",
8181
"default": "./dist/app.js"
8282
},
83+
"./node": {
84+
"types": "./types/p5.d.ts",
85+
"default": "./dist/app.node.js"
86+
},
8387
"./core": {
8488
"default": "./dist/core/main.js"
8589
},

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default [
193193
format: 'es',
194194
dir: 'dist'
195195
},
196-
external: /node_modules/,
196+
external: /node_modules\/(?!gifenc)/,
197197
plugins
198198
},
199199
...generateModuleBuild()

src/app.node.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// core
2+
import p5 from './core/main';
3+
4+
// shape
5+
import shape from './shape';
6+
shape(p5);
7+
8+
//accessibility
9+
import accessibility from './accessibility';
10+
accessibility(p5);
11+
12+
// color
13+
import color from './color';
14+
color(p5);
15+
16+
// core
17+
// currently, it only contains the test for parameter validation
18+
// import friendlyErrors from './core/friendly_errors';
19+
// friendlyErrors(p5);
20+
21+
// data
22+
import data from './data';
23+
data(p5);
24+
25+
// DOM
26+
import dom from './dom';
27+
dom(p5);
28+
29+
// image
30+
import image from './image';
31+
image(p5);
32+
33+
// io
34+
import io from './io';
35+
io(p5);
36+
37+
// math
38+
import math from './math';
39+
math(p5);
40+
41+
// utilities
42+
import utilities from './utilities';
43+
utilities(p5);
44+
45+
// webgl
46+
import webgl from './webgl';
47+
webgl(p5);
48+
49+
// typography
50+
import type from './type';
51+
type(p5);
52+
53+
// Shaders + filters
54+
import shader from './webgl/p5.Shader';
55+
p5.registerAddon(shader);
56+
import strands from './strands/p5.strands';
57+
p5.registerAddon(strands);
58+
59+
export default p5;
60+

src/core/environment.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ import * as C from './constants';
99

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

1314
fn._frameRate = 0;
14-
fn._lastFrameTime = window.performance.now();
15+
fn._lastFrameTime = globalThis.performance.now();
1516
fn._targetFrameRate = 60;
1617

17-
const _windowPrint = window.print;
18+
const windowPrint = isBrowser ? window.print : null;
1819
let windowPrintDisabled = false;
1920

2021
lifecycles.presetup = function(){
2122
const events = [
2223
'resize'
2324
];
2425

25-
for(const event of events){
26-
window.addEventListener(event, this[`_on${event}`].bind(this), {
27-
passive: false,
28-
signal: this._removeSignal
29-
});
26+
if(isBrowser){
27+
for(const event of events){
28+
window.addEventListener(event, this[`_on${event}`].bind(this), {
29+
passive: false,
30+
signal: this._removeSignal
31+
});
32+
}
3033
}
3134
};
3235

@@ -57,9 +60,9 @@ function environment(p5, fn, lifecycles){
5760
* }
5861
*/
5962
fn.print = function(...args) {
60-
if (!args.length) {
63+
if (!args.length && windowPrint !== null) {
6164
if (!windowPrintDisabled) {
62-
_windowPrint();
65+
windowPrint();
6366
if (
6467
window.confirm(
6568
'You just tried to print the webpage. Do you want to prevent this from running again?'
@@ -196,7 +199,7 @@ function environment(p5, fn, lifecycles){
196199
* }
197200
* }
198201
*/
199-
fn.focused = document.hasFocus();
202+
fn.focused = isBrowser ? document.hasFocus() : true;
200203

201204
/**
202205
* Changes the cursor's appearance.
@@ -578,7 +581,7 @@ function environment(p5, fn, lifecycles){
578581
* @alt
579582
* This example does not render anything.
580583
*/
581-
fn.displayWidth = screen.width;
584+
fn.displayWidth = isBrowser ? window.screen.width : 0;
582585

583586
/**
584587
* A `Number` variable that stores the height of the screen display.
@@ -606,7 +609,7 @@ function environment(p5, fn, lifecycles){
606609
* @alt
607610
* This example does not render anything.
608611
*/
609-
fn.displayHeight = screen.height;
612+
fn.displayHeight = isBrowser ? window.screen.height : 0;
610613

611614
/**
612615
* A `Number` variable that stores the width of the browser's viewport.
@@ -732,21 +735,11 @@ function environment(p5, fn, lifecycles){
732735
};
733736

734737
function getWindowWidth() {
735-
return (
736-
window.innerWidth ||
737-
(document.documentElement && document.documentElement.clientWidth) ||
738-
(document.body && document.body.clientWidth) ||
739-
0
740-
);
738+
return isBrowser ? document.documentElement.clientWidth : 0;
741739
}
742740

743741
function getWindowHeight() {
744-
return (
745-
window.innerHeight ||
746-
(document.documentElement && document.documentElement.clientHeight) ||
747-
(document.body && document.body.clientHeight) ||
748-
0
749-
);
742+
return isBrowser ? document.documentElement.clientHeight : 0;
750743
}
751744

752745
/**
@@ -806,7 +799,6 @@ function environment(p5, fn, lifecycles){
806799
* }
807800
*/
808801
fn.fullscreen = function(val) {
809-
// p5._validateParameters('fullscreen', arguments);
810802
// no arguments, return fullscreen or not
811803
if (typeof val === 'undefined') {
812804
return (
@@ -877,7 +869,6 @@ function environment(p5, fn, lifecycles){
877869
* @returns {Number} current pixel density of the sketch.
878870
*/
879871
fn.pixelDensity = function(val) {
880-
// p5._validateParameters('pixelDensity', arguments);
881872
let returnValue;
882873
if (typeof val === 'number') {
883874
if (val !== this._renderer._pixelDensity) {

src/core/friendly_errors/fes_core.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { translator } from '../internationalization';
2525
import errorTable from './browser_errors';
2626
import * as contants from '../constants';
2727

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

974-
window.addEventListener('load', checkForUserDefinedFunctions, false);
975-
window.addEventListener('error', p5._fesErrorMonitor, false);
976-
window.addEventListener('unhandledrejection', p5._fesErrorMonitor, false);
974+
lifecycles.presetup = function () {
975+
window.addEventListener('load', checkForUserDefinedFunctions, false);
976+
window.addEventListener('error', p5._fesErrorMonitor, false);
977+
window.addEventListener('unhandledrejection', p5._fesErrorMonitor, false);
978+
};
977979

978980
/**
979981
* Prints out all the colors in the color pallete with white text.
@@ -1133,7 +1135,7 @@ function fesCore(p5, fn){
11331135
// Exposing this primarily for unit testing.
11341136
fn._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode;
11351137

1136-
if (document.readyState !== 'complete') {
1138+
if (typeof document !== 'undefined' && document.readyState !== 'complete') {
11371139
window.addEventListener('error', helpForMisusedAtTopLevelCode, false);
11381140

11391141
// Our job is only to catch ReferenceErrors that are thrown when
@@ -1149,5 +1151,5 @@ function fesCore(p5, fn){
11491151
export default fesCore;
11501152

11511153
if (typeof p5 !== 'undefined') {
1152-
fesCore(p5, p5.prototype);
1154+
p5.registerAddon(fesCore);
11531155
}

src/core/friendly_errors/param_validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,5 +629,5 @@ function validateParams(p5, fn, lifecycles) {
629629
export default validateParams;
630630

631631
if (typeof p5 !== 'undefined') {
632-
validateParams(p5, p5.prototype);
632+
p5.registerAddon(validateParams);
633633
}

src/core/init.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { initialize as initTranslator } from './internationalization';
1313
* @return {Undefined}
1414
*/
1515
export const _globalInit = () => {
16+
if(typeof window === 'undefined') return;
1617
// Could have been any property defined within the p5 constructor.
1718
// If that property is already a part of the global object,
1819
// this code has already run before, likely due to a duplicate import
@@ -40,17 +41,20 @@ export const _globalInit = () => {
4041
};
4142

4243
// make a promise that resolves when the document is ready
43-
export const waitForDocumentReady = () =>
44-
new Promise((resolve, reject) => {
45-
// if the page is ready, initialize p5 immediately
46-
if (document.readyState === 'complete') {
47-
resolve();
48-
// if the page is still loading, add an event listener
49-
// and initialize p5 as soon as it finishes loading
50-
} else {
51-
window.addEventListener('load', resolve, false);
52-
}
53-
});
44+
export const waitForDocumentReady = () =>{
45+
if(typeof document !== 'undefined'){
46+
return new Promise((resolve, reject) => {
47+
// if the page is ready, initialize p5 immediately
48+
if (document.readyState === 'complete') {
49+
resolve();
50+
// if the page is still loading, add an event listener
51+
// and initialize p5 as soon as it finishes loading
52+
} else {
53+
window.addEventListener('load', resolve, false);
54+
}
55+
});
56+
}
57+
};
5458

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

0 commit comments

Comments
 (0)