like2d 2.10.2 → 2.11.1
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 +6 -4
- package/dist/audio/index.d.ts +1 -1
- package/dist/audio/index.d.ts.map +1 -1
- package/dist/engine.d.ts +5 -10
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +5 -14
- package/dist/events.d.ts +38 -21
- package/dist/events.d.ts.map +1 -1
- package/dist/graphics/canvas.d.ts +12 -4
- package/dist/graphics/canvas.d.ts.map +1 -1
- package/dist/graphics/canvas.js +13 -5
- package/dist/graphics/{drawing.d.ts → graphics.d.ts} +71 -50
- package/dist/graphics/graphics.d.ts.map +1 -0
- package/dist/graphics/{drawing.js → graphics.js} +156 -168
- package/dist/graphics/image.d.ts +22 -0
- package/dist/graphics/image.d.ts.map +1 -0
- package/dist/graphics/image.js +62 -0
- package/dist/graphics/index.d.ts +6 -15
- package/dist/graphics/index.d.ts.map +1 -1
- package/dist/graphics/index.js +2 -10
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -2
- package/dist/input/gamepad-mapping.d.ts +4 -4
- package/dist/input/gamepad-mapping.d.ts.map +1 -1
- package/dist/input/gamepad-mapping.js +4 -4
- package/dist/input/gamepad.d.ts +11 -1
- package/dist/input/gamepad.d.ts.map +1 -1
- package/dist/input/gamepad.js +11 -1
- package/dist/input/index.d.ts +13 -5
- package/dist/input/index.d.ts.map +1 -1
- package/dist/input/index.js +5 -1
- package/dist/input/input.d.ts +56 -5
- package/dist/input/input.d.ts.map +1 -1
- package/dist/input/input.js +61 -7
- package/dist/input/keyboard.d.ts +26 -3
- package/dist/input/keyboard.d.ts.map +1 -1
- package/dist/input/keyboard.js +24 -0
- package/dist/input/mouse.d.ts +34 -6
- package/dist/input/mouse.d.ts.map +1 -1
- package/dist/input/mouse.js +33 -4
- package/dist/like.d.ts +9 -9
- package/dist/like.d.ts.map +1 -1
- package/dist/like.js +0 -4
- package/dist/math/index.d.ts +8 -9
- package/dist/math/index.d.ts.map +1 -1
- package/dist/math/index.js +8 -9
- package/dist/math/vector2.d.ts +1 -0
- package/dist/math/vector2.d.ts.map +1 -1
- package/dist/prefab-scenes/index.d.ts +5 -3
- package/dist/prefab-scenes/index.d.ts.map +1 -1
- package/dist/prefab-scenes/index.js +4 -2
- package/dist/prefab-scenes/mapGamepad.d.ts +13 -0
- package/dist/prefab-scenes/mapGamepad.d.ts.map +1 -1
- package/dist/prefab-scenes/mapGamepad.js +13 -0
- package/dist/scene.d.ts +1 -6
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +0 -5
- package/dist/timer/timer.d.ts.map +1 -1
- package/dist/timer/timer.js +1 -2
- package/package.json +1 -2
- package/dist/graphics/drawing.d.ts.map +0 -1
|
@@ -27,59 +27,7 @@
|
|
|
27
27
|
* - Angles in radians, 0 is right, positive is clockwise
|
|
28
28
|
*/
|
|
29
29
|
import { Vec2 } from "../math/vector2";
|
|
30
|
-
|
|
31
|
-
constructor(path) {
|
|
32
|
-
Object.defineProperty(this, "path", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
writable: true,
|
|
36
|
-
value: void 0
|
|
37
|
-
});
|
|
38
|
-
Object.defineProperty(this, "element", {
|
|
39
|
-
enumerable: true,
|
|
40
|
-
configurable: true,
|
|
41
|
-
writable: true,
|
|
42
|
-
value: null
|
|
43
|
-
});
|
|
44
|
-
Object.defineProperty(this, "loadPromise", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
configurable: true,
|
|
47
|
-
writable: true,
|
|
48
|
-
value: void 0
|
|
49
|
-
});
|
|
50
|
-
Object.defineProperty(this, "isLoaded", {
|
|
51
|
-
enumerable: true,
|
|
52
|
-
configurable: true,
|
|
53
|
-
writable: true,
|
|
54
|
-
value: false
|
|
55
|
-
});
|
|
56
|
-
this.path = path;
|
|
57
|
-
this.loadPromise = new Promise((resolve, reject) => {
|
|
58
|
-
const img = new Image();
|
|
59
|
-
img.onload = () => {
|
|
60
|
-
this.element = img;
|
|
61
|
-
this.isLoaded = true;
|
|
62
|
-
resolve();
|
|
63
|
-
};
|
|
64
|
-
img.onerror = () => {
|
|
65
|
-
reject(new Error(`Failed to load image: ${path}`));
|
|
66
|
-
};
|
|
67
|
-
img.src = path;
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
isReady() {
|
|
71
|
-
return this.isLoaded;
|
|
72
|
-
}
|
|
73
|
-
ready() {
|
|
74
|
-
return this.loadPromise;
|
|
75
|
-
}
|
|
76
|
-
get size() {
|
|
77
|
-
return [this.element?.width ?? 0, this.element?.height ?? 0];
|
|
78
|
-
}
|
|
79
|
-
getElement() {
|
|
80
|
-
return this.element;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
30
|
+
import { ImageHandle } from "./image";
|
|
83
31
|
function parseColor(color) {
|
|
84
32
|
if (typeof color === "string")
|
|
85
33
|
return color;
|
|
@@ -117,57 +65,96 @@ function getFontHeight(ctx) {
|
|
|
117
65
|
return match ? parseInt(match[1]) : 16;
|
|
118
66
|
}
|
|
119
67
|
/**
|
|
120
|
-
*
|
|
121
|
-
* bound to the first arg.
|
|
68
|
+
* LIKE's way of drawing to the screen.
|
|
122
69
|
*
|
|
123
|
-
*
|
|
70
|
+
* More specifically: a system for wrapping canvas draw calls conveniently.
|
|
124
71
|
*
|
|
72
|
+
* - Reduces state in calls -- no `setColor`, etc. Everything is passed in.
|
|
73
|
+
* - Abstracts away common drawing operations.
|
|
74
|
+
*
|
|
75
|
+
* ### Examples
|
|
76
|
+
* Draw a spinning symbol in the center of the screen using transforms.
|
|
125
77
|
* ```ts
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
78
|
+
* function drawSpinningYinYang(like: Like) {
|
|
79
|
+
* const color1 = "black";
|
|
80
|
+
* const color2 = "white";
|
|
81
|
+
* const size = 50;
|
|
82
|
+
* const pos = Vec2.div(like.canvas.getSize(), 2); // calc center of screen
|
|
83
|
+
* const speed = 0.5; // rotations per second
|
|
84
|
+
*
|
|
85
|
+
* like.gfx.push();
|
|
86
|
+
* like.gfx.translate(pos);
|
|
87
|
+
* like.gfx.rotate(like.timer.getTime() * Math.PI * 2.0 * speed);
|
|
88
|
+
* like.gfx.scale(size);
|
|
89
|
+
* like.gfx.circle("fill", color1, [0, 0], 2);
|
|
90
|
+
* // use the arc parameter to fill in a semicircle. Note that it's clockwise from {x:1, y:0}.
|
|
91
|
+
* like.gfx.circle("fill", color2, [0, 0], 2, { arc: [Math.PI/2, Math.PI*3/2] });
|
|
92
|
+
* like.gfx.circle("fill", color2, [0, -1], 1);
|
|
93
|
+
* like.gfx.circle("fill", color1, [0, 1], 1);
|
|
94
|
+
* like.gfx.circle("fill", color2, [0, 1], 1/3);
|
|
95
|
+
* like.gfx.circle("fill", color1, [0, -1], 1/3);
|
|
96
|
+
* like.gfx.pop();
|
|
97
|
+
* }
|
|
129
98
|
*
|
|
99
|
+
* like.draw = (like: Like) => {
|
|
100
|
+
* drawSpinningYinYang(like);
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
130
103
|
*/
|
|
131
|
-
export
|
|
104
|
+
export class Graphics {
|
|
105
|
+
constructor(ctx) {
|
|
106
|
+
Object.defineProperty(this, "ctx", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true,
|
|
109
|
+
writable: true,
|
|
110
|
+
value: ctx
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Set the 2d drawing context for graphics.
|
|
115
|
+
*
|
|
116
|
+
* Be aware that that `like` can set this value at any time.
|
|
117
|
+
*/
|
|
118
|
+
setContext(ctx) {
|
|
119
|
+
this.ctx = ctx;
|
|
120
|
+
}
|
|
132
121
|
/**
|
|
133
122
|
* Clears the canvas with a solid color.
|
|
134
|
-
* @param ctx Canvas context.
|
|
135
123
|
* @param color Fill color.
|
|
136
124
|
*/
|
|
137
|
-
clear(
|
|
138
|
-
ctx.fillStyle = parseColor(color);
|
|
139
|
-
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
140
|
-
}
|
|
125
|
+
clear(color = [0, 0, 0, 1]) {
|
|
126
|
+
this.ctx.fillStyle = parseColor(color);
|
|
127
|
+
this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
|
128
|
+
}
|
|
141
129
|
/**
|
|
142
130
|
* Draws a rectangle.
|
|
143
|
-
* @param ctx Canvas context.
|
|
144
131
|
* @param mode Fill or line.
|
|
145
132
|
* @param color Fill or stroke color.
|
|
146
133
|
* @param rect Rectangle [x, y, w, h].
|
|
147
134
|
* @param props Optional stroke properties.
|
|
148
135
|
*/
|
|
149
|
-
rectangle(
|
|
136
|
+
rectangle(mode, color, rect, props) {
|
|
150
137
|
const c = applyColor(color);
|
|
151
138
|
if (mode === "fill") {
|
|
152
|
-
ctx.fillStyle = c;
|
|
153
|
-
ctx.fillRect(...rect);
|
|
139
|
+
this.ctx.fillStyle = c;
|
|
140
|
+
this.ctx.fillRect(...rect);
|
|
154
141
|
}
|
|
155
142
|
else {
|
|
156
|
-
setStrokeProps(ctx, props);
|
|
157
|
-
ctx.strokeStyle = c;
|
|
158
|
-
ctx.strokeRect(...rect);
|
|
143
|
+
setStrokeProps(this.ctx, props);
|
|
144
|
+
this.ctx.strokeStyle = c;
|
|
145
|
+
this.ctx.strokeRect(...rect);
|
|
159
146
|
}
|
|
160
|
-
}
|
|
147
|
+
}
|
|
161
148
|
/**
|
|
162
149
|
* Draws a circle or ellipse.
|
|
163
|
-
|
|
150
|
+
|
|
164
151
|
* @param mode Fill or line.
|
|
165
152
|
* @param color Fill or stroke color.
|
|
166
153
|
* @param position Center position.
|
|
167
154
|
* @param radii Radius (number) or [rx, ry] for ellipse.
|
|
168
155
|
* @param props Optional arc, center, and stroke properties. Center is true by default.
|
|
169
156
|
*/
|
|
170
|
-
circle(
|
|
157
|
+
circle(mode, color, position, radii, props) {
|
|
171
158
|
const center = (props && 'center' in props) ? props.center : true;
|
|
172
159
|
const c = applyColor(color);
|
|
173
160
|
const size = typeof radii === "number" ? [radii, radii] : radii;
|
|
@@ -175,43 +162,43 @@ export const draw = {
|
|
|
175
162
|
if (!center) {
|
|
176
163
|
position = Vec2.add(position, size);
|
|
177
164
|
}
|
|
178
|
-
ctx.save();
|
|
179
|
-
ctx.translate(...position);
|
|
180
|
-
ctx.scale(...size);
|
|
181
|
-
ctx.beginPath();
|
|
182
|
-
ctx.arc(0, 0, 1, startAngle, endAngle);
|
|
165
|
+
this.ctx.save();
|
|
166
|
+
this.ctx.translate(...position);
|
|
167
|
+
this.ctx.scale(...size);
|
|
168
|
+
this.ctx.beginPath();
|
|
169
|
+
this.ctx.arc(0, 0, 1, startAngle, endAngle);
|
|
183
170
|
if (mode == 'fill')
|
|
184
|
-
ctx.lineTo(0, 0);
|
|
185
|
-
ctx.closePath();
|
|
186
|
-
ctx.restore();
|
|
171
|
+
this.ctx.lineTo(0, 0);
|
|
172
|
+
this.ctx.closePath();
|
|
173
|
+
this.ctx.restore();
|
|
187
174
|
if (mode === "fill") {
|
|
188
|
-
ctx.fillStyle = c;
|
|
189
|
-
ctx.fill();
|
|
175
|
+
this.ctx.fillStyle = c;
|
|
176
|
+
this.ctx.fill();
|
|
190
177
|
}
|
|
191
178
|
else {
|
|
192
|
-
setStrokeProps(ctx, props);
|
|
193
|
-
ctx.strokeStyle = c;
|
|
194
|
-
ctx.stroke();
|
|
179
|
+
setStrokeProps(this.ctx, props);
|
|
180
|
+
this.ctx.strokeStyle = c;
|
|
181
|
+
this.ctx.stroke();
|
|
195
182
|
}
|
|
196
|
-
}
|
|
183
|
+
}
|
|
197
184
|
/**
|
|
198
185
|
* Draws connected line segments.
|
|
199
|
-
|
|
186
|
+
|
|
200
187
|
* @param color Stroke color.
|
|
201
188
|
* @param points Array of [x, y] positions.
|
|
202
189
|
* @param props Optional stroke properties.
|
|
203
190
|
*/
|
|
204
|
-
line(
|
|
191
|
+
line(color, points, props) {
|
|
205
192
|
if (points.length < 2)
|
|
206
193
|
return;
|
|
207
|
-
setStrokeProps(ctx, props);
|
|
208
|
-
ctx.beginPath();
|
|
194
|
+
setStrokeProps(this.ctx, props);
|
|
195
|
+
this.ctx.beginPath();
|
|
209
196
|
const [[x0, y0], ...rest] = points;
|
|
210
|
-
ctx.moveTo(x0, y0);
|
|
211
|
-
rest.forEach(([x, y]) => ctx.lineTo(x, y));
|
|
212
|
-
ctx.strokeStyle = applyColor(color);
|
|
213
|
-
ctx.stroke();
|
|
214
|
-
}
|
|
197
|
+
this.ctx.moveTo(x0, y0);
|
|
198
|
+
rest.forEach(([x, y]) => this.ctx.lineTo(x, y));
|
|
199
|
+
this.ctx.strokeStyle = applyColor(color);
|
|
200
|
+
this.ctx.stroke();
|
|
201
|
+
}
|
|
215
202
|
/**
|
|
216
203
|
* Draws text at a position.
|
|
217
204
|
*
|
|
@@ -222,44 +209,44 @@ export const draw = {
|
|
|
222
209
|
* to the left and right of its position. If you align right, your position
|
|
223
210
|
* becomes the upper-right corner of the text.
|
|
224
211
|
*
|
|
225
|
-
|
|
212
|
+
|
|
226
213
|
* @param color Fill color.
|
|
227
214
|
* @param text Text string.
|
|
228
215
|
* @param position Top-left position.
|
|
229
216
|
* @param props {@link PrintProps} Optional font, text limit, or alignment.
|
|
230
217
|
*/
|
|
231
|
-
print(
|
|
218
|
+
print(color, text, position, props) {
|
|
232
219
|
const [x, y] = position;
|
|
233
220
|
const { font = "16px sans-serif" } = props ?? {};
|
|
234
|
-
ctx.fillStyle = parseColor(color);
|
|
235
|
-
ctx.font = font;
|
|
236
|
-
ctx.textAlign = props?.align ?? "left";
|
|
221
|
+
this.ctx.fillStyle = parseColor(color);
|
|
222
|
+
this.ctx.font = font;
|
|
223
|
+
this.ctx.textAlign = props?.align ?? "left";
|
|
237
224
|
if (props && 'width' in props) {
|
|
238
225
|
const { width } = props;
|
|
239
|
-
const lines = wrapText(ctx, text, width);
|
|
240
|
-
const lineHeight = getFontHeight(ctx);
|
|
241
|
-
ctx.textBaseline = "top";
|
|
226
|
+
const lines = wrapText(this.ctx, text, width);
|
|
227
|
+
const lineHeight = getFontHeight(this.ctx);
|
|
228
|
+
this.ctx.textBaseline = "top";
|
|
242
229
|
lines.forEach((line, i) => {
|
|
243
|
-
ctx.fillText(line, x, y + i * lineHeight, width);
|
|
230
|
+
this.ctx.fillText(line, x, y + i * lineHeight, width);
|
|
244
231
|
});
|
|
245
|
-
ctx.textBaseline = "alphabetic";
|
|
232
|
+
this.ctx.textBaseline = "alphabetic";
|
|
246
233
|
}
|
|
247
234
|
else {
|
|
248
|
-
ctx.fillText(text, x, y);
|
|
235
|
+
this.ctx.fillText(text, x, y);
|
|
249
236
|
}
|
|
250
|
-
}
|
|
237
|
+
}
|
|
251
238
|
/**
|
|
252
239
|
* Draws an image.
|
|
253
240
|
*
|
|
254
241
|
* @remarks named "draw" because it draws anything _drawable_
|
|
255
242
|
* in the long run.
|
|
256
243
|
*
|
|
257
|
-
|
|
244
|
+
|
|
258
245
|
* @param handle Image handle from newImage.
|
|
259
246
|
* @param position Draw position.
|
|
260
247
|
* @param props Optional rotation, scale, origin, or quad.
|
|
261
248
|
*/
|
|
262
|
-
draw(
|
|
249
|
+
draw(handle, position, props) {
|
|
263
250
|
if (!handle.isReady())
|
|
264
251
|
return;
|
|
265
252
|
const element = handle.getElement();
|
|
@@ -269,120 +256,121 @@ export const draw = {
|
|
|
269
256
|
const { r = 0, scale = 1, origin = 0, quad } = props ?? {};
|
|
270
257
|
const [sx, sy] = typeof scale === "number" ? [scale, scale] : scale;
|
|
271
258
|
const [ox, oy] = typeof origin === "number" ? [origin, origin] : origin;
|
|
272
|
-
ctx.save();
|
|
273
|
-
ctx.translate(x, y);
|
|
274
|
-
ctx.rotate(r);
|
|
275
|
-
ctx.scale(sx, sy);
|
|
259
|
+
this.ctx.save();
|
|
260
|
+
this.ctx.translate(x, y);
|
|
261
|
+
this.ctx.rotate(r);
|
|
262
|
+
this.ctx.scale(sx, sy);
|
|
276
263
|
if (quad) {
|
|
277
264
|
const [qx, qy, qw, qh] = quad;
|
|
278
|
-
ctx.drawImage(element, qx, qy, qw, qh, -ox, -oy, qw, qh);
|
|
265
|
+
this.ctx.drawImage(element, qx, qy, qw, qh, -ox, -oy, qw, qh);
|
|
279
266
|
}
|
|
280
267
|
else {
|
|
281
|
-
ctx.drawImage(element, -ox, -oy);
|
|
268
|
+
this.ctx.drawImage(element, -ox, -oy);
|
|
282
269
|
}
|
|
283
|
-
ctx.restore();
|
|
284
|
-
}
|
|
270
|
+
this.ctx.restore();
|
|
271
|
+
}
|
|
285
272
|
/**
|
|
286
273
|
* Loads an image from a path.
|
|
287
274
|
* Unlike built-in loading, this pretends to be synchronous.
|
|
288
|
-
|
|
275
|
+
|
|
289
276
|
* @param path Image file path.
|
|
290
277
|
* @returns ImageHandle for use with draw.
|
|
291
278
|
*/
|
|
292
|
-
newImage(
|
|
279
|
+
newImage(path) {
|
|
293
280
|
return new ImageHandle(path);
|
|
294
|
-
}
|
|
281
|
+
}
|
|
295
282
|
/**
|
|
296
283
|
* Sets the clipping region.
|
|
297
|
-
|
|
284
|
+
|
|
298
285
|
* @param rect Clipping rectangle, or full canvas if omitted.
|
|
299
286
|
*/
|
|
300
|
-
clip(
|
|
301
|
-
ctx.beginPath();
|
|
287
|
+
clip(rect) {
|
|
288
|
+
this.ctx.beginPath();
|
|
302
289
|
if (rect) {
|
|
303
290
|
const [x, y, w, h] = rect;
|
|
304
|
-
ctx.rect(x, y, w, h);
|
|
291
|
+
this.ctx.rect(x, y, w, h);
|
|
305
292
|
}
|
|
306
293
|
else {
|
|
307
|
-
ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
294
|
+
this.ctx.rect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
|
308
295
|
}
|
|
309
|
-
ctx.clip();
|
|
310
|
-
}
|
|
296
|
+
this.ctx.clip();
|
|
297
|
+
}
|
|
311
298
|
/**
|
|
312
299
|
* Draws a polygon.
|
|
313
|
-
|
|
300
|
+
|
|
314
301
|
* @param mode Fill or line.
|
|
315
302
|
* @param color Fill or stroke color.
|
|
316
303
|
* @param points Array of [x, y] vertices.
|
|
317
304
|
* @param props Optional stroke properties.
|
|
318
305
|
*/
|
|
319
|
-
polygon(
|
|
306
|
+
polygon(mode, color, points, props) {
|
|
320
307
|
if (points.length < 3)
|
|
321
308
|
return;
|
|
322
309
|
const c = applyColor(color);
|
|
323
|
-
ctx.beginPath();
|
|
310
|
+
this.ctx.beginPath();
|
|
324
311
|
const [[x0, y0], ...rest] = points;
|
|
325
|
-
ctx.moveTo(x0, y0);
|
|
326
|
-
rest.forEach(([x, y]) => ctx.lineTo(x, y));
|
|
327
|
-
ctx.closePath();
|
|
312
|
+
this.ctx.moveTo(x0, y0);
|
|
313
|
+
rest.forEach(([x, y]) => this.ctx.lineTo(x, y));
|
|
314
|
+
this.ctx.closePath();
|
|
328
315
|
if (mode === "fill") {
|
|
329
|
-
ctx.fillStyle = c;
|
|
330
|
-
ctx.fill();
|
|
316
|
+
this.ctx.fillStyle = c;
|
|
317
|
+
this.ctx.fill();
|
|
331
318
|
}
|
|
332
319
|
else {
|
|
333
|
-
setStrokeProps(ctx, props);
|
|
334
|
-
ctx.strokeStyle = c;
|
|
335
|
-
ctx.stroke();
|
|
320
|
+
setStrokeProps(this.ctx, props);
|
|
321
|
+
this.ctx.strokeStyle = c;
|
|
322
|
+
this.ctx.stroke();
|
|
336
323
|
}
|
|
337
|
-
}
|
|
324
|
+
}
|
|
338
325
|
/**
|
|
339
326
|
* Draws individual pixels.
|
|
340
|
-
|
|
327
|
+
|
|
341
328
|
* @param color Fill color.
|
|
342
329
|
* @param pts Array of [x, y] positions.
|
|
343
330
|
*/
|
|
344
|
-
points(
|
|
345
|
-
ctx.fillStyle = applyColor(color);
|
|
346
|
-
pts.forEach(([x, y]) => ctx.fillRect(x, y, 1, 1));
|
|
347
|
-
}
|
|
331
|
+
points(color, pts) {
|
|
332
|
+
this.ctx.fillStyle = applyColor(color);
|
|
333
|
+
pts.forEach(([x, y]) => this.ctx.fillRect(x, y, 1, 1));
|
|
334
|
+
}
|
|
348
335
|
/**
|
|
349
336
|
* Saves canvas state.
|
|
350
|
-
|
|
337
|
+
|
|
351
338
|
*/
|
|
352
|
-
push(
|
|
353
|
-
ctx.save();
|
|
354
|
-
}
|
|
339
|
+
push() {
|
|
340
|
+
this.ctx.save();
|
|
341
|
+
}
|
|
355
342
|
/**
|
|
356
343
|
* Restores canvas state.
|
|
357
|
-
|
|
344
|
+
|
|
358
345
|
*/
|
|
359
|
-
pop(
|
|
360
|
-
ctx.restore();
|
|
361
|
-
}
|
|
346
|
+
pop() {
|
|
347
|
+
this.ctx.restore();
|
|
348
|
+
}
|
|
362
349
|
/**
|
|
363
350
|
* Applies a translation.
|
|
364
|
-
|
|
351
|
+
|
|
365
352
|
* @param offset [x, y] offset.
|
|
366
353
|
*/
|
|
367
|
-
translate(
|
|
354
|
+
translate(offset) {
|
|
368
355
|
const [x, y] = offset;
|
|
369
|
-
ctx.translate(x, y);
|
|
370
|
-
}
|
|
356
|
+
this.ctx.translate(x, y);
|
|
357
|
+
}
|
|
371
358
|
/**
|
|
372
359
|
* Applies a rotation.
|
|
373
|
-
|
|
360
|
+
|
|
374
361
|
* @param angle Rotation in radians.
|
|
375
362
|
*/
|
|
376
|
-
rotate(
|
|
377
|
-
ctx.rotate(angle);
|
|
378
|
-
}
|
|
363
|
+
rotate(angle) {
|
|
364
|
+
this.ctx.rotate(angle);
|
|
365
|
+
}
|
|
379
366
|
/**
|
|
380
367
|
* Applies a scale.
|
|
381
|
-
|
|
368
|
+
|
|
382
369
|
* @param factor Scale factor (number or [x, y]).
|
|
383
370
|
*/
|
|
384
|
-
scale(
|
|
371
|
+
scale(factor) {
|
|
385
372
|
const [sx, sy] = typeof factor === "number" ? [factor, factor] : factor;
|
|
386
|
-
ctx.scale(sx, sy);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
373
|
+
this.ctx.scale(sx, sy);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Vector2 } from "../math";
|
|
2
|
+
/**
|
|
3
|
+
* An image that can be drawn using {@link Graphics.draw}
|
|
4
|
+
*
|
|
5
|
+
* Unlike raw HTMLImageElement, there is no need to wait for it to load.
|
|
6
|
+
* If the image isn't loaded, it simply won't draw it at all.
|
|
7
|
+
*
|
|
8
|
+
* If you're planning on loading many large images, simply preload
|
|
9
|
+
* these image handles beforehand so that they're ready.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ImageHandle {
|
|
12
|
+
readonly path: string;
|
|
13
|
+
private element;
|
|
14
|
+
private loadPromise;
|
|
15
|
+
private isLoaded;
|
|
16
|
+
constructor(path: string);
|
|
17
|
+
isReady(): boolean;
|
|
18
|
+
ready(): Promise<void>;
|
|
19
|
+
get size(): Vector2;
|
|
20
|
+
getElement(): HTMLImageElement | null;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=image.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/graphics/image.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC;;;;;;;;GAQG;AACH,qBAAa,WAAW;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,OAAO,CAAiC;IAChD,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,QAAQ,CAAS;gBAEb,IAAI,EAAE,MAAM;IAiBxB,OAAO,IAAI,OAAO;IAIlB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,IAAI,IAAI,IAAI,OAAO,CAElB;IAED,UAAU,IAAI,gBAAgB,GAAG,IAAI;CAGtC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An image that can be drawn using {@link Graphics.draw}
|
|
3
|
+
*
|
|
4
|
+
* Unlike raw HTMLImageElement, there is no need to wait for it to load.
|
|
5
|
+
* If the image isn't loaded, it simply won't draw it at all.
|
|
6
|
+
*
|
|
7
|
+
* If you're planning on loading many large images, simply preload
|
|
8
|
+
* these image handles beforehand so that they're ready.
|
|
9
|
+
*/
|
|
10
|
+
export class ImageHandle {
|
|
11
|
+
constructor(path) {
|
|
12
|
+
Object.defineProperty(this, "path", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "element", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: null
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "loadPromise", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "isLoaded", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: false
|
|
35
|
+
});
|
|
36
|
+
this.path = path;
|
|
37
|
+
this.loadPromise = new Promise((resolve, reject) => {
|
|
38
|
+
const img = new Image();
|
|
39
|
+
img.onload = () => {
|
|
40
|
+
this.element = img;
|
|
41
|
+
this.isLoaded = true;
|
|
42
|
+
resolve();
|
|
43
|
+
};
|
|
44
|
+
img.onerror = () => {
|
|
45
|
+
reject(new Error(`Failed to load image: ${path}`));
|
|
46
|
+
};
|
|
47
|
+
img.src = path;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
isReady() {
|
|
51
|
+
return this.isLoaded;
|
|
52
|
+
}
|
|
53
|
+
ready() {
|
|
54
|
+
return this.loadPromise;
|
|
55
|
+
}
|
|
56
|
+
get size() {
|
|
57
|
+
return [this.element?.width ?? 0, this.element?.height ?? 0];
|
|
58
|
+
}
|
|
59
|
+
getElement() {
|
|
60
|
+
return this.element;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/dist/graphics/index.d.ts
CHANGED
|
@@ -1,19 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Where we draw with {@link Graphics} on to the {@link Canvas}.
|
|
2
3
|
* @module graphics
|
|
3
|
-
* @description a reduced-state, Love2D-like wrapper around browser canvas
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
export type {
|
|
7
|
-
export {
|
|
8
|
-
export type {
|
|
9
|
-
type
|
|
10
|
-
/**
|
|
11
|
-
* A graphics object with a canvas already attatched to it.
|
|
12
|
-
* Calling its methods will draw to the render canvas.
|
|
13
|
-
* See {@link graphics} for more info.
|
|
14
|
-
*/
|
|
15
|
-
export type BoundGraphics = {
|
|
16
|
-
[K in keyof typeof draw]: Bind<(typeof draw)[K]>;
|
|
17
|
-
};
|
|
18
|
-
export declare function bindGraphics(ctx: CanvasRenderingContext2D): BoundGraphics;
|
|
5
|
+
export type { Graphics } from "./graphics";
|
|
6
|
+
export type { Canvas } from "./canvas";
|
|
7
|
+
export type { ImageHandle } from './image';
|
|
8
|
+
export type { Color, DrawMode, ShapeProps, DrawProps, PrintProps, } from "./graphics";
|
|
9
|
+
export type { CanvasSize, CanvasModeOptions, } from "./canvas";
|
|
19
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graphics/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graphics/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,YAAY,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC,YAAY,EACV,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB,YAAY,EACV,KAAK,EACL,QAAQ,EACR,UAAU,EACV,SAAS,EACT,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,UAAU,EACV,iBAAiB,GAClB,MAAM,UAAU,CAAC"}
|
package/dist/graphics/index.js
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Where we draw with {@link Graphics} on to the {@link Canvas}.
|
|
2
3
|
* @module graphics
|
|
3
|
-
* @description a reduced-state, Love2D-like wrapper around browser canvas
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
export { draw } from "./drawing";
|
|
7
|
-
export function bindGraphics(ctx) {
|
|
8
|
-
const bound = {};
|
|
9
|
-
for (const [name, fn] of Object.entries(draw)) {
|
|
10
|
-
bound[name] = (...args) => fn(ctx, ...args);
|
|
11
|
-
}
|
|
12
|
-
return bound;
|
|
13
|
-
}
|
|
5
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* A cozy web-native 2D game framework.
|
|
3
|
+
*
|
|
4
4
|
*/
|
|
5
5
|
import type { Like } from './like';
|
|
6
|
-
export type { Like, TopLevelEventHandler } from './like';
|
|
7
|
-
export type { LikeEvent, EventType, EventMap } from './events';
|
|
6
|
+
export type { Like, TopLevelEventHandler, Callbacks, Callback } from './like';
|
|
7
|
+
export type { LikeEvent, EventType, EventMap, LikeCanvasElement, Dispatcher, LikeCanvasEventMap, LikeKeyboardEvent } from './events';
|
|
8
|
+
export type { Scene } from './scene';
|
|
9
|
+
export type { EngineProps } from './engine';
|
|
8
10
|
/**
|
|
9
11
|
* Create a new Like2D game instance attached to a DOM container.
|
|
10
12
|
*
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAYH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,YAAY,EAAE,IAAI,EAAE,oBAAoB,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC9E,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,iBAAiB,EAAE,UAAU,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AACrI,YAAY,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI,CAGvD"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* A cozy web-native 2D game framework.
|
|
3
|
+
*
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Here are the top-level events, modules, and the scene system.
|
|
7
|
+
*
|
|
8
|
+
* See {@link createLike} to get started.
|
|
9
|
+
*
|
|
10
|
+
* @module like
|
|
4
11
|
*/
|
|
5
12
|
import { Engine } from './engine';
|
|
6
13
|
/**
|