Skip to content

Commit 9bb1956

Browse files
committed
🎨 Add Canvas 2D support to framework
Expand framework to support Canvas 2D rendering alongside WebGL This change introduces: - New CanvasEffect class as an alternative to WebGLEffect - Complete documentation for Canvas 2D implementation - Example Glow Particles effect showcasing Canvas API - Updated README to highlight Canvas 2D support The modular architecture now lets developers choose between WebGL for shader-based effects or Canvas 2D for simpler drawing operations, making the framework more versatile.
1 parent 6ecc884 commit 9bb1956

File tree

4 files changed

+905
-103
lines changed

4 files changed

+905
-103
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LightScript Workshop is a modern TypeScript framework for creating beautiful RGB
1919

2020
- **🔷 Modern TypeScript** - Full type safety prevents runtime errors
2121
- **🔮 Three.js Integration** - Powerful WebGL rendering capabilities
22+
- **🎨 Canvas 2D Support** - Traditional drawing API for simpler effects
2223
- **⚡ Hot Reloading** - Instant visual feedback while coding
2324
- **🕹️ Declarative Controls** - Define UI elements with simple HTML
2425
- **⚙️ Optimized Build Pipeline** - Production-ready effects
@@ -73,6 +74,17 @@ A wave-based RGB effect showcasing smooth animation and minimal resource usage.
7374
- Multiple color modes
7475
- Optimized for performance
7576

77+
### ✨ Glow Particles
78+
79+
A vibrant particle system with glowing effects using Canvas 2D rendering.
80+
81+
**Key features:**
82+
83+
- Particle physics simulation
84+
- Glowing effects with shadows
85+
- Trail rendering with transparency
86+
- Multiple color schemes
87+
7688
## 💻 Development Workflow
7789

7890
1. **Create** a new effect directory in `src/effects/your-effect-name/`

docs/api-reference.md

+159-25
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ This document provides detailed information about all the APIs and utilities ava
2020

2121
## 🧠 BaseEffect Class
2222

23-
The `BaseEffect` class is the foundation of all lighting effects in the framework. It provides a structured approach to initializing WebGL, managing controls, and handling animation.
23+
The `BaseEffect` class is the foundation of all lighting effects in the framework. It provides a structured approach to initializing rendering contexts, managing controls, and handling animation. This is an abstract base class that should be extended by renderer-specific classes like `WebGLEffect` or `CanvasEffect`.
2424

2525
### Core Properties
2626

27-
| Property | Type | Description |
28-
| ---------------- | -------------------------------- | -------------------------------- |
29-
| `id` | `string` | Unique identifier for the effect |
30-
| `name` | `string` | Display name |
31-
| `debug` | `Function` | Debug logger instance |
32-
| `webGLContext` | `WebGLContext` | Three.js renderer context |
33-
| `material` | `THREE.ShaderMaterial` | Shader material instance |
34-
| `customUniforms` | `Record<string, THREE.IUniform>` | Custom shader uniforms |
35-
| `fragmentShader` | `string` | Fragment shader code |
36-
| `vertexShader` | `string` | Optional vertex shader code |
27+
| Property | Type | Description |
28+
| -------------- | ------------------- | -------------------------------- |
29+
| `id` | `string` | Unique identifier for the effect |
30+
| `name` | `string` | Display name |
31+
| `debug` | `Function` | Debug logger instance |
32+
| `animationId` | `number \| null` | Current animation frame ID |
33+
| `canvas` | `HTMLCanvasElement` | Canvas element for rendering |
34+
| `canvasWidth` | `number` | Canvas width |
35+
| `canvasHeight` | `number` | Canvas height |
3736

3837
### Constructor
3938

@@ -51,8 +50,6 @@ Creates a new effect instance with the specified configuration.
5150
- `debug`: (Optional) Enable debug logging
5251
- `canvasWidth`: (Optional) Canvas width, defaults to 320
5352
- `canvasHeight`: (Optional) Canvas height, defaults to 200
54-
- `fragmentShader`: GLSL fragment shader code
55-
- `vertexShader`: (Optional) GLSL vertex shader code
5653

5754
### Public Methods
5855

@@ -62,7 +59,7 @@ Creates a new effect instance with the specified configuration.
6259
public async initialize(): Promise<void>
6360
```
6461

65-
Initializes the effect, setting up WebGL, shader materials, and starting the animation loop.
62+
Initializes the effect, setting up rendering context, and starting the animation loop.
6663

6764
#### `update()`
6865

@@ -88,6 +85,26 @@ Stops the animation loop and cleans up resources.
8885

8986
When extending `BaseEffect`, you must implement these methods:
9087

88+
#### `initializeRenderer()`
89+
90+
```typescript
91+
protected abstract initializeRenderer(): Promise<void>
92+
```
93+
94+
Initialize the renderer-specific context and resources.
95+
96+
#### `render()`
97+
98+
```typescript
99+
protected abstract render(time: number): void
100+
```
101+
102+
Render a frame using the specific rendering technique.
103+
104+
**Parameters:**
105+
106+
- `time`: Current time in seconds
107+
91108
#### `initializeControls()`
92109

93110
```typescript
@@ -104,21 +121,13 @@ protected abstract getControlValues(): T
104121

105122
Retrieve current control values from global scope and process them as needed.
106123

107-
#### `createUniforms()`
108-
109-
```typescript
110-
protected abstract createUniforms(): Record<string, THREE.IUniform>
111-
```
112-
113-
Create custom shader uniforms for your effect.
114-
115-
#### `updateUniforms()`
124+
#### `updateParameters()`
116125

117126
```typescript
118-
protected abstract updateUniforms(controls: T): void
127+
protected abstract updateParameters(controls: T): void
119128
```
120129

121-
Update shader uniforms with current control values.
130+
Update effect parameters with current control values.
122131

123132
**Parameters:**
124133

@@ -158,6 +167,131 @@ Handle initialization errors. Override to customize error handling.
158167

159168
- `error`: The error that occurred
160169

170+
## 🖥️ WebGLEffect Class
171+
172+
The `WebGLEffect` class extends `BaseEffect` to provide WebGL-specific rendering capabilities using Three.js.
173+
174+
### Core Properties
175+
176+
| Property | Type | Description |
177+
| ---------------- | -------------------------------- | --------------------------- |
178+
| `webGLContext` | `WebGLContext` | Three.js renderer context |
179+
| `material` | `THREE.ShaderMaterial` | Shader material instance |
180+
| `customUniforms` | `Record<string, THREE.IUniform>` | Custom shader uniforms |
181+
| `fragmentShader` | `string` | Fragment shader code |
182+
| `vertexShader` | `string` | Optional vertex shader code |
183+
184+
### Constructor
185+
186+
```typescript
187+
constructor(config: WebGLEffectConfig)
188+
```
189+
190+
Creates a new WebGL effect instance with the specified configuration.
191+
192+
**Parameters:**
193+
194+
- `config`: Configuration object extending EffectConfig with:
195+
- `fragmentShader`: GLSL fragment shader code
196+
- `vertexShader`: (Optional) GLSL vertex shader code
197+
198+
### Methods to Implement
199+
200+
When extending `WebGLEffect`, you must implement these methods:
201+
202+
#### `createUniforms()`
203+
204+
```typescript
205+
protected abstract createUniforms(): Record<string, THREE.IUniform>
206+
```
207+
208+
Create custom shader uniforms for your effect.
209+
210+
#### `updateUniforms()`
211+
212+
```typescript
213+
protected abstract updateUniforms(controls: T): void
214+
```
215+
216+
Update shader uniforms with current control values.
217+
218+
**Parameters:**
219+
220+
- `controls`: Current control values object of type T
221+
222+
## 🎨 CanvasEffect Class
223+
224+
The `CanvasEffect` class extends `BaseEffect` to provide 2D Canvas-specific rendering capabilities, perfect for effects that don't require WebGL.
225+
226+
### Core Properties
227+
228+
| Property | Type | Description |
229+
| ----------------- | -------------------------- | -------------------------------- |
230+
| `ctx` | `CanvasRenderingContext2D` | 2D canvas rendering context |
231+
| `backgroundColor` | `string` | Default background color |
232+
| `lastFrameTime` | `number` | Time of last frame for delta |
233+
| `deltaTime` | `number` | Time since last frame in seconds |
234+
235+
### Constructor
236+
237+
```typescript
238+
constructor(config: CanvasEffectConfig)
239+
```
240+
241+
Creates a new Canvas 2D effect instance with the specified configuration.
242+
243+
**Parameters:**
244+
245+
- `config`: Configuration object extending EffectConfig with:
246+
- `backgroundColor`: (Optional) Default background color, defaults to "black"
247+
248+
### Methods to Implement
249+
250+
When extending `CanvasEffect`, you must implement these methods:
251+
252+
#### `draw()`
253+
254+
```typescript
255+
protected abstract draw(time: number, deltaTime: number): void
256+
```
257+
258+
Draw the effect on the canvas for the current frame.
259+
260+
**Parameters:**
261+
262+
- `time`: Current time in seconds
263+
- `deltaTime`: Time since last frame in seconds
264+
265+
#### `applyControls()`
266+
267+
```typescript
268+
protected abstract applyControls(controls: T): void
269+
```
270+
271+
Apply control values to the effect parameters.
272+
273+
**Parameters:**
274+
275+
- `controls`: Current control values object of type T
276+
277+
### Helper Methods
278+
279+
#### `clearCanvas()`
280+
281+
```typescript
282+
protected clearCanvas(): void
283+
```
284+
285+
Clears the canvas with the background color.
286+
287+
#### `loadResources()`
288+
289+
```typescript
290+
protected async loadResources(): Promise<void>
291+
```
292+
293+
Load effect resources (images, fonts, etc.). Override this method in subclasses if needed.
294+
161295
## 🎮 Controls System
162296

163297
The controls system allows effects to define user-configurable parameters.

0 commit comments

Comments
 (0)