woofjs-engine 0.3.0 → 0.4.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.
@@ -369,6 +369,10 @@ function installProjectGlobals(project, win, restoreGlobals) {
369
369
  "onMouseDown",
370
370
  "onMouseUp",
371
371
  "onMouseMove",
372
+ "onTouchStart",
373
+ "onTouchMove",
374
+ "onTouchEnd",
375
+ "onTouchCancel",
372
376
  "onUpdate",
373
377
  "scene",
374
378
  "go",
@@ -1861,6 +1865,7 @@ class Project2 {
1861
1865
  listeners = [];
1862
1866
  keyDownHandlers = new Set;
1863
1867
  keyUpHandlers = new Set;
1868
+ touchHandlers = { start: new Set, move: new Set, end: new Set, cancel: new Set };
1864
1869
  pointerDownHandlers = new Set;
1865
1870
  pointerUpHandlers = new Set;
1866
1871
  pointerMoveHandlers = new Set;
@@ -1926,6 +1931,7 @@ class Project2 {
1926
1931
  this.canvas = doc.createElement("canvas");
1927
1932
  this.canvas.width = this.width;
1928
1933
  this.canvas.height = this.height;
1934
+ this.canvas.style.touchAction = "none";
1929
1935
  this.canvas.style.backgroundSize = this.backdrop.size;
1930
1936
  this.canvas.style.backgroundRepeat = this.backdrop.repeat;
1931
1937
  const context = this.canvas.getContext("2d");
@@ -2087,6 +2093,8 @@ class Project2 {
2087
2093
  this.pointerDownHandlers.clear();
2088
2094
  this.pointerUpHandlers.clear();
2089
2095
  this.pointerMoveHandlers.clear();
2096
+ for (const handlers of Object.values(this.touchHandlers))
2097
+ handlers.clear();
2090
2098
  this.following = undefined;
2091
2099
  this.clearPen();
2092
2100
  this.invoke(() => setup(data));
@@ -2423,6 +2431,22 @@ class Project2 {
2423
2431
  this.pointerMoveHandlers.add(fn);
2424
2432
  return () => this.pointerMoveHandlers.delete(fn);
2425
2433
  }
2434
+ onTouchStart(fn) {
2435
+ this.touchHandlers.start.add(fn);
2436
+ return () => this.touchHandlers.start.delete(fn);
2437
+ }
2438
+ onTouchMove(fn) {
2439
+ this.touchHandlers.move.add(fn);
2440
+ return () => this.touchHandlers.move.delete(fn);
2441
+ }
2442
+ onTouchEnd(fn) {
2443
+ this.touchHandlers.end.add(fn);
2444
+ return () => this.touchHandlers.end.delete(fn);
2445
+ }
2446
+ onTouchCancel(fn) {
2447
+ this.touchHandlers.cancel.add(fn);
2448
+ return () => this.touchHandlers.cancel.delete(fn);
2449
+ }
2426
2450
  get mouseXSpeed() {
2427
2451
  return this.mouseSpeedX;
2428
2452
  }
@@ -2454,16 +2478,20 @@ class Project2 {
2454
2478
  for (const fn of this.keyUpHandlers)
2455
2479
  this.invoke(() => fn(k));
2456
2480
  });
2481
+ const activePointers = new Set;
2457
2482
  listen(this.win, "blur", () => {
2458
2483
  this.keysDown.clear();
2459
2484
  this.pendingKeys.clear();
2460
2485
  this.frameKeys.clear();
2461
2486
  this.mouseDown = false;
2487
+ for (const id of activePointers)
2488
+ for (const sprite of this.sprites)
2489
+ sprite.pointer("up", this.mouseX, this.mouseY, id, true);
2490
+ activePointers.clear();
2462
2491
  });
2463
- const activePointers = new Set;
2464
2492
  const pointer = (kind) => (event) => {
2465
2493
  const id = event.pointerId ?? 0;
2466
- if ((kind === "up" || kind === "cancel") && !activePointers.has(id))
2494
+ if ((kind === "up" || kind === "cancel" || kind === "move" && event.pointerType === "touch") && !activePointers.has(id))
2467
2495
  return;
2468
2496
  const rect = this.canvas.getBoundingClientRect();
2469
2497
  if (!rect.width || !rect.height)
@@ -2477,6 +2505,11 @@ class Project2 {
2477
2505
  if (kind === "up" || kind === "cancel")
2478
2506
  activePointers.delete(id);
2479
2507
  this.mouseDown = activePointers.size > 0;
2508
+ if (event.pointerType === "touch") {
2509
+ const touchKind = kind === "down" ? "start" : kind === "up" ? "end" : kind;
2510
+ for (const fn of this.touchHandlers[touchKind])
2511
+ this.invoke(() => fn(this.mouseX, this.mouseY, id));
2512
+ }
2480
2513
  const handlers = kind === "down" ? this.pointerDownHandlers : kind === "move" ? this.pointerMoveHandlers : this.pointerUpHandlers;
2481
2514
  for (const fn of handlers)
2482
2515
  this.invoke(() => fn(this.mouseX, this.mouseY));
@@ -2516,6 +2549,8 @@ class Project2 {
2516
2549
  this.pointerDownHandlers.clear();
2517
2550
  this.pointerUpHandlers.clear();
2518
2551
  this.pointerMoveHandlers.clear();
2552
+ for (const handlers of Object.values(this.touchHandlers))
2553
+ handlers.clear();
2519
2554
  this.scenes.clear();
2520
2555
  this.following = undefined;
2521
2556
  this.clearPen();
package/dist/index.js CHANGED
@@ -71,7 +71,7 @@ import {
71
71
  RepeatUntil2,
72
72
  Project2,
73
73
  woof2
74
- } from "./index-pw0zq53c.js";
74
+ } from "./index-w4yvg8ax.js";
75
75
  export {
76
76
  DOWN2 as DOWN,
77
77
  LEFT2 as LEFT,
package/dist/play.js CHANGED
@@ -47,7 +47,7 @@ import {
47
47
  getData2,
48
48
  arrayToString2,
49
49
  woof2
50
- } from "./index-pw0zq53c.js";
50
+ } from "./index-w4yvg8ax.js";
51
51
 
52
52
  // src/play.ts
53
53
  var current;
@@ -154,6 +154,10 @@ var onKeyUp = (fn) => project().onKeyUp(fn);
154
154
  var onMouseDown = (fn) => project().onMouseDown(fn);
155
155
  var onMouseUp = (fn) => project().onMouseUp(fn);
156
156
  var onMouseMove = (fn) => project().onMouseMove(fn);
157
+ var onTouchStart = (fn) => project().onTouchStart(fn);
158
+ var onTouchMove = (fn) => project().onTouchMove(fn);
159
+ var onTouchEnd = (fn) => project().onTouchEnd(fn);
160
+ var onTouchCancel = (fn) => project().onTouchCancel(fn);
157
161
  var mouse = {
158
162
  get x() {
159
163
  return project().mouseX;
@@ -253,6 +257,10 @@ export {
253
257
  onMouseDown,
254
258
  onMouseMove,
255
259
  onMouseUp,
260
+ onTouchCancel,
261
+ onTouchEnd,
262
+ onTouchMove,
263
+ onTouchStart,
256
264
  onUpdate,
257
265
  once,
258
266
  particles,
@@ -130,6 +130,14 @@ export declare const onMouseDown: Project["onMouseDown"];
130
130
  export declare const onMouseUp: Project["onMouseUp"];
131
131
  /** Subscribe to pointer movement. Example: `onMouseMove((x, y) => {})`. */
132
132
  export declare const onMouseMove: Project["onMouseMove"];
133
+ /** Subscribe to touch contacts and receive world coordinates and finger id. */
134
+ export declare const onTouchStart: Project["onTouchStart"];
135
+ /** Subscribe to touch movement. */
136
+ export declare const onTouchMove: Project["onTouchMove"];
137
+ /** Subscribe to completed touch contacts. */
138
+ export declare const onTouchEnd: Project["onTouchEnd"];
139
+ /** Subscribe to cancelled touch contacts. */
140
+ export declare const onTouchCancel: Project["onTouchCancel"];
133
141
  /** Pointer coordinates and button state for the beginner project. */
134
142
  export declare const mouse: {
135
143
  /** Pointer x coordinate in world units. Example: `mouse.x`. */
@@ -155,6 +155,7 @@ export declare class Project {
155
155
  private listeners;
156
156
  private keyDownHandlers;
157
157
  private keyUpHandlers;
158
+ private touchHandlers;
158
159
  private pointerDownHandlers;
159
160
  private pointerUpHandlers;
160
161
  private pointerMoveHandlers;
@@ -372,6 +373,14 @@ export declare class Project {
372
373
  onMouseUp(fn: (x: number, y: number) => void): () => void;
373
374
  /** Register a pointer-move callback and return an unsubscribe function. Example: `project.onMouseMove((x, y) => player.setPosition(x, y))`. */
374
375
  onMouseMove(fn: (x: number, y: number) => void): () => void;
376
+ /** Subscribe to touch contacts. Coordinates are world units; id identifies the finger. */
377
+ onTouchStart(fn: (x: number, y: number, id: number) => void): () => void;
378
+ /** Subscribe to movements of an active touch contact. */
379
+ onTouchMove(fn: (x: number, y: number, id: number) => void): () => void;
380
+ /** Subscribe to completed touch contacts. */
381
+ onTouchEnd(fn: (x: number, y: number, id: number) => void): () => void;
382
+ /** Subscribe to cancelled touch contacts. */
383
+ onTouchCancel(fn: (x: number, y: number, id: number) => void): () => void;
375
384
  /** Horizontal pointer displacement since the previous frame. Example: `project.mouseXSpeed`. */
376
385
  get mouseXSpeed(): number;
377
386
  /** Vertical pointer displacement since the previous frame. Example: `project.mouseYSpeed`. */
package/dist/ui.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import"./index-1jykaz1m.js";
2
2
  import {
3
3
  woof2
4
- } from "./index-pw0zq53c.js";
4
+ } from "./index-w4yvg8ax.js";
5
5
 
6
6
  // src/ui.ts
7
7
  var tokens = {
@@ -287,6 +287,10 @@
287
287
  onMouseDown: () => onMouseDown,
288
288
  onMouseMove: () => onMouseMove,
289
289
  onMouseUp: () => onMouseUp,
290
+ onTouchCancel: () => onTouchCancel,
291
+ onTouchEnd: () => onTouchEnd,
292
+ onTouchMove: () => onTouchMove,
293
+ onTouchStart: () => onTouchStart,
290
294
  onUpdate: () => onUpdate,
291
295
  once: () => once,
292
296
  particles: () => particles,
@@ -1016,6 +1020,10 @@
1016
1020
  "onMouseDown",
1017
1021
  "onMouseUp",
1018
1022
  "onMouseMove",
1023
+ "onTouchStart",
1024
+ "onTouchMove",
1025
+ "onTouchEnd",
1026
+ "onTouchCancel",
1019
1027
  "onUpdate",
1020
1028
  "scene",
1021
1029
  "go",
@@ -2508,6 +2516,7 @@
2508
2516
  listeners = [];
2509
2517
  keyDownHandlers = new Set;
2510
2518
  keyUpHandlers = new Set;
2519
+ touchHandlers = { start: new Set, move: new Set, end: new Set, cancel: new Set };
2511
2520
  pointerDownHandlers = new Set;
2512
2521
  pointerUpHandlers = new Set;
2513
2522
  pointerMoveHandlers = new Set;
@@ -2573,6 +2582,7 @@
2573
2582
  this.canvas = doc.createElement("canvas");
2574
2583
  this.canvas.width = this.width;
2575
2584
  this.canvas.height = this.height;
2585
+ this.canvas.style.touchAction = "none";
2576
2586
  this.canvas.style.backgroundSize = this.backdrop.size;
2577
2587
  this.canvas.style.backgroundRepeat = this.backdrop.repeat;
2578
2588
  const context = this.canvas.getContext("2d");
@@ -2734,6 +2744,8 @@
2734
2744
  this.pointerDownHandlers.clear();
2735
2745
  this.pointerUpHandlers.clear();
2736
2746
  this.pointerMoveHandlers.clear();
2747
+ for (const handlers of Object.values(this.touchHandlers))
2748
+ handlers.clear();
2737
2749
  this.following = undefined;
2738
2750
  this.clearPen();
2739
2751
  this.invoke(() => setup(data));
@@ -3070,6 +3082,22 @@
3070
3082
  this.pointerMoveHandlers.add(fn);
3071
3083
  return () => this.pointerMoveHandlers.delete(fn);
3072
3084
  }
3085
+ onTouchStart(fn) {
3086
+ this.touchHandlers.start.add(fn);
3087
+ return () => this.touchHandlers.start.delete(fn);
3088
+ }
3089
+ onTouchMove(fn) {
3090
+ this.touchHandlers.move.add(fn);
3091
+ return () => this.touchHandlers.move.delete(fn);
3092
+ }
3093
+ onTouchEnd(fn) {
3094
+ this.touchHandlers.end.add(fn);
3095
+ return () => this.touchHandlers.end.delete(fn);
3096
+ }
3097
+ onTouchCancel(fn) {
3098
+ this.touchHandlers.cancel.add(fn);
3099
+ return () => this.touchHandlers.cancel.delete(fn);
3100
+ }
3073
3101
  get mouseXSpeed() {
3074
3102
  return this.mouseSpeedX;
3075
3103
  }
@@ -3101,16 +3129,20 @@
3101
3129
  for (const fn of this.keyUpHandlers)
3102
3130
  this.invoke(() => fn(k));
3103
3131
  });
3132
+ const activePointers = new Set;
3104
3133
  listen(this.win, "blur", () => {
3105
3134
  this.keysDown.clear();
3106
3135
  this.pendingKeys.clear();
3107
3136
  this.frameKeys.clear();
3108
3137
  this.mouseDown = false;
3138
+ for (const id of activePointers)
3139
+ for (const sprite of this.sprites)
3140
+ sprite.pointer("up", this.mouseX, this.mouseY, id, true);
3141
+ activePointers.clear();
3109
3142
  });
3110
- const activePointers = new Set;
3111
3143
  const pointer = (kind) => (event) => {
3112
3144
  const id = event.pointerId ?? 0;
3113
- if ((kind === "up" || kind === "cancel") && !activePointers.has(id))
3145
+ if ((kind === "up" || kind === "cancel" || kind === "move" && event.pointerType === "touch") && !activePointers.has(id))
3114
3146
  return;
3115
3147
  const rect = this.canvas.getBoundingClientRect();
3116
3148
  if (!rect.width || !rect.height)
@@ -3124,6 +3156,11 @@
3124
3156
  if (kind === "up" || kind === "cancel")
3125
3157
  activePointers.delete(id);
3126
3158
  this.mouseDown = activePointers.size > 0;
3159
+ if (event.pointerType === "touch") {
3160
+ const touchKind = kind === "down" ? "start" : kind === "up" ? "end" : kind;
3161
+ for (const fn of this.touchHandlers[touchKind])
3162
+ this.invoke(() => fn(this.mouseX, this.mouseY, id));
3163
+ }
3127
3164
  const handlers = kind === "down" ? this.pointerDownHandlers : kind === "move" ? this.pointerMoveHandlers : this.pointerUpHandlers;
3128
3165
  for (const fn of handlers)
3129
3166
  this.invoke(() => fn(this.mouseX, this.mouseY));
@@ -3163,6 +3200,8 @@
3163
3200
  this.pointerDownHandlers.clear();
3164
3201
  this.pointerUpHandlers.clear();
3165
3202
  this.pointerMoveHandlers.clear();
3203
+ for (const handlers of Object.values(this.touchHandlers))
3204
+ handlers.clear();
3166
3205
  this.scenes.clear();
3167
3206
  this.following = undefined;
3168
3207
  this.clearPen();
@@ -3281,6 +3320,10 @@
3281
3320
  var onMouseDown = (fn) => project2().onMouseDown(fn);
3282
3321
  var onMouseUp = (fn) => project2().onMouseUp(fn);
3283
3322
  var onMouseMove = (fn) => project2().onMouseMove(fn);
3323
+ var onTouchStart = (fn) => project2().onTouchStart(fn);
3324
+ var onTouchMove = (fn) => project2().onTouchMove(fn);
3325
+ var onTouchEnd = (fn) => project2().onTouchEnd(fn);
3326
+ var onTouchCancel = (fn) => project2().onTouchCancel(fn);
3284
3327
  var mouse = {
3285
3328
  get x() {
3286
3329
  return project2().mouseX;
package/docs/api.json CHANGED
@@ -5355,5 +5355,85 @@
5355
5355
  "params": [],
5356
5356
  "example": "import type { EasingName } from \"woofjs-engine\";\n\nconst name: EasingName = 'easeOut';",
5357
5357
  "returns": "A string type."
5358
+ },
5359
+ {
5360
+ "area": "exports",
5361
+ "name": "onTouchStart",
5362
+ "signature": "onTouchStart(fn: (x: number, y: number, id: number) => void): () => void",
5363
+ "summary": "Subscribe to touch start events with world coordinates and pointer ID.",
5364
+ "params": [
5365
+ "fn: callback receiving x, y and pointer id"
5366
+ ],
5367
+ "example": "import { onTouchStart } from \"woofjs-engine/play\";\n\nonTouchStart((x, y, id) => console.log(x, y, id));"
5368
+ },
5369
+ {
5370
+ "area": "exports",
5371
+ "name": "onTouchMove",
5372
+ "signature": "onTouchMove(fn: (x: number, y: number, id: number) => void): () => void",
5373
+ "summary": "Subscribe to touch move events with world coordinates and pointer ID.",
5374
+ "params": [
5375
+ "fn: callback receiving x, y and pointer id"
5376
+ ],
5377
+ "example": "import { onTouchMove } from \"woofjs-engine/play\";\n\nonTouchMove((x, y, id) => console.log(x, y, id));"
5378
+ },
5379
+ {
5380
+ "area": "exports",
5381
+ "name": "onTouchEnd",
5382
+ "signature": "onTouchEnd(fn: (x: number, y: number, id: number) => void): () => void",
5383
+ "summary": "Subscribe to touch end events with world coordinates and pointer ID.",
5384
+ "params": [
5385
+ "fn: callback receiving x, y and pointer id"
5386
+ ],
5387
+ "example": "import { onTouchEnd } from \"woofjs-engine/play\";\n\nonTouchEnd((x, y, id) => console.log(x, y, id));"
5388
+ },
5389
+ {
5390
+ "area": "exports",
5391
+ "name": "onTouchCancel",
5392
+ "signature": "onTouchCancel(fn: (x: number, y: number, id: number) => void): () => void",
5393
+ "summary": "Subscribe to touch cancel events with world coordinates and pointer ID.",
5394
+ "params": [
5395
+ "fn: callback receiving x, y and pointer id"
5396
+ ],
5397
+ "example": "import { onTouchCancel } from \"woofjs-engine/play\";\n\nonTouchCancel((x, y, id) => console.log(x, y, id));"
5398
+ },
5399
+ {
5400
+ "area": "Project",
5401
+ "name": "onTouchStart",
5402
+ "signature": "onTouchStart(fn: (x: number, y: number, id: number) => void): () => void",
5403
+ "summary": "Subscribe to touch start events with world coordinates and pointer ID.",
5404
+ "params": [
5405
+ "fn: callback receiving x, y and pointer id"
5406
+ ],
5407
+ "example": "project.onTouchStart((x, y, id) => console.log(x, y, id));"
5408
+ },
5409
+ {
5410
+ "area": "Project",
5411
+ "name": "onTouchMove",
5412
+ "signature": "onTouchMove(fn: (x: number, y: number, id: number) => void): () => void",
5413
+ "summary": "Subscribe to touch move events with world coordinates and pointer ID.",
5414
+ "params": [
5415
+ "fn: callback receiving x, y and pointer id"
5416
+ ],
5417
+ "example": "project.onTouchMove((x, y, id) => console.log(x, y, id));"
5418
+ },
5419
+ {
5420
+ "area": "Project",
5421
+ "name": "onTouchEnd",
5422
+ "signature": "onTouchEnd(fn: (x: number, y: number, id: number) => void): () => void",
5423
+ "summary": "Subscribe to touch end events with world coordinates and pointer ID.",
5424
+ "params": [
5425
+ "fn: callback receiving x, y and pointer id"
5426
+ ],
5427
+ "example": "project.onTouchEnd((x, y, id) => console.log(x, y, id));"
5428
+ },
5429
+ {
5430
+ "area": "Project",
5431
+ "name": "onTouchCancel",
5432
+ "signature": "onTouchCancel(fn: (x: number, y: number, id: number) => void): () => void",
5433
+ "summary": "Subscribe to touch cancel events with world coordinates and pointer ID.",
5434
+ "params": [
5435
+ "fn: callback receiving x, y and pointer id"
5436
+ ],
5437
+ "example": "project.onTouchCancel((x, y, id) => console.log(x, y, id));"
5358
5438
  }
5359
5439
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "woofjs-engine",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "A browser game engine for JavaScript and TypeScript, ported from WoofJS, with components for shadcn/ui, shadcn-svelte and Optimus UI.",
6
6
  "types": "./dist/types/index.d.ts",