@vived/app 2.9.0 → 2.9.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.
@@ -1,3 +1,4 @@
1
+ import { Vector2 } from ".";
1
2
  export interface TouchController {
2
3
  onTouchStart?: (e: TouchEvent) => void;
3
4
  onTouchMove?: (e: TouchEvent) => void;
@@ -6,8 +7,13 @@ export interface TouchController {
6
7
  }
7
8
  export interface TouchInput {
8
9
  eventElement: HTMLElement | undefined;
10
+ eventElementBounds?: DOMRect;
9
11
  setEventElement: (eventElement: HTMLElement) => void;
10
12
  clearInputElement: () => void;
13
+ updateInputElementBounds: () => void;
14
+ getTouchOffsetPosition: (touch: Touch) => Vector2;
15
+ getDistatnceBetweenTouches: (touch1: Touch, touch2: Touch) => number;
16
+ getTouchsCenter: (touches: TouchList) => Vector2;
11
17
  registerController: (controller: TouchController) => void;
12
18
  removeController: (controller: TouchController) => void;
13
19
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.makeTouchInput = void 0;
4
+ const _1 = require(".");
4
5
  function makeTouchInput() {
5
6
  return new TouchInputImp();
6
7
  }
@@ -9,6 +10,40 @@ class TouchInputImp {
9
10
  constructor() {
10
11
  this.controllers = [];
11
12
  this._eventElement = undefined;
13
+ this.updateInputElementBounds = () => {
14
+ if (!this._eventElement)
15
+ return;
16
+ this.eventElementBounds = this._eventElement.getBoundingClientRect(); // Expensive so we cache
17
+ };
18
+ this.getTouchOffsetPosition = (touch) => {
19
+ if (!this.eventElementBounds) {
20
+ console.warn("No target bounds");
21
+ return _1.Vector2.Zero();
22
+ }
23
+ const x = touch.clientX - this.eventElementBounds.x;
24
+ const y = touch.clientY - this.eventElementBounds.y;
25
+ return new _1.Vector2(x, y);
26
+ };
27
+ this.getDistatnceBetweenTouches = (touch1, touch2) => {
28
+ const t1 = this.getTouchOffsetPosition(touch1);
29
+ const t2 = this.getTouchOffsetPosition(touch2);
30
+ const delta = t2.subtract(t1);
31
+ return delta.magnitued;
32
+ };
33
+ this.getTouchsCenter = (touches) => {
34
+ const touchCount = touches.length;
35
+ if (touchCount === 0)
36
+ return _1.Vector2.Zero();
37
+ let sumX = 0;
38
+ let sumY = 0;
39
+ for (let i = 0; i < touchCount; i++) {
40
+ const touch = touches[i];
41
+ const touchPos = this.getTouchOffsetPosition(touch);
42
+ sumX += touchPos.x;
43
+ sumY += touchPos.y;
44
+ }
45
+ return new _1.Vector2(sumX / touches.length, sumY / touches.length);
46
+ };
12
47
  this.setEventElement = (eventElement) => {
13
48
  if (this._eventElement)
14
49
  this.clearInputElement();
@@ -16,6 +51,7 @@ class TouchInputImp {
16
51
  eventElement.addEventListener("touchmove", this.onTouchMove, false);
17
52
  eventElement.addEventListener("touchend", this.onTouchEnd, false);
18
53
  eventElement.addEventListener("touchcancel", this.onTouchCancel, false);
54
+ this.eventElementBounds = eventElement.getBoundingClientRect();
19
55
  this._eventElement = eventElement;
20
56
  };
21
57
  this.onTouchStart = (e) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vived/app",
3
- "version": "2.9.0",
3
+ "version": "2.9.1",
4
4
  "description": "Public package for the VIVED Learning App system",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",