animatePaper.js
An animation library for paper.js.
See a live demo on jsbin.
TypeScript
TypeScript declarations are available as of 1.2.1, in dist/src/animatePaper.d.ts
.
details)
Changelog from 0.x to 1.x (paper
is now a peerDependency, this should remove unnecessary code from your dependency tree.- The
segmentGrow
property andgrow
effect have been removed (this feature was very buggy). - When using
rotate
orscale
properties, you can provide a new setting :center
(orrotateCenter
/scaleCenter
) (default isitem.position
). Animation
supports a new optionrepeat
(defaults to0
).settings.complete
callback takes theAnimation
object as 1st argument.- Color support for
paper.Group
animation (1.1.*) - rgb, gray, hsl, hbs Color formats are now supported (1.1.*)
- bug fix : negative absolute position supported (relative values must be of string type) (1.2.*)
- bug fix : allow 0 duration (1.2.*)
- custom easings : you can now pass a
function
(p: number) => number
tosettings.easing
(1.2.*)
How to use :
npm and browserify
npm install --save paper-animate
then
import * as animatePaper from "paper-animate";
or var animatePaper = require("paper-animate")
bower
bower install paper-animate --save
directly in the browser
(not recommended)
Get the minified file in dist/paper-animate-browser.min.js
, and include it in your page, after paper.js.
Features :
- Animation of multiple properties at the same time,
- easing,
- chaining
This is a work in progress, and any help or feedback is more than welcome.
So far, only opacity
, position
, scale
, rotate
, translate
, fillColor
and strokeColor
are supported, but I add more whenever I have the time.
Animate an Item
(you can animate a Group
too)
You can either use a predefined animation :
var myCircle = 505035;animatePaperfx;
Predefined animations available by default : shake
, fadeIn
, fadeOut
, slideUp
, slideDown
, splash
. You can try them on this demo.
Or animate properties :
var myCircle = 505035;animatePaper;
When animating position
or color properties, you can provide either relative or absolute values :
var square = 75 75 5050;squarestrokeColor = 'green';square;
Note : Relative values must be strings.
Repeat
If you want your Animation
to run more than once, you can use the settings.repeat
option (defaults to 0
).
If settings.repeat
is a number > 0, your animation will run settings.repeat
additional times.
If you set settings.repeat
to true
, the animation will repeat infinitely until you call animatePaper.stop(item, true, true)
(the third parameter should be true, otherwise only the current Animation
will be stopped).
If you set settings.repeat
to a function, it will be called at the end of every "loop" and the Animation
will repeat itself as long as settings.repeat
returns true
.
This feature works best with relative values (e.g. '+myVal'
instead of myVal
), if you repeat an animation with absolute values you won't get the desired result.
animatePaper;animatePaper;; var c = 0;animatePaper;
The lib also extends Item.prototype
with .animate()
and .stop()
methods, which means you can also use
myCircle;
If you want to perform multiple animations successively, you can provide an array of parameters objects :
var star = 455052545;starfillColor = "black";staropacity = 0;star;
This is especially helpful when adding predefined animations to the library, it helps avoiding callback hell.
You can stop all running animations on an item by calling :
animatePaper;// orstar;
The stop
method can take a goToEnd
argument.
If true, all the animations will take their final value and complete
callbacks will be called.
Easing
By default, the supported easing functions are : linear, swing, easeInSine, easeOutSine, easeInOutSine, easeInCirc, easeOutCirc, easeInOutCirc, easeInElastic, easeOutElastic, easeInOutElastic, easeInBack, easeOutBack, easeInOutBack, easeInBounce, easeOutBounce, easeInOutBounce, easeInQuad, easeOutQuad, easeInOutQuad, easeInCubic, easeOutCubic, easeInOutCubic, easeInQuart, easeOutQuart, easeInOutQuart, easeInQuint, easeOutQuint, easeInOutQuint, easeInExpo, easeOutExpo, easeInOutExpo.
If you want to use more easing functions, settings.easing
can take a (p: number) => number
function as a value, so that you can use your own or use some from an external library such as bezier-easing.
animatePaper;
Alternatively, you can use the animatePaper.extendEasing(myEasingFunctions)
method to add your own easing functions or override any existing easing.
The method takes only one argument : an object in which keys are easing names, and values are easing functions:
animatePaper;
Learn more about easing here.
Extend property hooks
If you want to add support for a new property or override the library's behavior for properties that are already supported,
you can use animatePaper.extendPropHooks(myPropHooks);
.
myPropHooks
should be an object in which keys are property names, and values are "hook objects".
Each "hook object" can have a get
, set
and ease
method, and will be used to interface the animation with the property.
For example, say you want to add support for color animation:
animatePaper;
When these functions are used, they are passed only one argument : the Tween object (see the doc in doc/ for more details),
exept for the ease()
function which gets the eased percent as second parameter.
- The
get()
function must return the current value of theTween.item
's property. - The
set()
function must set the value of theTween.item
's property withTween.now
(which will most likely be the result ofget()
orease()
) - The
ease()
function must return the eased value. The second parameter is the eased percent.
Add your own animations to the lib
To do so, simply add properties to animatePaper.fx
, like so :
animatePaperfx { var myAnimations = ...; item;};animatePaperfx;
Contributing
as of 1.2.1 the lib uses TypeScript, so make your changes in src/*.ts then build with gulp build-paper-animate
and gulp build-paper-animate-browser
TODOS
- Change how
item.data._animatePaperVals
works to allow multiple animations of the same property at the same time. - Change
Tween
so that we garantee values are right at0
and1
positions, to avoid problems with imprecise numbers (floating point). See "Negative position" in tests.js. - Add tests
Help needed !
I'm a beginner in paper.js, so if you spot a mistake or want to add something to the lib, any help would be appreciated :-)
Author
camille dot hodoul at gmail dot com
Contributors
- User pueding for bug fixes and delay feature.
- Users s-light and StratusBase for feedback, ideas and contributions (Group Color support, bug fixes).
@Eartz_HC