@jiant/canvable 0.0.1-fix

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 ADDED
@@ -0,0 +1,85 @@
1
+ # Canvable
2
+
3
+ Canvable é uma biblioteca para criação de cenas em canvas com um sistema de nós, permitindo a construção de elementos e interações de forma intuitiva, similar a uma engine de jogos 2D. A biblioteca oferece suporte para objetos gráficos, entrada de usuário, câmera, e muito mais, permitindo criar experiências interativas de maneira modular e flexível.
4
+
5
+ ## Instalação
6
+
7
+ ### Usando NPM
8
+
9
+ Se você está utilizando o NPM, pode instalar a biblioteca com:
10
+
11
+ ```bash
12
+ npm install @jiant/canvable
13
+ ```
14
+
15
+ ### Usando Yarn
16
+
17
+ Se você está utilizando o Yarn, pode instalar com:
18
+
19
+ ```bash
20
+ yarn add @jiant/canvable
21
+ ```
22
+
23
+ ## Exemplos de Uso
24
+
25
+ ### Criando uma Cena
26
+
27
+ A seguir está um exemplo básico de como criar uma cena e adicionar um objeto nela:
28
+
29
+ ```typescript
30
+ import { Circle, getCanvas, Scene } from "@jiant/canvable";
31
+ import "./style.css";
32
+
33
+ // Obtendo o canvas
34
+ const canvas = getCanvas("app")!;
35
+ const ctx = canvas.getContext("2d")!;
36
+
37
+ // Criando a cena
38
+ const scene = new Scene(ctx);
39
+
40
+ // Criando um círculo
41
+ const circle = new Circle();
42
+
43
+ // Adicionando o círculo à cena
44
+ scene.addObject(circle);
45
+ ```
46
+
47
+ ### Loop de Jogo
48
+
49
+ Você pode adicionar um loop de jogo para atualizar e mover os objetos da cena:
50
+
51
+ ```typescript
52
+ scene.gameLoop((deltaTime, total) => {
53
+ circle.pos.x = Math.sin(total) * 100 + 200;
54
+ });
55
+ ```
56
+
57
+ ---
58
+
59
+ ## Funcionalidades
60
+
61
+ ### Cenas e Objetos
62
+
63
+ - **Cena**: Contém e gerencia objetos, como círculos, retângulos, entre outros. Permite manipulação de câmera e desenhos.
64
+ - **Objetos**: Elementos gráficos que podem ser adicionados à cena, como `Circle`, `Rectangle`, etc.
65
+
66
+ ### Funções de Entrada
67
+
68
+ - **isPressed**: Verifica se uma tecla ou botão está pressionado continuamente.
69
+ - **justPressed**: Detecta o momento exato em que uma tecla ou botão é pressionado pela primeira vez.
70
+
71
+ ### Câmera
72
+
73
+ - Suporte para uma câmera 2D que pode ser movida e ampliada (zoom).
74
+
75
+ ---
76
+
77
+ ## Contribuições
78
+
79
+ Contribuições são bem-vindas! Se você deseja melhorar ou adicionar novos recursos à biblioteca, sinta-se à vontade para abrir um **Pull Request**.
80
+
81
+ ---
82
+
83
+ ## Licença
84
+
85
+ Este projeto está licenciado sob a [MIT License](LICENSE).
@@ -0,0 +1,239 @@
1
+ /**
2
+ * A 2D vector class for mathematical operations and physics calculations
3
+ */
4
+ declare class Vector2D {
5
+ x: number;
6
+ y: number;
7
+ /**
8
+ * Creates a new Vector2D instance
9
+ * @param x - The x component of the vector
10
+ * @param y - The y component of the vector
11
+ */
12
+ constructor(x?: number, y?: number);
13
+ /**
14
+ * Adds another vector to this vector
15
+ * @param vector - The vector to add
16
+ * @param self - If true, modifies the current vector in place, otherwise returns a new vector
17
+ * @returns A new Vector2D representing the sum
18
+ */
19
+ add(vector: Vector2D, self?: boolean): Vector2D;
20
+ /**
21
+ * Subtracts another vector from this vector
22
+ * @param vector - The vector to subtract
23
+ * @param self - If true, modifies the current vector in place, otherwise returns a new vector
24
+ * @returns A new Vector2D representing the difference
25
+ */
26
+ subtract(vector: Vector2D, self?: boolean): Vector2D;
27
+ /**
28
+ * Multiplies this vector by a scalar value
29
+ * @param scalar - The scalar value to multiply by
30
+ * @returns A new Vector2D scaled by the input value
31
+ */
32
+ multiply(scalar: number): Vector2D;
33
+ /**
34
+ * Divides this vector by a scalar value
35
+ * @param scalar - The scalar value to divide by
36
+ * @throws {Error} If scalar is zero
37
+ * @returns A new Vector2D divided by the scalar value
38
+ */
39
+ divide(scalar: number): Vector2D;
40
+ /**
41
+ * Calculates the magnitude (length) of this vector
42
+ * @returns The magnitude of the vector
43
+ */
44
+ magnitude(): number;
45
+ /**
46
+ * Creates a normalized (unit) vector pointing in the same direction
47
+ * @returns A new Vector2D with magnitude 1 (or 0 if this vector is 0)
48
+ */
49
+ normalize(): Vector2D;
50
+ /**
51
+ * Calculates the dot product with another vector
52
+ * @param vector - The vector to calculate the dot product with
53
+ * @returns The dot product of the two vectors
54
+ */
55
+ dot(vector: Vector2D): number;
56
+ /**
57
+ * Calculates the 2D cross product with another vector
58
+ * @param vector - The vector to calculate the cross product with
59
+ * @returns The magnitude of the cross product
60
+ */
61
+ cross(vector: Vector2D): number;
62
+ /**
63
+ * Calculates the angle between this vector and another vector
64
+ * @param vector - The vector to calculate the angle with
65
+ * @returns The angle in radians between the two vectors
66
+ */
67
+ angle(vector: Vector2D): number;
68
+ /**
69
+ * Rotates the vector by a given angle
70
+ * @param angle - The angle to rotate by (in radians)
71
+ * @returns A new Vector2D rotated by the specified angle
72
+ */
73
+ rotate(angle: number): Vector2D;
74
+ /**
75
+ * Calculates the distance to another vector
76
+ * @param vector - The vector to calculate the distance to
77
+ * @returns The distance between the two vectors
78
+ */
79
+ distance(vector: Vector2D): number;
80
+ /**
81
+ * Projects this vector onto another vector
82
+ * @param vector - The vector to project onto
83
+ * @returns A new Vector2D representing the projection
84
+ */
85
+ project(vector: Vector2D): Vector2D;
86
+ /**
87
+ * Reflects this vector across a normal
88
+ * @param normal - The normal vector to reflect across
89
+ * @returns A new Vector2D representing the reflection
90
+ */
91
+ reflect(normal: Vector2D): Vector2D;
92
+ /**
93
+ * Linearly interpolates between this vector and another vector
94
+ * @param vector - The vector to interpolate towards
95
+ * @param t - The interpolation parameter (0-1)
96
+ * @returns A new Vector2D representing the interpolated vector
97
+ */
98
+ lerp(vector: Vector2D, t: number): Vector2D;
99
+ /**
100
+ * Limits the magnitude of this vector to a maximum value
101
+ * @param max - The maximum magnitude
102
+ * @returns A new Vector2D with limited magnitude
103
+ */
104
+ limit(max: number): Vector2D;
105
+ /**
106
+ * Checks if this vector is equal to another vector within a tolerance
107
+ * @param vector - The vector to compare with
108
+ * @param tolerance - The maximum difference allowed (default: 0.000001)
109
+ * @returns True if the vectors are equal within the tolerance
110
+ */
111
+ equals(vector: Vector2D, tolerance?: number): boolean;
112
+ /**
113
+ * Creates a copy of this vector
114
+ * @returns A new Vector2D with the same components
115
+ */
116
+ clone(): Vector2D;
117
+ /**
118
+ * Converts the vector to a string representation
119
+ * @returns A string representation of the vector
120
+ */
121
+ toString(): string;
122
+ /**
123
+ * Creates a vector pointing in a specific direction
124
+ * @param angle - The angle in radians
125
+ * @param magnitude - The length of the vector (default: 1)
126
+ * @returns A new Vector2D with the specified angle and magnitude
127
+ */
128
+ static fromAngle(angle: number, magnitude?: number): Vector2D;
129
+ /**
130
+ * Creates a random vector with the specified magnitude
131
+ * @param magnitude - The length of the vector (default: 1)
132
+ * @returns A new Vector2D with random direction and specified magnitude
133
+ */
134
+ static random(magnitude?: number): Vector2D;
135
+ /**
136
+ * Adds two vectors together
137
+ * @param v1 - The first vector
138
+ * @param v2 - The second vector
139
+ * @returns A new Vector2D representing the sum
140
+ */
141
+ static add(v1: Vector2D, v2: Vector2D): Vector2D;
142
+ /**
143
+ * Subtracts one vector from another
144
+ * @param v1 - The vector to subtract from
145
+ * @param v2 - The vector to subtract
146
+ * @returns A new Vector2D representing the difference
147
+ */
148
+ static subtract(v1: Vector2D, v2: Vector2D): Vector2D;
149
+ /**
150
+ * Calculates the distance between two vectors
151
+ * @param v1 - The first vector
152
+ * @param v2 - The second vector
153
+ * @returns The distance between the vectors
154
+ */
155
+ static distance(v1: Vector2D, v2: Vector2D): number;
156
+ }
157
+
158
+ declare const TWO_PI: number;
159
+
160
+ declare class Camera extends Node {
161
+ zoom: number;
162
+ constructor(x?: number, y?: number, zoom?: number);
163
+ update(deltaTime: number): void;
164
+ transformCoordinates(position: Vector2D): Vector2D;
165
+ }
166
+
167
+ declare class InputManager extends Node {
168
+ private keyState;
169
+ private keyJustPressed;
170
+ private mouseState;
171
+ private mouseJustPressed;
172
+ constructor();
173
+ private setupListeners;
174
+ private handleKeyDown;
175
+ private handleKeyUp;
176
+ private handleMouseDown;
177
+ private handleMouseUp;
178
+ justPressed(keyOrButton: string | number): boolean;
179
+ isPressed(keyOrButton: string | number): boolean;
180
+ postUpdate(): void;
181
+ }
182
+
183
+ interface SceneOptions {
184
+ camera?: Camera;
185
+ inputManager?: InputManager;
186
+ }
187
+ interface Updatable {
188
+ update(deltaTime: number): void;
189
+ }
190
+ interface Drawable {
191
+ draw(ctx: CanvasRenderingContext2D): void;
192
+ }
193
+ declare class Scene extends Node {
194
+ camera: Camera;
195
+ inputManager: InputManager;
196
+ ctx: CanvasRenderingContext2D;
197
+ private lastTime;
198
+ private totalTime;
199
+ constructor(ctx: CanvasRenderingContext2D, opts?: SceneOptions);
200
+ addObject(object: Node): void;
201
+ setup(): void;
202
+ resizeCanvas(): void;
203
+ update(deltaTime: number): void;
204
+ draw(ctx: CanvasRenderingContext2D): void;
205
+ gameLoop(callback?: (deltaTime: number, totalTime: number) => void, targetFPS?: number): void;
206
+ }
207
+
208
+ interface NodeConfig {
209
+ id?: string;
210
+ pos?: Vector2D;
211
+ }
212
+ declare class Node {
213
+ id: string;
214
+ pos: Vector2D;
215
+ children: Node[];
216
+ scene?: Scene;
217
+ constructor(config?: NodeConfig);
218
+ addObject(object: Node): void;
219
+ removeObject(object: Node): void;
220
+ private generateID;
221
+ }
222
+
223
+ interface CircleConfig {
224
+ vel?: Vector2D;
225
+ size?: number;
226
+ friction?: number;
227
+ startsWithBoundingBox?: boolean;
228
+ }
229
+ declare class Circle extends Node {
230
+ vel: Vector2D;
231
+ size: number;
232
+ friction: number;
233
+ constructor(config?: CircleConfig);
234
+ draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
235
+ }
236
+
237
+ declare function getCanvas(id: string): HTMLCanvasElement;
238
+
239
+ export { Camera, Circle, type CircleConfig, type Drawable, Node, type NodeConfig, Scene, type SceneOptions, TWO_PI, type Updatable, Vector2D, getCanvas };
package/dist/main.d.ts ADDED
@@ -0,0 +1,239 @@
1
+ /**
2
+ * A 2D vector class for mathematical operations and physics calculations
3
+ */
4
+ declare class Vector2D {
5
+ x: number;
6
+ y: number;
7
+ /**
8
+ * Creates a new Vector2D instance
9
+ * @param x - The x component of the vector
10
+ * @param y - The y component of the vector
11
+ */
12
+ constructor(x?: number, y?: number);
13
+ /**
14
+ * Adds another vector to this vector
15
+ * @param vector - The vector to add
16
+ * @param self - If true, modifies the current vector in place, otherwise returns a new vector
17
+ * @returns A new Vector2D representing the sum
18
+ */
19
+ add(vector: Vector2D, self?: boolean): Vector2D;
20
+ /**
21
+ * Subtracts another vector from this vector
22
+ * @param vector - The vector to subtract
23
+ * @param self - If true, modifies the current vector in place, otherwise returns a new vector
24
+ * @returns A new Vector2D representing the difference
25
+ */
26
+ subtract(vector: Vector2D, self?: boolean): Vector2D;
27
+ /**
28
+ * Multiplies this vector by a scalar value
29
+ * @param scalar - The scalar value to multiply by
30
+ * @returns A new Vector2D scaled by the input value
31
+ */
32
+ multiply(scalar: number): Vector2D;
33
+ /**
34
+ * Divides this vector by a scalar value
35
+ * @param scalar - The scalar value to divide by
36
+ * @throws {Error} If scalar is zero
37
+ * @returns A new Vector2D divided by the scalar value
38
+ */
39
+ divide(scalar: number): Vector2D;
40
+ /**
41
+ * Calculates the magnitude (length) of this vector
42
+ * @returns The magnitude of the vector
43
+ */
44
+ magnitude(): number;
45
+ /**
46
+ * Creates a normalized (unit) vector pointing in the same direction
47
+ * @returns A new Vector2D with magnitude 1 (or 0 if this vector is 0)
48
+ */
49
+ normalize(): Vector2D;
50
+ /**
51
+ * Calculates the dot product with another vector
52
+ * @param vector - The vector to calculate the dot product with
53
+ * @returns The dot product of the two vectors
54
+ */
55
+ dot(vector: Vector2D): number;
56
+ /**
57
+ * Calculates the 2D cross product with another vector
58
+ * @param vector - The vector to calculate the cross product with
59
+ * @returns The magnitude of the cross product
60
+ */
61
+ cross(vector: Vector2D): number;
62
+ /**
63
+ * Calculates the angle between this vector and another vector
64
+ * @param vector - The vector to calculate the angle with
65
+ * @returns The angle in radians between the two vectors
66
+ */
67
+ angle(vector: Vector2D): number;
68
+ /**
69
+ * Rotates the vector by a given angle
70
+ * @param angle - The angle to rotate by (in radians)
71
+ * @returns A new Vector2D rotated by the specified angle
72
+ */
73
+ rotate(angle: number): Vector2D;
74
+ /**
75
+ * Calculates the distance to another vector
76
+ * @param vector - The vector to calculate the distance to
77
+ * @returns The distance between the two vectors
78
+ */
79
+ distance(vector: Vector2D): number;
80
+ /**
81
+ * Projects this vector onto another vector
82
+ * @param vector - The vector to project onto
83
+ * @returns A new Vector2D representing the projection
84
+ */
85
+ project(vector: Vector2D): Vector2D;
86
+ /**
87
+ * Reflects this vector across a normal
88
+ * @param normal - The normal vector to reflect across
89
+ * @returns A new Vector2D representing the reflection
90
+ */
91
+ reflect(normal: Vector2D): Vector2D;
92
+ /**
93
+ * Linearly interpolates between this vector and another vector
94
+ * @param vector - The vector to interpolate towards
95
+ * @param t - The interpolation parameter (0-1)
96
+ * @returns A new Vector2D representing the interpolated vector
97
+ */
98
+ lerp(vector: Vector2D, t: number): Vector2D;
99
+ /**
100
+ * Limits the magnitude of this vector to a maximum value
101
+ * @param max - The maximum magnitude
102
+ * @returns A new Vector2D with limited magnitude
103
+ */
104
+ limit(max: number): Vector2D;
105
+ /**
106
+ * Checks if this vector is equal to another vector within a tolerance
107
+ * @param vector - The vector to compare with
108
+ * @param tolerance - The maximum difference allowed (default: 0.000001)
109
+ * @returns True if the vectors are equal within the tolerance
110
+ */
111
+ equals(vector: Vector2D, tolerance?: number): boolean;
112
+ /**
113
+ * Creates a copy of this vector
114
+ * @returns A new Vector2D with the same components
115
+ */
116
+ clone(): Vector2D;
117
+ /**
118
+ * Converts the vector to a string representation
119
+ * @returns A string representation of the vector
120
+ */
121
+ toString(): string;
122
+ /**
123
+ * Creates a vector pointing in a specific direction
124
+ * @param angle - The angle in radians
125
+ * @param magnitude - The length of the vector (default: 1)
126
+ * @returns A new Vector2D with the specified angle and magnitude
127
+ */
128
+ static fromAngle(angle: number, magnitude?: number): Vector2D;
129
+ /**
130
+ * Creates a random vector with the specified magnitude
131
+ * @param magnitude - The length of the vector (default: 1)
132
+ * @returns A new Vector2D with random direction and specified magnitude
133
+ */
134
+ static random(magnitude?: number): Vector2D;
135
+ /**
136
+ * Adds two vectors together
137
+ * @param v1 - The first vector
138
+ * @param v2 - The second vector
139
+ * @returns A new Vector2D representing the sum
140
+ */
141
+ static add(v1: Vector2D, v2: Vector2D): Vector2D;
142
+ /**
143
+ * Subtracts one vector from another
144
+ * @param v1 - The vector to subtract from
145
+ * @param v2 - The vector to subtract
146
+ * @returns A new Vector2D representing the difference
147
+ */
148
+ static subtract(v1: Vector2D, v2: Vector2D): Vector2D;
149
+ /**
150
+ * Calculates the distance between two vectors
151
+ * @param v1 - The first vector
152
+ * @param v2 - The second vector
153
+ * @returns The distance between the vectors
154
+ */
155
+ static distance(v1: Vector2D, v2: Vector2D): number;
156
+ }
157
+
158
+ declare const TWO_PI: number;
159
+
160
+ declare class Camera extends Node {
161
+ zoom: number;
162
+ constructor(x?: number, y?: number, zoom?: number);
163
+ update(deltaTime: number): void;
164
+ transformCoordinates(position: Vector2D): Vector2D;
165
+ }
166
+
167
+ declare class InputManager extends Node {
168
+ private keyState;
169
+ private keyJustPressed;
170
+ private mouseState;
171
+ private mouseJustPressed;
172
+ constructor();
173
+ private setupListeners;
174
+ private handleKeyDown;
175
+ private handleKeyUp;
176
+ private handleMouseDown;
177
+ private handleMouseUp;
178
+ justPressed(keyOrButton: string | number): boolean;
179
+ isPressed(keyOrButton: string | number): boolean;
180
+ postUpdate(): void;
181
+ }
182
+
183
+ interface SceneOptions {
184
+ camera?: Camera;
185
+ inputManager?: InputManager;
186
+ }
187
+ interface Updatable {
188
+ update(deltaTime: number): void;
189
+ }
190
+ interface Drawable {
191
+ draw(ctx: CanvasRenderingContext2D): void;
192
+ }
193
+ declare class Scene extends Node {
194
+ camera: Camera;
195
+ inputManager: InputManager;
196
+ ctx: CanvasRenderingContext2D;
197
+ private lastTime;
198
+ private totalTime;
199
+ constructor(ctx: CanvasRenderingContext2D, opts?: SceneOptions);
200
+ addObject(object: Node): void;
201
+ setup(): void;
202
+ resizeCanvas(): void;
203
+ update(deltaTime: number): void;
204
+ draw(ctx: CanvasRenderingContext2D): void;
205
+ gameLoop(callback?: (deltaTime: number, totalTime: number) => void, targetFPS?: number): void;
206
+ }
207
+
208
+ interface NodeConfig {
209
+ id?: string;
210
+ pos?: Vector2D;
211
+ }
212
+ declare class Node {
213
+ id: string;
214
+ pos: Vector2D;
215
+ children: Node[];
216
+ scene?: Scene;
217
+ constructor(config?: NodeConfig);
218
+ addObject(object: Node): void;
219
+ removeObject(object: Node): void;
220
+ private generateID;
221
+ }
222
+
223
+ interface CircleConfig {
224
+ vel?: Vector2D;
225
+ size?: number;
226
+ friction?: number;
227
+ startsWithBoundingBox?: boolean;
228
+ }
229
+ declare class Circle extends Node {
230
+ vel: Vector2D;
231
+ size: number;
232
+ friction: number;
233
+ constructor(config?: CircleConfig);
234
+ draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
235
+ }
236
+
237
+ declare function getCanvas(id: string): HTMLCanvasElement;
238
+
239
+ export { Camera, Circle, type CircleConfig, type Drawable, Node, type NodeConfig, Scene, type SceneOptions, TWO_PI, type Updatable, Vector2D, getCanvas };