virtualJoystick.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import { Vector3, Vector2 } from "../Maths/math.vector.js";
  2. import { StringDictionary } from "./stringDictionary.js";
  3. // Mainly based on these 2 articles :
  4. // Creating an universal virtual touch joystick working for all Touch models thanks to Hand.JS : http://blogs.msdn.com/b/davrous/archive/2013/02/22/creating-an-universal-virtual-touch-joystick-working-for-all-touch-models-thanks-to-hand-js.aspx
  5. // & on Seb Lee-Delisle original work: http://seb.ly/2011/04/multi-touch-game-controller-in-javascripthtml5-for-ipad/
  6. /**
  7. * Defines the potential axis of a Joystick
  8. */
  9. export var JoystickAxis;
  10. (function (JoystickAxis) {
  11. /** X axis */
  12. JoystickAxis[JoystickAxis["X"] = 0] = "X";
  13. /** Y axis */
  14. JoystickAxis[JoystickAxis["Y"] = 1] = "Y";
  15. /** Z axis */
  16. JoystickAxis[JoystickAxis["Z"] = 2] = "Z";
  17. })(JoystickAxis || (JoystickAxis = {}));
  18. /**
  19. * Class used to define virtual joystick (used in touch mode)
  20. */
  21. export class VirtualJoystick {
  22. static _GetDefaultOptions() {
  23. return {
  24. puckSize: 40,
  25. containerSize: 60,
  26. color: "cyan",
  27. puckImage: undefined,
  28. containerImage: undefined,
  29. position: undefined,
  30. alwaysVisible: false,
  31. limitToContainer: false,
  32. };
  33. }
  34. /**
  35. * Creates a new virtual joystick
  36. * @param leftJoystick defines that the joystick is for left hand (false by default)
  37. * @param customizations Defines the options we want to customize the VirtualJoystick
  38. */
  39. constructor(leftJoystick, customizations) {
  40. this._released = false;
  41. const options = {
  42. ...VirtualJoystick._GetDefaultOptions(),
  43. ...customizations,
  44. };
  45. if (leftJoystick) {
  46. this._leftJoystick = true;
  47. }
  48. else {
  49. this._leftJoystick = false;
  50. }
  51. VirtualJoystick._GlobalJoystickIndex++;
  52. // By default left & right arrow keys are moving the X
  53. // and up & down keys are moving the Y
  54. this._axisTargetedByLeftAndRight = JoystickAxis.X;
  55. this._axisTargetedByUpAndDown = JoystickAxis.Y;
  56. this.reverseLeftRight = false;
  57. this.reverseUpDown = false;
  58. // collections of pointers
  59. this._touches = new StringDictionary();
  60. this.deltaPosition = Vector3.Zero();
  61. this._joystickSensibility = 25;
  62. this._inversedSensibility = 1 / (this._joystickSensibility / 1000);
  63. this._onResize = () => {
  64. VirtualJoystick._VJCanvasWidth = window.innerWidth;
  65. VirtualJoystick._VJCanvasHeight = window.innerHeight;
  66. if (VirtualJoystick.Canvas) {
  67. VirtualJoystick.Canvas.width = VirtualJoystick._VJCanvasWidth;
  68. VirtualJoystick.Canvas.height = VirtualJoystick._VJCanvasHeight;
  69. }
  70. VirtualJoystick._HalfWidth = VirtualJoystick._VJCanvasWidth / 2;
  71. };
  72. // injecting a canvas element on top of the canvas 3D game
  73. if (!VirtualJoystick.Canvas) {
  74. window.addEventListener("resize", this._onResize, false);
  75. VirtualJoystick.Canvas = document.createElement("canvas");
  76. VirtualJoystick._VJCanvasWidth = window.innerWidth;
  77. VirtualJoystick._VJCanvasHeight = window.innerHeight;
  78. VirtualJoystick.Canvas.width = window.innerWidth;
  79. VirtualJoystick.Canvas.height = window.innerHeight;
  80. VirtualJoystick.Canvas.style.width = "100%";
  81. VirtualJoystick.Canvas.style.height = "100%";
  82. VirtualJoystick.Canvas.style.position = "absolute";
  83. VirtualJoystick.Canvas.style.backgroundColor = "transparent";
  84. VirtualJoystick.Canvas.style.top = "0px";
  85. VirtualJoystick.Canvas.style.left = "0px";
  86. VirtualJoystick.Canvas.style.zIndex = "5";
  87. VirtualJoystick.Canvas.style.touchAction = "none"; // fix https://forum.babylonjs.com/t/virtualjoystick-needs-to-set-style-touch-action-none-explicitly/9562
  88. // Support for jQuery PEP polyfill
  89. VirtualJoystick.Canvas.setAttribute("touch-action", "none");
  90. const context = VirtualJoystick.Canvas.getContext("2d");
  91. if (!context) {
  92. throw new Error("Unable to create canvas for virtual joystick");
  93. }
  94. VirtualJoystick._VJCanvasContext = context;
  95. VirtualJoystick._VJCanvasContext.strokeStyle = "#ffffff";
  96. VirtualJoystick._VJCanvasContext.lineWidth = 2;
  97. document.body.appendChild(VirtualJoystick.Canvas);
  98. }
  99. VirtualJoystick._HalfWidth = VirtualJoystick.Canvas.width / 2;
  100. this.pressed = false;
  101. this.limitToContainer = options.limitToContainer;
  102. // default joystick color
  103. this._joystickColor = options.color;
  104. // default joystick size
  105. this.containerSize = options.containerSize;
  106. this.puckSize = options.puckSize;
  107. if (options.position) {
  108. this.setPosition(options.position.x, options.position.y);
  109. }
  110. if (options.puckImage) {
  111. this.setPuckImage(options.puckImage);
  112. }
  113. if (options.containerImage) {
  114. this.setContainerImage(options.containerImage);
  115. }
  116. if (options.alwaysVisible) {
  117. VirtualJoystick._AlwaysVisibleSticks++;
  118. }
  119. // must come after position potentially set
  120. this.alwaysVisible = options.alwaysVisible;
  121. this._joystickPointerId = -1;
  122. // current joystick position
  123. this._joystickPointerPos = new Vector2(0, 0);
  124. this._joystickPreviousPointerPos = new Vector2(0, 0);
  125. // origin joystick position
  126. this._joystickPointerStartPos = new Vector2(0, 0);
  127. this._deltaJoystickVector = new Vector2(0, 0);
  128. this._onPointerDownHandlerRef = (evt) => {
  129. this._onPointerDown(evt);
  130. };
  131. this._onPointerMoveHandlerRef = (evt) => {
  132. this._onPointerMove(evt);
  133. };
  134. this._onPointerUpHandlerRef = (evt) => {
  135. this._onPointerUp(evt);
  136. };
  137. VirtualJoystick.Canvas.addEventListener("pointerdown", this._onPointerDownHandlerRef, false);
  138. VirtualJoystick.Canvas.addEventListener("pointermove", this._onPointerMoveHandlerRef, false);
  139. VirtualJoystick.Canvas.addEventListener("pointerup", this._onPointerUpHandlerRef, false);
  140. VirtualJoystick.Canvas.addEventListener("pointerout", this._onPointerUpHandlerRef, false);
  141. VirtualJoystick.Canvas.addEventListener("contextmenu", (evt) => {
  142. evt.preventDefault(); // Disables system menu
  143. }, false);
  144. requestAnimationFrame(() => {
  145. this._drawVirtualJoystick();
  146. });
  147. }
  148. /**
  149. * Defines joystick sensibility (ie. the ratio between a physical move and virtual joystick position change)
  150. * @param newJoystickSensibility defines the new sensibility
  151. */
  152. setJoystickSensibility(newJoystickSensibility) {
  153. this._joystickSensibility = newJoystickSensibility;
  154. this._inversedSensibility = 1 / (this._joystickSensibility / 1000);
  155. }
  156. _onPointerDown(e) {
  157. let positionOnScreenCondition;
  158. e.preventDefault();
  159. if (this._leftJoystick === true) {
  160. positionOnScreenCondition = e.clientX < VirtualJoystick._HalfWidth;
  161. }
  162. else {
  163. positionOnScreenCondition = e.clientX > VirtualJoystick._HalfWidth;
  164. }
  165. if (positionOnScreenCondition && this._joystickPointerId < 0) {
  166. // First contact will be dedicated to the virtual joystick
  167. this._joystickPointerId = e.pointerId;
  168. if (this._joystickPosition) {
  169. this._joystickPointerStartPos = this._joystickPosition.clone();
  170. this._joystickPointerPos = this._joystickPosition.clone();
  171. this._joystickPreviousPointerPos = this._joystickPosition.clone();
  172. // in case the user only clicks down && doesn't move:
  173. // this ensures the delta is properly set
  174. this._onPointerMove(e);
  175. }
  176. else {
  177. this._joystickPointerStartPos.x = e.clientX;
  178. this._joystickPointerStartPos.y = e.clientY;
  179. this._joystickPointerPos = this._joystickPointerStartPos.clone();
  180. this._joystickPreviousPointerPos = this._joystickPointerStartPos.clone();
  181. }
  182. this._deltaJoystickVector.x = 0;
  183. this._deltaJoystickVector.y = 0;
  184. this.pressed = true;
  185. this._touches.add(e.pointerId.toString(), e);
  186. }
  187. else {
  188. // You can only trigger the action buttons with a joystick declared
  189. if (VirtualJoystick._GlobalJoystickIndex < 2 && this._action) {
  190. this._action();
  191. this._touches.add(e.pointerId.toString(), { x: e.clientX, y: e.clientY, prevX: e.clientX, prevY: e.clientY });
  192. }
  193. }
  194. }
  195. _onPointerMove(e) {
  196. // If the current pointer is the one associated to the joystick (first touch contact)
  197. if (this._joystickPointerId == e.pointerId) {
  198. // limit to container if need be
  199. if (this.limitToContainer) {
  200. const vector = new Vector2(e.clientX - this._joystickPointerStartPos.x, e.clientY - this._joystickPointerStartPos.y);
  201. const distance = vector.length();
  202. if (distance > this.containerSize) {
  203. vector.scaleInPlace(this.containerSize / distance);
  204. }
  205. this._joystickPointerPos.x = this._joystickPointerStartPos.x + vector.x;
  206. this._joystickPointerPos.y = this._joystickPointerStartPos.y + vector.y;
  207. }
  208. else {
  209. this._joystickPointerPos.x = e.clientX;
  210. this._joystickPointerPos.y = e.clientY;
  211. }
  212. // create delta vector
  213. this._deltaJoystickVector = this._joystickPointerPos.clone();
  214. this._deltaJoystickVector = this._deltaJoystickVector.subtract(this._joystickPointerStartPos);
  215. // if a joystick is always visible, there will be clipping issues if
  216. // you drag the puck from one over the container of the other
  217. if (0 < VirtualJoystick._AlwaysVisibleSticks) {
  218. if (this._leftJoystick) {
  219. this._joystickPointerPos.x = Math.min(VirtualJoystick._HalfWidth, this._joystickPointerPos.x);
  220. }
  221. else {
  222. this._joystickPointerPos.x = Math.max(VirtualJoystick._HalfWidth, this._joystickPointerPos.x);
  223. }
  224. }
  225. const directionLeftRight = this.reverseLeftRight ? -1 : 1;
  226. const deltaJoystickX = (directionLeftRight * this._deltaJoystickVector.x) / this._inversedSensibility;
  227. switch (this._axisTargetedByLeftAndRight) {
  228. case JoystickAxis.X:
  229. this.deltaPosition.x = Math.min(1, Math.max(-1, deltaJoystickX));
  230. break;
  231. case JoystickAxis.Y:
  232. this.deltaPosition.y = Math.min(1, Math.max(-1, deltaJoystickX));
  233. break;
  234. case JoystickAxis.Z:
  235. this.deltaPosition.z = Math.min(1, Math.max(-1, deltaJoystickX));
  236. break;
  237. }
  238. const directionUpDown = this.reverseUpDown ? 1 : -1;
  239. const deltaJoystickY = (directionUpDown * this._deltaJoystickVector.y) / this._inversedSensibility;
  240. switch (this._axisTargetedByUpAndDown) {
  241. case JoystickAxis.X:
  242. this.deltaPosition.x = Math.min(1, Math.max(-1, deltaJoystickY));
  243. break;
  244. case JoystickAxis.Y:
  245. this.deltaPosition.y = Math.min(1, Math.max(-1, deltaJoystickY));
  246. break;
  247. case JoystickAxis.Z:
  248. this.deltaPosition.z = Math.min(1, Math.max(-1, deltaJoystickY));
  249. break;
  250. }
  251. }
  252. else {
  253. const data = this._touches.get(e.pointerId.toString());
  254. if (data) {
  255. data.x = e.clientX;
  256. data.y = e.clientY;
  257. }
  258. }
  259. }
  260. _onPointerUp(e) {
  261. if (this._joystickPointerId == e.pointerId) {
  262. this._clearPreviousDraw();
  263. this._joystickPointerId = -1;
  264. this.pressed = false;
  265. }
  266. else {
  267. const touch = this._touches.get(e.pointerId.toString());
  268. if (touch) {
  269. VirtualJoystick._VJCanvasContext.clearRect(touch.prevX - 44, touch.prevY - 44, 88, 88);
  270. }
  271. }
  272. this._deltaJoystickVector.x = 0;
  273. this._deltaJoystickVector.y = 0;
  274. this._touches.remove(e.pointerId.toString());
  275. }
  276. /**
  277. * Change the color of the virtual joystick
  278. * @param newColor a string that must be a CSS color value (like "red") or the hexa value (like "#FF0000")
  279. */
  280. setJoystickColor(newColor) {
  281. this._joystickColor = newColor;
  282. }
  283. /**
  284. * Size of the joystick's container
  285. */
  286. set containerSize(newSize) {
  287. this._joystickContainerSize = newSize;
  288. this._clearContainerSize = ~~(this._joystickContainerSize * 2.1);
  289. this._clearContainerSizeOffset = ~~(this._clearContainerSize / 2);
  290. }
  291. get containerSize() {
  292. return this._joystickContainerSize;
  293. }
  294. /**
  295. * Size of the joystick's puck
  296. */
  297. set puckSize(newSize) {
  298. this._joystickPuckSize = newSize;
  299. this._clearPuckSize = ~~(this._joystickPuckSize * 2.1);
  300. this._clearPuckSizeOffset = ~~(this._clearPuckSize / 2);
  301. }
  302. get puckSize() {
  303. return this._joystickPuckSize;
  304. }
  305. /**
  306. * Clears the set position of the joystick
  307. */
  308. clearPosition() {
  309. this.alwaysVisible = false;
  310. this._joystickPosition = null;
  311. }
  312. /**
  313. * Defines whether or not the joystick container is always visible
  314. */
  315. set alwaysVisible(value) {
  316. if (this._alwaysVisible === value) {
  317. return;
  318. }
  319. if (value && this._joystickPosition) {
  320. VirtualJoystick._AlwaysVisibleSticks++;
  321. this._alwaysVisible = true;
  322. }
  323. else {
  324. VirtualJoystick._AlwaysVisibleSticks--;
  325. this._alwaysVisible = false;
  326. }
  327. }
  328. get alwaysVisible() {
  329. return this._alwaysVisible;
  330. }
  331. /**
  332. * Sets the constant position of the Joystick container
  333. * @param x X axis coordinate
  334. * @param y Y axis coordinate
  335. */
  336. setPosition(x, y) {
  337. // just in case position is moved while the container is visible
  338. if (this._joystickPointerStartPos) {
  339. this._clearPreviousDraw();
  340. }
  341. this._joystickPosition = new Vector2(x, y);
  342. }
  343. /**
  344. * Defines a callback to call when the joystick is touched
  345. * @param action defines the callback
  346. */
  347. setActionOnTouch(action) {
  348. this._action = action;
  349. }
  350. /**
  351. * Defines which axis you'd like to control for left & right
  352. * @param axis defines the axis to use
  353. */
  354. setAxisForLeftRight(axis) {
  355. switch (axis) {
  356. case JoystickAxis.X:
  357. case JoystickAxis.Y:
  358. case JoystickAxis.Z:
  359. this._axisTargetedByLeftAndRight = axis;
  360. break;
  361. default:
  362. this._axisTargetedByLeftAndRight = JoystickAxis.X;
  363. break;
  364. }
  365. }
  366. /**
  367. * Defines which axis you'd like to control for up & down
  368. * @param axis defines the axis to use
  369. */
  370. setAxisForUpDown(axis) {
  371. switch (axis) {
  372. case JoystickAxis.X:
  373. case JoystickAxis.Y:
  374. case JoystickAxis.Z:
  375. this._axisTargetedByUpAndDown = axis;
  376. break;
  377. default:
  378. this._axisTargetedByUpAndDown = JoystickAxis.Y;
  379. break;
  380. }
  381. }
  382. /**
  383. * Clears the canvas from the previous puck / container draw
  384. */
  385. _clearPreviousDraw() {
  386. const jp = this._joystickPosition || this._joystickPointerStartPos;
  387. // clear container pixels
  388. VirtualJoystick._VJCanvasContext.clearRect(jp.x - this._clearContainerSizeOffset, jp.y - this._clearContainerSizeOffset, this._clearContainerSize, this._clearContainerSize);
  389. // clear puck pixels + 1 pixel for the change made before it moved
  390. VirtualJoystick._VJCanvasContext.clearRect(this._joystickPreviousPointerPos.x - this._clearPuckSizeOffset - 1, this._joystickPreviousPointerPos.y - this._clearPuckSizeOffset - 1, this._clearPuckSize + 2, this._clearPuckSize + 2);
  391. }
  392. /**
  393. * Loads `urlPath` to be used for the container's image
  394. * @param urlPath defines the urlPath of an image to use
  395. */
  396. setContainerImage(urlPath) {
  397. const image = new Image();
  398. image.src = urlPath;
  399. image.onload = () => (this._containerImage = image);
  400. }
  401. /**
  402. * Loads `urlPath` to be used for the puck's image
  403. * @param urlPath defines the urlPath of an image to use
  404. */
  405. setPuckImage(urlPath) {
  406. const image = new Image();
  407. image.src = urlPath;
  408. image.onload = () => (this._puckImage = image);
  409. }
  410. /**
  411. * Draws the Virtual Joystick's container
  412. */
  413. _drawContainer() {
  414. const jp = this._joystickPosition || this._joystickPointerStartPos;
  415. this._clearPreviousDraw();
  416. if (this._containerImage) {
  417. VirtualJoystick._VJCanvasContext.drawImage(this._containerImage, jp.x - this.containerSize, jp.y - this.containerSize, this.containerSize * 2, this.containerSize * 2);
  418. }
  419. else {
  420. // outer container
  421. VirtualJoystick._VJCanvasContext.beginPath();
  422. VirtualJoystick._VJCanvasContext.strokeStyle = this._joystickColor;
  423. VirtualJoystick._VJCanvasContext.lineWidth = 2;
  424. VirtualJoystick._VJCanvasContext.arc(jp.x, jp.y, this.containerSize, 0, Math.PI * 2, true);
  425. VirtualJoystick._VJCanvasContext.stroke();
  426. VirtualJoystick._VJCanvasContext.closePath();
  427. // inner container
  428. VirtualJoystick._VJCanvasContext.beginPath();
  429. VirtualJoystick._VJCanvasContext.lineWidth = 6;
  430. VirtualJoystick._VJCanvasContext.strokeStyle = this._joystickColor;
  431. VirtualJoystick._VJCanvasContext.arc(jp.x, jp.y, this.puckSize, 0, Math.PI * 2, true);
  432. VirtualJoystick._VJCanvasContext.stroke();
  433. VirtualJoystick._VJCanvasContext.closePath();
  434. }
  435. }
  436. /**
  437. * Draws the Virtual Joystick's puck
  438. */
  439. _drawPuck() {
  440. if (this._puckImage) {
  441. VirtualJoystick._VJCanvasContext.drawImage(this._puckImage, this._joystickPointerPos.x - this.puckSize, this._joystickPointerPos.y - this.puckSize, this.puckSize * 2, this.puckSize * 2);
  442. }
  443. else {
  444. VirtualJoystick._VJCanvasContext.beginPath();
  445. VirtualJoystick._VJCanvasContext.strokeStyle = this._joystickColor;
  446. VirtualJoystick._VJCanvasContext.lineWidth = 2;
  447. VirtualJoystick._VJCanvasContext.arc(this._joystickPointerPos.x, this._joystickPointerPos.y, this.puckSize, 0, Math.PI * 2, true);
  448. VirtualJoystick._VJCanvasContext.stroke();
  449. VirtualJoystick._VJCanvasContext.closePath();
  450. }
  451. }
  452. _drawVirtualJoystick() {
  453. // canvas released? don't continue iterating
  454. if (this._released) {
  455. return;
  456. }
  457. if (this.alwaysVisible) {
  458. this._drawContainer();
  459. }
  460. if (this.pressed) {
  461. this._touches.forEach((key, touch) => {
  462. if (touch.pointerId === this._joystickPointerId) {
  463. if (!this.alwaysVisible) {
  464. this._drawContainer();
  465. }
  466. this._drawPuck();
  467. // store current pointer for next clear
  468. this._joystickPreviousPointerPos = this._joystickPointerPos.clone();
  469. }
  470. else {
  471. VirtualJoystick._VJCanvasContext.clearRect(touch.prevX - 44, touch.prevY - 44, 88, 88);
  472. VirtualJoystick._VJCanvasContext.beginPath();
  473. VirtualJoystick._VJCanvasContext.fillStyle = "white";
  474. VirtualJoystick._VJCanvasContext.beginPath();
  475. VirtualJoystick._VJCanvasContext.strokeStyle = "red";
  476. VirtualJoystick._VJCanvasContext.lineWidth = 6;
  477. VirtualJoystick._VJCanvasContext.arc(touch.x, touch.y, 40, 0, Math.PI * 2, true);
  478. VirtualJoystick._VJCanvasContext.stroke();
  479. VirtualJoystick._VJCanvasContext.closePath();
  480. touch.prevX = touch.x;
  481. touch.prevY = touch.y;
  482. }
  483. });
  484. }
  485. requestAnimationFrame(() => {
  486. this._drawVirtualJoystick();
  487. });
  488. }
  489. /**
  490. * Release internal HTML canvas
  491. */
  492. releaseCanvas() {
  493. if (VirtualJoystick.Canvas) {
  494. VirtualJoystick.Canvas.removeEventListener("pointerdown", this._onPointerDownHandlerRef);
  495. VirtualJoystick.Canvas.removeEventListener("pointermove", this._onPointerMoveHandlerRef);
  496. VirtualJoystick.Canvas.removeEventListener("pointerup", this._onPointerUpHandlerRef);
  497. VirtualJoystick.Canvas.removeEventListener("pointerout", this._onPointerUpHandlerRef);
  498. window.removeEventListener("resize", this._onResize);
  499. document.body.removeChild(VirtualJoystick.Canvas);
  500. VirtualJoystick.Canvas = null;
  501. }
  502. this._released = true;
  503. }
  504. }
  505. // Used to draw the virtual joystick inside a 2D canvas on top of the WebGL rendering canvas
  506. VirtualJoystick._GlobalJoystickIndex = 0;
  507. VirtualJoystick._AlwaysVisibleSticks = 0;
  508. //# sourceMappingURL=virtualJoystick.js.map