rapid-render 1.0.15 → 1.0.17

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/README.md CHANGED
@@ -3,11 +3,18 @@
3
3
  </p>
4
4
 
5
5
  <p align="center">
6
- A Immediate-mode, high-performance WebGL 2D renderer for browser games.
6
+ <a href="https://www.npmjs.com/package/rapid-render"><img src="https://img.shields.io/npm/v/rapid-render?logo=npm&label=npm" alt="npm version"></a>
7
+ <a href="https://www.npmjs.com/package/rapid-render"><img src="https://img.shields.io/badge/gzipped-22.5%20kB-5C7CFA" alt="gzipped size"></a>
8
+ <a href="https://github.com/Nightre/Rapid.js/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/rapid-render" alt="license"></a>
9
+ <img src="https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white" alt="TypeScript strict">
7
10
  </p>
8
11
 
9
12
  <p align="center">
10
- <a href="https://nightre.github.io/Rapid.js/">Website</a>
13
+ An immediate-mode, high-performance WebGL 2D renderer for browser games.
14
+ </p>
15
+
16
+ <p align="center">
17
+ <a href="https://nightre.github.io/Rapid.js/">Website & Examples</a>
11
18
  |
12
19
  <a href="https://nightre.github.io/Rapid.js/docs.html">Docs</a>
13
20
  |
@@ -20,23 +27,25 @@
20
27
 
21
28
  ## What is Rapid?
22
29
 
23
- Rapid is a focused WebGL 2D rendering engine for games and visual tools. lightweight just **69 kB** (**~20 kB gzipped**) and handles only the rendering layer, leaving your game architecture entirely in your hands.
30
+ Rapid is a focused WebGL 2D rendering engine for games and visual tools.
31
+
32
+ If you want Pixi-level rendering without letting your renderer dictate how your game is organized, Rapid.js is for you!
33
+
34
+ ## Full 2D Toolkit
24
35
 
25
- If you don't want your renderer to dictate how your game is organized, Rapid.js is for you!
36
+ Particles, Light & Shadow, Custom Shaders, Lines, Masks, Render textures, Sprites, Custom Geometry, Filters and more. all from one focused WebGL renderer. Yet it is only **22.5 kB gzipped**.
26
37
 
27
- ## Highlights
38
+ ## Architecture Recipes
28
39
 
29
- - **Rendering speed**
30
- An efficient batching system significantly reduces draw calls, allowing Rapid.js to maintain smooth, stable performance even when rendering large numbers of sprites at once.
40
+ Rapid.js handles only the rendering layer, leaving your game architecture entirely in your hands.
31
41
 
32
- - **Powerful custom shaders**
33
- Add sprite and geometry effects through shader hooks while still using Rapid's normal renderer, transforms, textures, and draw APIs.
42
+ Rapid.js is frame-stateless, making it easy to build games with different architectures. Below are minimal, working implementations of several popular architectures built on Rapid.js, each in **around 100 lines** of JavaScript.
34
43
 
35
- - **Flexible transforms**
36
- Fast, flexible, matrix-powered transforms for motion and hierarchies. **Retain the matrix tree after traversal**; local changes update only affected subtrees. No rebuild required.
44
+ - **Architecture recipes**
45
+ [Game Object](recipes/game-object.js) · [Component](recipes/component.js) · [Display List](recipes/display-list.js) · [ECS Integration](recipes/ecs-integration.js) · [Immediate Mode](recipes/immediate-mode.js)
37
46
 
38
- - **A complete 2D toolkit**
39
- Draw sprites, lines, masks, particles, render textures, text, and custom geometry from one compact WebGL renderer.
47
+ - **Game-engine-style recipes**
48
+ [Kaplay-like](recipes/game-engine/kaplay-like.js) · [Pixi-like](recipes/game-engine/pixi-like.js) · [Phaser-like](recipes/game-engine/phaser-like.js) · [Excalibur-like](recipes/game-engine/excalibur-like.js) · [LittleJS-like](recipes/game-engine/littlejs-like.js) · [p5-like](recipes/game-engine/p5-like.js)
40
49
 
41
50
  ## Install
42
51
 
@@ -44,10 +53,16 @@ If you don't want your renderer to dictate how your game is organized, Rapid.js
44
53
  npm install rapid-render
45
54
  ```
46
55
 
56
+ Or via the unpkg CDN
57
+
58
+ ```html
59
+ <script src="https://unpkg.com/rapid-render/dist/rapid-render.umd.cjs"></script>
60
+ ```
61
+
47
62
  ## Quick Start
48
63
 
49
64
  ```ts
50
- import { Rapid, Color } from "rapid-render";
65
+ import { Rapid } from "rapid-render";
51
66
 
52
67
  const canvas = document.querySelector("canvas")!;
53
68
  const rapid = new Rapid({canvas});
@@ -64,7 +79,7 @@ rapid.flush();
64
79
 
65
80
  ## Render a scene
66
81
 
67
- Expressing complex game hierarchies doesn't require `DisplayObject` trees. `rapid.matrixStack` brings the familiar, intuitive `save()` and `restore()` flow from Canvas 2D into high-performance WebGL, letting you compose parent-child relationships with zero object allocation.
82
+ `rapid.matrixStack` brings the familiar, intuitive `save()` and `restore()` flow from Canvas 2D into high-performance WebGL. It reuses typed-array-backed matrix storage and avoids per-transform matrix allocation.
68
83
 
69
84
  ```ts
70
85
  // root
@@ -86,8 +101,10 @@ stack.save();
86
101
  // 3.enemies
87
102
  stack.save();
88
103
  for (let i = 0; i < 2; i++) {
89
- stack.translate(x, y);
90
- rapid.drawSprite(enemies[i]); // enemy
104
+ stack.save();
105
+ stack.translate(enemyX[i], enemyY[i]);
106
+ rapid.drawSprite(enemies[i]); // enemy
107
+ stack.restore();
91
108
  }
92
109
  stack.restore(); // 3.enemies
93
110
  stack.restore(); // 2.world
@@ -98,7 +115,7 @@ rapid.drawSprite(ui);
98
115
 
99
116
  ## Reuse and Update Matrix Subtrees
100
117
 
101
- `rapid.matrixStack` does not sacrifice the flexibility of a retained scene graph. Use `customMatrix` to render with any matrix in the hierarchy(even after its stack scope has been popped)
118
+ Use `customMatrix` to render with any matrix in the hierarchy (even after its stack scope has been popped).
102
119
 
103
120
  When you modify a node's local matrix, call `updateMatrixSubtree()` to automatically recalculate that node and all affected descendant world matrices, without rebuilding the entire matrix hierarchy.
104
121
 
@@ -135,6 +152,8 @@ rapid.drawSprite({
135
152
  rapid.flush();
136
153
  ```
137
154
 
155
+ With this flexible matrix stack, you can build your own architecture with minimal friction. It doesn't care how you organize your game logic. You can use ECS, scene graphs, components, or any hybrid approach you prefer.
156
+
138
157
  For more information about matrix transformations, see the [Transformations](https://nightre.github.io/Rapid.js/docs.html#transformations).
139
158
 
140
159
  ## Benchmark
package/dist/buffer.d.ts CHANGED
@@ -104,11 +104,6 @@ export declare class DynamicArrayBuffer {
104
104
  * The number of elements currently stored in the buffer.
105
105
  */
106
106
  get length(): number;
107
- /**
108
- * Resets the buffer's used element count, effectively emptying it.
109
- * Same behavior as `clear`.
110
- */
111
- reset(): void;
112
107
  }
113
108
  /**
114
109
  * A specialized dynamic buffer intended for WebGL operations.
package/dist/color.d.ts CHANGED
@@ -66,7 +66,6 @@ export declare class Color {
66
66
  */
67
67
  setRGBA(r: number, g: number, b: number, a: number): void;
68
68
  setHSL(h: number, s: number, l: number): Color;
69
- toHex(): string;
70
69
  /**
71
70
  * Copies the RGBA values from another color.
72
71
  * @param color - The color to copy from.
@@ -88,7 +87,6 @@ export declare class Color {
88
87
  * @param color - The color to compare with.
89
88
  * @returns True if the colors are equal, otherwise false.
90
89
  */
91
- equal(color: Color): boolean;
92
90
  equals(color: Color): boolean;
93
91
  /**
94
92
  * Creates a Color instance from normalized float components (0–1 range).
@@ -104,7 +102,6 @@ export declare class Color {
104
102
  static FromHSL(h: number, s: number, l: number): Color;
105
103
  static FromRGB(r: number, g: number, b: number): Color;
106
104
  static fromNorm(r: number, g: number, b: number, a?: number): Color;
107
- static clampColorByte(value: number): number;
108
105
  static packColor(r: number, g: number, b: number, a: number, premultipliedAlpha: boolean): number;
109
106
  /**
110
107
  * Creates a Color instance from a hexadecimal color string.
@@ -142,5 +139,4 @@ export declare class Color {
142
139
  static White: Color;
143
140
  static Black: Color;
144
141
  static Transparent: Color;
145
- static TRANSPARENT: Color;
146
142
  }
@@ -0,0 +1,29 @@
1
+ import { Rapid } from '../render';
2
+ import { CustomGlShader, UniformValue } from '../webgl/glshader';
3
+ import { Vec2 } from '../math';
4
+ import { RenderTexture, Texture } from '../texture';
5
+ import { Color } from '../color';
6
+ import { MatrixSaveState } from '../matrix-engine';
7
+ export declare const MAX_LIGHTS = 8;
8
+ export declare class LightShader extends CustomGlShader {
9
+ constructor(rapid: Rapid, vs?: string, fs?: string, usedTextureUnitNum?: number, uniforms?: Record<string, UniformValue>);
10
+ }
11
+ export type LightMatrix = number | MatrixSaveState;
12
+ export interface ILight {
13
+ lightMatrix: LightMatrix | LightMatrix[];
14
+ normalMap: Texture;
15
+ lightTexture: Texture[] | Texture;
16
+ metallic?: number;
17
+ roughness?: number;
18
+ color?: Color | Color[];
19
+ lightHeight?: number | number[];
20
+ customShader?: LightShader;
21
+ }
22
+ export declare class Light {
23
+ shader: CustomGlShader;
24
+ rapid: Rapid;
25
+ rt: RenderTexture;
26
+ constructor(rapid: Rapid);
27
+ lightShadow(texture: Texture, lightMatrix: LightMatrix, points: Array<Vec2>): RenderTexture | undefined;
28
+ setupLight(option: ILight): CustomGlShader;
29
+ }
@@ -1,9 +1,9 @@
1
- import { Color } from './color';
2
- import { Vec2 } from './math';
3
- import { Rapid } from './render';
4
- import { Texture } from './texture';
5
- import { DynamicArrayBuffer } from './buffer';
6
- import { CustomGlShader } from './webgl/glshader';
1
+ import { Color } from '../color';
2
+ import { Vec2 } from '../math';
3
+ import { Rapid } from '../render';
4
+ import { Texture } from '../texture';
5
+ import { DynamicArrayBuffer } from '../buffer';
6
+ import { CustomGlShader } from '../webgl/glshader';
7
7
  /**
8
8
  * Describes an animated attribute: it transitions from `start` to `end`
9
9
  * over the particle's lifetime, optionally with a per-second damping factor.
package/dist/index.d.ts CHANGED
@@ -1,13 +1,8 @@
1
- export * from './buffer';
2
1
  export * from './color';
3
- export * from './matrix-engine';
4
2
  export * from './render';
5
- export * from './draw';
6
3
  export * from './texture';
7
4
  export * from './webgl/glshader';
8
- export * from './region/region';
9
- export * from './region/spriteRegion';
10
- export * from './region/graphicRegion';
11
5
  export * from './math';
12
- export * from './particle';
13
- export { LineTextureMode, type ILineRenderOptions } from './line';
6
+ export * from './extensions/particle';
7
+ export * from './extensions/light';
8
+ export * from './line';
package/dist/math.d.ts CHANGED
@@ -11,16 +11,13 @@ export declare class Vec2 {
11
11
  set(x: number, y?: number): Vec2;
12
12
  add(v: Vec2): Vec2;
13
13
  subtract(v: Vec2): Vec2;
14
- sub(v: Vec2): Vec2;
15
14
  multiply(f: number | Vec2): Vec2;
16
- mul(f: number | Vec2): Vec2;
17
15
  divide(f: number | Vec2): Vec2;
18
16
  dot(v: Vec2): number;
19
17
  cross(v: Vec2): number;
20
18
  distanceTo(v: Vec2): number;
21
19
  clone(): Vec2;
22
20
  to(vec: Vec2): void;
23
- copy(): Vec2;
24
21
  equals(vec: Vec2): boolean;
25
22
  perpendicular(): this;
26
23
  invert(): this;
@@ -204,6 +204,11 @@ export declare class MatrixStore {
204
204
  multiplyAffineInPlace(index: number, a: number, b: number, c: number, dValue: number, tx: number, ty: number): void;
205
205
  multiplyAffine(indexIn: number, indexOut: number, a: number, b: number, c: number, dValue: number, tx: number, ty: number): void;
206
206
  }
207
+ export interface MatrixSaveState {
208
+ world: number;
209
+ local: number;
210
+ step: number;
211
+ }
207
212
  /**
208
213
  * A highly performant MatrixStack useful for hierarchal scene graphs.
209
214
  * Handles both local and world transformations automatically.
@@ -221,8 +226,8 @@ export declare class MatrixStack {
221
226
  stepAction: DynamicArrayBuffer;
222
227
  /** Records the parent world matrix for each step (used by updateMatrixSubtree). */
223
228
  stepParentM: DynamicArrayBuffer;
224
- /** Buffer used during hierarchy traversal updates. */
225
- parentStack: DynamicArrayBuffer;
229
+ stepClose: DynamicArrayBuffer;
230
+ stepStack: DynamicArrayBuffer;
226
231
  /** The index of the current local matrix in the matrix store. */
227
232
  curLocalM: number;
228
233
  /** The index of the current world matrix in the matrix store. */
@@ -237,20 +242,23 @@ export declare class MatrixStack {
237
242
  * Equivalent to context.save().
238
243
  * @returns The current step counter before saving.
239
244
  */
240
- save(): {
241
- world: number;
242
- local: number;
243
- step: number;
244
- };
245
+ save(): MatrixSaveState;
245
246
  /**
246
247
  * Restores the matrix state from the top of the stack.
247
248
  * Equivalent to context.restore().
248
249
  */
249
250
  restore(): void;
250
- /**
251
- * Evaluates and updates matrices from the given step. Used primarily for deferred transformation.
252
- * @param step - The initial step to update matrices from.
253
- */
251
+ restoreAll(): void;
252
+ getStep(step: number | {
253
+ step: number;
254
+ }): number;
255
+ private walkSubtree;
256
+ getParent(step: number | {
257
+ step: number;
258
+ }): number;
259
+ getChildren(step: number | {
260
+ step: number;
261
+ }): number[];
254
262
  updateMatrixSubtree(step: number | {
255
263
  step: number;
256
264
  }): void;