Skip to content

Commit 5b15f63

Browse files
committed
Add per-file TypeScript definitions and update docs
Introduced individual .d.ts files alongside each source file in src/ and extras/jsm/zui.d.ts, replacing the previous monolithic types.d.ts (now removed). Updated documentation in CLAUDE.md to reflect the new TypeScript definition structure and improved TypeScript compilation instructions. Updated build outputs and package.json accordingly.
1 parent c82c509 commit 5b15f63

56 files changed

Lines changed: 6280 additions & 6232 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CLAUDE.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,16 @@ The Two class provides factory methods for creating and adding objects to the sc
8787

8888
### Running Tests
8989
- Manual browser testing via HTML files: `tests/index.html` and `tests/noWebGL.html`
90-
- TypeScript compilation testing: `cd tests/typescript && npx tsc index.ts` to verify types work correctly
90+
- TypeScript compilation testing: `npx tsc --noEmit --skipLibCheck tests/typescript/index.ts` to verify types work correctly
9191

9292
## Key Files to Understand
9393

9494
- `src/two.js` - Main class with factory methods and core logic
9595
- `src/constants.js` - Global constants, types, and configuration
9696
- `src/utils/interpret-svg.js` - SVG parsing and import functionality
9797
- `utils/build.js` - Build system configuration
98-
- `types.d.ts` - TypeScript definitions for the entire library
98+
- `src/**/*.d.ts` - TypeScript definitions collocated with source files (e.g., `src/vector.d.ts` alongside `src/vector.js`)
99+
- `src/two.d.ts` - Main TypeScript entry point that aggregates all type exports
99100

100101
## Dependencies
101102

@@ -151,7 +152,7 @@ Designed for modern browsers with ES6+ support. Uses feature detection for rende
151152
- Open `tests/index.html` in browser for manual testing
152153
- Test new features across Canvas, SVG, and WebGL renderers
153154
- Check `tests/noWebGL.html` for fallback scenarios
154-
- TypeScript compilation tests: Run `cd tests/typescript && npx tsc index.ts` to verify TypeScript definitions work correctly
155+
- TypeScript compilation tests: Run `npx tsc --noEmit --skipLibCheck tests/typescript/index.ts` to verify TypeScript definitions work correctly
155156
- Manual browser testing required - no automated test runner
156157

157158
## File Organization Rules
@@ -160,7 +161,10 @@ Designed for modern browsers with ES6+ support. Uses feature detection for rende
160161
- Effects belong in `src/effects/`
161162
- Utilities in `src/utils/` should be pure functions
162163
- Export new classes in `src/two.js` main file
163-
- Add TypeScript definitions to `types.d.ts`
164+
- **TypeScript definitions**: Create a `.d.ts` file alongside each source file (e.g., `src/vector.d.ts` next to `src/vector.js`)
165+
- Each `.d.ts` file contains a `declare module 'two.js/src/...'` block matching the module path
166+
- Import statements go at the END of the module declaration
167+
- Main type exports are aggregated in `src/two.d.ts`
164168
- Renderers are in `src/renderers/` - modify with caution
165169

166170
## Performance Guidelines

build/two.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ var Two = (() => {
12201220
* @name Two.PublishDate
12211221
* @property {String} - The automatically generated publish date in the build process to verify version release candidates.
12221222
*/
1223-
PublishDate: "2025-12-22T19:56:52.386Z",
1223+
PublishDate: "2026-01-05T18:28:31.207Z",
12241224
/**
12251225
* @name Two.Identifier
12261226
* @property {String} - String prefix for all Two.js object's ids. This trickles down to SVG ids.

build/two.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/two.module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ var Constants = {
12281228
* @name Two.PublishDate
12291229
* @property {String} - The automatically generated publish date in the build process to verify version release candidates.
12301230
*/
1231-
PublishDate: "2025-12-22T19:56:52.386Z",
1231+
PublishDate: "2026-01-05T18:28:31.207Z",
12321232
/**
12331233
* @name Two.Identifier
12341234
* @property {String} - String prefix for all Two.js object's ids. This trickles down to SVG ids.

extras/jsm/zui.d.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
declare module 'two.js/extras/jsm/zui' {
2+
/**
3+
* @name Two.ZUI
4+
* @class
5+
* @param {Group} group - The scene or group to
6+
* @param {HTMLElement} [domElement=document.body] - The HTML Element to attach event listeners to.
7+
*/
8+
export class ZUI {
9+
static Surface: Surface;
10+
static Clamp(v: any, min: any, max: any): number;
11+
static Limit: {
12+
min: number;
13+
max: number;
14+
clone: () => {};
15+
};
16+
static TranslateMatrix(m: any, x: any, y: any): any;
17+
static PositionToScale(pos: any): number;
18+
static ScaleToPosition(scale: any): number;
19+
constructor(group?: Group, domElement?: HTMLElement);
20+
limits: {
21+
scale: {};
22+
x: {};
23+
y: {};
24+
};
25+
viewport: any;
26+
viewportOffset: {
27+
top: number;
28+
left: number;
29+
matrix: Matrix;
30+
};
31+
surfaceMatrix: Matrix;
32+
surfaces: any[];
33+
add(surface: any): ZUI;
34+
addLimits(min: number, max: number, type?: number): ZUI;
35+
clientToSurface(v?: { x?: number; y?: number; z?: number }): {
36+
x: number;
37+
y: number;
38+
z: number;
39+
};
40+
surfaceToClient(v?: { x?: number; y?: number; z?: number }): {
41+
x: number;
42+
y: number;
43+
z: number;
44+
};
45+
zoomBy(byF: any, clientX: any, clientY: any): ZUI;
46+
zoomSet(zoom: any, clientX: any, clientY: any): ZUI;
47+
zoom: number;
48+
scale: any;
49+
translateSurface(x: any, y: any): ZUI;
50+
updateOffset(): ZUI;
51+
updateSurface(): ZUI;
52+
reset(): ZUI;
53+
fitToLimits(s: any): number;
54+
}
55+
import { Matrix } from 'two.js/src/matrix';
56+
import { Group } from 'two.js/src/group';
57+
class Surface {
58+
constructor(object: any);
59+
object: any;
60+
limits(
61+
min: any,
62+
max: any
63+
):
64+
| Surface
65+
| {
66+
min: any;
67+
max: any;
68+
};
69+
min: any;
70+
max: any;
71+
apply(px: any, py: any, s: any): Surface;
72+
}
73+
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"version": "v0.8.23",
44
"description": "A renderer agnostic two-dimensional drawing api for the web.",
55
"module": "build/two.module.js",
6-
"types": "types.d.ts",
6+
"types": "src/two.d.ts",
77
"files": [
88
"package.json",
99
"LICENSE",
1010
"README.md",
1111
"build",
1212
"extras",
13-
"src",
14-
"types.d.ts"
13+
"src"
1514
],
1615
"scripts": {
1716
"build": "node ./utils/build",

src/anchor.d.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
declare module 'two.js/src/anchor' {
2+
/**
3+
* @class
4+
* @name Two.Anchor
5+
* @param {Number} [x=0] - The x position of the root anchor point.
6+
* @param {Number} [y=0] - The y position of the root anchor point.
7+
* @param {Number} [ax=0] - The x position of the left handle point.
8+
* @param {Number} [ay=0] - The y position of the left handle point.
9+
* @param {Number} [bx=0] - The x position of the right handle point.
10+
* @param {Number} [by=0] - The y position of the right handle point.
11+
* @param {String} [command=Two.Commands.move] - The command to describe how to render. Applicable commands are {@link Two.Commands}
12+
13+
* @description An object that holds 3 {@link Two.Vector}s, the anchor point and its corresponding handles: `left` and `right`. In order to properly describe the bezier curve about the point there is also a command property to describe what type of drawing should occur when Two.js renders the anchors.
14+
*/
15+
export class Anchor extends Vector {
16+
static makeBroadcast(scope: Anchor): () => void;
17+
/**
18+
* @name Two.Anchor.fromObject
19+
* @function
20+
* @param {Object} obj - Object notation of a {@link Two.Anchor} to create a new instance
21+
* @returns {Two.Anchor}
22+
* @description Create a new {@link Two.Anchor} from an object notation of a {@link Two.Anchor}.
23+
* @nota-bene Works in conjunction with {@link Two.Anchor#toObject}
24+
*/
25+
static fromObject(
26+
obj:
27+
| object
28+
| {
29+
x?: number;
30+
y?: number;
31+
command?: Commands[keyof Commands];
32+
relative?: boolean;
33+
controls?: {
34+
left: { x: number; y: number } | Vector;
35+
right: { x: number; y: number } | Vector;
36+
};
37+
rx?: number;
38+
ry?: number;
39+
xAxisRotation?: number;
40+
largeArcFlag?: number;
41+
}
42+
): Anchor;
43+
constructor(
44+
x?: number,
45+
y?: number,
46+
ax?: number,
47+
ay?: number,
48+
bx?: number,
49+
by?: number,
50+
command?: Commands[keyof Commands]
51+
);
52+
controls: {
53+
left: Vector;
54+
right: Vector;
55+
};
56+
command: Commands[keyof Commands];
57+
relative: boolean;
58+
rx?: number;
59+
ry?: number;
60+
xAxisRotation?: number;
61+
largeArcFlag?: number;
62+
sweepFlag?: number;
63+
/**
64+
* @name Two.Anchor#copy
65+
* @function
66+
* @param {Two.Anchor} v - The anchor to apply values to.
67+
* @description Copy the properties of one {@link Two.Anchor} onto another.
68+
*/
69+
copy(anchor: Anchor): Anchor;
70+
/**
71+
* @name Two.Anchor#clone
72+
* @function
73+
* @returns {Two.Anchor}
74+
* @description Create a new {@link Two.Anchor}, set all its values to the current instance and return it for use.
75+
*/
76+
clone(): Anchor;
77+
/**
78+
* @name Two.Anchor#toObject
79+
* @function
80+
* @returns {Object} - An object with properties filled out to mirror {@link Two.Anchor}.
81+
* @description Create a JSON compatible plain object of the current instance. Intended for use with storing values in a database.
82+
* @nota-bene Works in conjunction with {@link Two.Anchor.fromObject}
83+
*/
84+
toObject(): object;
85+
/**
86+
* @name Two.Anchor#toString
87+
* @function
88+
* @returns {String} - A String with comma-separated values reflecting the various values on the current instance.
89+
* @description Create a string form of the current instance. Intended for use with storing values in a database. This is lighter to store than the JSON compatible {@link Two.Anchor#toObject}.
90+
*/
91+
toString(): string;
92+
}
93+
import { Vector } from 'two.js/src/vector';
94+
import { Commands } from 'two.js/src/utils/path-commands';
95+
}

src/children.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
declare module 'two.js/src/children' {
2+
/**
3+
* @class
4+
* @name Two.Group.Children
5+
6+
* @description A children collection which is accesible both by index and by object `id`.
7+
*/
8+
export class Children extends Collection<Shape> {
9+
constructor(children?: Shape[]);
10+
constructor(...args: Shape[]);
11+
/**
12+
* @name Two.Group.Children#ids
13+
* @property {Object} - Map of all elements in the list keyed by `id`s.
14+
*/
15+
ids: { [id: string]: Shape };
16+
/**
17+
* @function
18+
* @name Two.Group.Children#attach
19+
* @param {Shape[]} children - The objects which extend {@link Two.Shape} to be added.
20+
* @description Adds elements to the `ids` map.
21+
*/
22+
attach(children: Shape[]): Children;
23+
/**
24+
* @function
25+
* @name Two.Group.Children#detach
26+
* @param {Shape[]} children - The objects which extend {@link Two.Shape} to be removed.
27+
* @description Removes elements to the `ids` map.
28+
*/
29+
detach(children: Shape[]): Children;
30+
}
31+
import { Collection } from 'two.js/src/collection';
32+
import { Shape } from 'two.js/src/shape';
33+
}

src/collection.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
declare module 'two.js/src/collection' {
2+
/**
3+
* @name Two.Collection
4+
* @class
5+
6+
* @description An `Array` like object with additional event propagation on actions. `pop`, `shift`, and `splice` trigger `removed` events. `push`, `unshift`, and `splice` with more than 2 arguments trigger 'inserted'. Finally, `sort` and `reverse` trigger `order` events.
7+
*/
8+
export class Collection<T = any> extends Array<T> {
9+
constructor(...args: any[]);
10+
/**
11+
* @private
12+
*/
13+
private _events;
14+
private set _bound(arg: boolean);
15+
private get _bound(): boolean;
16+
addEventListener(...args: any[]): any;
17+
on(...args: any[]): any;
18+
bind(...args: any[]): any;
19+
removeEventListener(...args: any[]): any;
20+
off(...args: any[]): any;
21+
unbind(...args: any[]): any;
22+
dispatchEvent(...args: any[]): any;
23+
trigger(...args: any[]): any;
24+
listen(...args: any[]): any;
25+
ignore(...args: any[]): any;
26+
}
27+
}

src/constants.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare module 'two.js/src/constants' {
2+
export interface Constants {
3+
NextFrameId: number;
4+
Types: {
5+
webgl: 'WebGLRenderer';
6+
svg: 'SVGRenderer';
7+
canvas: 'CanvasRenderer';
8+
};
9+
Version: string;
10+
PublishDate: string;
11+
Identifier: string;
12+
Resolution: number;
13+
AutoCalculateImportedMatrices: boolean;
14+
Instances: Two[];
15+
uniqueId(): number;
16+
}
17+
import Two from 'two.js';
18+
}

0 commit comments

Comments
 (0)