Skip to content
/ math Public

A lightweight math utility library that provides commonly used mathematical functions for JavaScript and TypeScript applications.

License

Notifications You must be signed in to change notification settings

feldhaus/math

Repository files navigation

@feldhaus/math

A lightweight math utility library that provides commonly used mathematical functions for JavaScript and TypeScript applications.

Installation

You can install the package via npm:

npm install @feldhaus/math

Available Functions

  • clamp: Clamps a number between a minimum and maximum value. Source
  • inverseLerp: Calculates the relative position of a value within a range. Source
  • lerp: Linearly interpolates between two values. Source
  • repeat: Repeats a number within a range by wrapping it around. Source
  • normalizeAngle: Normalizes an angle to the range [0, 2π]. Source

Available Constants

  • RAD2DEG: Conversion factor from radians to degrees 180 / Math.PI.
  • DEG2RAD: Conversion factor from degrees to radians Math.PI / 180.
  • HALF_PI: Half of Pi (π/2) Math.PI / 2.
  • QUARTER_PI: Quarter of Pi (π/4) Math.PI / 4.
  • TWO_PI: Two times Pi (2π) Math.PI * 2.
  • TAU: Tau, which is equivalent to 2π Math.PI * 2.

Examples

import { clamp } from '@feldhaus/math';

const clampedValue = clamp(5, 1, 10); // Output: 5
import { inverseLerp } from '@feldhaus/math';

const relativeValue = inverseLerp(10, 20, 15); // Output: 0.5
import { lerp } from '@feldhaus/math';

const interpolatedValue = lerp(0, 10, 0.5); // Output: 5
import { repeat } from '@feldhaus/repeat';

repeat(12, 5); // Output: 2
repeat(400, 360); // Output: 40
import { normalizeAngle } from '@feldhaus/normalizeAngle';

normalizeAngle(3 * Math.PI); // Output: π (approximately 3.14159)

Usage in Browser

You can also include the library directly in your HTML file using a UMD build:

<script src="https://unpkg.com/@feldhaus/math/dist/index.umd.js"></script>

This will expose the library as FMath on the global window object, allowing you to use the functions like this:

<script>
  const clampedValue = FMath.clamp(1, 5, 10);
  console.log(clampedValue); // Output: 5
</script>

License

MIT

About

A lightweight math utility library that provides commonly used mathematical functions for JavaScript and TypeScript applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published