Search code examples
javascripthtmlcanvastweeneaseljs

Animating Color with Tweenjs


I've recently switched to EaselJs and would like to animate the color of a circle.

The code I've got so far is this:

var shape = new createjs.Shape();
shape.graphics.beginFill(createjs.Graphics.getRGB(255, 0, 0));
shape.graphics.drawCircle(0, 0, 10);
stage.addChild(shape);

var tween = createjs.Tween.get(shape, { loop: true }).
    to({color:"#00FF00" }, 1000);

but that doesn't work. What is the right property to animate?


Solution

  • I don't think it's possible to animate the color of a shape like that, afterall the drawing of your shape could include many different colors, how should the Tween know which of those colors to modify? There are two ways that I can think of right know:

    Method 1) Use a createjs.ColorFilter, apply it to your shape and then tween the properties of your filter:

    var shape = new createjs.Shape();
    //making the shape shape white, so we can work with the multiplyers of the filter
    shape.graphics.beginFill(createjs.Graphics.getRGB(255, 255, 255));
    shape.graphics.drawCircle(0, 0, 10);
    stage.addChild(shape);
    var filter = new createjs.ColorFilter(1,0,0,1); //green&blue = 0, only red and alpha stay
    shape.filters = [filter];
    
    var tween = createjs.Tween.get(filter, { loop: true }).
        to({redMultiplier:0, greenMultiplier:1 }, 1000);
    

    I did not test this, so...let me know it this works. Also note, that filters are only applied/updated when calling cache() or updateCache() on your shape, so you will have to add that to your tick-function.

    Method 2) Or you just redraw the shape every frame...but 1) is probably easier.