Skip to content

Commit 27f7624

Browse files
authored
Merge pull request #7365 from limzykenneth/2.0-io
[p5.js 2.0] IO refactoring
2 parents fe0cae3 + d71c498 commit 27f7624

31 files changed

+2398
-3479
lines changed

package-lock.json

+219-211
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@rollup/plugin-node-resolve": "^15.2.3",
3838
"@rollup/plugin-replace": "^5.0.7",
3939
"@rollup/plugin-terser": "^0.4.4",
40-
"@vitest/browser": "^2.0.4",
40+
"@vitest/browser": "^2.1.5",
4141
"all-contributors-cli": "^6.19.0",
4242
"concurrently": "^8.2.2",
4343
"connect-modrewrite": "^0.10.1",
@@ -48,14 +48,15 @@
4848
"i18next": "^19.0.2",
4949
"i18next-browser-languagedetector": "^4.0.1",
5050
"lint-staged": "^15.1.0",
51+
"msw": "^2.6.3",
5152
"rollup": "^4.9.6",
5253
"rollup-plugin-string": "^3.0.0",
5354
"rollup-plugin-visualizer": "^5.12.0",
5455
"typescript": "^5.5.4",
5556
"unplugin-swc": "^1.4.2",
5657
"vite": "^5.0.2",
5758
"vite-plugin-string": "^1.2.2",
58-
"vitest": "^2.0.4",
59+
"vitest": "^2.1.5",
5960
"webdriverio": "^9.0.7",
6061
"zod": "^3.23.8"
6162
},
@@ -83,5 +84,10 @@
8384
"hooks": {
8485
"pre-commit": "lint-staged"
8586
}
87+
},
88+
"msw": {
89+
"workerDirectory": [
90+
"test"
91+
]
8692
}
8793
}

preview/vite.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import vitePluginString from 'vite-plugin-string';
33

44
export default defineConfig({
55
root: './',
6+
appType: 'mpa',
67
plugins: [
78
vitePluginString({
89
include: [

src/core/main.js

-2
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,6 @@ import rendering from './rendering';
665665
import renderer from './p5.Renderer';
666666
import renderer2D from './p5.Renderer2D';
667667
import graphics from './p5.Graphics';
668-
// import element from './p5.Element';
669668

670669
p5.registerAddon(transform);
671670
p5.registerAddon(structure);
@@ -674,6 +673,5 @@ p5.registerAddon(rendering);
674673
p5.registerAddon(renderer);
675674
p5.registerAddon(renderer2D);
676675
p5.registerAddon(graphics);
677-
// p5.registerAddon(element);
678676

679677
export default p5;

src/core/p5.Graphics.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @for p5
55
*/
66

7-
import p5 from './main';
7+
// import p5 from './main';
88
import * as constants from './constants';
99
import primitives2D from '../shape/2d_primitives';
1010
import attributes from '../shape/attributes';
@@ -15,6 +15,7 @@ import image from '../image/image';
1515
import loadingDisplaying from '../image/loading_displaying';
1616
import pixels from '../image/pixels';
1717
import transform from './transform';
18+
import { Framebuffer } from '../webgl/p5.Framebuffer';
1819

1920
import primitives3D from '../webgl/3d_primitives';
2021
import light from '../webgl/light';
@@ -30,7 +31,7 @@ class Graphics {
3031
this._pInst = pInst;
3132
this._renderer = new renderers[r](this._pInst, w, h, false, canvas);
3233

33-
p5.prototype._initializeInstanceVariables.apply(this);
34+
this._initializeInstanceVariables(this);
3435

3536
this._renderer._applyDefaults();
3637
return this;
@@ -552,7 +553,7 @@ class Graphics {
552553
* </div>
553554
*/
554555
createFramebuffer(options) {
555-
return new p5.Framebuffer(this._renderer, options);
556+
return new Framebuffer(this._renderer, options);
556557
}
557558

558559
_assert3d(name) {
@@ -561,6 +562,29 @@ class Graphics {
561562
`${name}() is only supported in WEBGL mode. If you'd like to use 3D graphics and WebGL, see https://p5js.org/examples/form-3d-primitives.html for more information.`
562563
);
563564
};
565+
566+
_initializeInstanceVariables() {
567+
this._accessibleOutputs = {
568+
text: false,
569+
grid: false,
570+
textLabel: false,
571+
gridLabel: false
572+
};
573+
574+
this._styles = [];
575+
576+
this._bezierDetail = 20;
577+
this._curveDetail = 20;
578+
579+
this._colorMode = constants.RGB;
580+
this._colorMaxes = {
581+
rgb: [255, 255, 255, 255],
582+
hsb: [360, 100, 100, 1],
583+
hsl: [360, 100, 100, 1]
584+
};
585+
586+
this._downKeys = {}; //Holds the key codes of currently pressed keys
587+
}
564588
};
565589

566590
function graphics(p5, fn){

src/image/image.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* for drawing images to the main display canvas.
1111
*/
1212
import * as omggif from 'omggif';
13+
import { Element } from '../dom/p5.Element';
14+
import { Framebuffer } from '../webgl/p5.Framebuffer';
1315

1416
function image(p5, fn){
1517
/**
@@ -278,10 +280,10 @@ function image(p5, fn){
278280
if (args[0] instanceof HTMLCanvasElement) {
279281
htmlCanvas = args[0];
280282
args.shift();
281-
} else if (args[0] instanceof p5.Element) {
283+
} else if (args[0] instanceof Element) {
282284
htmlCanvas = args[0].elt;
283285
args.shift();
284-
} else if (args[0] instanceof p5.Framebuffer) {
286+
} else if (args[0] instanceof Framebuffer) {
285287
const framebuffer = args[0];
286288
temporaryGraphics = this.createGraphics(framebuffer.width,
287289
framebuffer.height);
@@ -325,6 +327,7 @@ function image(p5, fn){
325327
}
326328

327329
htmlCanvas.toBlob(blob => {
330+
console.log("here");
328331
fn.downloadFile(blob, filename, extension);
329332
if(temporaryGraphics) temporaryGraphics.remove();
330333
}, mimeType);
@@ -658,10 +661,10 @@ function image(p5, fn){
658661
fn.saveFrames = function(fName, ext, _duration, _fps, callback) {
659662
p5._validateParameters('saveFrames', arguments);
660663
let duration = _duration || 3;
661-
duration = fn.constrain(duration, 0, 15);
664+
duration = Math.max(Math.min(duration, 15), 0);
662665
duration = duration * 1000;
663666
let fps = _fps || 15;
664-
fps = fn.constrain(fps, 0, 22);
667+
fps = Math.max(Math.min(fps, 22), 0);
665668
let count = 0;
666669

667670
const makeFrame = fn._makeFrame;

0 commit comments

Comments
 (0)