@munusshih/p5.export 0.1.0-beta.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Munus Shih
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,156 @@
1
+ # p5.export
2
+
3
+ A [p5.js](https://p5js.org) add-on for one-line exports — **PNG, JPG, WebP, animated WebP, GIF, MP4, WebM, SVG, PDF, STL, OBJ** — with an optional floating GUI panel.
4
+
5
+ Works with **p5.js 1.x and 2.x**. p5 is brought by you (CDN or bundler); this package only ships the add-on.
6
+
7
+ ## Install
8
+
9
+ ### Drop-in `<script>`
10
+
11
+ ```html
12
+ <script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.js"></script>
13
+ <script src="https://cdn.jsdelivr.net/npm/p5.export"></script>
14
+ <script src="sketch.js"></script>
15
+ ```
16
+
17
+ ### npm + bundler
18
+
19
+ ```bash
20
+ npm install p5.export
21
+ ```
22
+
23
+ ```js
24
+ import 'p5'; // however you bring p5 onto the page
25
+ import 'p5.export'; // auto-registers against the global p5
26
+ ```
27
+
28
+ That's it. The GUI auto-mounts after `setup()` runs. Disable it with `window.p5ExportConfig = { autoGUI: false }` declared before this script loads, or remove it later with `removeExportGUI()`.
29
+
30
+ ## API
31
+
32
+ Every export attaches to the p5 prototype, so the calls work like any other p5 function — global mode or instance mode.
33
+
34
+ ### Images
35
+
36
+ ```js
37
+ savePNG('artwork') // 'artwork.png'
38
+ saveJPG('artwork', { quality: 0.9 })
39
+ saveWebP('artwork')
40
+ saveImage('png', 'artwork', { scale: 2 }) // any of png/jpg/webp/jpeg
41
+
42
+ // `scale` re-renders at 1×–4× pixelDensity. Trails / fade overlays are
43
+ // preserved by replaying the recorded draw history (no `redraw()` reset).
44
+ savePNG('big', { scale: 4 })
45
+ ```
46
+
47
+ ### Vector
48
+
49
+ ```js
50
+ saveSVG('artwork')
51
+ saveSVG('artwork', { forceVector: true }) // skip raster-fallback threshold
52
+ saveSVG('artwork', { raster: true }) // embed canvas PNG inside an SVG
53
+ saveSVG('artwork', { silent: true }) // skip the fallback confirm dialog
54
+
55
+ savePDF('artwork')
56
+ savePDF('artwork', { scale: 2 })
57
+ ```
58
+
59
+ `saveSVG()` records every Canvas2D draw call as the sketch runs and replays them onto an SVG context. Trails, fade rects, blend modes, gradients (linear / radial — emitted as native SVG gradients), and `drawingContext.setLineDash` all survive. WebGL renderers can only be embedded as a single raster `<image>`; the function prompts before doing so unless `{ silent: true }` or `{ raster: true }` is passed.
60
+
61
+ For dense sketches whose vector replay would balloon past 2 million ops, the export falls back to raster-in-SVG and asks the user to confirm. To force vector regardless of size, pass `{ forceVector: true }`.
62
+
63
+ #### Hybrid SVG
64
+
65
+ Mark expensive sections of your draw so they get rasterised inside the otherwise-vector SVG:
66
+
67
+ ```js
68
+ function setup() {
69
+ createCanvas(800, 800);
70
+ // …vector shapes…
71
+ beginRasterRegion();
72
+ for (let i = 0; i < 8000; i++) {
73
+ fill(random(255), random(255), random(255), 80);
74
+ rect(random(width), random(height), 2, 2);
75
+ }
76
+ endRasterRegion();
77
+ // …more vector shapes on top…
78
+ }
79
+ ```
80
+
81
+ ### Video (record once, export to multiple formats)
82
+
83
+ ```js
84
+ const buffer = createVideoBuffer({ duration: 3, fps: 30 }); // starts immediately
85
+
86
+ // later, after the buffer auto-stops or you call `stopVideoBuffer()`:
87
+ await exportVideoBuffer(buffer, { format: 'mp4', filename: 'clip' });
88
+ await exportVideoBuffer(buffer, { format: 'webm', filename: 'clip' });
89
+ await exportVideoBuffer(buffer, { format: 'webp', filename: 'clip' }); // animated WebP
90
+ await exportVideoBuffer(buffer, { format: 'gif', filename: 'clip' });
91
+ ```
92
+
93
+ Frames are captured in real RGBA at the canvas's native resolution. `exportVideoBuffer()` is re-callable on the same buffer — no re-recording — so users can pick the format they want from the GUI without losing the take.
94
+
95
+ You can also do a one-shot `recordVideo({ format: 'mp4', duration: 3, filename: 'clip' })` if you don't need the multi-format buffer.
96
+
97
+ ### 3D (WEBGL only)
98
+
99
+ ```js
100
+ const myModel = buildGeometry(() => sphere(50));
101
+ saveSTL(myModel, 'sphere.stl');
102
+ saveSTL(myModel, 'sphere.stl', { binary: true });
103
+
104
+ // Or pass a callback that p5 will run inside buildGeometry for you:
105
+ saveSTL(() => { sphere(50); cone(20, 40); }, 'scene.stl');
106
+ saveOBJ(() => { sphere(50); }, 'scene.obj');
107
+ ```
108
+
109
+ These delegate to p5.js 2.x's native `p5.Geometry.saveStl` / `saveObj`, so the encoded files match what p5 itself produces.
110
+
111
+ ### GUI
112
+
113
+ The panel auto-mounts post-setup. You can also drive it manually:
114
+
115
+ ```js
116
+ createExportGUI({ title: 'my sketch', filename: 'artwork', collapsed: false });
117
+ removeExportGUI();
118
+ ```
119
+
120
+ Drag from the header to move it. The two tabs (Image / Video) share the same compact form. The Video tab shows a low-res preview with a scrubbable timeline before you commit to encoding.
121
+
122
+ ## Browser support
123
+
124
+ | Feature | Chrome | Firefox | Safari |
125
+ |---|---|---|---|
126
+ | PNG / JPG / WebP image | ✓ | ✓ | ✓ (WebP 16+) |
127
+ | SVG (vector replay) | ✓ | ✓ | ✓ |
128
+ | PDF | ✓ | ✓ | ✓ |
129
+ | GIF | ✓ | ✓ | ✓ |
130
+ | Animated WebP | ✓ | ✓ | ✓ (16+) |
131
+ | MP4 (WebCodecs / MediaRecorder) | ✓ | partial | partial |
132
+ | WebM (WebCodecs / MediaRecorder) | ✓ | ✓ | partial |
133
+ | STL / OBJ (p5 2.x WEBGL) | ✓ | ✓ | ✓ |
134
+
135
+ MP4 falls back to WebM automatically if the browser's `MediaRecorder` can't encode H.264.
136
+
137
+ ## Notes
138
+
139
+ - **p5 1.x**: image, GIF, WebM/MP4, PDF, SVG, and the GUI all work. The SVG path uses a getter-based rebind on the prototype globals so `p5.pattern` and similar 1.x add-ons keep working through the recorder.
140
+ - **STL/OBJ** require a `p5.Geometry`, which only exists in p5 2.x's WEBGL renderer.
141
+ - The GUI's status row uses the `hidden` attribute, so empty messages take no vertical space.
142
+ - The vector SVG recorder caps at 60 frames of draw history by default. Override with `setSVGRecordingFrames(n)` (use `Infinity` to keep everything; expect memory growth on long-running sketches).
143
+
144
+ ## Develop
145
+
146
+ ```bash
147
+ npm install
148
+ npm run build # writes dist/p5.export.js + dist/p5.export.min.js
149
+ npm run dev # vite dev server at /examples/index.html
150
+ npm run test:sketches # headless harness over examples/ — exports each format
151
+ npm run test:webp # sanity test for the animated WebP encoder
152
+ ```
153
+
154
+ ## License
155
+
156
+ [MIT](./LICENSE)