Search code examples
javascriptcanvaseaseljsgsap

How to tween a line color in easeljs canvas


I have a series of dots with connected lines that I am animating in an easel.js canvas. The dots move around, and the lines stay connected to them as they move. As the dots move, I'm animating their color, so I want the lines to animate color as well.

I tried calling a color tween on the line, but it requires that I cache the line first.

For a circle, that's easy - I get the radius and, since its registration is in the center, its x and y coordinates and width and height are easy to calculate (for a circle with r=100 at 50,50, its cache would be cache(0,0,100,100). But for a line, I'm not sure how to reference the right coordinates for the cache statement, especially since the line start position, end position, and length are always changing.

Anyone have a way to do this?

I'm using greensock's timelinemax / tweenlite with the easeljs plugin to handle all the animations, if that's helpful.


Solution

  • If TweenLite handles color tweens, then you should just be able to update the "style" of your line any time:

    var shape = new createjs.Shape();
    var colorCommand = shape.graphics.beginStroke("#000000").command;
    shape.graphics.moveTo(0,0).lineTo(100,100); // Draw the line
    // Any time
    colorCommand.style = "#ff0000";
    // So in a tween:
    TweenLite.to(colorCommand, 20, {style:"#00ffff"});
    

    If you are using EaselJS, you can also use TweenJS, which has a ColorPlugin. Using similar code:

    createjs.Tween.get(colorCommand).to({style:"#00fffff"}, 20000);
    

    Here is a fiddle I made tweening the color of a line with TweenJS https://jsfiddle.net/lannymcnie/5zxpb944/

    Cheers.