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.
- package/README.md +34 -35
- package/dist/core/audio.d.ts +12 -9
- package/dist/core/audio.d.ts.map +1 -1
- package/dist/core/audio.js +7 -4
- package/dist/core/canvas.d.ts +58 -0
- package/dist/core/canvas.d.ts.map +1 -0
- package/dist/core/canvas.js +209 -0
- package/dist/core/events.d.ts +92 -7
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/events.js +20 -0
- package/dist/core/gamepad-mapping.d.ts +57 -18
- package/dist/core/gamepad-mapping.d.ts.map +1 -1
- package/dist/core/gamepad-mapping.js +23 -223
- package/dist/core/gamepad.d.ts +34 -58
- package/dist/core/gamepad.d.ts.map +1 -1
- package/dist/core/gamepad.js +79 -213
- package/dist/core/graphics.d.ts +175 -64
- package/dist/core/graphics.d.ts.map +1 -1
- package/dist/core/graphics.js +294 -198
- package/dist/core/input-state.d.ts +2 -2
- package/dist/core/input-state.d.ts.map +1 -1
- package/dist/core/input.d.ts +22 -15
- package/dist/core/input.d.ts.map +1 -1
- package/dist/core/input.js +25 -37
- package/dist/core/keyboard.d.ts +7 -7
- package/dist/core/keyboard.d.ts.map +1 -1
- package/dist/core/keyboard.js +24 -31
- package/dist/core/like.d.ts +98 -44
- package/dist/core/like.d.ts.map +1 -1
- package/dist/core/like.js +8 -0
- package/dist/core/mouse.d.ts +45 -22
- package/dist/core/mouse.d.ts.map +1 -1
- package/dist/core/mouse.js +90 -78
- package/dist/core/timer.d.ts +2 -5
- package/dist/core/timer.d.ts.map +1 -1
- package/dist/core/timer.js +2 -14
- package/dist/engine.d.ts +61 -11
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +119 -102
- package/dist/index.d.ts +42 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +35 -14
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +1 -0
- package/dist/math/rect.d.ts +71 -0
- package/dist/math/rect.d.ts.map +1 -0
- package/dist/{core → math}/rect.js +8 -0
- package/dist/math/vector2.d.ts +78 -0
- package/dist/math/vector2.d.ts.map +1 -0
- package/dist/{core → math}/vector2.js +24 -0
- package/dist/prefab-scenes/index.d.ts +7 -0
- package/dist/prefab-scenes/index.d.ts.map +1 -0
- package/dist/prefab-scenes/index.js +6 -0
- package/dist/prefab-scenes/startScreen.d.ts +59 -0
- package/dist/prefab-scenes/startScreen.d.ts.map +1 -0
- package/dist/{scenes/startup.js → prefab-scenes/startScreen.js} +47 -7
- package/dist/scene.d.ts +103 -5
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +28 -1
- package/package.json +18 -2
- package/dist/core/canvas-config.d.ts +0 -22
- package/dist/core/canvas-config.d.ts.map +0 -1
- package/dist/core/canvas-config.js +0 -14
- package/dist/core/canvas-manager.d.ts +0 -25
- package/dist/core/canvas-manager.d.ts.map +0 -1
- package/dist/core/canvas-manager.js +0 -178
- package/dist/core/gamepad-buttons.d.ts +0 -23
- package/dist/core/gamepad-buttons.d.ts.map +0 -1
- package/dist/core/gamepad-buttons.js +0 -36
- package/dist/core/rect.d.ts +0 -26
- package/dist/core/rect.d.ts.map +0 -1
- package/dist/core/vector2.d.ts +0 -26
- package/dist/core/vector2.d.ts.map +0 -1
- package/dist/gamecontrollerdb.txt +0 -2221
- package/dist/scenes/startup.d.ts +0 -18
- package/dist/scenes/startup.d.ts.map +0 -1
package/dist/core/gamepad.js
CHANGED
|
@@ -1,237 +1,103 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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:
|
|
23
|
+
value: dispatch
|
|
33
24
|
});
|
|
34
|
-
Object.defineProperty(this, "
|
|
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, "
|
|
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:
|
|
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(
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
169
|
-
|
|
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
|
-
|
|
173
|
-
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
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
|
-
|
|
220
|
-
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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
|
}
|
package/dist/core/graphics.d.ts
CHANGED
|
@@ -1,22 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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?:
|
|
15
|
-
lineJoin?:
|
|
40
|
+
lineCap?: CanvasLineCap;
|
|
41
|
+
lineJoin?: CanvasLineJoin;
|
|
16
42
|
miterLimit?: number;
|
|
17
43
|
};
|
|
18
44
|
export type DrawProps = ShapeProps & {
|
|
19
|
-
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?:
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
export declare
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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(
|
|
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,
|
|
1
|
+
{"version":3,"file":"graphics.d.ts","sourceRoot":"","sources":["../../src/core/graphics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,KAAK,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAEhC;;;;GAIG;AACH,MAAM,MAAM,KAAK,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC;AAE/D,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG;IACnC,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,CAAC,CAAC,EAAE,MA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