npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anselan/maprange

v0.10.0

Published

Map values from one range to another

Downloads

81

Readme

Map Range in JS

Inspired by the map function in the creative coding framework Processing and its counterpart ofMap in openFrameworks. Especially useful for creative coding applications, e.g. animations.

Just show me some examples!

Percentages

The value 0.5 in the range [0,1] should return as 50 in the range [0,100]:

const result = remap(0.5, [0,1], [0, 100]); // result === 50

Inverse output

The value 10% should count as as 100% minus 10% (i.e. 90%) if we are counting backwards, i.e.

const result = remap(10, [0, 100], [100, 0]); // notice reversed output range; result === 90

Normalised (float) position to pixels

// A pixel in the middle of the screen, normalised in the range [0,1]
const [x, y] = [0.5, 0.5]

// Convert it to a position on display with dimensions 1920x1080
const [px, py] = remapCoords([x,y], [1,1], [1920,1080])
console.log(px, py) // "960, 540"

Animating from a sine wave to heights in metres

You'll see the value of x modulate from -1 to 1 smoothly over time. The remapping keeps the result in a range between 10 and 1000 (mm? who knows)

const start = Date.now();
setInterval(() => {
  const elapsed = Date.now() - start;
  const x = Math.sin(elapsed / 1000);
  const height = remap(x, [-1, 1], [10, 1000])
  console.log(elapsed, x, height);
}, 40)

Details

remap

Basic remapping of a value from one range to another.

remap (value, inputRange, targetRange, clamp, shouldRound)

...where the parameters are:

  • value: a number
  • inputRange, targetRange: arrays of exactly 2 elements each, i.e [min,max]
  • clamp (optional; default false): whether to constrain the result within the given target range
  • shouldRound (optional; default false): whether to round to nearest integer, i.e. only whole numbers

Provide the range (inputMin, inputMax) that your value is currently in, and specify a new range (outputMin, outputMax) and you'll get back a new value as per the target range.

remapArray

Remap many values at once.

remapArray (values, inputRange, targetRange, clamp, shouldRound)

... where the parameters are the same as above, except that values is an array values, not a single value.

remapCoords

Remap coordinates in multiple dimensions.

remapCoords (inputCoords, inputDimensions, targetDimensions, clamp, shouldRound

Given a set of "coordinates" in X dimensions,

  • inputCoords is an array representing coordinates in X dimensions, i.e. should have a length of X
  • inputDimensions is an array representing maximum extents for the input coordinate system, one per dimension. Should therefore also have a length of X
  • targetDimensions is an array representing maximum extents for the target coordinate system you want to remap your coordinates to. Should therefore also have a length of X

Keep in mind:

  • The minimum extent for each dimension is always assumed to be zero (0)
  • Unlike remap and remapArray, the default for shouldRound is true because this is convenient for normalised-to-pixel translations which is the most common use case

Installation

npm install @anselan/maprange

Import the basics:

import { remap } from '@anselan/maprange'

...or import other functions you need:

import { remap, remapArray, remapCoords } from '@anselan/maprange'

If you cannot use import, try require:

const { remap } = require('@anselan/maprange`)

Why not use something else?

Differs from the following similar JS packages:

None of the above provide the following convenience features:

  • remapArray
  • remapCoords

Hopefully, you also find the array-format ranges ([0,1], etc.) more readable than the APIs referenced above, too.