Skip to content

Commit

Permalink
feat(core): add MovementEvent type and movement computation function
Browse files Browse the repository at this point in the history
Introduced the MovementEvent type to encapsulate all relevant data for a movement event, including direction, velocity, and distance in the swiperia-core package. Implemented a function to compute these properties based on start and end points and duration, enabling detailed tracking of movement characteristics in swipe gestures.
  • Loading branch information
samavati committed Apr 13, 2024
1 parent 8c37af5 commit deb980a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/swiperia-core/src/lib/movement/MovementEvent.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Direction } from '../direction/Direction.type';
import type { Point } from '../point/Point.type';
import type { Velocity } from '../velocity/Velocity.type';

export interface MovementEvent {
/**
*
*/
direction: Direction | null;
source: Point;
target: Point;
deltaX: number;
deltaY: number;
/**
* absolute deltaX
*/
absX: number;
/**
* absolute deltaY
*/
absY: number;
/**
* √(absX^2 + absY^2) / time - "absolute velocity" (speed)
*/
velocity: number;
/**
* deltaX/time, deltaY/time] - velocity per axis
*/
vxvy: Velocity;
/**
* distance traveled
*/
distance: number;
}
2 changes: 2 additions & 0 deletions packages/swiperia-core/src/lib/movement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { movement } from './movement';
export type { MovementEvent } from './MovementEvent.type';
29 changes: 29 additions & 0 deletions packages/swiperia-core/src/lib/movement/movement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { MovementEvent } from './MovementEvent.type';
import type { Point } from '../point/Point.type';

import { direction } from '../direction/direction';
import { distance } from '../point/distance';
import { velocity, vxvy } from '../velocity/velocity';

export const movement = (
source: Point,
target: Point,
duration: number
): MovementEvent => {
const deltaX = target[0] - source[0];
const deltaY = target[1] - source[1];
const absX = Math.abs(deltaX);
const absY = Math.abs(deltaY);
return {
source,
target,
deltaX,
deltaY,
absX,
absY,
direction: direction(source, target),
velocity: velocity(source, target, duration),
vxvy: vxvy(source, target, duration),
distance: distance(source, target),
};
};

0 comments on commit deb980a

Please sign in to comment.