Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

animate-prop

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

animate-prop

Single, dependency-free function to tween a property. Use that on canvas or anywhere else.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

animate-prop module size module gzipped size

Single, dependency-free function to tween a property. Use that on canvas or anywhere else.

This is a very simple and low-level function tween/animate/update a value over time. If you're looking for something that updates DOM elements directly, try the excellent but 60 times heavier TweenLite (+its CSS Plugin).

Originally from the article "Animation with HTML5 Canvas", where you can see more examples.

Usage

// We'll animate the property x, starting from 5
var obj = { x: 5 };

// 10 is the final value that the property x will have after 1000 milliseconds
animateProp(obj, 'x', 10, 1000);

// use the object in your drawing loop
context.moveTo(obj.x, 100);
setInterval(function () {
	// progressively draw a line
	context.lineTo(obj.x, 100);
	context.stroke();
}, 16); // don't use setInterval, it's just an example

You can provide an easing function

// see https://github.com/jaz303/easiness/blob/master/raw.js
var easeInOutSine = function (progress) {
	return Math.sin(Math.PI/2 * progress);
};
var obj2 = { scale: 1 };
animateProp(obj2, 'scale', 2, 300, easeInOutSine, function () {
	console.log('Eased animation done!')
});

Or just wait for it to be done, without an easing

var obj3 = { opacity: 0 }; //            v--- easing:false = linear tween
animateProp(obj3, 'opacity', 0.5, 300, false, function () {
	console.log('Linear animation done!')
});

Animate multiple properties with any duration

var obj = { x: 5, y: 100 };

animateProp(obj, 'x', 10, 1000);
animateProp(obj, 'y', 300, 500);

Sequence tweens

var obj = { x: 5, y: 100 };

animateProp(obj, 'x', 10, 1000, function () {
	animateProp(obj, 'y', 300, 500);	
});

With browserify

npm install --save animate-prop
var animateProp = require('animate-prop');

API

animateProp(object, propertyName, finalValue, duration[, easing[, callback]])

parameterdescription
objectType: object, required
The object with the property to tween
propertyNameType: string, required
The property key to animate (e.g. 'height')
finalValueType: number, required
The final value that the property will have
durationType: number, required
The duration of the tween
easingType: function or boolean, default: linear
Given a number from 0 to 1, returns an eased number from 0 to 1 (optional)
callbackType: function, default: none
Function to be called when the tween is over (optional)

Files

Here's an explanation of the files included in this repo

  • index.js: source file, in ES6
  • dist/animate-prop.js: browser-ready file with AMD or a global variable called animateProp
  • dist/animate-prop.min.js: same as above, minified
  • dist/animate-prop.node.js: used by node/browserify with require('animate-prop')
  • dist/animate-prop.node.min.js: same as above, but minified, for byte counting only

Dependencies

No dependencies, but it needs window.requestAnimationFrame to be there (IE10+ or with polyfill)

Easing functions are not included. Provide your own or use easiness

Used on

License

MIT © Federico Brigante

Keywords

FAQs

Package last updated on 19 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc