canvas-js-3d 0.1.0 → 0.1.11

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.
Files changed (2) hide show
  1. package/README.md +261 -10
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,15 +1,266 @@
1
- Canvas JS 3D is a lightweight 3D graphics library that uses the Canvas API for wireframe rendering. The library has zero dependencies and is written in purely vanilla JavaScript.
1
+ # canvas-js-3d
2
2
 
3
+ A lightweight 3D graphics library that uses the Canvas API for wireframe rendering. Zero dependencies, pure vanilla JavaScript.
3
4
 
5
+ ## Features
4
6
 
5
- Features
7
+ - **Pure JavaScript** - No external dependencies
8
+ - **ES6 Modules** - Clean, modular architecture
9
+ - **Wavefront OBJ Loading** - Import 3D models from .obj files
10
+ - **Wireframe Rendering** - Canvas 2D-based edge rendering
11
+ - **Transform System** - Position, rotation, and scale
12
+ - **Frame Update Hook** - Per-frame logic with delta-time for framerate-independent animations
13
+ - **Scene Object System** - Manage multiple meshes with independent transforms
6
14
 
7
- * Pure JavaScript - No external dependencies
8
- * ES6 Modules - Clean, modular architecture
9
- * Wavefront OBJ Loading - Import 3D models from .obj files
10
- * Wireframe Rendering - Canvas 2D-based edge rendering
11
- * Transform System - Position, rotation, and scale
12
- * Frame Update Hook - Per-frame client logic with delta-time for framerate-independent animations
13
- * Scene Object System - Manage multiple meshes with independent transforms
14
- * Mobile Responsive Demo - Touch-friendly demo with responsive layout
15
+ ## Quick Start
15
16
 
17
+ ### Installation
18
+
19
+ ```bash
20
+ npm install canvas-js-3d
21
+ ```
22
+
23
+ ### Setup (No Bundler Required)
24
+
25
+ This library works with ES6 modules and requires an import map to resolve the package without a bundler.
26
+
27
+ Create your HTML file:
28
+
29
+ ```html
30
+ <!DOCTYPE html>
31
+ <html>
32
+ <head>
33
+ <title>canvas-js-3d Example</title>
34
+ <script type="importmap">
35
+ {
36
+ "imports": {
37
+ "canvas-js-3d": "./node_modules/canvas-js-3d/src/index.js"
38
+ }
39
+ }
40
+ </script>
41
+ </head>
42
+ <body>
43
+ <canvas id="canvas" width="800" height="800"></canvas>
44
+ <script type="module" src="./app.js"></script>
45
+ </body>
46
+ </html>
47
+ ```
48
+
49
+ **Important**: You must run a local server. The library won't work if you open the HTML file directly with `file://`. Use the included dev server:
50
+
51
+ ```bash
52
+ npm run dev
53
+ ```
54
+
55
+ Then open `http://localhost:8080` in your browser.
56
+
57
+ ### Tutorial: Create a Rotating Cube
58
+
59
+ Create `app.js`:
60
+
61
+ ```javascript
62
+ import { Engine, Mesh, SceneObject, Transform, Vector3 } from 'canvas-js-3d';
63
+
64
+ const canvas = document.getElementById('canvas');
65
+ const engine = new Engine(canvas, 'cyan', 'black');
66
+
67
+ // Define vertices for a cube
68
+ const vertices = [
69
+ new Vector3(-1, -1, -1), // 0: back-bottom-left
70
+ new Vector3( 1, -1, -1), // 1: back-bottom-right
71
+ new Vector3( 1, 1, -1), // 2: back-top-right
72
+ new Vector3(-1, 1, -1), // 3: back-top-left
73
+ new Vector3(-1, -1, 1), // 4: front-bottom-left
74
+ new Vector3( 1, -1, 1), // 5: front-bottom-right
75
+ new Vector3( 1, 1, 1), // 6: front-top-right
76
+ new Vector3(-1, 1, 1), // 7: front-top-left
77
+ ];
78
+
79
+ // Define faces as arrays of vertex indices
80
+ const faceIndices = [
81
+ [0, 1, 2, 3], // back
82
+ [4, 5, 6, 7], // front
83
+ [0, 4, 7, 3], // left
84
+ [1, 5, 6, 2], // right
85
+ [3, 2, 6, 7], // top
86
+ [0, 1, 5, 4], // bottom
87
+ ];
88
+
89
+ const mesh = new Mesh(vertices, faceIndices);
90
+ const cube = new SceneObject(mesh, new Transform(new Vector3(0, 0, 5), 0, 1));
91
+
92
+ engine.addSceneObject(cube);
93
+ engine.onUpdate = (dt) => { cube.transform.rotation += dt; };
94
+ engine.start();
95
+ ```
96
+
97
+ ### Tutorial: Load and Render an OBJ
98
+
99
+ Here's an example that loads a 3D model from an OBJ file:
100
+
101
+ ```javascript
102
+ // app.js
103
+ import { Engine, SceneObject, Transform, Vector3, WavefrontMeshConverter } from 'canvas-js-3d';
104
+
105
+ // Get canvas and create engine with foreground/background colors
106
+ const canvas = document.getElementById('canvas');
107
+ const engine = new Engine(canvas, 'green', 'black');
108
+
109
+ // Load an OBJ mesh from URL
110
+ const mesh = await WavefrontMeshConverter.fromUrl('./model.obj');
111
+
112
+ // Create a scene object with position, rotation, and scale
113
+ const obj = new SceneObject(
114
+ mesh,
115
+ new Transform(new Vector3(0, 0, 5), 0, 1) // position, rotation, scale
116
+ );
117
+
118
+ // Add to engine
119
+ engine.addSceneObject(obj);
120
+
121
+ // Animate: rotate the object each frame
122
+ engine.onUpdate = (deltaTime) => {
123
+ obj.transform.rotation += Math.PI * 0.5 * deltaTime;
124
+ };
125
+
126
+ // Start the render loop
127
+ engine.start();
128
+ ```
129
+
130
+ Make sure `model.obj` is in the same directory as your HTML file, or adjust the path accordingly.
131
+
132
+ ## Architecture
133
+
134
+ ### Rendering Pipeline
135
+
136
+ ```
137
+ Mesh (vertices + face indices)
138
+
139
+ SceneObject (applies Transform: scale → rotate → translate)
140
+
141
+ Camera.projectSceneObject() (perspective projection via x/z, y/z division)
142
+
143
+ Renderer (Canvas 2D wireframe edges)
144
+ ```
145
+
146
+ ### Module Structure
147
+
148
+ ```
149
+ src/
150
+ ├── index.js # Main entry point
151
+ ├── math/ # Vector and transform math
152
+ │ ├── vector2.js
153
+ │ ├── vector3.js
154
+ │ └── transform.js
155
+ ├── core/ # Scene data structures and engine
156
+ │ ├── mesh.js
157
+ │ ├── sceneObject.js
158
+ │ └── engine.js
159
+ ├── rendering/ # Display components
160
+ │ ├── camera.js
161
+ │ └── renderer.js
162
+ └── wavefront-loading/ # OBJ file loading
163
+ ├── index.js
164
+ ├── wavefront-mesh-converter.js
165
+ ├── wavefront-file-loader.js
166
+ ├── wavefront-lexer.js
167
+ └── wavefront-parser.js
168
+ ```
169
+
170
+ ## Usage
171
+
172
+ ### Engine
173
+
174
+ The main render loop and scene manager.
175
+
176
+ ```javascript
177
+ const engine = new Engine(canvas, 'white', 'black'); // canvas, fgColor, bgColor
178
+
179
+ engine.addSceneObject(obj); // Add object to scene
180
+ engine.removeSceneObject(obj); // Remove object from scene
181
+
182
+ engine.onUpdate = (deltaTime) => {
183
+ // Called every frame with time since last frame (in seconds)
184
+ };
185
+
186
+ engine.start(); // Start render loop
187
+ engine.stop(); // Stop render loop
188
+ ```
189
+
190
+ ### SceneObject
191
+
192
+ Combines a Mesh with a Transform for positioning in the scene.
193
+
194
+ ```javascript
195
+ const obj = new SceneObject(mesh, transform);
196
+
197
+ obj.mesh; // The Mesh geometry
198
+ obj.transform; // The Transform (position, rotation, scale)
199
+ ```
200
+
201
+ ### Transform
202
+
203
+ Defines position, rotation, and scale. Transform order: scale → rotate → translate.
204
+
205
+ ```javascript
206
+ const transform = new Transform(
207
+ new Vector3(0, 0, 5), // position
208
+ 0, // rotation (radians, XZ plane)
209
+ 1.0 // scale (uniform)
210
+ );
211
+
212
+ transform.position = new Vector3(1, 2, 3);
213
+ transform.rotation += Math.PI * 0.5;
214
+ transform.scale = 2.0;
215
+ ```
216
+
217
+ ### Vector3
218
+
219
+ Immutable-style 3D vector math. Methods return new instances.
220
+
221
+ ```javascript
222
+ const v = new Vector3(1, 2, 3);
223
+
224
+ v.getTranslated(new Vector3(1, 0, 0)); // Returns new Vector3(2, 2, 3)
225
+ v.getScaled(2); // Returns new Vector3(2, 4, 6)
226
+ v.getRotatedXZ(Math.PI / 2); // Rotate around Y axis
227
+ ```
228
+
229
+ ### WavefrontMeshConverter
230
+
231
+ Load OBJ files from various sources.
232
+
233
+ ```javascript
234
+ // From URL
235
+ const mesh = await WavefrontMeshConverter.fromUrl('./model.obj');
236
+
237
+ // From File object (e.g., from <input type="file">)
238
+ const mesh = await WavefrontMeshConverter.fromFile(file);
239
+
240
+ // From file dialog (opens browser file picker)
241
+ const mesh = await WavefrontMeshConverter.fromFileDialog();
242
+
243
+ // From raw OBJ text
244
+ const mesh = WavefrontMeshConverter.fromText(objString);
245
+ ```
246
+
247
+ ## Troubleshooting
248
+
249
+ ### "Failed to resolve module specifier" error
250
+
251
+ Make sure you have:
252
+ 1. The import map in your HTML `<head>` (see Setup section above)
253
+ 2. `type="module"` on your script tag
254
+ 3. A local server running (not opening the file with `file://`)
255
+
256
+ ### Canvas is blank
257
+
258
+ Check that:
259
+ 1. Your canvas has width and height attributes
260
+ 2. Objects are positioned at z > 0 (positive z is away from the camera)
261
+ 3. The scale and rotation values are reasonable
262
+ 4. `engine.start()` is called
263
+
264
+ ## License
265
+
266
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-js-3d",
3
- "version": "0.1.0",
3
+ "version": "0.1.11",
4
4
  "description": "Canvas JS 3D is a lightweight 3D graphics library that uses the Canvas API for wireframe rendering.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",