UNPKG

react-timekeeper

Version:

Time picker based on the style of the Google Keep app

88 lines (68 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calcAnimationAngle = calcAnimationAngle; exports.deg = deg; exports.isWithinRadius = isWithinRadius; exports.rad = rad; exports.transform = transform; var _constants = require("./constants"); const { cos, sin } = Math; const pi = Math.PI; const ANGLE_PER_INCREMENT = 360 / _constants.VISIBLE_NUMBERS_PER_CIRCLE; function rad(deg) { return deg / (180 / pi); } function deg(rad) { return rad * (180 / pi); } // translate number position function translateX(index, transform) { return sin(rad(index * -ANGLE_PER_INCREMENT - 180)) * (_constants.CLOCK_RADIUS - transform) + _constants.CLOCK_RADIUS - _constants.NUMBER_RADIUS_REGULAR / 2; } function translateY(index, transform) { return cos(rad(index * -ANGLE_PER_INCREMENT - 180)) * (_constants.CLOCK_RADIUS - transform) + _constants.CLOCK_RADIUS - _constants.NUMBER_RADIUS_REGULAR / 2; } // calculate number position for animation function transform(index, t) { const x = translateX(index, t); const y = translateY(index, t); return "translate(".concat(x, "px, ").concat(y, "px)"); } function isWithinRadius(x, y, radius) { return Math.sqrt(x * x + y * y) < radius; } // normalize any angles to 0-360 deg function normalize(angle) { return (angle % 360 + 360) % 360; } /* calculates the shortest angle between the prev and next angle to animate to - positive spins clockwise, negative is ccw - prev is the previous angle - can literally be almost any value, eg: 480 is valid, -480 is valid - next is the angle to rotate to - is always between 0-360 - must return an angle relative to the previous, so once again this value can be any negative or positive value (like prev) function normalizes each angle, creates an upper and lower bound based on previous angle and figures out which direction is shorter for next - then diff and add/subtract to previous angle */ function calcAnimationAngle(prev, next) { const p = normalize(prev); const n = normalize(next); let lower = p; let upper = p; // TODO - implement without while loops while (n < lower) { lower -= 360; } while (n >= upper) { upper += 360; } if (upper - n < n - lower) { return prev - (upper - n); } else { return prev + (n - lower); } }