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

@freder/piecewise

v0.7.0

Published

a library for creating composable easing and envelope functions.

Downloads

155

Readme

@freder/piecewise

a library for creating composable easing and envelope functions.

installation

# yarn
yarn add @freder/piecewise

# npm
npm install --save @freder/piecewise

example: easing function

const piecewise = require('@freder/piecewise');

function identity(t) {
	return t;
}

function always(c) {
	return (t) => c;
}

const piecewiseEasingFn = piecewise.easing([
	{
		tInterval: [0, 0.5],
		tMap: [0, 1], // optional
		easingFn: identity,
	},
	{
		tInterval: [0.5, 0.8],
		easingFn: always(1),
	},
	{
		tInterval: [0.8, 1],
		tMap: [1, 0], // reverse
		easingFn: identity,
	},
]);

tInterval is mapped to tMap, with which easingFn is called. easingFn only takes one argument t.

visualization:

example: envelope function

const piecewiseEnvelopeFn = piecewise.easing([
	{
		tInterval: [0, 0.5],
		tMap: [0, 1],
		easingFn: identity,
	},
	{
		tInterval: [0.5, 1],
		tMap: [1, 0],
		easingFn: identity,
	},
]);

const finalFn = piecewise.envelope(piecewiseEnvelopeFn, piecewiseEasingFn);

envelope returns the product of the values of an envelope function and an easing function at time t.

visualization: grey: easing function red: envelope function black: resulting function

example: mix function

const f1 = identity;
const f2 = always(1);
const finalFn = piecewise.mix(f1, f2, 0.7);

mix returns a new function that calculates the weighted mean of two functions at time t.

visualization: grey: f1, f2 black: resulting function

example: crossfade function

const easingFn = piecewise.easing([
	{
		tInterval: [0, 0.6],
		tMap: [0, 1],
		easingFn: always(0),
	},
	{
		tInterval: [0.6, 0.8],
		tMap: [0, 1],
		easingFn: identity,
	},
	{
		tInterval: [0.8, 1],
		tMap: [0, 1],
		easingFn: always(1),
	},
]);

const f1 = always(0.75);
const f2 = always(0.25);
const finalFn = piecewise.crossfade(easingFn, f1, f2);

crossfade mixes two functions and is controlled by easingFn.

visualization: grey: f1, f2 red: easing function black: resulting function

visualization

visualization-example.js

utility functions

const easing = require('easing-js');
const { utils } = require('@freder/piecewise');

// wrap penner easing function to only be a function of `t`:
const wrappedEasingFn = utils.wrapPennerFunction(easing.linear);
wrappedEasingFn(0.7); // → 0.7