UI Calculators
A library of interface calculators.
Use
Install
npm install ui-calc --save
Import
;
API
angle()
Calculate the angle between two 2D points, in radians
const a = x: 0 y: 0 ;const b = x: 45 y: 45 ; ; // 0.7853981633974483
Radians can be converted to degrees using radiansToDegrees()
.
degreesToRadians()
Convert degrees to radians
; // 0.7853981633974483
dilate()
Dilate the difference between two sequential values.
(a, b, dilationFactor)
const a = 10;const b = 20; ; // 20 (no change); // 15; // 30
distance()
Calculate the difference between two n-dimensional points (max 3)
; // 600; // 63.639; // 10
hypotenuse()
Calculate the hypotenuse of a triangle, given the length of sides A and B
; // 58.31
getProgressFromValue()
Return a progress given a value and a range
const value = 500;const min = 0;const max = 1000; ; // 0.5
getValueFromProgress()
Return a value given a progress (0-1) and a range
const progress = 025;const min = 100;const max = 500; ; // 200
offset()
Calculate the offset between two objects of numerical properties
const a = foo: 5 ;const b = foo: 15 bar: 20 ; ; // { foo: 10, bar: 0 }
pointFromAngleAndDistance()
Calculate new 2D point given an origin point, angle and distance
const origin = x: 100 y: 200 ;const angle = 45;const distance = 100; ;/* Returns: { x: 170.71067811865476, y: 270.71067811865476 }*/
radiansToDegrees()
Convert a value from radians to degrees
; // 45
relativeValue()
Calculates a relative value (ie "+=50") as applied to a numerical value
Valid operators are +
, -
, /
and *
.
; // 55; // 100
random()
Generates a random number, optionally within a range
; // Random number between 0 and 1; // Random number between 15 and 30
restrict()
Restricts the provided value to within the provided range
const value = 2000;const min = 0;const max = 1000; ; // 1000
smooth()
Smooth a value between its old and new values over a duration of time
const newValue = 100;const oldValue = 50;const frameDuration = 16667;const noSmoothing = 0;const heavySmoothing = 100; ; // 100; // 60.41
speedPerFrame()
Calculate speed per frame given speed per second and frame duration
const speedPerSecond = 300;const frameDuration = 16; ; // 4.8
speedPerSecond()
Calculate speed per second given speed per frame and frame duration
const speedPerFrame = 10;const frameDuration = 16; ; // 625