@canvas-tile-engine/core 0.0.4 → 0.2.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/dist/index.d.mts +190 -50
- package/dist/index.d.ts +190 -50
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -54,6 +54,7 @@ type CanvasTileEngineConfig = {
|
|
|
54
54
|
maxWidth?: number;
|
|
55
55
|
maxHeight?: number;
|
|
56
56
|
};
|
|
57
|
+
responsive?: "preserve-scale" | "preserve-viewport" | false;
|
|
57
58
|
eventHandlers?: EventHandlers;
|
|
58
59
|
bounds?: {
|
|
59
60
|
minX: number;
|
|
@@ -146,23 +147,27 @@ type DrawObject = {
|
|
|
146
147
|
radius?: number | number[];
|
|
147
148
|
};
|
|
148
149
|
type Rect = DrawObject;
|
|
150
|
+
type Circle = Omit<DrawObject, "rotate" | "radius">;
|
|
151
|
+
type ImageItem = Omit<DrawObject, "style"> & {
|
|
152
|
+
img: HTMLImageElement;
|
|
153
|
+
};
|
|
154
|
+
type Text = Omit<DrawObject, "radius" | "size"> & {
|
|
155
|
+
text: string;
|
|
156
|
+
/** Font size in world units (scales with zoom). Default: 1 */
|
|
157
|
+
size?: number;
|
|
158
|
+
style?: {
|
|
159
|
+
fillStyle?: string;
|
|
160
|
+
/** Font family (default: "sans-serif") */
|
|
161
|
+
fontFamily?: string;
|
|
162
|
+
textAlign?: CanvasTextAlign;
|
|
163
|
+
textBaseline?: CanvasTextBaseline;
|
|
164
|
+
};
|
|
165
|
+
};
|
|
149
166
|
type Line = {
|
|
150
167
|
from: Coords;
|
|
151
168
|
to: Coords;
|
|
152
|
-
style: {
|
|
153
|
-
strokeStyle?: string;
|
|
154
|
-
lineWidth?: number;
|
|
155
|
-
};
|
|
156
|
-
};
|
|
157
|
-
type Circle = Omit<DrawObject, "rotate" | "radius">;
|
|
158
|
-
type Text = {
|
|
159
|
-
coords: Coords;
|
|
160
|
-
text: string;
|
|
161
169
|
};
|
|
162
170
|
type Path = Coords[];
|
|
163
|
-
type ImageItem = Omit<DrawObject, "style"> & {
|
|
164
|
-
img: HTMLImageElement;
|
|
165
|
-
};
|
|
166
171
|
|
|
167
172
|
/**
|
|
168
173
|
* Core engine wiring camera, config, renderer, events, and draw helpers.
|
|
@@ -179,36 +184,148 @@ declare class CanvasTileEngine {
|
|
|
179
184
|
images: ImageLoader;
|
|
180
185
|
private sizeController;
|
|
181
186
|
private animationController;
|
|
187
|
+
private responsiveWatcher?;
|
|
182
188
|
canvasWrapper: HTMLDivElement;
|
|
183
189
|
canvas: HTMLCanvasElement;
|
|
184
|
-
/**
|
|
190
|
+
/**
|
|
191
|
+
* Callback when center coordinates change (pan or zoom).
|
|
192
|
+
* @param coords - Center world coordinates: `{ x, y }`
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* engine.onCoordsChange = (coords) => {
|
|
196
|
+
* console.log(`Center: ${coords.x}, ${coords.y}`);
|
|
197
|
+
* };
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
185
200
|
onCoordsChange?: (coords: Coords) => void;
|
|
186
201
|
private _onClick?;
|
|
202
|
+
/**
|
|
203
|
+
* Callback when a tile is clicked (mouse or touch tap).
|
|
204
|
+
* @param coords - World coordinates: `raw` (exact), `snapped` (floored to tile)
|
|
205
|
+
* @param mouse - Canvas-relative position: `raw` (exact), `snapped` (tile-aligned)
|
|
206
|
+
* @param client - Viewport position: `raw` (exact), `snapped` (tile-aligned)
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* engine.onClick = (coords, mouse, client) => {
|
|
210
|
+
* console.log(`Clicked tile: ${coords.snapped.x}, ${coords.snapped.y}`);
|
|
211
|
+
* };
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
187
214
|
get onClick(): onClickCallback | undefined;
|
|
188
215
|
set onClick(cb: onClickCallback | undefined);
|
|
189
216
|
private _onRightClick?;
|
|
217
|
+
/**
|
|
218
|
+
* Callback when a tile is right-clicked.
|
|
219
|
+
* @param coords - World coordinates: `raw` (exact), `snapped` (floored to tile)
|
|
220
|
+
* @param mouse - Canvas-relative position: `raw` (exact), `snapped` (tile-aligned)
|
|
221
|
+
* @param client - Viewport position: `raw` (exact), `snapped` (tile-aligned)
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* engine.onRightClick = (coords) => {
|
|
225
|
+
* showContextMenu(coords.snapped.x, coords.snapped.y);
|
|
226
|
+
* };
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
190
229
|
get onRightClick(): onRightClickCallback | undefined;
|
|
191
230
|
set onRightClick(cb: onRightClickCallback | undefined);
|
|
192
231
|
private _onHover?;
|
|
232
|
+
/**
|
|
233
|
+
* Callback when hovering over tiles.
|
|
234
|
+
* @param coords - World coordinates: `raw` (exact), `snapped` (floored to tile)
|
|
235
|
+
* @param mouse - Canvas-relative position: `raw` (exact), `snapped` (tile-aligned)
|
|
236
|
+
* @param client - Viewport position: `raw` (exact), `snapped` (tile-aligned)
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* engine.onHover = (coords) => {
|
|
240
|
+
* setHoveredTile({ x: coords.snapped.x, y: coords.snapped.y });
|
|
241
|
+
* };
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
193
244
|
get onHover(): onHoverCallback | undefined;
|
|
194
245
|
set onHover(cb: onHoverCallback | undefined);
|
|
195
246
|
private _onMouseDown?;
|
|
247
|
+
/**
|
|
248
|
+
* Callback on mouse/touch down.
|
|
249
|
+
* @param coords - World coordinates: `raw` (exact), `snapped` (floored to tile)
|
|
250
|
+
* @param mouse - Canvas-relative position: `raw` (exact), `snapped` (tile-aligned)
|
|
251
|
+
* @param client - Viewport position: `raw` (exact), `snapped` (tile-aligned)
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* engine.onMouseDown = (coords) => {
|
|
255
|
+
* startPainting(coords.snapped.x, coords.snapped.y);
|
|
256
|
+
* };
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
196
259
|
get onMouseDown(): onMouseDownCallback | undefined;
|
|
197
260
|
set onMouseDown(cb: onMouseDownCallback | undefined);
|
|
198
261
|
private _onMouseUp?;
|
|
262
|
+
/**
|
|
263
|
+
* Callback on mouse/touch up.
|
|
264
|
+
* @param coords - World coordinates: `raw` (exact), `snapped` (floored to tile)
|
|
265
|
+
* @param mouse - Canvas-relative position: `raw` (exact), `snapped` (tile-aligned)
|
|
266
|
+
* @param client - Viewport position: `raw` (exact), `snapped` (tile-aligned)
|
|
267
|
+
* @example
|
|
268
|
+
* ```ts
|
|
269
|
+
* engine.onMouseUp = (coords) => {
|
|
270
|
+
* stopPainting();
|
|
271
|
+
* };
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
199
274
|
get onMouseUp(): onMouseUpCallback | undefined;
|
|
200
275
|
set onMouseUp(cb: onMouseUpCallback | undefined);
|
|
201
276
|
private _onMouseLeave?;
|
|
277
|
+
/**
|
|
278
|
+
* Callback when mouse/touch leaves the canvas.
|
|
279
|
+
* @param coords - World coordinates: `raw` (exact), `snapped` (floored to tile)
|
|
280
|
+
* @param mouse - Canvas-relative position: `raw` (exact), `snapped` (tile-aligned)
|
|
281
|
+
* @param client - Viewport position: `raw` (exact), `snapped` (tile-aligned)
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* engine.onMouseLeave = () => {
|
|
285
|
+
* clearHoveredTile();
|
|
286
|
+
* };
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
202
289
|
get onMouseLeave(): onMouseLeaveCallback | undefined;
|
|
203
290
|
set onMouseLeave(cb: onMouseLeaveCallback | undefined);
|
|
204
291
|
private _onDraw?;
|
|
292
|
+
/**
|
|
293
|
+
* Callback after each draw frame. Use for custom canvas drawing.
|
|
294
|
+
* @param ctx - The canvas 2D rendering context
|
|
295
|
+
* @param info - Frame info: `scale`, `width`, `height`, `coords` (center)
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* engine.onDraw = (ctx, info) => {
|
|
299
|
+
* ctx.fillStyle = "red";
|
|
300
|
+
* ctx.fillText(`Scale: ${info.scale}`, 10, 20);
|
|
301
|
+
* };
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
205
304
|
get onDraw(): onDrawCallback | undefined;
|
|
206
305
|
set onDraw(cb: onDrawCallback | undefined);
|
|
207
306
|
private _onResize?;
|
|
307
|
+
/**
|
|
308
|
+
* Callback on canvas resize.
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* engine.onResize = () => {
|
|
312
|
+
* console.log("Canvas resized:", engine.getSize());
|
|
313
|
+
* };
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
208
316
|
get onResize(): (() => void) | undefined;
|
|
209
317
|
set onResize(cb: (() => void) | undefined);
|
|
210
318
|
private _onZoom?;
|
|
211
|
-
/**
|
|
319
|
+
/**
|
|
320
|
+
* Callback when zoom level changes (wheel or pinch).
|
|
321
|
+
* @param scale - The new scale value
|
|
322
|
+
* @example
|
|
323
|
+
* ```ts
|
|
324
|
+
* engine.onZoom = (scale) => {
|
|
325
|
+
* console.log(`Zoom level: ${scale}`);
|
|
326
|
+
* };
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
212
329
|
get onZoom(): ((scale: number) => void) | undefined;
|
|
213
330
|
set onZoom(cb: ((scale: number) => void) | undefined);
|
|
214
331
|
/**
|
|
@@ -242,6 +359,12 @@ declare class CanvasTileEngine {
|
|
|
242
359
|
* @returns Current canvas scale.
|
|
243
360
|
*/
|
|
244
361
|
getScale(): number;
|
|
362
|
+
/**
|
|
363
|
+
* Set the canvas scale directly, clamped to min/max bounds.
|
|
364
|
+
* @param newScale The desired scale value.
|
|
365
|
+
* @throws {ConfigValidationError} If scale is not a positive finite number.
|
|
366
|
+
*/
|
|
367
|
+
setScale(newScale: number): void;
|
|
245
368
|
/**
|
|
246
369
|
* Zoom in by a given factor, centered on the viewport.
|
|
247
370
|
* @param factor Zoom multiplier (default: 1.5). Higher values zoom in more.
|
|
@@ -275,7 +398,11 @@ declare class CanvasTileEngine {
|
|
|
275
398
|
minY: number;
|
|
276
399
|
maxY: number;
|
|
277
400
|
};
|
|
278
|
-
/**
|
|
401
|
+
/**
|
|
402
|
+
* Set center coordinates from outside (adjusts the camera accordingly).
|
|
403
|
+
* @param newCenter The new center coordinates.
|
|
404
|
+
* @throws {ConfigValidationError} If coordinates are not finite numbers.
|
|
405
|
+
*/
|
|
279
406
|
updateCoords(newCenter: Coords): void;
|
|
280
407
|
/**
|
|
281
408
|
* Smoothly move the camera center to target coordinates over the given duration.
|
|
@@ -283,6 +410,7 @@ declare class CanvasTileEngine {
|
|
|
283
410
|
* @param y Target world y.
|
|
284
411
|
* @param durationMs Animation duration in milliseconds (default: 500ms). Set to 0 for instant move.
|
|
285
412
|
* @param onComplete Optional callback fired when animation completes.
|
|
413
|
+
* @throws {ConfigValidationError} If coordinates are not finite numbers.
|
|
286
414
|
*/
|
|
287
415
|
goCoords(x: number, y: number, durationMs?: number, onComplete?: () => void): void;
|
|
288
416
|
/**
|
|
@@ -335,7 +463,7 @@ declare class CanvasTileEngine {
|
|
|
335
463
|
* @param items Rectangle definitions.
|
|
336
464
|
* @param layer Layer order (lower draws first).
|
|
337
465
|
*/
|
|
338
|
-
drawRect(items:
|
|
466
|
+
drawRect(items: Rect | Array<Rect>, layer?: number): LayerHandle;
|
|
339
467
|
/**
|
|
340
468
|
* Draw rectangles with pre-rendering cache (canvas renderer only).
|
|
341
469
|
* Renders all items once to an offscreen canvas, then blits the visible portion each frame.
|
|
@@ -345,7 +473,7 @@ declare class CanvasTileEngine {
|
|
|
345
473
|
* @param cacheKey Unique key for this cache (e.g., "minimap-items").
|
|
346
474
|
* @param layer Layer order (lower draws first).
|
|
347
475
|
*/
|
|
348
|
-
drawStaticRect(items: Array<
|
|
476
|
+
drawStaticRect(items: Array<Rect>, cacheKey: string, layer?: number): LayerHandle;
|
|
349
477
|
/**
|
|
350
478
|
* Draw circles with pre-rendering cache (canvas renderer only).
|
|
351
479
|
* Renders all items once to an offscreen canvas, then blits the visible portion each frame.
|
|
@@ -354,7 +482,7 @@ declare class CanvasTileEngine {
|
|
|
354
482
|
* @param cacheKey Unique key for this cache (e.g., "minimap-circles").
|
|
355
483
|
* @param layer Layer order (lower draws first).
|
|
356
484
|
*/
|
|
357
|
-
drawStaticCircle(items: Array<
|
|
485
|
+
drawStaticCircle(items: Array<Circle>, cacheKey: string, layer?: number): LayerHandle;
|
|
358
486
|
/**
|
|
359
487
|
* Draw images with pre-rendering cache (canvas renderer only).
|
|
360
488
|
* Renders all items once to an offscreen canvas, then blits the visible portion each frame.
|
|
@@ -364,9 +492,7 @@ declare class CanvasTileEngine {
|
|
|
364
492
|
* @param cacheKey Unique key for this cache (e.g., "terrain-cache").
|
|
365
493
|
* @param layer Layer order (lower draws first).
|
|
366
494
|
*/
|
|
367
|
-
drawStaticImage(items: Array<
|
|
368
|
-
img: HTMLImageElement;
|
|
369
|
-
}>, cacheKey: string, layer?: number): LayerHandle;
|
|
495
|
+
drawStaticImage(items: Array<ImageItem>, cacheKey: string, layer?: number): LayerHandle;
|
|
370
496
|
/**
|
|
371
497
|
* Clear a static rendering cache.
|
|
372
498
|
* @param cacheKey The cache key to clear, or undefined to clear all caches.
|
|
@@ -378,13 +504,7 @@ declare class CanvasTileEngine {
|
|
|
378
504
|
* @param style Line style overrides.
|
|
379
505
|
* @param layer Layer order.
|
|
380
506
|
*/
|
|
381
|
-
drawLine(items: Array<{
|
|
382
|
-
from: Coords;
|
|
383
|
-
to: Coords;
|
|
384
|
-
}> | {
|
|
385
|
-
from: Coords;
|
|
386
|
-
to: Coords;
|
|
387
|
-
}, style?: {
|
|
507
|
+
drawLine(items: Array<Line> | Line, style?: {
|
|
388
508
|
strokeStyle?: string;
|
|
389
509
|
lineWidth?: number;
|
|
390
510
|
}, layer?: number): LayerHandle;
|
|
@@ -393,32 +513,36 @@ declare class CanvasTileEngine {
|
|
|
393
513
|
* @param items Circle definitions.
|
|
394
514
|
* @param layer Layer order.
|
|
395
515
|
*/
|
|
396
|
-
drawCircle(items:
|
|
516
|
+
drawCircle(items: Circle | Array<Circle>, layer?: number): LayerHandle;
|
|
397
517
|
/**
|
|
398
518
|
* Draw one or many texts at world positions (canvas renderer only).
|
|
399
|
-
* @param items Text definitions.
|
|
400
|
-
* @param style Text style overrides.
|
|
519
|
+
* @param items Text definitions with position, text, size, and style.
|
|
401
520
|
* @param layer Layer order.
|
|
521
|
+
* @example
|
|
522
|
+
* ```ts
|
|
523
|
+
* engine.drawText({
|
|
524
|
+
* x: 0,
|
|
525
|
+
* y: 0,
|
|
526
|
+
* text: "Hello",
|
|
527
|
+
* size: 1, // 1 tile height
|
|
528
|
+
* style: { fillStyle: "black", fontFamily: "Arial" }
|
|
529
|
+
* });
|
|
530
|
+
*
|
|
531
|
+
* // Multiple texts
|
|
532
|
+
* engine.drawText([
|
|
533
|
+
* { x: 0, y: 0, text: "A", size: 2 },
|
|
534
|
+
* { x: 1, y: 0, text: "B", size: 2 }
|
|
535
|
+
* ]);
|
|
536
|
+
* ```
|
|
402
537
|
*/
|
|
403
|
-
drawText(items: Array<
|
|
404
|
-
coords: Coords;
|
|
405
|
-
text: string;
|
|
406
|
-
}> | {
|
|
407
|
-
coords: Coords;
|
|
408
|
-
text: string;
|
|
409
|
-
}, style?: {
|
|
410
|
-
fillStyle?: string;
|
|
411
|
-
font?: string;
|
|
412
|
-
textAlign?: CanvasTextAlign;
|
|
413
|
-
textBaseline?: CanvasTextBaseline;
|
|
414
|
-
}, layer?: number): LayerHandle;
|
|
538
|
+
drawText(items: Array<Text> | Text, layer?: number): LayerHandle;
|
|
415
539
|
/**
|
|
416
540
|
* Draw one or many polylines through world points (canvas renderer only).
|
|
417
541
|
* @param items Polyline point collections.
|
|
418
542
|
* @param style Stroke style overrides.
|
|
419
543
|
* @param layer Layer order.
|
|
420
544
|
*/
|
|
421
|
-
drawPath(items: Array<
|
|
545
|
+
drawPath(items: Array<Path> | Path, style?: {
|
|
422
546
|
strokeStyle?: string;
|
|
423
547
|
lineWidth?: number;
|
|
424
548
|
}, layer?: number): LayerHandle;
|
|
@@ -428,11 +552,7 @@ declare class CanvasTileEngine {
|
|
|
428
552
|
* @param items Image definitions.
|
|
429
553
|
* @param layer Layer order.
|
|
430
554
|
*/
|
|
431
|
-
drawImage(items: Array<
|
|
432
|
-
img: HTMLImageElement;
|
|
433
|
-
}> | (Omit<DrawObject, "style"> & {
|
|
434
|
-
img: HTMLImageElement;
|
|
435
|
-
}), layer?: number): LayerHandle;
|
|
555
|
+
drawImage(items: Array<ImageItem> | ImageItem, layer?: number): LayerHandle;
|
|
436
556
|
/**
|
|
437
557
|
* Draw grid lines at specified cell size (canvas renderer only).
|
|
438
558
|
* @param cellSize Size of each grid cell in world units.
|
|
@@ -546,4 +666,24 @@ declare const VISIBILITY_BUFFER: {
|
|
|
546
666
|
readonly TILE_BUFFER: 1;
|
|
547
667
|
};
|
|
548
668
|
|
|
549
|
-
|
|
669
|
+
/**
|
|
670
|
+
* Convert grid-based dimensions to pixel-based config.
|
|
671
|
+
* @param options Grid configuration with columns, rows, and cell size.
|
|
672
|
+
* @returns Config object with size and scale properties.
|
|
673
|
+
* @example
|
|
674
|
+
* ```ts
|
|
675
|
+
* const config = {
|
|
676
|
+
* ...gridToSize({ columns: 12, rows: 12, cellSize: 50 }),
|
|
677
|
+
* backgroundColor: "#337426",
|
|
678
|
+
* };
|
|
679
|
+
* // config.size = { width: 600, height: 600 }
|
|
680
|
+
* // config.scale = 50
|
|
681
|
+
* ```
|
|
682
|
+
*/
|
|
683
|
+
declare function gridToSize(options: {
|
|
684
|
+
columns: number;
|
|
685
|
+
rows: number;
|
|
686
|
+
cellSize: number;
|
|
687
|
+
}): Pick<CanvasTileEngineConfig, "size" | "scale">;
|
|
688
|
+
|
|
689
|
+
export { COORDINATE_OVERLAY, CanvasTileEngine, type CanvasTileEngineConfig, type Circle, type Coords, DEBUG_HUD, DEFAULT_VALUES, type DrawObject, type EventHandlers, type ImageItem, type LayerHandle, type Line, type Path, RENDER_DEFAULTS, type Rect, SCALE_LIMITS, SIZE_LIMITS, type Text, VISIBILITY_BUFFER, gridToSize, type onClickCallback, type onDrawCallback, type onHoverCallback, type onMouseDownCallback, type onMouseLeaveCallback, type onMouseUpCallback, type onRightClickCallback, type onZoomCallback };
|