EaselJS: change shape fill color on mouse click - Javascript Canvas

Javascript examples for Canvas:Mouse

Description

EaselJS: change shape fill color on mouse click

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://code.createjs.com/easeljs-0.5.0.min.js"></script> 
      <script type="text/javascript">
    window.onload=( function() {/*from w  w  w  .  j  a va 2  s  . co  m*/
var stage = new createjs.Stage("test");
createjs.Touch.enable(stage);
stage.enableMouseOver(10);
var padding = 1;
var width = 55;
var height = 55;
var cols = 15;
for(var i=0;i<200;i++) {
   var s = new createjs.Shape();
    s.overColor = "#3281FF"
    s.outColor = "#FF0000"
    s.graphics.beginFill(s.outColor).drawRect(0, 0, width, height).endFill();
    s.x = (width + padding) * (i%cols);
    s.y = (height + padding) * (i/cols|0);
    s.onMouseOver = handleMouseOver;
    s.onMouseOut = handleMouseOut;
    stage.addChild(s)
}
createjs.Ticker.addListener(stage);
function handleMouseOver(event) {
   var target = event.target;
   target.graphics.clear().beginFill(target.overColor).drawRect(0, 0, width, height).endFill();
}
function handleMouseOut(event) {
    var target = event.target;
    target.graphics.clear().beginFill(target.outColor).drawRect(0, 0, width, height).endFill();
}
function tick() {
  stage.update();
}
    });

      </script> 
   </head> 
   <body> 
      <canvas id="test" width="500" height="500"></canvas>  
   </body>
</html>

Related Tutorials