like2d 2.7.4 → 2.9.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.
Files changed (77) hide show
  1. package/README.md +34 -35
  2. package/dist/core/audio.d.ts +12 -9
  3. package/dist/core/audio.d.ts.map +1 -1
  4. package/dist/core/audio.js +7 -4
  5. package/dist/core/canvas.d.ts +58 -0
  6. package/dist/core/canvas.d.ts.map +1 -0
  7. package/dist/core/canvas.js +209 -0
  8. package/dist/core/events.d.ts +92 -7
  9. package/dist/core/events.d.ts.map +1 -1
  10. package/dist/core/events.js +20 -0
  11. package/dist/core/gamepad-mapping.d.ts +57 -18
  12. package/dist/core/gamepad-mapping.d.ts.map +1 -1
  13. package/dist/core/gamepad-mapping.js +23 -223
  14. package/dist/core/gamepad.d.ts +34 -58
  15. package/dist/core/gamepad.d.ts.map +1 -1
  16. package/dist/core/gamepad.js +79 -213
  17. package/dist/core/graphics.d.ts +175 -64
  18. package/dist/core/graphics.d.ts.map +1 -1
  19. package/dist/core/graphics.js +294 -198
  20. package/dist/core/input-state.d.ts +2 -2
  21. package/dist/core/input-state.d.ts.map +1 -1
  22. package/dist/core/input.d.ts +22 -15
  23. package/dist/core/input.d.ts.map +1 -1
  24. package/dist/core/input.js +25 -37
  25. package/dist/core/keyboard.d.ts +7 -7
  26. package/dist/core/keyboard.d.ts.map +1 -1
  27. package/dist/core/keyboard.js +24 -31
  28. package/dist/core/like.d.ts +98 -44
  29. package/dist/core/like.d.ts.map +1 -1
  30. package/dist/core/like.js +8 -0
  31. package/dist/core/mouse.d.ts +45 -22
  32. package/dist/core/mouse.d.ts.map +1 -1
  33. package/dist/core/mouse.js +90 -78
  34. package/dist/core/timer.d.ts +2 -5
  35. package/dist/core/timer.d.ts.map +1 -1
  36. package/dist/core/timer.js +2 -14
  37. package/dist/engine.d.ts +61 -11
  38. package/dist/engine.d.ts.map +1 -1
  39. package/dist/engine.js +119 -102
  40. package/dist/index.d.ts +42 -21
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +35 -14
  43. package/dist/math/index.d.ts +2 -0
  44. package/dist/math/index.d.ts.map +1 -0
  45. package/dist/math/index.js +1 -0
  46. package/dist/math/rect.d.ts +71 -0
  47. package/dist/math/rect.d.ts.map +1 -0
  48. package/dist/{core → math}/rect.js +8 -0
  49. package/dist/math/vector2.d.ts +78 -0
  50. package/dist/math/vector2.d.ts.map +1 -0
  51. package/dist/{core → math}/vector2.js +24 -0
  52. package/dist/prefab-scenes/index.d.ts +7 -0
  53. package/dist/prefab-scenes/index.d.ts.map +1 -0
  54. package/dist/prefab-scenes/index.js +6 -0
  55. package/dist/prefab-scenes/startScreen.d.ts +59 -0
  56. package/dist/prefab-scenes/startScreen.d.ts.map +1 -0
  57. package/dist/{scenes/startup.js → prefab-scenes/startScreen.js} +47 -7
  58. package/dist/scene.d.ts +103 -5
  59. package/dist/scene.d.ts.map +1 -1
  60. package/dist/scene.js +28 -1
  61. package/package.json +18 -2
  62. package/dist/core/canvas-config.d.ts +0 -22
  63. package/dist/core/canvas-config.d.ts.map +0 -1
  64. package/dist/core/canvas-config.js +0 -14
  65. package/dist/core/canvas-manager.d.ts +0 -25
  66. package/dist/core/canvas-manager.d.ts.map +0 -1
  67. package/dist/core/canvas-manager.js +0 -178
  68. package/dist/core/gamepad-buttons.d.ts +0 -23
  69. package/dist/core/gamepad-buttons.d.ts.map +0 -1
  70. package/dist/core/gamepad-buttons.js +0 -36
  71. package/dist/core/rect.d.ts +0 -26
  72. package/dist/core/rect.d.ts.map +0 -1
  73. package/dist/core/vector2.d.ts +0 -26
  74. package/dist/core/vector2.d.ts.map +0 -1
  75. package/dist/gamecontrollerdb.txt +0 -2221
  76. package/dist/scenes/startup.d.ts +0 -18
  77. package/dist/scenes/startup.d.ts.map +0 -1
@@ -1,237 +1,103 @@
1
- import { getGPName, GP } from './gamepad-buttons';
2
- import { InputStateTracker } from './input-state';
3
- import { gamepadMapping } from './gamepad-mapping';
4
- export { GP, getGPName };
5
- const AXIS_DEADZONE = 0.15;
6
- function applyDeadzone(value, deadzone = AXIS_DEADZONE) {
7
- if (Math.abs(value) < deadzone)
8
- return 0;
9
- const sign = value < 0 ? -1 : 1;
10
- const magnitude = Math.abs(value);
11
- return sign * (magnitude - deadzone) / (1 - deadzone);
12
- }
13
- function applyRadialDeadzone(x, y, deadzone = AXIS_DEADZONE) {
14
- const magnitude = Math.sqrt(x * x + y * y);
15
- if (magnitude < deadzone)
16
- return { x: 0, y: 0 };
17
- const scale = (magnitude - deadzone) / (magnitude * (1 - deadzone));
18
- return { x: x * scale, y: y * scale };
19
- }
20
- export class Gamepad {
21
- constructor() {
22
- Object.defineProperty(this, "buttonTrackers", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: new Map()
27
- });
28
- Object.defineProperty(this, "connectedGamepads", {
1
+ import { nameToNumber, numberToName } from './gamepad-mapping';
2
+ const newButtonTracker = () => ({ prev: new Set(), current: new Set() });
3
+ /** LIKE Gamepad Wrapper
4
+ *
5
+ * - Allows events/callbacks to be sent from joy buttons
6
+ * - Extends stateful API with justPressed
7
+ *
8
+ * # Examples
9
+ *
10
+ * ### Binding events
11
+ * ```ts
12
+ * like.gamepadpressed = (idx: number, _num: number, button: string) => {
13
+ * console.log(`Button ${button} pressed on controller ${idx}`);
14
+ * }
15
+ * ```
16
+ */
17
+ export class GamepadInternal {
18
+ constructor(dispatch) {
19
+ Object.defineProperty(this, "dispatch", {
29
20
  enumerable: true,
30
21
  configurable: true,
31
22
  writable: true,
32
- value: new Map()
23
+ value: dispatch
33
24
  });
34
- Object.defineProperty(this, "buttonMappings", {
25
+ Object.defineProperty(this, "buttonTrackers", {
35
26
  enumerable: true,
36
27
  configurable: true,
37
28
  writable: true,
38
29
  value: new Map()
39
30
  });
40
- Object.defineProperty(this, "onButtonEvent", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: void 0
45
- });
46
- Object.defineProperty(this, "onConnected", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: void 0
51
- });
52
- Object.defineProperty(this, "onDisconnected", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: void 0
57
- });
58
- // Event handler references for cleanup
59
- Object.defineProperty(this, "gamepadConnectedHandler", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: void 0
64
- });
65
- Object.defineProperty(this, "gamepadDisconnectedHandler", {
66
- enumerable: true,
67
- configurable: true,
68
- writable: true,
69
- value: void 0
70
- });
71
- Object.defineProperty(this, "blurHandler", {
31
+ Object.defineProperty(this, "abort", {
72
32
  enumerable: true,
73
33
  configurable: true,
74
34
  writable: true,
75
- value: void 0
35
+ value: new AbortController()
76
36
  });
77
- // Bind event handlers
78
- this.gamepadConnectedHandler = this.handleGamepadConnected.bind(this);
79
- this.gamepadDisconnectedHandler = this.handleGamepadDisconnected.bind(this);
80
- this.blurHandler = this.handleBlur.bind(this);
81
37
  // Register event listeners
82
- window.addEventListener('gamepadconnected', this.gamepadConnectedHandler);
83
- window.addEventListener('gamepaddisconnected', this.gamepadDisconnectedHandler);
84
- window.addEventListener('blur', this.blurHandler);
85
- }
86
- handleGamepadConnected(e) {
87
- this.onGamepadConnectedInternal(e.gamepad);
88
- this.onConnected?.(e.gamepad);
89
- }
90
- handleGamepadDisconnected(e) {
91
- this.onGamepadDisconnectedInternal(e.gamepad.index);
92
- this.onDisconnected?.(e.gamepad.index);
93
- }
94
- handleBlur() {
95
- for (const tracker of this.buttonTrackers.values()) {
96
- tracker.clear();
97
- }
98
- }
99
- setCallbacks(callbacks) {
100
- this.onConnected = callbacks.onConnected;
101
- this.onDisconnected = callbacks.onDisconnected;
102
- }
103
- dispose() {
104
- window.removeEventListener('gamepadconnected', this.gamepadConnectedHandler);
105
- window.removeEventListener('gamepaddisconnected', this.gamepadDisconnectedHandler);
106
- window.removeEventListener('blur', this.blurHandler);
107
- this.connectedGamepads.clear();
108
- this.buttonTrackers.clear();
109
- this.buttonMappings.clear();
110
- }
111
- async init() {
112
- await gamepadMapping.loadDatabase();
113
- }
114
- onGamepadConnectedInternal(gamepad) {
115
- this.connectedGamepads.set(gamepad.index, gamepad);
116
- this.buttonTrackers.set(gamepad.index, new InputStateTracker());
117
- const mapping = gamepadMapping.getMapping(gamepad);
118
- this.buttonMappings.set(gamepad.index, mapping);
119
- console.log(`[Gamepad] Connected: "${gamepad.id}"`);
120
- if (mapping.vendor !== null && mapping.product !== null) {
121
- console.log(`[Gamepad] Vendor: 0x${mapping.vendor.toString(16).padStart(4, '0')}, Product: 0x${mapping.product.toString(16).padStart(4, '0')}`);
122
- }
123
- const mappingType = gamepad.mapping === 'standard' ? 'browser standard' : (mapping.hasMapping ? 'SDL DB' : 'unmapped');
124
- console.log(`[Gamepad] Mapped as: "${mapping.controllerName}" (${mappingType})`);
125
- }
126
- onGamepadDisconnectedInternal(gamepadIndex) {
127
- this.connectedGamepads.delete(gamepadIndex);
128
- this.buttonTrackers.delete(gamepadIndex);
129
- this.buttonMappings.delete(gamepadIndex);
130
- }
131
- update() {
132
- const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
133
- for (let i = 0; i < gamepads.length; i++) {
134
- const gamepad = gamepads[i];
135
- if (gamepad) {
136
- this.connectedGamepads.set(i, gamepad);
137
- let tracker = this.buttonTrackers.get(i);
138
- if (!tracker) {
139
- tracker = new InputStateTracker();
140
- this.buttonTrackers.set(i, tracker);
141
- }
142
- // Get or update the button mapping for this gamepad
143
- let mapping = this.buttonMappings.get(i);
144
- if (!mapping) {
145
- mapping = gamepadMapping.getMapping(gamepad);
146
- this.buttonMappings.set(i, mapping);
147
- }
148
- const pressedButtons = new Set();
149
- for (let j = 0; j < gamepad.buttons.length; j++) {
150
- if (gamepad.buttons[j].pressed) {
151
- // Map the raw button index to standard button index
152
- const standardIndex = mapping.toStandard.get(j);
153
- if (standardIndex !== undefined) {
154
- pressedButtons.add(standardIndex);
155
- }
156
- }
157
- }
158
- const changes = tracker.update(pressedButtons);
159
- for (const buttonIndex of changes.justPressed) {
160
- this.onButtonEvent?.(i, buttonIndex, getGPName(buttonIndex), true);
161
- }
162
- for (const buttonIndex of changes.justReleased) {
163
- this.onButtonEvent?.(i, buttonIndex, getGPName(buttonIndex), false);
164
- }
38
+ window.addEventListener("gamepadconnected", (ev) => {
39
+ if (ev.gamepad.mapping == "standard") {
40
+ console.log(`[Gamepad] Connected standard gamepad ${ev.gamepad.id}.`);
165
41
  }
166
- }
167
- }
168
- isConnected(gamepadIndex) {
169
- return this.connectedGamepads.has(gamepadIndex);
42
+ else {
43
+ console.log(`[Gamepad] Connected non-standard gamepad ${ev.gamepad.id}. Consider remapping.`);
44
+ }
45
+ console.log(`[Gamepad] buttons: ${ev.gamepad.buttons.length}, axes: ${ev.gamepad.axes.length}`);
46
+ this.buttonTrackers.set(ev.gamepad.index, newButtonTracker());
47
+ }, { signal: this.abort.signal });
48
+ window.addEventListener("gamepaddisconnected", (ev) => {
49
+ console.log(`[Gamepad] Disconnected ${ev.gamepad.id}`);
50
+ this.buttonTrackers.delete(ev.gamepad.index);
51
+ }, { signal: this.abort.signal });
52
+ window.addEventListener("blur", () => {
53
+ this.buttonTrackers.forEach(({ current, prev }) => { current.clear(); prev.clear(); });
54
+ }, { signal: this.abort.signal });
55
+ }
56
+ _update() {
57
+ const gamepads = navigator.getGamepads().filter((v) => v !== null);
58
+ gamepads.forEach((domGp, gpIndex) => {
59
+ const tracker = this.buttonTrackers.get(gpIndex);
60
+ if (!tracker)
61
+ return;
62
+ const prev = tracker.current;
63
+ tracker.current = new Set(domGp.buttons
64
+ .map((btn, i) => (btn.pressed ? i : null))
65
+ .filter((v) => v != null));
66
+ domGp.buttons.forEach((_, i) => {
67
+ if (i > 15)
68
+ return;
69
+ if (tracker.prev.has(i) != tracker.current.has(i)) {
70
+ this.dispatch(tracker.prev.has(i) ? "gamepadreleased" : "gamepadpressed", [gpIndex, i, GamepadInternal.getButtonName(i)]);
71
+ }
72
+ });
73
+ tracker.prev = prev;
74
+ });
170
75
  }
171
- /**
172
- * Check if a button is currently pressed on a specific gamepad
173
- * Uses mapped button indices (standard layout)
174
- */
175
- isButtonDown(gamepadIndex, buttonIndex) {
176
- const tracker = this.buttonTrackers.get(gamepadIndex);
177
- return tracker ? tracker.isDown(buttonIndex) : false;
76
+ isButtonDown(target, buttonRaw) {
77
+ const btn = GamepadInternal.getButtonNumber(buttonRaw);
78
+ return this.buttonTrackers.get(target)?.current.has(btn);
178
79
  }
179
- isButtonDownOnAny(buttonIndex) {
180
- for (const tracker of this.buttonTrackers.values()) {
181
- if (tracker.isDown(buttonIndex))
182
- return true;
80
+ isButtonJustPressed(target, buttonRaw) {
81
+ const btn = GamepadInternal.getButtonNumber(buttonRaw);
82
+ const bt = this.buttonTrackers.get(target);
83
+ if (bt) {
84
+ return !bt.prev.has(btn) && bt.current.has(btn);
183
85
  }
184
- return false;
185
- }
186
- getPressedButtons(gamepadIndex) {
187
- const tracker = this.buttonTrackers.get(gamepadIndex);
188
- return tracker ? tracker.getCurrentState() : new Set();
189
- }
190
- getConnectedGamepads() {
191
- return Array.from(this.connectedGamepads.keys());
192
- }
193
- /**
194
- * Get the raw Gamepad object for a specific index
195
- */
196
- getGamepad(gamepadIndex) {
197
- return this.connectedGamepads.get(gamepadIndex);
198
- }
199
- /**
200
- * Get the button mapping for a specific gamepad
201
- */
202
- getButtonMapping(gamepadIndex) {
203
- return this.buttonMappings.get(gamepadIndex);
204
- }
205
- /**
206
- * Check if a gamepad has a known mapping from the database
207
- */
208
- hasMapping(gamepadIndex) {
209
- const mapping = this.buttonMappings.get(gamepadIndex);
210
- return mapping?.hasMapping ?? false;
211
86
  }
212
- /**
213
- * Get the controller name for a specific gamepad
214
- */
215
- getControllerName(gamepadIndex) {
216
- const mapping = this.buttonMappings.get(gamepadIndex);
217
- return mapping?.controllerName;
87
+ getGamepad(target) {
88
+ return navigator.getGamepads()?.[target] ?? null;
218
89
  }
219
- getAxis(gamepadIndex, axisIndex) {
220
- const gamepad = this.connectedGamepads.get(gamepadIndex);
221
- if (!gamepad || axisIndex < 0 || axisIndex >= gamepad.axes.length)
222
- return 0;
223
- return applyDeadzone(gamepad.axes[axisIndex]);
90
+ _dispose() {
91
+ this.abort.abort();
224
92
  }
225
- getLeftStick(gamepadIndex) {
226
- const gamepad = this.connectedGamepads.get(gamepadIndex);
227
- if (!gamepad || gamepad.axes.length < 2)
228
- return { x: 0, y: 0 };
229
- return applyRadialDeadzone(gamepad.axes[0], gamepad.axes[1]);
93
+ static getButtonName(button) {
94
+ return typeof button == "number"
95
+ ? (numberToName.get(button) ?? `Button${button}`)
96
+ : button;
230
97
  }
231
- getRightStick(gamepadIndex) {
232
- const gamepad = this.connectedGamepads.get(gamepadIndex);
233
- if (!gamepad || gamepad.axes.length < 4)
234
- return { x: 0, y: 0 };
235
- return applyRadialDeadzone(gamepad.axes[2], gamepad.axes[3]);
98
+ static getButtonNumber(button) {
99
+ return typeof button == "number"
100
+ ? button
101
+ : (nameToNumber.get(button) ?? Number(button.substring(6)));
236
102
  }
237
103
  }
@@ -1,22 +1,48 @@
1
- import type { Vector2 } from './vector2';
2
- import type { Rect } from './rect';
3
- type DrawMode = 'fill' | 'line';
1
+ /**
2
+ * @module graphics
3
+ * @description a reduced-state, Love2D-like wrapper around browser canvas
4
+ *
5
+ * # Graphics Module
6
+ *
7
+ * A wrapper around browser Canvas.
8
+ * In standard usage `like.gfx` gives a {@link BoundGraphics} object with a canvas already on it.
9
+ * So, you can for example call `like.gfx.rectangle('fill', 'green', [10, 10, 30, 30])`
10
+ *
11
+ * ## State Isolation
12
+ * Each drawing operation resets relevant canvas state before executing:
13
+ * - Stroke properties (`lineWidth`, `lineCap`, `lineJoin`, `miterLimit`) are always set to defaults first
14
+ * - No state leakage between drawing calls
15
+ *
16
+ * ## Predicable Parameter Ordering
17
+ * - No clunky argument overrides that could affect positionality.
18
+ * - **Required arguments** come first as positional parameters
19
+ * - **Optional arguments** are grouped in a trailing `props` object
20
+ * - **Mode** `'fill' | 'line'` is the first arg if relevent.
21
+ * - **Color** then {@link Color}, if relevant -- there is no `setColor`.
22
+ *
23
+ * ## Note: Coordinate System is unchanged from native Canvas.
24
+ * - Origin (0, 0) at top-left
25
+ * - X increases right
26
+ * - Y increases down
27
+ * - Angles in radians, 0 is right, positive is clockwise
28
+ */
29
+ import type { Vector2 } from "../math/vector2";
30
+ import type { Rectangle } from "../math/rect";
31
+ type DrawMode = "fill" | "line";
32
+ /**
33
+ * - RGBA array with values 0-1: `[r, g, b, a]`
34
+ * - Alpha defaults to 1 if omitted
35
+ * - CSS color strings also accepted: `"red"`, `"#ff0000"`, `"rgb(255,0,0)"`
36
+ */
4
37
  export type Color = [number, number, number, number?] | string;
5
- export type Quad = Rect;
6
- export type { Vector2, Rect };
7
- export type Canvas = {
8
- size: Vector2;
9
- element: HTMLCanvasElement;
10
- ctx: CanvasRenderingContext2D;
11
- };
12
38
  export type ShapeProps = {
13
39
  lineWidth?: number;
14
- lineCap?: 'butt' | 'round' | 'square';
15
- lineJoin?: 'bevel' | 'miter' | 'round';
40
+ lineCap?: CanvasLineCap;
41
+ lineJoin?: CanvasLineJoin;
16
42
  miterLimit?: number;
17
43
  };
18
44
  export type DrawProps = ShapeProps & {
19
- quad?: Quad;
45
+ quad?: Rectangle;
20
46
  r?: number;
21
47
  scale?: number | Vector2;
22
48
  origin?: number | Vector2;
@@ -24,12 +50,7 @@ export type DrawProps = ShapeProps & {
24
50
  export type PrintProps = {
25
51
  font?: string;
26
52
  limit?: number;
27
- align?: 'left' | 'center' | 'right';
28
- };
29
- export type GraphicsState = {
30
- screenCtx: CanvasRenderingContext2D;
31
- currentCtx: CanvasRenderingContext2D;
32
- canvases: Map<Canvas, true>;
53
+ align?: CanvasTextAlign;
33
54
  };
34
55
  export declare class ImageHandle {
35
56
  readonly path: string;
@@ -42,51 +63,141 @@ export declare class ImageHandle {
42
63
  get size(): Vector2;
43
64
  getElement(): HTMLImageElement | null;
44
65
  }
45
- export declare function newState(ctx: CanvasRenderingContext2D): GraphicsState;
46
- export declare function clear(s: GraphicsState, color?: Color): void;
47
- export declare function rectangle(s: GraphicsState, mode: DrawMode, color: Color, rect: Rect, props?: ShapeProps): void;
48
- export declare function circle(s: GraphicsState, mode: DrawMode, color: Color, position: Vector2, radii: number | Vector2, props?: ShapeProps & {
49
- angle?: number;
50
- arc?: [number, number];
51
- }): void;
52
- export declare function line(s: GraphicsState, color: Color, points: Vector2[], props?: ShapeProps): void;
53
- export declare function print(s: GraphicsState, color: Color, text: string, position: Vector2, props?: PrintProps): void;
54
- export declare function drawImage(s: GraphicsState, handle: ImageHandle, position: Vector2, props?: DrawProps): void;
55
- export declare function getCanvasSize(s: GraphicsState): Vector2;
56
- export declare function newImage(_s: GraphicsState, path: string): ImageHandle;
57
- export declare function newCanvas(s: GraphicsState, size: Vector2): Canvas;
58
- export declare function setCanvas(s: GraphicsState, canvas?: Canvas | null): void;
59
- export declare function clip(s: GraphicsState, rect?: Rect): void;
60
- export declare function polygon(s: GraphicsState, mode: DrawMode, color: Color, points: Vector2[], props?: ShapeProps): void;
61
- export declare function points(s: GraphicsState, color: Color, pts: Vector2[]): void;
62
- export declare function push(s: GraphicsState): void;
63
- export declare function pop(s: GraphicsState): void;
64
- export declare function translate(s: GraphicsState, offset: Vector2): void;
65
- export declare function rotate(s: GraphicsState, angle: number): void;
66
- export declare function scale(s: GraphicsState, factor: number | Vector2): void;
67
- type Bind<F> = F extends (s: GraphicsState, ...args: infer A) => infer R ? (...args: A) => R : never;
68
- export type BoundGraphics = {
69
- [K in keyof typeof graphicsFns]: Bind<(typeof graphicsFns)[K]>;
66
+ /**
67
+ * A ready-made pure module for drawing to non-LIKE canvases.
68
+ *
69
+ * Acts as the core of the graphics system.
70
+ *
71
+ * import { pure as gfx } from "like/internal/graphics"
72
+ * gfx.clear(my2dContext, "red");
73
+ */
74
+ export declare const pure: {
75
+ /**
76
+ * Clears the canvas with a solid color.
77
+ * @param ctx Canvas context.
78
+ * @param color Fill color.
79
+ */
80
+ clear(ctx: CanvasRenderingContext2D, color?: Color): void;
81
+ /**
82
+ * Draws a rectangle.
83
+ * @param ctx Canvas context.
84
+ * @param mode Fill or line.
85
+ * @param color Fill or stroke color.
86
+ * @param rect Rectangle [x, y, w, h].
87
+ * @param props Optional stroke properties.
88
+ */
89
+ rectangle(ctx: CanvasRenderingContext2D, mode: DrawMode, color: Color, rect: Rectangle, props?: ShapeProps): void;
90
+ /**
91
+ * Draws a circle or ellipse.
92
+ * @param ctx Canvas context.
93
+ * @param mode Fill or line.
94
+ * @param color Fill or stroke color.
95
+ * @param position Center position.
96
+ * @param radii Radius (number) or [rx, ry] for ellipse.
97
+ * @param props Optional angle, arc, or stroke properties.
98
+ */
99
+ circle(ctx: CanvasRenderingContext2D, mode: DrawMode, color: Color, position: Vector2, radii: number | Vector2, props?: ShapeProps & {
100
+ angle?: number;
101
+ arc?: [number, number];
102
+ center: boolean;
103
+ }): void;
104
+ /**
105
+ * Draws connected line segments.
106
+ * @param ctx Canvas context.
107
+ * @param color Stroke color.
108
+ * @param points Array of [x, y] positions.
109
+ * @param props Optional stroke properties.
110
+ */
111
+ line(ctx: CanvasRenderingContext2D, color: Color, points: Vector2[], props?: ShapeProps): void;
112
+ /**
113
+ * Draws text at position.
114
+ * @param ctx Canvas context.
115
+ * @param color Fill color.
116
+ * @param text Text string.
117
+ * @param position Top-left position.
118
+ * @param props Optional font, text limit, or alignment.
119
+ */
120
+ print(ctx: CanvasRenderingContext2D, color: Color, text: string, position: Vector2, props?: PrintProps): void;
121
+ /**
122
+ * Draws an image.
123
+ *
124
+ * @remarks named "draw" because it draws anything _drawable_
125
+ * in the long run.
126
+ *
127
+ * @param ctx Canvas context.
128
+ * @param handle Image handle from newImage.
129
+ * @param position Draw position.
130
+ * @param props Optional rotation, scale, origin, or quad.
131
+ */
132
+ draw(ctx: CanvasRenderingContext2D, handle: ImageHandle, position: Vector2, props?: DrawProps): void;
133
+ /**
134
+ * Loads an image from a path.
135
+ * Unlike built-in loading, this pretends to be synchronous.
136
+ * @param ctx Canvas context.
137
+ * @param path Image file path.
138
+ * @returns ImageHandle for use with draw.
139
+ */
140
+ newImage(_ctx: CanvasRenderingContext2D, path: string): ImageHandle;
141
+ /**
142
+ * Sets the clipping region.
143
+ * @param ctx Canvas context.
144
+ * @param rect Clipping rectangle, or full canvas if omitted.
145
+ */
146
+ clip(ctx: CanvasRenderingContext2D, rect?: Rectangle): void;
147
+ /**
148
+ * Draws a polygon.
149
+ * @param ctx Canvas context.
150
+ * @param mode Fill or line.
151
+ * @param color Fill or stroke color.
152
+ * @param points Array of [x, y] vertices.
153
+ * @param props Optional stroke properties.
154
+ */
155
+ polygon(ctx: CanvasRenderingContext2D, mode: DrawMode, color: Color, points: Vector2[], props?: ShapeProps): void;
156
+ /**
157
+ * Draws individual pixels.
158
+ * @param ctx Canvas context.
159
+ * @param color Fill color.
160
+ * @param pts Array of [x, y] positions.
161
+ */
162
+ points(ctx: CanvasRenderingContext2D, color: Color, pts: Vector2[]): void;
163
+ /**
164
+ * Saves canvas state.
165
+ * @param ctx Canvas context.
166
+ */
167
+ push(ctx: CanvasRenderingContext2D): void;
168
+ /**
169
+ * Restores canvas state.
170
+ * @param ctx Canvas context.
171
+ */
172
+ pop(ctx: CanvasRenderingContext2D): void;
173
+ /**
174
+ * Applies a translation.
175
+ * @param ctx Canvas context.
176
+ * @param offset [x, y] offset.
177
+ */
178
+ translate(ctx: CanvasRenderingContext2D, offset: Vector2): void;
179
+ /**
180
+ * Applies a rotation.
181
+ * @param ctx Canvas context.
182
+ * @param angle Rotation in radians.
183
+ */
184
+ rotate(ctx: CanvasRenderingContext2D, angle: number): void;
185
+ /**
186
+ * Applies a scale.
187
+ * @param ctx Canvas context.
188
+ * @param factor Scale factor (number or [x, y]).
189
+ */
190
+ scale(ctx: CanvasRenderingContext2D, factor: number | Vector2): void;
70
191
  };
71
- declare const graphicsFns: {
72
- readonly clear: typeof clear;
73
- readonly rectangle: typeof rectangle;
74
- readonly circle: typeof circle;
75
- readonly line: typeof line;
76
- readonly print: typeof print;
77
- readonly draw: typeof drawImage;
78
- readonly getCanvasSize: typeof getCanvasSize;
79
- readonly newCanvas: typeof newCanvas;
80
- readonly setCanvas: typeof setCanvas;
81
- readonly clip: typeof clip;
82
- readonly polygon: typeof polygon;
83
- readonly points: typeof points;
84
- readonly newImage: typeof newImage;
85
- readonly push: typeof push;
86
- readonly pop: typeof pop;
87
- readonly translate: typeof translate;
88
- readonly rotate: typeof rotate;
89
- readonly scale: typeof scale;
192
+ type Bind<F> = F extends (ctx: CanvasRenderingContext2D, ...args: infer A) => infer R ? (...args: A) => R : never;
193
+ /**
194
+ * A graphics object with a canvas already attatched to it.
195
+ * Calling its methods will draw to the render canvas.
196
+ * See {@link graphics} for more info.
197
+ */
198
+ export type BoundGraphics = {
199
+ [K in keyof typeof pure]: Bind<(typeof pure)[K]>;
90
200
  };
91
- export declare function bindGraphics(s: GraphicsState): BoundGraphics;
201
+ export declare function bindGraphics(ctx: CanvasRenderingContext2D): BoundGraphics;
202
+ export {};
92
203
  //# sourceMappingURL=graphics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"graphics.d.ts","sourceRoot":"","sources":["../../src/core/graphics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,KAAK,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAEhC,MAAM,MAAM,KAAK,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC;AAC/D,MAAM,MAAM,IAAI,GAAG,IAAI,CAAC;AAExB,YAAY,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAE9B,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,iBAAiB,CAAC;IAC3B,GAAG,EAA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