sliver-engine 0.0.1-alpha.0
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 +99 -0
- package/dist/Assets/Shapes/Arrow.d.ts +12 -0
- package/dist/Assets/Shapes/Arrow.d.ts.map +1 -0
- package/dist/Assets/Shapes/Arrow.js +32 -0
- package/dist/Assets/Shapes/Arrow.js.map +1 -0
- package/dist/Assets/Shapes/index.d.ts +4 -0
- package/dist/Assets/Shapes/index.d.ts.map +1 -0
- package/dist/Assets/Shapes/index.js +2 -0
- package/dist/Assets/Shapes/index.js.map +1 -0
- package/dist/CanvasController/SpriteLibrary.d.ts +27 -0
- package/dist/CanvasController/SpriteLibrary.d.ts.map +1 -0
- package/dist/CanvasController/SpriteLibrary.js +113 -0
- package/dist/CanvasController/SpriteLibrary.js.map +1 -0
- package/dist/CanvasController/Transformations.d.ts +5 -0
- package/dist/CanvasController/Transformations.d.ts.map +1 -0
- package/dist/CanvasController/Transformations.js +19 -0
- package/dist/CanvasController/Transformations.js.map +1 -0
- package/dist/CanvasController/index.d.ts +214 -0
- package/dist/CanvasController/index.d.ts.map +1 -0
- package/dist/CanvasController/index.js +347 -0
- package/dist/CanvasController/index.js.map +1 -0
- package/dist/Context/index.d.ts +53 -0
- package/dist/Context/index.d.ts.map +1 -0
- package/dist/Context/index.js +103 -0
- package/dist/Context/index.js.map +1 -0
- package/dist/Events/decorators.d.ts +61 -0
- package/dist/Events/decorators.d.ts.map +1 -0
- package/dist/Events/decorators.js +368 -0
- package/dist/Events/decorators.js.map +1 -0
- package/dist/Events/index.d.ts +50 -0
- package/dist/Events/index.d.ts.map +1 -0
- package/dist/Events/index.js +73 -0
- package/dist/Events/index.js.map +1 -0
- package/dist/Events/keyAccumulator.d.ts +9 -0
- package/dist/Events/keyAccumulator.d.ts.map +1 -0
- package/dist/Events/keyAccumulator.js +17 -0
- package/dist/Events/keyAccumulator.js.map +1 -0
- package/dist/Game/Saves/index.d.ts +39 -0
- package/dist/Game/Saves/index.d.ts.map +1 -0
- package/dist/Game/Saves/index.js +213 -0
- package/dist/Game/Saves/index.js.map +1 -0
- package/dist/Game/index.d.ts +45 -0
- package/dist/Game/index.d.ts.map +1 -0
- package/dist/Game/index.js +101 -0
- package/dist/Game/index.js.map +1 -0
- package/dist/GameObject/Decorators/index.d.ts +8 -0
- package/dist/GameObject/Decorators/index.d.ts.map +1 -0
- package/dist/GameObject/Decorators/index.js +65 -0
- package/dist/GameObject/Decorators/index.js.map +1 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.d.ts +23 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.d.ts.map +1 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.js +458 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.js.map +1 -0
- package/dist/GameObject/Hitboxes/index.d.ts +50 -0
- package/dist/GameObject/Hitboxes/index.d.ts.map +1 -0
- package/dist/GameObject/Hitboxes/index.js +306 -0
- package/dist/GameObject/Hitboxes/index.js.map +1 -0
- package/dist/GameObject/Library/Button.d.ts +15 -0
- package/dist/GameObject/Library/Button.d.ts.map +1 -0
- package/dist/GameObject/Library/Button.js +45 -0
- package/dist/GameObject/Library/Button.js.map +1 -0
- package/dist/GameObject/Library/Clickableshape.d.ts +14 -0
- package/dist/GameObject/Library/Clickableshape.d.ts.map +1 -0
- package/dist/GameObject/Library/Clickableshape.js +36 -0
- package/dist/GameObject/Library/Clickableshape.js.map +1 -0
- package/dist/GameObject/Library/FloatingView.d.ts +20 -0
- package/dist/GameObject/Library/FloatingView.d.ts.map +1 -0
- package/dist/GameObject/Library/FloatingView.js +39 -0
- package/dist/GameObject/Library/FloatingView.js.map +1 -0
- package/dist/GameObject/Library/Group.d.ts +8 -0
- package/dist/GameObject/Library/Group.d.ts.map +1 -0
- package/dist/GameObject/Library/Group.js +26 -0
- package/dist/GameObject/Library/Group.js.map +1 -0
- package/dist/GameObject/Library/HoverableShape.d.ts +15 -0
- package/dist/GameObject/Library/HoverableShape.d.ts.map +1 -0
- package/dist/GameObject/Library/HoverableShape.js +41 -0
- package/dist/GameObject/Library/HoverableShape.js.map +1 -0
- package/dist/GameObject/Library/SceneTranslator.d.ts +14 -0
- package/dist/GameObject/Library/SceneTranslator.d.ts.map +1 -0
- package/dist/GameObject/Library/SceneTranslator.js +104 -0
- package/dist/GameObject/Library/SceneTranslator.js.map +1 -0
- package/dist/GameObject/Library/ScrollView.d.ts +41 -0
- package/dist/GameObject/Library/ScrollView.d.ts.map +1 -0
- package/dist/GameObject/Library/ScrollView.js +112 -0
- package/dist/GameObject/Library/ScrollView.js.map +1 -0
- package/dist/GameObject/Library/ShowOnHover.d.ts +13 -0
- package/dist/GameObject/Library/ShowOnHover.d.ts.map +1 -0
- package/dist/GameObject/Library/ShowOnHover.js +33 -0
- package/dist/GameObject/Library/ShowOnHover.js.map +1 -0
- package/dist/GameObject/Library/Text.d.ts +25 -0
- package/dist/GameObject/Library/Text.d.ts.map +1 -0
- package/dist/GameObject/Library/Text.js +35 -0
- package/dist/GameObject/Library/Text.js.map +1 -0
- package/dist/GameObject/Library/TextBox.d.ts +86 -0
- package/dist/GameObject/Library/TextBox.d.ts.map +1 -0
- package/dist/GameObject/Library/TextBox.js +344 -0
- package/dist/GameObject/Library/TextBox.js.map +1 -0
- package/dist/GameObject/Library/index.d.ts +11 -0
- package/dist/GameObject/Library/index.d.ts.map +1 -0
- package/dist/GameObject/Library/index.js +11 -0
- package/dist/GameObject/Library/index.js.map +1 -0
- package/dist/GameObject/Walker.d.ts +66 -0
- package/dist/GameObject/Walker.d.ts.map +1 -0
- package/dist/GameObject/Walker.js +461 -0
- package/dist/GameObject/Walker.js.map +1 -0
- package/dist/GameObject/index.d.ts +97 -0
- package/dist/GameObject/index.d.ts.map +1 -0
- package/dist/GameObject/index.js +368 -0
- package/dist/GameObject/index.js.map +1 -0
- package/dist/Lib/Math.d.ts +2 -0
- package/dist/Lib/Math.d.ts.map +1 -0
- package/dist/Lib/Math.js +4 -0
- package/dist/Lib/Math.js.map +1 -0
- package/dist/Lib/MinHeap/index.d.ts +12 -0
- package/dist/Lib/MinHeap/index.d.ts.map +1 -0
- package/dist/Lib/MinHeap/index.js +64 -0
- package/dist/Lib/MinHeap/index.js.map +1 -0
- package/dist/Lib/PathfindingProxy/index.d.ts +16 -0
- package/dist/Lib/PathfindingProxy/index.d.ts.map +1 -0
- package/dist/Lib/PathfindingProxy/index.js +48 -0
- package/dist/Lib/PathfindingProxy/index.js.map +1 -0
- package/dist/Lib/Vector/index.d.ts +24 -0
- package/dist/Lib/Vector/index.d.ts.map +1 -0
- package/dist/Lib/Vector/index.js +94 -0
- package/dist/Lib/Vector/index.js.map +1 -0
- package/dist/Lib/index.d.ts +5 -0
- package/dist/Lib/index.d.ts.map +1 -0
- package/dist/Lib/index.js +5 -0
- package/dist/Lib/index.js.map +1 -0
- package/dist/Scenes/SceneManager/Transitions.d.ts +37 -0
- package/dist/Scenes/SceneManager/Transitions.d.ts.map +1 -0
- package/dist/Scenes/SceneManager/Transitions.js +165 -0
- package/dist/Scenes/SceneManager/Transitions.js.map +1 -0
- package/dist/Scenes/SceneManager/index.d.ts +24 -0
- package/dist/Scenes/SceneManager/index.d.ts.map +1 -0
- package/dist/Scenes/SceneManager/index.js +136 -0
- package/dist/Scenes/SceneManager/index.js.map +1 -0
- package/dist/Scenes/index.d.ts +36 -0
- package/dist/Scenes/index.d.ts.map +1 -0
- package/dist/Scenes/index.js +196 -0
- package/dist/Scenes/index.js.map +1 -0
- package/dist/ScriptedEvents/Combinators.d.ts +17 -0
- package/dist/ScriptedEvents/Combinators.d.ts.map +1 -0
- package/dist/ScriptedEvents/Combinators.js +127 -0
- package/dist/ScriptedEvents/Combinators.js.map +1 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.d.ts +13 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.d.ts.map +1 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.js +41 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.js.map +1 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.d.ts +4 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.d.ts.map +1 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.js +14 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.js.map +1 -0
- package/dist/ScriptedEvents/index.d.ts +38 -0
- package/dist/ScriptedEvents/index.d.ts.map +1 -0
- package/dist/ScriptedEvents/index.js +58 -0
- package/dist/ScriptedEvents/index.js.map +1 -0
- package/dist/SoundManager/index.d.ts +69 -0
- package/dist/SoundManager/index.d.ts.map +1 -0
- package/dist/SoundManager/index.js +208 -0
- package/dist/SoundManager/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Sliver Engine
|
|
2
|
+
|
|
3
|
+
Pre-alpha TypeScript/HTML5 Canvas 2D game engine.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
`npm install sliver-engine`
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
```ts
|
|
10
|
+
import {
|
|
11
|
+
CanvasController,
|
|
12
|
+
Game,
|
|
13
|
+
Scene,
|
|
14
|
+
SceneManager,
|
|
15
|
+
SoundManager,
|
|
16
|
+
} from "sliver-engine";
|
|
17
|
+
|
|
18
|
+
const canvas = new CanvasController(800, 600);
|
|
19
|
+
|
|
20
|
+
const mainScene = new Scene("main", "#0b0a18");
|
|
21
|
+
const scenes = new SceneManager({ main: mainScene }, mainScene);
|
|
22
|
+
|
|
23
|
+
const game = new Game({
|
|
24
|
+
canvas,
|
|
25
|
+
scenes,
|
|
26
|
+
soundManager: new SoundManager(),
|
|
27
|
+
ticksPerSecond: 60,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
game.start();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Sliver Engine appends the `<canvas>` to `#canvas-container` if present, otherwise to `document.body`.
|
|
34
|
+
|
|
35
|
+
## Engine features (Sliver Engine)
|
|
36
|
+
- **Game loop + context**: tick/render split, configurable TPS, shared `GameContext` (tick/frame counters, pressed keys, message bus, sound manager).
|
|
37
|
+
- **Saves**: base64-encoded save files stored in `localStorage`, with `onLoadSaveFile(save => ...)` callback during `game.setup()`.
|
|
38
|
+
- **Scenes**: scene stack, per-scene camera offset, background fill, opacity + overlay, gravity, and transitions (fade/slide/flash with easing).
|
|
39
|
+
- **Rendering**: `CanvasController` + `ShapeDrawer` for primitives/text, clipping and rotation helpers, font loading, and sprite + sprite-sheet support (frame rendering, scaling, mirroring) with `renderSprite` / `renderSpriteAnimation` helpers.
|
|
40
|
+
- **Input/events**: keyboard + mouse + wheel events, propagation control, and ergonomic decorators (`@onClick`, `@onMouseRelease`, `@onHover`, `@onStopHovering`, `@onMouseWheel`, `@onMouseWheelOverHitbox`, `@onKeyPressed`/combo, `@onChildrenEvents`, `@grabbable()`).
|
|
41
|
+
- **Game objects**: `GameObject` base with tick/render hooks, child hierarchy, relative positioning, opacity/rotation, simple movement via `speed`, and helpers to change scenes / publish messages / query keys.
|
|
42
|
+
- **Hitboxes + collisions**: circle + (rotatable) square hitboxes, point checks, “will intersect” checks, debug rendering, and collision resolution with restitution + friction and immovable bodies.
|
|
43
|
+
- **Audio**: `SoundManager` (Web Audio) with preload, master gain, per-sound volume, looping, playback-rate, and start/end/duration controls.
|
|
44
|
+
- **Reusable object/widgets**: `Button`, `Text`, `ScrollView` (clipping + scrollbar + wheel), `FloatingView` (rise + fade), `ShowOnHover`, `Group`, `SceneTranslator`, and basic shape helpers (`ClickableShape`, `HoverableShape`).
|
|
45
|
+
|
|
46
|
+
## Repository layout
|
|
47
|
+
- `src/` – Engine sources.
|
|
48
|
+
- `src/index.ts` – Library entrypoint (exports the public API).
|
|
49
|
+
- `dist/` – Build output (generated by `npm run build`).
|
|
50
|
+
- `src/CanvasController` – Canvas primitives, font loading, sprite + sprite-sheet helpers.
|
|
51
|
+
- `src/Context` – `GameContext` + message bus.
|
|
52
|
+
- `src/Events` – Event types, dispatcher, decorators, and `KeyAccumulator`.
|
|
53
|
+
- `src/Game` – Game loop orchestration.
|
|
54
|
+
- `src/GameObject` – `GameObject`, hitboxes/collisions, sprite render decorators, and reusable widgets.
|
|
55
|
+
- `src/Scenes` – `Scene` + `SceneManager` and transition helpers.
|
|
56
|
+
- `src/SoundManager` – Web Audio wrapper.
|
|
57
|
+
- `src/Lib` – Utilities (`Vector`, `MinHeap`, `PathfindingProxy`, ...).
|
|
58
|
+
- `src/Assets` – Shape render helpers (arrow/chevrons).
|
|
59
|
+
|
|
60
|
+
## Running locally
|
|
61
|
+
1) Install deps: `bun install`
|
|
62
|
+
2) Typecheck: `bun run typecheck`
|
|
63
|
+
3) Build: `bun run build`
|
|
64
|
+
|
|
65
|
+
## Documentation (Docusaurus)
|
|
66
|
+
The docs site lives in `website/` and is **not** part of the published npm package.
|
|
67
|
+
|
|
68
|
+
### Run locally
|
|
69
|
+
1) Install docs deps: `bun run docs:install`
|
|
70
|
+
2) Start the dev server: `bun run docs:start`
|
|
71
|
+
|
|
72
|
+
### Docs versioning
|
|
73
|
+
Docs are versioned (initial snapshot: `0.0.1-alpha.0`). For a new engine release, snapshot the current docs into a new version:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
bun run docs:version -- 0.0.1-alpha.1
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Commit the generated `website/versioned_docs/`, `website/versioned_sidebars/`, and `website/versions.json`.
|
|
80
|
+
|
|
81
|
+
When you decide how to publish, update `website/docusaurus.config.js` (`url`, `baseUrl`, and the GitHub link).
|
|
82
|
+
|
|
83
|
+
## Extending
|
|
84
|
+
- Add a scene by instantiating `Scene` and registering it in `SceneManager` (see how `src/LamenEmpire/index.ts` wires scenes today).
|
|
85
|
+
- Create a game object by subclassing `GameObject`, adding hitboxes, and using decorators like `@onClick` / `@onHover` / `@onKeyPressed`.
|
|
86
|
+
- Use `GameContext` from objects to switch/push/pop/transition scenes, publish/subscribe messages, query pressed keys, and play sounds.
|
|
87
|
+
- Save/load game state using `game.saves` (or `context.getGame().saves` from a `GameObject`).
|
|
88
|
+
|
|
89
|
+
## Saving & loading
|
|
90
|
+
- Create a save: `const id = game.saves.create({ ...anySerializableState }, { label: "slot 1" });`
|
|
91
|
+
- List saves: `game.saves.list()`
|
|
92
|
+
- Read a save: `const save = game.saves.read(id)`
|
|
93
|
+
- Load hook (runs during `game.setup()`): `game.onLoadSaveFile((save) => { /* restore state */ });`
|
|
94
|
+
|
|
95
|
+
## Notes
|
|
96
|
+
- Browser-only (DOM + Canvas + Web Audio).
|
|
97
|
+
- Tooling uses Bun (TypeScript 5).
|
|
98
|
+
- If you use the decorator helpers, enable `experimentalDecorators: true` in your TS config.
|
|
99
|
+
- `src/LamenEmpire` is a dogfooding game and is excluded from the published npm package.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ShapeRendererFn } from ".";
|
|
2
|
+
import type { Vector } from "../../Lib/Vector";
|
|
3
|
+
export declare const renderArrow: (context: CanvasRenderingContext2D, position: Vector, size: Vector) => void;
|
|
4
|
+
export declare const renderChevron: (context: CanvasRenderingContext2D, position: Vector, size: Vector) => void;
|
|
5
|
+
export type ShapeRenderOptions = {
|
|
6
|
+
strokeWidth?: number;
|
|
7
|
+
color?: string;
|
|
8
|
+
lineCap?: CanvasLineCap;
|
|
9
|
+
lineJoin?: CanvasLineJoin;
|
|
10
|
+
};
|
|
11
|
+
export declare const shapeWithParams: (shape: ShapeRendererFn, options?: ShapeRenderOptions) => ShapeRendererFn;
|
|
12
|
+
//# sourceMappingURL=Arrow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Arrow.d.ts","sourceRoot":"","sources":["../../../src/Assets/Shapes/Arrow.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,GAAG,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,eAAO,MAAM,WAAW,GACtB,SAAS,wBAAwB,EACjC,UAAU,MAAM,EAChB,MAAM,MAAM,SAab,CAAC;AAEF,eAAO,MAAM,aAAa,GACxB,SAAS,wBAAwB,EACjC,UAAU,MAAM,EAChB,MAAM,MAAM,SAWb,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B,CAAC;AAEF,eAAO,MAAM,eAAe,GACzB,OAAO,eAAe,EAAE,UAAS,kBAAuB,KAAG,eAS3D,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const renderArrow = (context, position, size) => {
|
|
2
|
+
const centerX = position.x + size.x / 2;
|
|
3
|
+
const centerY = position.y + size.y / 2;
|
|
4
|
+
context.beginPath();
|
|
5
|
+
context.moveTo(centerX - size.x / 2, centerY);
|
|
6
|
+
context.lineTo(centerX + size.x / 2, centerY);
|
|
7
|
+
context.lineTo(centerX + size.x / 2 - size.x * 0.1, centerY - size.y * 0.1);
|
|
8
|
+
context.moveTo(centerX + size.x / 2, centerY);
|
|
9
|
+
context.lineTo(centerX + size.x / 2 - size.x * 0.1, centerY + size.y * 0.1);
|
|
10
|
+
context.stroke();
|
|
11
|
+
context.closePath();
|
|
12
|
+
};
|
|
13
|
+
export const renderChevron = (context, position, size) => {
|
|
14
|
+
const centerX = position.x + size.x / 2;
|
|
15
|
+
const centerY = position.y + size.y / 2;
|
|
16
|
+
context.beginPath();
|
|
17
|
+
context.moveTo(centerX - size.x / 2, centerY - size.y / 2);
|
|
18
|
+
context.lineTo(centerX, centerY);
|
|
19
|
+
context.lineTo(centerX - size.x / 2, centerY + size.y / 2);
|
|
20
|
+
context.stroke();
|
|
21
|
+
context.closePath();
|
|
22
|
+
};
|
|
23
|
+
export const shapeWithParams = (shape, options = {}) => (context, position, size) => {
|
|
24
|
+
context.save();
|
|
25
|
+
context.lineWidth = options.strokeWidth || 2;
|
|
26
|
+
context.strokeStyle = options.color || "black";
|
|
27
|
+
context.lineCap = options.lineCap || "round";
|
|
28
|
+
context.lineJoin = options.lineJoin || "round";
|
|
29
|
+
shape(context, position, size);
|
|
30
|
+
context.restore();
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=Arrow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Arrow.js","sourceRoot":"","sources":["../../../src/Assets/Shapes/Arrow.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,OAAiC,EACjC,QAAgB,EAChB,IAAY,EACZ,EAAE;IACF,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACxC,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IAExC,OAAO,CAAC,SAAS,EAAE,CAAC;IACpB,OAAO,CAAC,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IAC9C,OAAO,CAAC,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IAC9C,OAAO,CAAC,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,GAAG,EAAE,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;IAC5E,OAAO,CAAC,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IAC9C,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AAG,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC;IAC/C,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC;IAC7C,OAAO,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC;IAC/C,KAAK,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC/B,OAAO,CAAC,OAAO,EAAE,CAAC;AACpB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/Assets/Shapes/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,MAAM,eAAe,GAAG,CAC5B,OAAO,EAAE,wBAAwB,EACjC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,KACT,IAAI,CAAC;AAEV,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Assets/Shapes/index.ts"],"names":[],"mappings":"AAQA,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Vector } from "../Lib/Vector";
|
|
2
|
+
declare class SpriteLibrary {
|
|
3
|
+
private sprites;
|
|
4
|
+
private spriteSheets;
|
|
5
|
+
loadSprite(name: string, url: URL): Promise<void>;
|
|
6
|
+
loadSvgSprite(name: string, url: string): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Load a fixed size sprite sheet
|
|
9
|
+
*/
|
|
10
|
+
loadSpriteSheet(name: string, url: URL, frameWidth: number, frameHeight: number): Promise<void>;
|
|
11
|
+
getSprite(name: string): HTMLImageElement | undefined;
|
|
12
|
+
getSpriteSheetFrameSize(sheetName: string): {
|
|
13
|
+
frameWidth: number;
|
|
14
|
+
frameHeight: number;
|
|
15
|
+
} | null;
|
|
16
|
+
/**
|
|
17
|
+
* Render the sprite frame by its index in the sheet
|
|
18
|
+
* Indexes grow from left to right, top to bottom
|
|
19
|
+
*/
|
|
20
|
+
drawSpriteFrame(ctx: CanvasRenderingContext2D, sheetName: string, index: number, position: Vector, scale?: number, mirrorHorizontal?: boolean, mirrorVertical?: boolean): void;
|
|
21
|
+
/**
|
|
22
|
+
* Render a specific cell from the sprite sheet grid
|
|
23
|
+
*/
|
|
24
|
+
drawSpriteGrid(ctx: CanvasRenderingContext2D, sheetName: string, col: number, row: number, position: Vector, scale?: number): void;
|
|
25
|
+
}
|
|
26
|
+
export { SpriteLibrary };
|
|
27
|
+
//# sourceMappingURL=SpriteLibrary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SpriteLibrary.d.ts","sourceRoot":"","sources":["../../src/CanvasController/SpriteLibrary.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,cAAM,aAAa;IACjB,OAAO,CAAC,OAAO,CAA4C;IAC3D,OAAO,CAAC,YAAY,CAQN;IAER,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjD,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoB7D;;OAEG;IACG,eAAe,CACnB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,GAAG,EACR,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC;IAoBhB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAIrD,uBAAuB,CACrB,SAAS,EAAE,MAAM,GAChB;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;IAMrD;;;OAGG;IACH,eAAe,CACb,GAAG,EAAE,wBAAwB,EAC7B,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,KAAK,SAAI,EACT,gBAAgB,UAAQ,EACxB,cAAc,UAAQ,GACrB,IAAI;IAoDP;;OAEG;IACH,cAAc,CACZ,GAAG,EAAE,wBAAwB,EAC7B,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,KAAK,SAAI,GACR,IAAI;CAuBR;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
class SpriteLibrary {
|
|
2
|
+
sprites = new Map();
|
|
3
|
+
spriteSheets = new Map();
|
|
4
|
+
async loadSprite(name, url) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const img = new Image();
|
|
7
|
+
img.onload = () => {
|
|
8
|
+
this.sprites.set(name, img);
|
|
9
|
+
resolve();
|
|
10
|
+
};
|
|
11
|
+
img.onerror = reject;
|
|
12
|
+
img.src = url.toString();
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async loadSvgSprite(name, url) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const img = new Image();
|
|
18
|
+
img.onload = () => {
|
|
19
|
+
this.sprites.set(name, img);
|
|
20
|
+
resolve();
|
|
21
|
+
};
|
|
22
|
+
img.onerror = reject;
|
|
23
|
+
fetch(url)
|
|
24
|
+
.then((response) => response.text())
|
|
25
|
+
.then((svgText) => {
|
|
26
|
+
const svgBlob = new Blob([svgText], { type: "image/svg+xml" });
|
|
27
|
+
const url = URL.createObjectURL(svgBlob);
|
|
28
|
+
img.src = url;
|
|
29
|
+
})
|
|
30
|
+
.catch(reject);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Load a fixed size sprite sheet
|
|
35
|
+
*/
|
|
36
|
+
async loadSpriteSheet(name, url, frameWidth, frameHeight) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const img = new Image();
|
|
39
|
+
img.onload = () => {
|
|
40
|
+
const columns = Math.floor(img.width / frameWidth);
|
|
41
|
+
this.spriteSheets.set(name, {
|
|
42
|
+
image: img,
|
|
43
|
+
frameWidth,
|
|
44
|
+
frameHeight,
|
|
45
|
+
columns,
|
|
46
|
+
});
|
|
47
|
+
resolve();
|
|
48
|
+
};
|
|
49
|
+
img.onerror = reject;
|
|
50
|
+
img.src = url.toString();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
getSprite(name) {
|
|
54
|
+
return this.sprites.get(name);
|
|
55
|
+
}
|
|
56
|
+
getSpriteSheetFrameSize(sheetName) {
|
|
57
|
+
const sheet = this.spriteSheets.get(sheetName);
|
|
58
|
+
if (!sheet)
|
|
59
|
+
return null;
|
|
60
|
+
return { frameWidth: sheet.frameWidth, frameHeight: sheet.frameHeight };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Render the sprite frame by its index in the sheet
|
|
64
|
+
* Indexes grow from left to right, top to bottom
|
|
65
|
+
*/
|
|
66
|
+
drawSpriteFrame(ctx, sheetName, index, position, scale = 1, mirrorHorizontal = false, mirrorVertical = false) {
|
|
67
|
+
const sheet = this.spriteSheets.get(sheetName);
|
|
68
|
+
if (!sheet)
|
|
69
|
+
return;
|
|
70
|
+
const { image, frameWidth, frameHeight, columns } = sheet;
|
|
71
|
+
const sx = (index % columns) * frameWidth;
|
|
72
|
+
const sy = Math.floor(index / columns) * frameHeight;
|
|
73
|
+
ctx.imageSmoothingEnabled = false; // nearest neighbor
|
|
74
|
+
// Save the current context state
|
|
75
|
+
ctx.save();
|
|
76
|
+
// Apply transformations if needed
|
|
77
|
+
if (mirrorHorizontal || mirrorVertical) {
|
|
78
|
+
// Calculate the center of the sprite
|
|
79
|
+
const centerX = position.x + (frameWidth * scale) / 2;
|
|
80
|
+
const centerY = position.y + (frameHeight * scale) / 2;
|
|
81
|
+
// Translate to the center of the sprite
|
|
82
|
+
ctx.translate(centerX, centerY);
|
|
83
|
+
// Apply mirroring
|
|
84
|
+
if (mirrorHorizontal) {
|
|
85
|
+
ctx.scale(-1, 1);
|
|
86
|
+
}
|
|
87
|
+
if (mirrorVertical) {
|
|
88
|
+
ctx.scale(1, -1);
|
|
89
|
+
}
|
|
90
|
+
// Translate back to the original position
|
|
91
|
+
ctx.translate(-centerX, -centerY);
|
|
92
|
+
}
|
|
93
|
+
// Draw the image with the applied transformations
|
|
94
|
+
ctx.drawImage(image, sx, sy, frameWidth, frameHeight, position.x, position.y, frameWidth * scale, frameHeight * scale);
|
|
95
|
+
// Restore the context state
|
|
96
|
+
ctx.restore();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Render a specific cell from the sprite sheet grid
|
|
100
|
+
*/
|
|
101
|
+
drawSpriteGrid(ctx, sheetName, col, row, position, scale = 1) {
|
|
102
|
+
const sheet = this.spriteSheets.get(sheetName);
|
|
103
|
+
if (!sheet)
|
|
104
|
+
return;
|
|
105
|
+
const { image, frameWidth, frameHeight } = sheet;
|
|
106
|
+
const sx = col * frameWidth;
|
|
107
|
+
const sy = row * frameHeight;
|
|
108
|
+
ctx.imageSmoothingEnabled = false;
|
|
109
|
+
ctx.drawImage(image, sx, sy, frameWidth, frameHeight, position.x, position.y, frameWidth * scale, frameHeight * scale);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export { SpriteLibrary };
|
|
113
|
+
//# sourceMappingURL=SpriteLibrary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SpriteLibrary.js","sourceRoot":"","sources":["../../src/CanvasController/SpriteLibrary.ts"],"names":[],"mappings":"AAEA,MAAM,aAAa;IACT,OAAO,GAAkC,IAAI,GAAG,EAAE,CAAC;IACnD,YAAY,GAQhB,IAAI,GAAG,EAAE,CAAC;IAEd,KAAK,CAAC,U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,CAAC,CAAC,EACV,QAAQ,CAAC,CAAC,EACV,UAAU,GAAG,KAAK,EAClB,WAAW,GAAG,KAAK,CACpB,CAAC;QAEF,4BAA4B;QAC5B,GAAG,CAAC,OAAO,EAAE,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,cAAc,CACZ,GAA6B,EAC7B,SAAiB,EACjB,GAAW,EACX,GAAW,EACX,QAAgB,EAChB,KAAK,GAAG,CAAC;QAET,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC/C,IAAI,CAAC,KAAK;YAAE,OAAO;QAEnB,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,KAAK,CAAC;QAEjD,MAAM,EAAE,GAAG,GAAG,GAAG,UAAU,CAAC;QAC5B,MAAM,EAAE,GAAG,GAAG,GAAG,WAAW,CAAC;QAE7B,GAAG,CAAC,qBAAqB,GAAG,KAAK,CAAC;QAElC,GAAG,CAAC,SAAS,CACX,KAAK,EACL,EAAE,EACF,EAAE,EACF,UAAU,EACV,WAAW,EACX,QAAQ,CAAC,CAAC,EACV,QAAQ,CAAC,CAAC,EACV,UAAU,GAAG,KAAK,EAClB,WAAW,GAAG,KAAK,CACpB,CAAC;IACJ,CAAC;CACF;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ShapeRendererFn } from "../Assets/Shapes";
|
|
2
|
+
import type { Vector } from "../Lib/Vector";
|
|
3
|
+
export declare const withRotation: (angle: number, shape: ShapeRendererFn) => ShapeRendererFn;
|
|
4
|
+
export declare const withTranslation: (translationVector: Vector, shape: ShapeRendererFn) => ShapeRendererFn;
|
|
5
|
+
//# sourceMappingURL=Transformations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Transformations.d.ts","sourceRoot":"","sources":["../../src/CanvasController/Transformations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,eAAO,MAAM,YAAY,GACvB,OAAO,MAAM,EACb,OAAO,eAAe,KACrB,eASF,CAAC;AAEF,eAAO,MAAM,eAAe,GAC1B,mBAAmB,MAAM,EACzB,OAAO,eAAe,KACrB,eAOF,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const withRotation = (angle, shape) => {
|
|
2
|
+
return (ctx, position, size) => {
|
|
3
|
+
ctx.save();
|
|
4
|
+
ctx.translate(position.x + size.x / 2, position.y + size.y / 2);
|
|
5
|
+
ctx.rotate(angle);
|
|
6
|
+
ctx.translate(-(position.x + size.x / 2), -(position.y + size.y / 2));
|
|
7
|
+
shape(ctx, position, size);
|
|
8
|
+
ctx.restore();
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export const withTranslation = (translationVector, shape) => {
|
|
12
|
+
return (ctx, position, size) => {
|
|
13
|
+
ctx.save();
|
|
14
|
+
ctx.translate(translationVector.x, translationVector.y);
|
|
15
|
+
shape(ctx, position, size);
|
|
16
|
+
ctx.restore();
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=Transformations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Transformations.js","sourceRoot":"","sources":["../../src/CanvasController/Transformations.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,KAAa,EACb,KAAsB,EACL,EAAE;IACnB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QAC7B,GAAG,CAAC,IAAI,EAAE,CAAC;QACX,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAChE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACtE,KAAK,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC3B,GAAG,CAAC,OAAO,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,iBAAyB,EACzB,KAAsB,EACL,EAAE;IACnB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QAC7B,GAAG,CAAC,IAAI,EAAE,CAAC;QACX,GAAG,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;QACxD,KAAK,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC3B,GAAG,CAAC,OAAO,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type { Vector } from "../Lib/Vector";
|
|
2
|
+
import { SpriteLibrary } from "./SpriteLibrary";
|
|
3
|
+
declare class ShapeDrawer {
|
|
4
|
+
private context;
|
|
5
|
+
private spriteLibrary;
|
|
6
|
+
private defaultFont;
|
|
7
|
+
constructor(context: CanvasRenderingContext2D, spriteLibrary: SpriteLibrary);
|
|
8
|
+
/**
|
|
9
|
+
* Executes drawing instructions with a temporary alpha multiplier.
|
|
10
|
+
*/
|
|
11
|
+
withOpacity(opacity: number, draw: () => void): void;
|
|
12
|
+
/**
|
|
13
|
+
* Draws a rectangle on the canvas with configurable fill/stroke behavior.
|
|
14
|
+
*
|
|
15
|
+
* @param x - The x-coordinate of the rectangle's top-left corner
|
|
16
|
+
* @param y - The y-coordinate of the rectangle's top-left corner
|
|
17
|
+
* @param width - The width of the rectangle
|
|
18
|
+
* @param height - The height of the rectangle
|
|
19
|
+
* @param color - The color to use for filling or stroking (default: "black")
|
|
20
|
+
* @param filled - If true, fills the rectangle; if false, strokes the outline (default: true)
|
|
21
|
+
* @param opacity - Alpha multiplier applied while drawing (default: 1)
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Draw a filled red rectangle
|
|
25
|
+
* shapeDrawer.drawRectangle(10, 10, 100, 50, "red", true);
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Draw a blue rectangle outline
|
|
29
|
+
* shapeDrawer.drawRectangle(10, 10, 100, 50, "blue", false);
|
|
30
|
+
*/
|
|
31
|
+
drawRectangle(x: number, y: number, width: number, height: number, color?: string, filled?: boolean, opacity?: number): void;
|
|
32
|
+
/**
|
|
33
|
+
* Sets the default font family used for text rendering.
|
|
34
|
+
*
|
|
35
|
+
* @param font - The font family name to use as default (e.g., "Arial", "Verdana")
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Set default font to Arial
|
|
39
|
+
* shapeDrawer.setDefaultFont("Arial");
|
|
40
|
+
*/
|
|
41
|
+
setDefaultFont(font: string): void;
|
|
42
|
+
getDefaultFont(): string;
|
|
43
|
+
/**
|
|
44
|
+
* Draws a straight line between two points with customizable appearance.
|
|
45
|
+
*
|
|
46
|
+
* @param start - The starting point of the line as a Vector
|
|
47
|
+
* @param end - The ending point of the line as a Vector
|
|
48
|
+
* @param width - The thickness of the line in pixels
|
|
49
|
+
* @param color - The color of the line (default: "red")
|
|
50
|
+
* @param lineCap - The style of the line endings (default: "round")
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Draw a thick blue line with square caps
|
|
54
|
+
* shapeDrawer.drawLine(new Vector(10, 10), new Vector(100, 50), 5, "blue", "square");
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Draw a thin red line with default round caps
|
|
58
|
+
* shapeDrawer.drawLine(new Vector(0, 0), new Vector(100, 100), 1);
|
|
59
|
+
*/
|
|
60
|
+
drawLine(start: Vector, end: Vector, width: number, color?: string, lineCap?: CanvasLineCap): void;
|
|
61
|
+
/**
|
|
62
|
+
* Draws a circle with customizable appearance and optional center dot.
|
|
63
|
+
*
|
|
64
|
+
* @param x - The x-coordinate of the circle's center
|
|
65
|
+
* @param y - The y-coordinate of the circle's center
|
|
66
|
+
* @param radius - The radius of the circle in pixels
|
|
67
|
+
* @param color - The color to use for filling or stroking the circle (default: "black")
|
|
68
|
+
* @param filled - If true, fills the circle; if false, strokes the outline (default: true)
|
|
69
|
+
* @param centerDot - If true, draws a small dot at the center of the circle (default: true)
|
|
70
|
+
* @param centerDotColor - The color of the center dot (default: "red")
|
|
71
|
+
* @param centerDotRadius - The radius of the center dot in pixels (default: 2)
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* // Draw a filled blue circle with default center dot
|
|
75
|
+
* shapeDrawer.drawCircle(100, 100, 50, "blue", true);
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* // Draw a red circle outline with no center dot
|
|
79
|
+
* shapeDrawer.drawCircle(100, 100, 50, "red");
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* // Draw a green circle with custom center dot
|
|
83
|
+
* shapeDrawer.drawCircle(100, 100, 50, "green", true, true, "yellow", 3);
|
|
84
|
+
*/
|
|
85
|
+
drawCircle(x: number, y: number, radius: number, color?: string, filled?: boolean, centerDot?: boolean, centerDotColor?: string, centerDotRadius?: number): void;
|
|
86
|
+
/**
|
|
87
|
+
* Renders text at the specified position with customizable styling.
|
|
88
|
+
*
|
|
89
|
+
* @param text - The text string to render
|
|
90
|
+
* @param x - The x-coordinate of the text position
|
|
91
|
+
* @param y - The y-coordinate of the text position
|
|
92
|
+
* @param color - The color of the text (default: "black")
|
|
93
|
+
* @param size - The font size in pixels (default: "16px")
|
|
94
|
+
* @param align - The text alignment (default: "center")
|
|
95
|
+
* @param font - Optional font family to override the default font
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* // Draw centered black text with default font
|
|
99
|
+
* shapeDrawer.drawText("Hello", 100, 50);
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* // Draw right-aligned red text with custom font
|
|
103
|
+
* shapeDrawer.drawText("World", 100, 50, "red", "20px", "right", "Verdana");
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* // Draw left-aligned text with default font but custom size
|
|
107
|
+
* shapeDrawer.drawText("Welcome", 100, 50, "black", "12px", "left");
|
|
108
|
+
*/
|
|
109
|
+
drawText(text: string, x: number, y: number, color?: string, size?: string, align?: CanvasTextAlign, font?: string): void;
|
|
110
|
+
/**
|
|
111
|
+
* Clears the canvas and fills it with a solid background color.
|
|
112
|
+
*
|
|
113
|
+
* @param color - The color to fill the canvas with (default: "white")
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* // Clear canvas with default white background
|
|
117
|
+
* shapeDrawer.drawBackground();
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* // Clear canvas with custom gray background
|
|
121
|
+
* shapeDrawer.drawBackground("#f0f0f0");
|
|
122
|
+
*/
|
|
123
|
+
drawBackground(color?: string): void;
|
|
124
|
+
/**
|
|
125
|
+
* Executes drawing operations within a clipped rectangular region.
|
|
126
|
+
*
|
|
127
|
+
* This method saves the current canvas state, creates a clipping region,
|
|
128
|
+
* executes the provided drawing function, and then restores the original state.
|
|
129
|
+
*
|
|
130
|
+
* @param x - The x-coordinate of the clipping rectangle's top-left corner
|
|
131
|
+
* @param y - The y-coordinate of the clipping rectangle's top-left corner
|
|
132
|
+
* @param width - The width of the clipping rectangle
|
|
133
|
+
* @param height - The height of the clipping rectangle
|
|
134
|
+
* @param draw - A function containing the drawing operations to perform within the clipped region
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* // Draw shapes only within a specific region
|
|
138
|
+
* shapeDrawer.withClippingRect(50, 50, 200, 200, () => {
|
|
139
|
+
* shapeDrawer.drawCircle(100, 100, 50, "blue");
|
|
140
|
+
* shapeDrawer.drawText("Clipped", 100, 100);
|
|
141
|
+
* });
|
|
142
|
+
*/
|
|
143
|
+
withClippingRect(x: number, y: number, width: number, height: number, draw: () => void): void;
|
|
144
|
+
/**
|
|
145
|
+
* Executes drawing operations with a temporary rotation around a pivot point.
|
|
146
|
+
*
|
|
147
|
+
* This method saves the current canvas state, applies the rotation transform,
|
|
148
|
+
* executes the provided drawing function, and then restores the original state.
|
|
149
|
+
*
|
|
150
|
+
* @param x - The x-coordinate of the rotation pivot
|
|
151
|
+
* @param y - The y-coordinate of the rotation pivot
|
|
152
|
+
* @param angle - Rotation angle in radians
|
|
153
|
+
* @param draw - A function containing the drawing operations to perform while rotated
|
|
154
|
+
*/
|
|
155
|
+
withRotation(x: number, y: number, angle: number, draw: () => void): void;
|
|
156
|
+
/**
|
|
157
|
+
* Draws a sprite from the sprite library onto the canvas.
|
|
158
|
+
*
|
|
159
|
+
* @param spriteName - The name of the sprite to draw (must be loaded in the SpriteLibrary)
|
|
160
|
+
* @param x - The x-coordinate of the top-left corner where the sprite will be drawn
|
|
161
|
+
* @param y - The y-coordinate of the top-left corner where the sprite will be drawn
|
|
162
|
+
* @param width - The width to scale the sprite to
|
|
163
|
+
* @param height - The height to scale the sprite to
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* // Draw a sprite named "player" at position (100, 100) with size 50x50
|
|
167
|
+
* shapeDrawer.drawSprite("player", 100, 100, 50, 50);
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* // Draw a sprite with different dimensions
|
|
171
|
+
* shapeDrawer.drawSprite("enemy", 200, 200, 30, 40);
|
|
172
|
+
*/
|
|
173
|
+
drawSprite(spriteName: string, x: number, y: number, width: number, height: number): void;
|
|
174
|
+
/**
|
|
175
|
+
* Loads a font (local URL or Google Fonts stylesheet) and registers it for use.
|
|
176
|
+
*
|
|
177
|
+
* This method handles both local font files and Google Fonts URLs. For Google Fonts,
|
|
178
|
+
* it injects the stylesheet and waits for the font to be available. For local fonts,
|
|
179
|
+
* it creates a new FontFace object and adds it to the document's font collection.
|
|
180
|
+
*
|
|
181
|
+
* @param fontName - The name of the font to load (must match the font family name)
|
|
182
|
+
* @param url - The URL of the font file or Google Fonts stylesheet
|
|
183
|
+
* @returns A Promise that resolves when the font is successfully loaded and available
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* // Load a Google Font
|
|
187
|
+
* await shapeDrawer.loadFont("Roboto", "https://fonts.googleapis.com/css2?family=Roboto");
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* // Load a local font file
|
|
191
|
+
* await shapeDrawer.loadFont("CustomFont", "/fonts/custom-font.woff2");
|
|
192
|
+
*
|
|
193
|
+
* @throws Will throw an error if the font fails to load
|
|
194
|
+
*/
|
|
195
|
+
loadFont(fontName: string, url: string): Promise<void>;
|
|
196
|
+
}
|
|
197
|
+
declare class CanvasController {
|
|
198
|
+
private canvas;
|
|
199
|
+
private context;
|
|
200
|
+
private spriteLibrary;
|
|
201
|
+
private shapeDrawer;
|
|
202
|
+
constructor(width: number, height: number);
|
|
203
|
+
clearCanvas(): void;
|
|
204
|
+
getCanvas(): HTMLCanvasElement;
|
|
205
|
+
getContext(): CanvasRenderingContext2D;
|
|
206
|
+
getShapeDrawer(): ShapeDrawer;
|
|
207
|
+
getSpriteLibrary(): SpriteLibrary;
|
|
208
|
+
applyTranslation(translationVector: Vector): void;
|
|
209
|
+
reset(): void;
|
|
210
|
+
applyRotation(angle: number): void;
|
|
211
|
+
}
|
|
212
|
+
export * from "./Transformations";
|
|
213
|
+
export { CanvasController, ShapeDrawer, SpriteLibrary };
|
|
214
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|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