vanilla-swipe
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -14,3 +14,3 @@ import { State, ConstructorProps } from './types'; | ||
cleanupMouseListeners(): void; | ||
getPosition(e: TouchEvent | MouseEvent): import("./types").EventData; | ||
getEventData(e: TouchEvent | MouseEvent): import("./types").EventData; | ||
handleSwipeStart(e: any): void; | ||
@@ -17,0 +17,0 @@ handleSwipeMove(e: any): void; |
@@ -158,16 +158,8 @@ "use strict"; | ||
}, { | ||
key: "getPosition", | ||
value: function getPosition(e) { | ||
var _this$state = this.state, | ||
x = _this$state.x, | ||
y = _this$state.y, | ||
start = _this$state.start; | ||
key: "getEventData", | ||
value: function getEventData(e) { | ||
var rotationAngle = this.props.rotationAngle; | ||
var movingPosition = Utils.calculateMovingPosition(e); | ||
var rotatePosition = Utils.rotateByAngle(movingPosition, rotationAngle); | ||
return Utils.calculatePosition({ | ||
x: x, | ||
y: y, | ||
start: start | ||
}, rotatePosition); | ||
return Utils.calculatePosition(this.state, rotatePosition); | ||
} | ||
@@ -185,3 +177,3 @@ }, { | ||
this.state = { | ||
this.state = Utils.getInitialState({ | ||
start: Date.now(), | ||
@@ -191,3 +183,3 @@ x: x, | ||
isSwiping: false | ||
}; | ||
}); | ||
} | ||
@@ -197,15 +189,17 @@ }, { | ||
value: function handleSwipeMove(e) { | ||
var _this$state2 = this.state, | ||
x = _this$state2.x, | ||
y = _this$state2.y, | ||
isSwiping = _this$state2.isSwiping; | ||
var _this$state = this.state, | ||
x = _this$state.x, | ||
y = _this$state.y, | ||
isSwiping = _this$state.isSwiping; | ||
if (!x || !y || Utils.checkIsMoreThanSingleTouches(e)) return; | ||
var _this$getPosition = this.getPosition(e), | ||
absX = _this$getPosition.absX, | ||
absY = _this$getPosition.absY, | ||
deltaX = _this$getPosition.deltaX, | ||
deltaY = _this$getPosition.deltaY, | ||
duration = _this$getPosition.duration, | ||
velocity = _this$getPosition.velocity; | ||
var _this$getEventData = this.getEventData(e), | ||
absX = _this$getEventData.absX, | ||
absY = _this$getEventData.absY, | ||
deltaX = _this$getEventData.deltaX, | ||
deltaY = _this$getEventData.deltaY, | ||
directionX = _this$getEventData.directionX, | ||
directionY = _this$getEventData.directionY, | ||
duration = _this$getEventData.duration, | ||
velocity = _this$getEventData.velocity; | ||
@@ -226,2 +220,4 @@ var _this$props4 = this.props, | ||
absY: absY, | ||
directionX: directionX, | ||
directionY: directionY, | ||
duration: duration, | ||
@@ -240,2 +236,4 @@ velocity: velocity | ||
absY: absY, | ||
directionX: directionX, | ||
directionY: directionY, | ||
duration: duration, | ||
@@ -252,10 +250,8 @@ velocity: velocity | ||
onTap = _this$props5.onTap; | ||
var position = this.getEventData(e); | ||
if (this.state.isSwiping) { | ||
var position = this.getPosition(e); | ||
onSwiped && onSwiped(e, position); | ||
} else { | ||
var _position = this.getPosition(e); | ||
onTap && onTap(e, _position); | ||
onTap && onTap(e, position); | ||
} | ||
@@ -262,0 +258,0 @@ |
@@ -1,7 +0,1 @@ | ||
export interface State { | ||
x: number; | ||
y: number; | ||
start: number; | ||
isSwiping: boolean; | ||
} | ||
export interface ConstructorProps { | ||
@@ -21,12 +15,23 @@ element?: HTMLElement | null; | ||
} | ||
export declare type EventHandler = { | ||
(e: Event, data: EventData): void; | ||
}; | ||
export declare type EventData = { | ||
absX: number; | ||
absY: number; | ||
deltaX: number; | ||
deltaY: number; | ||
absX: number; | ||
absY: number; | ||
directionX: string; | ||
directionY: string; | ||
duration: number; | ||
velocity: number; | ||
}; | ||
export declare type State = { | ||
x: number; | ||
y: number; | ||
start: number; | ||
isSwiping: boolean; | ||
traceX: Trace; | ||
traceY: Trace; | ||
}; | ||
export declare type Trace = number[]; | ||
export declare type EventHandler = { | ||
(e: Event, data: EventData): void; | ||
}; |
@@ -1,8 +0,3 @@ | ||
import { EventData } from '../types'; | ||
export declare function calculatePosition(prevPos: prevPosition, nextPos: nextPosition): EventData; | ||
declare type prevPosition = { | ||
x: number; | ||
y: number; | ||
start: number; | ||
}; | ||
import { EventData, State } from '../types'; | ||
export declare function calculatePosition(state: State, nextPos: nextPosition): EventData; | ||
declare type nextPosition = { | ||
@@ -9,0 +4,0 @@ x: number; |
"use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -12,14 +14,28 @@ value: true | ||
function calculatePosition(prevPos, nextPos) { | ||
var deltaX = nextPos.x - prevPos.x; | ||
var deltaY = prevPos.y - nextPos.y; | ||
var _updateTrace = require("./updateTrace"); | ||
var Utils = _interopRequireWildcard(require("./index")); | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
function calculatePosition(state, nextPos) { | ||
var deltaX = nextPos.x - state.x; | ||
var deltaY = state.y - nextPos.y; | ||
var absX = Math.abs(deltaX); | ||
var absY = Math.abs(deltaY); | ||
var duration = (0, _calculateDuration.calculateDuration)(prevPos.start, Date.now()); | ||
(0, _updateTrace.updateTrace)(state.traceX, deltaX); | ||
(0, _updateTrace.updateTrace)(state.traceY, deltaY); | ||
var directionX = Utils.calculateDirection(state.traceX, 'x'); | ||
var directionY = Utils.calculateDirection(state.traceY, 'y'); | ||
var duration = (0, _calculateDuration.calculateDuration)(state.start, Date.now()); | ||
var velocity = (0, _calculateVelocity.calculateVelocity)(absX, absY, duration); | ||
return { | ||
absX: absX, | ||
absY: absY, | ||
deltaX: deltaX, | ||
deltaY: deltaY, | ||
absX: absX, | ||
absY: absY, | ||
directionX: directionX, | ||
directionY: directionY, | ||
duration: duration, | ||
@@ -26,0 +42,0 @@ velocity: velocity |
@@ -1,2 +0,2 @@ | ||
export declare const getInitialState: () => { | ||
export declare const getInitialState: (options?: {}) => { | ||
x: number; | ||
@@ -6,2 +6,4 @@ y: number; | ||
isSwiping: boolean; | ||
traceX: never[]; | ||
traceY: never[]; | ||
}; |
@@ -8,11 +8,20 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var getInitialState = function getInitialState() { | ||
return { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return _objectSpread({ | ||
x: 0, | ||
y: 0, | ||
start: 0, | ||
isSwiping: false | ||
}; | ||
isSwiping: false, | ||
traceX: [], | ||
traceY: [] | ||
}, options); | ||
}; | ||
exports.getInitialState = getInitialState; |
@@ -5,2 +5,3 @@ export * from "./getInitialState"; | ||
export * from "./calculateDuration"; | ||
export * from "./calculateDirection"; | ||
export * from "./calculatePosition"; | ||
@@ -13,1 +14,2 @@ export * from "./calculateMovingPosition"; | ||
export * from "./getOptions"; | ||
export * from "./updateTrace"; |
@@ -55,2 +55,14 @@ "use strict"; | ||
var _calculateDirection = require("./calculateDirection"); | ||
Object.keys(_calculateDirection).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _calculateDirection[key]; | ||
} | ||
}); | ||
}); | ||
var _calculatePosition = require("./calculatePosition"); | ||
@@ -138,2 +150,14 @@ | ||
}); | ||
}); | ||
var _updateTrace = require("./updateTrace"); | ||
Object.keys(_updateTrace).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _updateTrace[key]; | ||
} | ||
}); | ||
}); |
{ | ||
"name": "vanilla-swipe", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Tiny vanilla JS library to detect swipe direction.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -34,9 +34,12 @@ # vanilla-swipe | ||
type EventData = { | ||
absX: number; | ||
absY: number; | ||
deltaX: number; | ||
deltaY: number; | ||
absX: number; | ||
absY: number; | ||
directionX: 'LEFT' | 'RIGHT' | 'NONE'; | ||
directionY: 'TOP' | 'BOTTOM' | 'NONE'; | ||
duration: number; // ms | ||
velocity: number; // (px/ms) | ||
}; | ||
``` | ||
@@ -85,3 +88,3 @@ ### _Props_ | ||
function handler() { | ||
console.log(...arguments); // -> Event, { deltaX, deltaY, absX, absY, duration, velocity } | ||
console.log(...arguments); // -> Event, EventData | ||
} | ||
@@ -88,0 +91,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35753
36
752
108