UNPKG

sigma

Version:

A JavaScript library dedicated to graph drawing.

147 lines (146 loc) 4.62 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getWheelDelta = exports.getTouchCoords = exports.getTouchesArray = exports.getMouseCoords = exports.getPosition = exports.getY = exports.getX = void 0; /** * Sigma.js Captor Class * ====================== * @module */ var events_1 = require("events"); /** * Captor utils functions * ====================== */ /** * Extract the local X position from a mouse event or touch object. * * @param {event} e - A mouse event or touch object. * @return {number} The local X value of the mouse. */ function getX(e) { if (typeof e.offsetX !== "undefined") return e.offsetX; if (typeof e.clientX !== "undefined") return e.clientX; throw new Error("Captor: could not extract x from event."); } exports.getX = getX; /** * Extract the local Y position from a mouse event or touch object. * * @param {event} e - A mouse event or touch object. * @return {number} The local Y value of the mouse. */ function getY(e) { if (typeof e.offsetY !== "undefined") return e.offsetY; if (typeof e.clientY !== "undefined") return e.clientY; throw new Error("Captor: could not extract y from event."); } exports.getY = getY; /** * Extract the local X and Y coordinates from a mouse event or touch object. * * @param {event} e - A mouse event or touch object. * @return {number} The local Y value of the mouse. */ function getPosition(e) { return { x: getX(e), y: getY(e), }; } exports.getPosition = getPosition; /** * Convert mouse coords to sigma coords. * * @param {event} e - A mouse event or touch object. * * @return {object} */ function getMouseCoords(e) { return { x: getX(e), y: getY(e), clientX: e.clientX, clientY: e.clientY, ctrlKey: e.ctrlKey, metaKey: e.metaKey, altKey: e.altKey, shiftKey: e.shiftKey, // TODO: this is not ideal... But I am wondering why we don't just pass the event through preventDefault: e.preventDefault.bind(e), original: e, }; } exports.getMouseCoords = getMouseCoords; var MAX_TOUCHES = 2; function getTouchesArray(touches) { var arr = []; for (var i = 0, l = Math.min(touches.length, MAX_TOUCHES); i < l; i++) arr.push(touches[i]); return arr; } exports.getTouchesArray = getTouchesArray; /** * Convert touch coords to sigma coords. */ function getTouchCoords(e) { return { touches: getTouchesArray(e.touches).map(getPosition), ctrlKey: e.ctrlKey, metaKey: e.metaKey, altKey: e.altKey, shiftKey: e.shiftKey, // TODO: same as for getMouseCoords preventDefault: e.preventDefault.bind(e), original: e, }; } exports.getTouchCoords = getTouchCoords; /** * Extract the wheel delta from a mouse event or touch object. * * @param {event} e - A mouse event or touch object. * @return {number} The wheel delta of the mouse. */ function getWheelDelta(e) { // TODO: check those ratios again to ensure a clean Chrome/Firefox compat if (typeof e.deltaY !== "undefined") return (e.deltaY * -3) / 360; if (typeof e.detail !== "undefined") return e.detail / -9; throw new Error("Captor: could not extract delta from event."); } exports.getWheelDelta = getWheelDelta; /** * Abstract class representing a captor like the user's mouse or touch controls. */ var Captor = /** @class */ (function (_super) { __extends(Captor, _super); function Captor(container, camera) { var _this = _super.call(this) || this; // Properties _this.container = container; _this.camera = camera; return _this; } return Captor; }(events_1.EventEmitter)); exports.default = Captor;