@jiant/canvable 0.0.3 → 0.0.5
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 +85 -85
- package/dist/main.d.mts +89 -11
- package/dist/main.d.ts +89 -11
- package/dist/main.js +300 -60
- package/dist/main.mjs +296 -60
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -1,85 +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).
|
|
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).
|
package/dist/main.d.mts
CHANGED
|
@@ -204,15 +204,18 @@ declare class Scene extends Node {
|
|
|
204
204
|
camera: Camera;
|
|
205
205
|
inputManager: InputManager;
|
|
206
206
|
ctx: CanvasRenderingContext2D;
|
|
207
|
+
private animationFrameId;
|
|
208
|
+
private running;
|
|
207
209
|
private lastTime;
|
|
208
210
|
private totalTime;
|
|
209
211
|
constructor(ctx: CanvasRenderingContext2D, opts?: SceneOptions);
|
|
210
212
|
addObject(object: Node): void;
|
|
211
213
|
setup(): void;
|
|
212
214
|
resizeCanvas(): void;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
updateFrame(deltaTime: number): void;
|
|
216
|
+
drawFrame(ctx: CanvasRenderingContext2D): void;
|
|
217
|
+
run(callback?: (deltaTime: number, totalTime: number) => void, targetFPS?: number): void;
|
|
218
|
+
stop(): void;
|
|
216
219
|
}
|
|
217
220
|
|
|
218
221
|
interface NodeConfig {
|
|
@@ -225,39 +228,114 @@ declare class Node {
|
|
|
225
228
|
id: string;
|
|
226
229
|
children: Node[];
|
|
227
230
|
scene?: Scene;
|
|
231
|
+
boundingBox?: BoundingBox;
|
|
232
|
+
update?(dt: number): void;
|
|
233
|
+
draw?(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
228
234
|
constructor(scene?: Scene, config?: NodeConfig);
|
|
229
235
|
addObject(object: Node): void;
|
|
230
236
|
removeObject(object: Node): void;
|
|
231
237
|
private generateID;
|
|
232
238
|
}
|
|
233
239
|
|
|
240
|
+
interface CollisionResult {
|
|
241
|
+
isColliding: boolean;
|
|
242
|
+
mtv?: Vector2D;
|
|
243
|
+
}
|
|
234
244
|
declare abstract class BoundingBox extends Node {
|
|
235
|
-
abstract checkCollision(other: BoundingBox):
|
|
245
|
+
abstract checkCollision(other: BoundingBox): CollisionResult;
|
|
236
246
|
abstract isPointInside(_point: Vector2D): boolean;
|
|
237
247
|
}
|
|
238
248
|
|
|
249
|
+
declare class CircleBoundingBox extends BoundingBox {
|
|
250
|
+
radius: number;
|
|
251
|
+
constructor(scene?: Scene, radius?: number);
|
|
252
|
+
checkCollision(other: BoundingBox): CollisionResult;
|
|
253
|
+
isPointInside(_point: Vector2D): boolean;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
declare class SquareBoundingBox extends BoundingBox {
|
|
257
|
+
size: Vector2D;
|
|
258
|
+
constructor(scene?: Scene, size?: Vector2D);
|
|
259
|
+
checkCollision(other: BoundingBox): CollisionResult;
|
|
260
|
+
isPointInside(_point: Vector2D): boolean;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
interface KinematicBodyConfig {
|
|
264
|
+
speed?: number;
|
|
265
|
+
friction?: number;
|
|
266
|
+
maxSpeed?: number;
|
|
267
|
+
}
|
|
268
|
+
declare class KinematicBody extends Node {
|
|
269
|
+
shape: Node;
|
|
270
|
+
velocity: Vector2D;
|
|
271
|
+
speed: number;
|
|
272
|
+
friction: number;
|
|
273
|
+
maxSpeed: number;
|
|
274
|
+
isColliding: boolean;
|
|
275
|
+
constructor(scene: Scene, shape: Node, config?: KinematicBodyConfig);
|
|
276
|
+
applyForce(x: number, y: number, dt: number): void;
|
|
277
|
+
update(dt: number): void;
|
|
278
|
+
private resolveCollisions;
|
|
279
|
+
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
declare class StaticBody extends Node {
|
|
283
|
+
shape: Node;
|
|
284
|
+
constructor(scene: Scene, shape: Node);
|
|
285
|
+
update(_dt: number): void;
|
|
286
|
+
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
287
|
+
}
|
|
288
|
+
|
|
239
289
|
interface CircleConfig {
|
|
240
290
|
vel?: Vector2D;
|
|
241
291
|
size?: number;
|
|
242
292
|
friction?: number;
|
|
243
293
|
startsWithBoundingBox?: boolean;
|
|
294
|
+
style?: {
|
|
295
|
+
fillStyle?: string;
|
|
296
|
+
strokeStyle?: string;
|
|
297
|
+
lineWidth?: number;
|
|
298
|
+
};
|
|
244
299
|
}
|
|
245
300
|
declare class Circle extends Node {
|
|
246
301
|
vel: Vector2D;
|
|
247
302
|
size: number;
|
|
248
303
|
friction: number;
|
|
249
|
-
|
|
304
|
+
style: {
|
|
305
|
+
fillStyle?: string;
|
|
306
|
+
strokeStyle?: string;
|
|
307
|
+
lineWidth?: number;
|
|
308
|
+
};
|
|
250
309
|
constructor(scene?: Scene, config?: CircleConfig);
|
|
310
|
+
update(_deltaTime: number): void;
|
|
251
311
|
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
252
312
|
}
|
|
253
313
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
314
|
+
interface SquareConfig {
|
|
315
|
+
vel?: Vector2D;
|
|
316
|
+
size?: Vector2D;
|
|
317
|
+
friction?: number;
|
|
318
|
+
startsWithBoundingBox?: boolean;
|
|
319
|
+
style?: {
|
|
320
|
+
fillStyle?: string;
|
|
321
|
+
strokeStyle?: string;
|
|
322
|
+
lineWidth?: number;
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
declare class Square extends Node {
|
|
326
|
+
vel: Vector2D;
|
|
327
|
+
size: Vector2D;
|
|
328
|
+
friction: number;
|
|
329
|
+
style: {
|
|
330
|
+
fillStyle?: string;
|
|
331
|
+
strokeStyle?: string;
|
|
332
|
+
lineWidth?: number;
|
|
333
|
+
};
|
|
334
|
+
constructor(scene?: Scene, config?: SquareConfig);
|
|
335
|
+
update(_deltaTime: number): void;
|
|
336
|
+
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
259
337
|
}
|
|
260
338
|
|
|
261
339
|
declare function getCanvas(id: string): HTMLCanvasElement;
|
|
262
340
|
|
|
263
|
-
export { BoundingBox, Camera, Circle, CircleBoundingBox, type CircleConfig, type Drawable, Node, type NodeConfig, Scene, type SceneOptions, TWO_PI, type Updatable, Vector2D, getCanvas };
|
|
341
|
+
export { BoundingBox, Camera, Circle, CircleBoundingBox, type CircleConfig, type CollisionResult, type Drawable, KinematicBody, type KinematicBodyConfig, Node, type NodeConfig, Scene, type SceneOptions, Square, SquareBoundingBox, type SquareConfig, StaticBody, TWO_PI, type Updatable, Vector2D, getCanvas };
|
package/dist/main.d.ts
CHANGED
|
@@ -204,15 +204,18 @@ declare class Scene extends Node {
|
|
|
204
204
|
camera: Camera;
|
|
205
205
|
inputManager: InputManager;
|
|
206
206
|
ctx: CanvasRenderingContext2D;
|
|
207
|
+
private animationFrameId;
|
|
208
|
+
private running;
|
|
207
209
|
private lastTime;
|
|
208
210
|
private totalTime;
|
|
209
211
|
constructor(ctx: CanvasRenderingContext2D, opts?: SceneOptions);
|
|
210
212
|
addObject(object: Node): void;
|
|
211
213
|
setup(): void;
|
|
212
214
|
resizeCanvas(): void;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
updateFrame(deltaTime: number): void;
|
|
216
|
+
drawFrame(ctx: CanvasRenderingContext2D): void;
|
|
217
|
+
run(callback?: (deltaTime: number, totalTime: number) => void, targetFPS?: number): void;
|
|
218
|
+
stop(): void;
|
|
216
219
|
}
|
|
217
220
|
|
|
218
221
|
interface NodeConfig {
|
|
@@ -225,39 +228,114 @@ declare class Node {
|
|
|
225
228
|
id: string;
|
|
226
229
|
children: Node[];
|
|
227
230
|
scene?: Scene;
|
|
231
|
+
boundingBox?: BoundingBox;
|
|
232
|
+
update?(dt: number): void;
|
|
233
|
+
draw?(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
228
234
|
constructor(scene?: Scene, config?: NodeConfig);
|
|
229
235
|
addObject(object: Node): void;
|
|
230
236
|
removeObject(object: Node): void;
|
|
231
237
|
private generateID;
|
|
232
238
|
}
|
|
233
239
|
|
|
240
|
+
interface CollisionResult {
|
|
241
|
+
isColliding: boolean;
|
|
242
|
+
mtv?: Vector2D;
|
|
243
|
+
}
|
|
234
244
|
declare abstract class BoundingBox extends Node {
|
|
235
|
-
abstract checkCollision(other: BoundingBox):
|
|
245
|
+
abstract checkCollision(other: BoundingBox): CollisionResult;
|
|
236
246
|
abstract isPointInside(_point: Vector2D): boolean;
|
|
237
247
|
}
|
|
238
248
|
|
|
249
|
+
declare class CircleBoundingBox extends BoundingBox {
|
|
250
|
+
radius: number;
|
|
251
|
+
constructor(scene?: Scene, radius?: number);
|
|
252
|
+
checkCollision(other: BoundingBox): CollisionResult;
|
|
253
|
+
isPointInside(_point: Vector2D): boolean;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
declare class SquareBoundingBox extends BoundingBox {
|
|
257
|
+
size: Vector2D;
|
|
258
|
+
constructor(scene?: Scene, size?: Vector2D);
|
|
259
|
+
checkCollision(other: BoundingBox): CollisionResult;
|
|
260
|
+
isPointInside(_point: Vector2D): boolean;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
interface KinematicBodyConfig {
|
|
264
|
+
speed?: number;
|
|
265
|
+
friction?: number;
|
|
266
|
+
maxSpeed?: number;
|
|
267
|
+
}
|
|
268
|
+
declare class KinematicBody extends Node {
|
|
269
|
+
shape: Node;
|
|
270
|
+
velocity: Vector2D;
|
|
271
|
+
speed: number;
|
|
272
|
+
friction: number;
|
|
273
|
+
maxSpeed: number;
|
|
274
|
+
isColliding: boolean;
|
|
275
|
+
constructor(scene: Scene, shape: Node, config?: KinematicBodyConfig);
|
|
276
|
+
applyForce(x: number, y: number, dt: number): void;
|
|
277
|
+
update(dt: number): void;
|
|
278
|
+
private resolveCollisions;
|
|
279
|
+
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
declare class StaticBody extends Node {
|
|
283
|
+
shape: Node;
|
|
284
|
+
constructor(scene: Scene, shape: Node);
|
|
285
|
+
update(_dt: number): void;
|
|
286
|
+
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
287
|
+
}
|
|
288
|
+
|
|
239
289
|
interface CircleConfig {
|
|
240
290
|
vel?: Vector2D;
|
|
241
291
|
size?: number;
|
|
242
292
|
friction?: number;
|
|
243
293
|
startsWithBoundingBox?: boolean;
|
|
294
|
+
style?: {
|
|
295
|
+
fillStyle?: string;
|
|
296
|
+
strokeStyle?: string;
|
|
297
|
+
lineWidth?: number;
|
|
298
|
+
};
|
|
244
299
|
}
|
|
245
300
|
declare class Circle extends Node {
|
|
246
301
|
vel: Vector2D;
|
|
247
302
|
size: number;
|
|
248
303
|
friction: number;
|
|
249
|
-
|
|
304
|
+
style: {
|
|
305
|
+
fillStyle?: string;
|
|
306
|
+
strokeStyle?: string;
|
|
307
|
+
lineWidth?: number;
|
|
308
|
+
};
|
|
250
309
|
constructor(scene?: Scene, config?: CircleConfig);
|
|
310
|
+
update(_deltaTime: number): void;
|
|
251
311
|
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
252
312
|
}
|
|
253
313
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
314
|
+
interface SquareConfig {
|
|
315
|
+
vel?: Vector2D;
|
|
316
|
+
size?: Vector2D;
|
|
317
|
+
friction?: number;
|
|
318
|
+
startsWithBoundingBox?: boolean;
|
|
319
|
+
style?: {
|
|
320
|
+
fillStyle?: string;
|
|
321
|
+
strokeStyle?: string;
|
|
322
|
+
lineWidth?: number;
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
declare class Square extends Node {
|
|
326
|
+
vel: Vector2D;
|
|
327
|
+
size: Vector2D;
|
|
328
|
+
friction: number;
|
|
329
|
+
style: {
|
|
330
|
+
fillStyle?: string;
|
|
331
|
+
strokeStyle?: string;
|
|
332
|
+
lineWidth?: number;
|
|
333
|
+
};
|
|
334
|
+
constructor(scene?: Scene, config?: SquareConfig);
|
|
335
|
+
update(_deltaTime: number): void;
|
|
336
|
+
draw(ctx: CanvasRenderingContext2D, selected?: boolean): void;
|
|
259
337
|
}
|
|
260
338
|
|
|
261
339
|
declare function getCanvas(id: string): HTMLCanvasElement;
|
|
262
340
|
|
|
263
|
-
export { BoundingBox, Camera, Circle, CircleBoundingBox, type CircleConfig, type Drawable, Node, type NodeConfig, Scene, type SceneOptions, TWO_PI, type Updatable, Vector2D, getCanvas };
|
|
341
|
+
export { BoundingBox, Camera, Circle, CircleBoundingBox, type CircleConfig, type CollisionResult, type Drawable, KinematicBody, type KinematicBodyConfig, Node, type NodeConfig, Scene, type SceneOptions, Square, SquareBoundingBox, type SquareConfig, StaticBody, TWO_PI, type Updatable, Vector2D, getCanvas };
|