@rosalana/sandbox 0.0.5 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AnyUniformValue, HookCallback, SandboxOptions, UniformSchema, WebGLVersion } from "./types";
1
+ import type { AnyUniformValue, HookCallback, ModuleDefinition, SandboxOptions, TextureOptions, TextureSchema, TextureSource, UniformSchema, WebGLVersion } from "./types";
2
2
  export * from "./types";
3
3
  export * from "./errors";
4
4
  /**
@@ -50,6 +50,34 @@ export declare class Sandbox {
50
50
  * });
51
51
  */
52
52
  static create(canvas: HTMLCanvasElement, options?: SandboxOptions): Sandbox;
53
+ /**
54
+ * Define a shader module that can be imported in shader source with `#import <function> from "module_name"`.
55
+ * @example
56
+ * Sandbox.defineModule("my_module", source, { options });
57
+ * // Then in shader:
58
+ * // #import myFunc from "my_module"
59
+ * // void main() {
60
+ * // myFunc();
61
+ * // }
62
+ */
63
+ static defineModule(name: ModuleDefinition["name"], source: ModuleDefinition["source"], options?: ModuleDefinition["options"]): void;
64
+ /**
65
+ * Get the list of available shader modules that can be used with `#import` in shader source.
66
+ */
67
+ static availableModules(): {
68
+ name: string;
69
+ methods: string[];
70
+ uniforms: {
71
+ name: string;
72
+ type: import("./types").GLSLType;
73
+ }[];
74
+ options: Record<string, Record<string, import("./types").ModuleMethodOption>> | undefined;
75
+ }[];
76
+ /**
77
+ * Compile a shader source with Sandbox's shader preprocessor and return the final GLSL code.
78
+ * This is useful for debugging shader code or precompiling shaders for production use.
79
+ */
80
+ static compile(shaderSource: string): string;
53
81
  private resolveOptions;
54
82
  private setupListeners;
55
83
  private destroyListeners;
@@ -83,6 +111,28 @@ export declare class Sandbox {
83
111
  * Get current uniform value.
84
112
  */
85
113
  getUniform<T extends AnyUniformValue>(name: string): T | undefined;
114
+ /**
115
+ * Set a texture for a sampler2D uniform.
116
+ * @example
117
+ * sandbox.setTexture("u_texture", imageElement);
118
+ * sandbox.setTexture("u_texture", imageElement, { wrap: "repeat" });
119
+ */
120
+ setTexture(name: string, source: TextureSource, options?: TextureOptions): this;
121
+ /**
122
+ * Set multiple textures at once.
123
+ * @example
124
+ * sandbox.setTextures({
125
+ * u_texture: imageElement,
126
+ * u_detail: { source: detailImg, wrap: "repeat" },
127
+ * });
128
+ */
129
+ setTextures(textures: TextureSchema): this;
130
+ /**
131
+ * Remove a texture and free its GPU resources.
132
+ * @example
133
+ * sandbox.removeTexture("u_texture");
134
+ */
135
+ removeTexture(name: string): this;
86
136
  /**
87
137
  * Update shaders.
88
138
  * @example
@@ -95,6 +145,14 @@ export declare class Sandbox {
95
145
  * sandbox.setFragment(fragmentSource);
96
146
  */
97
147
  setFragment(fragment: string): this;
148
+ /**
149
+ * Get current fragment shader source.
150
+ */
151
+ getFragment(): string;
152
+ /**
153
+ * Get current vertex shader source.
154
+ */
155
+ getVertex(): string;
98
156
  /**
99
157
  * Set the max frame rate runtime
100
158
  *
@@ -107,6 +165,12 @@ export declare class Sandbox {
107
165
  * Add a runtime render hook.
108
166
  */
109
167
  hook(callback: HookCallback, when?: "before" | "after"): () => void;
168
+ /**
169
+ * Runtime configure the module behavior
170
+ * @example
171
+ * sandbox.module("my_module", { intensity: 0.5 });
172
+ */
173
+ module<T extends Record<string, AnyUniformValue>>(name: string, config: T): this;
110
174
  /**
111
175
  * Start animation loop.
112
176
  */
@@ -155,6 +219,40 @@ export declare class Sandbox {
155
219
  * Get canvas element.
156
220
  */
157
221
  get canvas(): HTMLCanvasElement;
222
+ /**
223
+ * Export current frame as a data URL string.
224
+ * Requires `preserveDrawingBuffer: true` if called while playing.
225
+ * @example
226
+ * const url = sandbox.renderAt(1.5).exportAsURL("image/png");
227
+ */
228
+ exportAsURL(type?: "image/png" | "image/jpeg", quality?: number): string;
229
+ /**
230
+ * Export current frame as a Blob.
231
+ * Requires `preserveDrawingBuffer: true` if called while playing.
232
+ * @example
233
+ * const blob = await sandbox.renderAt(1.5).exportAsBlob("image/png");
234
+ */
235
+ exportAsBlob(type?: "image/png" | "image/jpeg", quality?: number): Promise<Blob>;
236
+ /**
237
+ * Export current frame as an HTMLImageElement.
238
+ * Requires `preserveDrawingBuffer: true` if called while playing.
239
+ * @example
240
+ * const img = sandbox.renderAt(1.5).exportAsImage("image/png");
241
+ * img.onload = () => document.body.appendChild(img);
242
+ */
243
+ exportAsImage(type?: "image/png" | "image/jpeg", quality?: number): HTMLImageElement;
244
+ /**
245
+ * Capture the canvas as a MediaStream for video calls or recording.
246
+ * @example
247
+ * // WebRTC video call
248
+ * const stream = sandbox.stream(30);
249
+ * peerConnection.addTrack(stream.getVideoTracks()[0], stream);
250
+ *
251
+ * @example
252
+ * // Record to video file
253
+ * const recorder = new MediaRecorder(sandbox.stream(30));
254
+ */
255
+ stream(fps?: number): MediaStream;
158
256
  /**
159
257
  * Destroy sandbox and release all resources.
160
258
  * @example