The simplest way I could think of is to update backgroundColor using setProperties method of a Surface.
Do that in Surface's render method, which is called on every render tick. Remember to return render spec (spec id).
Instead of using strings or hex values for colors, use rgb() or rgba() which accept number values.
Number values are obtained from Transitionable objects using get method.
Toggle number values using Transitionable's set method in "click" event handler.
Remember to pass tween options when setting Transitionable value (duration and easing curve) for a smooth animated effect.
Here's the code:
var bgColorRed = new Transitionable(0);
var bgColorGreen = new Transitionable(255);
var bgColorBlue = new Transitionable(0);
var colorTweenTime = 500;
var clicked = false;
var square = new Surface({
size: [200, 200],
content: 'Hello.',
properties: {
lineHeight: '200px',
textAlign: 'center'
}
});
square.render = function() {
var red = Math.ceil(bgColorRed.get()),
green = Math.ceil(bgColorGreen.get()),
blue = Math.ceil(bgColorBlue.get());
this.setProperties({
backgroundColor: 'rgb(' + red + ', ' + green + ', ' + blue + ')'
});
return this.id;
};
square.on('click', function() {
if (clicked) {
bgColorRed.set(0, { duration: colorTweenTime, curve: 'easeIn' });
bgColorGreen.set(255, { duration: colorTweenTime, curve: 'easeIn' });
} else {
bgColorRed.set(255, { duration: colorTweenTime, curve: 'easeIn' });
bgColorGreen.set(0, { duration: colorTweenTime, curve: 'easeIn' });
}
clicked = !clicked;
});
jsFiddle available here: http://jsfiddle.net/mcr85/6fx9jo9e/