canvasengine 2.0.0-beta.14 → 2.0.0-beta.15

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.ts CHANGED
@@ -6,6 +6,8 @@ export { isObservable } from 'rxjs';
6
6
  import { Node } from 'yoga-layout';
7
7
  import * as PIXI from 'pixi.js';
8
8
  import { FederatedPointerEvent, ObservablePoint, Graphics as Graphics$1, Texture, TextStyle, Matrix } from 'pixi.js';
9
+ import * as howler from 'howler';
10
+ export { howler as Howl };
9
11
  export { Howler } from 'howler';
10
12
  import * as popmotion from 'popmotion';
11
13
 
@@ -74,6 +76,7 @@ type DragProps = {
74
76
  end?: () => void;
75
77
  snap?: SignalOrPrimitive<number>;
76
78
  direction?: SignalOrPrimitive<'x' | 'y' | 'all'>;
79
+ keyToPress?: SignalOrPrimitive<string[]>;
77
80
  viewport?: {
78
81
  edgeThreshold?: SignalOrPrimitive<number>;
79
82
  maxSpeed?: SignalOrPrimitive<number>;
package/dist/index.js CHANGED
@@ -1222,6 +1222,29 @@ function loop(itemsSubject, createElementFn) {
1222
1222
  el.destroy();
1223
1223
  elementMap.delete(change.index);
1224
1224
  });
1225
+ } else if (change.type === "update" && change.index !== void 0 && change.items.length === 1) {
1226
+ const index = change.index;
1227
+ const newItem = change.items[0];
1228
+ if (index >= elements.length || elements[index] === void 0 || !elementMap.has(index)) {
1229
+ const newElement = createElementFn(newItem, index);
1230
+ if (newElement) {
1231
+ elements.splice(index, 0, newElement);
1232
+ elementMap.set(index, newElement);
1233
+ } else {
1234
+ console.warn(`Element creation returned null for index ${index} during add-like update.`);
1235
+ }
1236
+ } else {
1237
+ const oldElement = elements[index];
1238
+ oldElement.destroy();
1239
+ const newElement = createElementFn(newItem, index);
1240
+ if (newElement) {
1241
+ elements[index] = newElement;
1242
+ elementMap.set(index, newElement);
1243
+ } else {
1244
+ elements.splice(index, 1);
1245
+ elementMap.delete(index);
1246
+ }
1247
+ }
1225
1248
  }
1226
1249
  subscriber.next({
1227
1250
  elements: [...elements]
@@ -1485,7 +1508,8 @@ var Sound = class extends Directive {
1485
1508
  onMount(element) {
1486
1509
  const { props } = element;
1487
1510
  const tick2 = props.context.tick;
1488
- const { src, autoplay, loop: loop2, volume, spatial } = props.sound;
1511
+ const propsSound = props.sound.value ?? props.sound;
1512
+ const { src, autoplay, loop: loop2, volume, spatial } = propsSound;
1489
1513
  this.sound = new Howl({
1490
1514
  src,
1491
1515
  autoplay,
@@ -1493,8 +1517,8 @@ var Sound = class extends Directive {
1493
1517
  volume
1494
1518
  });
1495
1519
  for (let event of EVENTS) {
1496
- if (!props.sound[event]) continue;
1497
- const fn = props.sound[event];
1520
+ if (!propsSound[event]) continue;
1521
+ const fn = propsSound[event];
1498
1522
  this.eventsFn.push(fn);
1499
1523
  this.sound.on(event, fn);
1500
1524
  }
@@ -1514,7 +1538,7 @@ var Sound = class extends Directive {
1514
1538
  }
1515
1539
  }
1516
1540
  onUpdate(props) {
1517
- const { volume, loop: loop2, mute, seek, playing, rate, spatial } = props;
1541
+ const { volume, loop: loop2, mute, seek, playing, rate, spatial } = props.value ?? props;
1518
1542
  if (volume != void 0) this.sound.volume(volume);
1519
1543
  if (loop2 != void 0) this.sound.loop(loop2);
1520
1544
  if (mute != void 0) this.sound.mute(mute);
@@ -1594,12 +1618,18 @@ var Drag = class extends Directive {
1594
1618
  this.viewport = null;
1595
1619
  this.animationFrameId = null;
1596
1620
  this.lastPointerPosition = new Point();
1621
+ this.pressedKeys = /* @__PURE__ */ new Set();
1622
+ this.pointerIsDown = false;
1597
1623
  this.onDragMoveHandler = () => {
1598
1624
  };
1599
1625
  this.onDragEndHandler = () => {
1600
1626
  };
1601
1627
  this.onDragStartHandler = () => {
1602
1628
  };
1629
+ this.onKeyDownHandler = () => {
1630
+ };
1631
+ this.onKeyUpHandler = () => {
1632
+ };
1603
1633
  this.subscriptions = [];
1604
1634
  }
1605
1635
  onInit(element) {
@@ -1607,6 +1637,8 @@ var Drag = class extends Directive {
1607
1637
  this.onDragMoveHandler = this.onDragMove.bind(this);
1608
1638
  this.onDragEndHandler = this.onDragEnd.bind(this);
1609
1639
  this.onDragStartHandler = this.onPointerDown.bind(this);
1640
+ this.onKeyDownHandler = this.onKeyDown.bind(this);
1641
+ this.onKeyUpHandler = this.onKeyUp.bind(this);
1610
1642
  }
1611
1643
  onMount(element) {
1612
1644
  const { rootElement, canvasSize, viewport, tick: tick2 } = element.props.context;
@@ -1631,6 +1663,9 @@ var Drag = class extends Directive {
1631
1663
  instance.on("pointerdown", this.onDragStartHandler);
1632
1664
  this.stageRef.on("pointerup", this.onDragEndHandler);
1633
1665
  this.stageRef.on("pointerupoutside", this.onDragEndHandler);
1666
+ const keysToPress = dragProps.keyToPress ? dragProps.keyToPress : [];
1667
+ window.addEventListener("keydown", this.onKeyDownHandler);
1668
+ window.addEventListener("keyup", this.onKeyUpHandler);
1634
1669
  this.subscriptions = [
1635
1670
  tick2.observable.subscribe(() => {
1636
1671
  if (this.isDragging && this.viewport) {
@@ -1645,7 +1680,8 @@ var Drag = class extends Directive {
1645
1680
  const options = useProps(drag?.value ?? drag, {
1646
1681
  snap: 0,
1647
1682
  viewport: {},
1648
- direction: "all"
1683
+ direction: "all",
1684
+ keyToPress: []
1649
1685
  });
1650
1686
  options.viewport = useProps(options.viewport, {
1651
1687
  edgeThreshold: 300,
@@ -1755,7 +1791,8 @@ var Drag = class extends Directive {
1755
1791
  * Handles drag end event and stops viewport movement
1756
1792
  */
1757
1793
  onDragEnd() {
1758
- if (!this.isDragging || !this.elementRef) return;
1794
+ this.pointerIsDown = false;
1795
+ if (!this.isDragging) return;
1759
1796
  const dragProps = this.dragProps;
1760
1797
  this.isDragging = false;
1761
1798
  dragProps?.end?.();
@@ -1763,16 +1800,61 @@ var Drag = class extends Directive {
1763
1800
  this.stageRef.off("pointermove", this.onDragMoveHandler);
1764
1801
  }
1765
1802
  }
1803
+ onKeyDown(event) {
1804
+ this.pressedKeys.add(event.code);
1805
+ this.pressedKeys.add(event.key.toLowerCase());
1806
+ if (this.pointerIsDown && !this.isDragging && this.areRequiredKeysPressed()) {
1807
+ this.startDrag();
1808
+ }
1809
+ }
1810
+ onKeyUp(event) {
1811
+ this.pressedKeys.delete(event.code);
1812
+ this.pressedKeys.delete(event.key.toLowerCase());
1813
+ if (this.isDragging && !this.areRequiredKeysPressed()) {
1814
+ this.onDragEnd();
1815
+ }
1816
+ }
1817
+ areRequiredKeysPressed() {
1818
+ const keyToPress = this.dragProps.keyToPress ? this.dragProps.keyToPress : [];
1819
+ if (!keyToPress || keyToPress.length === 0) {
1820
+ return true;
1821
+ }
1822
+ return keyToPress.some((key) => {
1823
+ if (this.pressedKeys.has(key)) {
1824
+ return true;
1825
+ }
1826
+ if (key.toLowerCase() === "space") {
1827
+ return this.pressedKeys.has("Space") || this.pressedKeys.has(" ");
1828
+ }
1829
+ if (key.toLowerCase() === "shift") {
1830
+ return this.pressedKeys.has("ShiftLeft") || this.pressedKeys.has("ShiftRight");
1831
+ }
1832
+ if (key.toLowerCase() === "control" || key.toLowerCase() === "ctrl") {
1833
+ return this.pressedKeys.has("ControlLeft") || this.pressedKeys.has("ControlRight");
1834
+ }
1835
+ if (key.toLowerCase() === "alt") {
1836
+ return this.pressedKeys.has("AltLeft") || this.pressedKeys.has("AltRight");
1837
+ }
1838
+ return false;
1839
+ });
1840
+ }
1766
1841
  onPointerDown(event) {
1767
1842
  if (!this.elementRef?.componentInstance || !this.stageRef || !this.elementRef.componentInstance.parent) return;
1843
+ this.pointerIsDown = true;
1768
1844
  const instance = this.elementRef.componentInstance;
1769
1845
  const parent = instance.parent;
1770
- const dragProps = this.dragProps;
1771
1846
  const parentLocalPointer = parent.toLocal(event.global);
1772
1847
  this.offsetInParent.x = parentLocalPointer.x - instance.position.x;
1773
1848
  this.offsetInParent.y = parentLocalPointer.y - instance.position.y;
1774
- this.isDragging = true;
1775
1849
  this.lastPointerPosition.copyFrom(event.global);
1850
+ if (this.areRequiredKeysPressed()) {
1851
+ this.startDrag();
1852
+ }
1853
+ }
1854
+ startDrag() {
1855
+ if (this.isDragging || !this.stageRef) return;
1856
+ this.isDragging = true;
1857
+ const dragProps = this.dragProps;
1776
1858
  dragProps?.start?.();
1777
1859
  this.stageRef.on("pointermove", this.onDragMoveHandler);
1778
1860
  }
@@ -1793,8 +1875,12 @@ var Drag = class extends Directive {
1793
1875
  this.stageRef.off("pointerup", this.onDragEndHandler);
1794
1876
  this.stageRef.off("pointerupoutside", this.onDragEndHandler);
1795
1877
  }
1878
+ window.removeEventListener("keydown", this.onKeyDownHandler);
1879
+ window.removeEventListener("keyup", this.onKeyUpHandler);
1796
1880
  this.stageRef = null;
1797
1881
  this.viewport = null;
1882
+ this.pressedKeys.clear();
1883
+ this.pointerIsDown = false;
1798
1884
  }
1799
1885
  };
1800
1886
  registerDirective("drag", Drag);
@@ -2533,6 +2619,7 @@ function ParticlesEmitter(props) {
2533
2619
  }
2534
2620
 
2535
2621
  // src/components/Sprite.ts
2622
+ import { Howl as Howl2 } from "howler";
2536
2623
  import { computed, effect as effect7, isSignal as isSignal4 } from "@signe/reactive";
2537
2624
  import {
2538
2625
  Assets,
@@ -2854,6 +2941,12 @@ var CanvasSprite = class extends DisplayObject(PixiSprite) {
2854
2941
  }
2855
2942
  const sound = this.currentAnimation.data.sound;
2856
2943
  if (sound) {
2944
+ new Howl2({
2945
+ src: sound,
2946
+ autoplay: true,
2947
+ loop: false,
2948
+ volume: 1
2949
+ });
2857
2950
  }
2858
2951
  this.update({
2859
2952
  deltaRatio: 1
@@ -3495,6 +3588,7 @@ var RadialGradient = class {
3495
3588
 
3496
3589
  // src/index.ts
3497
3590
  import { isObservable } from "rxjs";
3591
+ import * as Howl3 from "howler";
3498
3592
  export {
3499
3593
  Canvas2 as Canvas,
3500
3594
  Circle,
@@ -3504,6 +3598,7 @@ export {
3504
3598
  Easing,
3505
3599
  Ellipse,
3506
3600
  Graphics,
3601
+ Howl3 as Howl,
3507
3602
  Howler,
3508
3603
  NineSliceSprite,
3509
3604
  ParticlesEmitter,