HTML5 Image Filters on drawImage

Hello there,
I'm new on Box2dWeb and also on Html5 Canvas,
trying to learn how to combine those 2 by developing something nice!
..so this is what I'm trying to do...
I have this code and till now i managed to add the html5 pixel filter on all my Canvas, but i need to understand how to draw each box with the filter and not the whole canvas! Any help will be much appreciated! 🙂

Code: Select all

<html>
    <head>
        <title>box2dweb</title>
        <script type="text/javascript" src="box2dweb.js"></script>
    </head>
    <body onload="init();" style="margin:0px">       
        <canvas id="canvas" width="640" height="480" style="background-color:#333333;"></canvas>
        <div id="assets" style="display:none">
            <img id="crate" src="crate.png" />
        </div>
       </body>
</html>

<script type="text/javascript">
function init() {
   var b2Vec2 = Box2D.Common.Math.b2Vec2;
   var b2AABB = Box2D.Collision.b2AABB;
   var b2BodyDef = Box2D.Dynamics.b2BodyDef;
   var b2Body = Box2D.Dynamics.b2Body;
   var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
   var b2Fixture = Box2D.Dynamics.b2Fixture;
   var b2World = Box2D.Dynamics.b2World;
   var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
   var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
   
   var canvas = document.getElementById("canvas");
   var context = canvas.getContext("2d");

    var img = document.getElementById('orig');
     
   var worldScale = 30;
   
   var world = new b2World(new b2Vec2(0, 10),true);
   
   var canvasPosition = getElementPosition(canvas);

// ************************* FILTERS ***************************
      Filters = {};
      Filters.filterImage = function(filter, image) {
        var args = [context.getImageData(0,0,canvas.width,canvas.height)];
        for (var i=2; i<arguments.length; i++) {
          args.push(arguments[i]);
        }
        return filter.apply(null, args);
      };

      Filters.grayscale = function(pixels, args) {
        var d = pixels.data;
        for (var i=0; i<d.length; i+=4) {
          var r = d[i];
          var g = d[i+1];
          var b = d[i+2];
          // CIE luminance for the RGB
          var v = 0.2126*r + 0.7152*g + 0.0722*b;
          d[i] = d[i+1] = d[i+2] = v
        }
        return pixels;
      };
// ***********************************************************   
   
   debugDraw();             
   window.setInterval(update,1000/60);
   
   createBox(640,30,320,480,b2Body.b2_staticBody,null);
   createBox(640,30,320,0,b2Body.b2_staticBody,null);
   createBox(30,480,0,240,b2Body.b2_staticBody,null);
   createBox(30,480,640,240,b2Body.b2_staticBody,null);
   
   document.addEventListener("mousedown",function(e){
      createBox(64,64,e.clientX-canvasPosition.x,e.clientY-canvasPosition.y,b2Body.b2_dynamicBody,document.getElementById("crate"));
   });
   
   function createBox(width,height,pX,pY,type,data){      
      var bodyDef = new b2BodyDef;
      bodyDef.type = type;
      bodyDef.position.Set(pX/worldScale,pY/worldScale);
      bodyDef.userData=data;
      var polygonShape = new b2PolygonShape;
      polygonShape.SetAsBox(width/2/worldScale,height/2/worldScale);
      var fixtureDef = new b2FixtureDef;
      fixtureDef.density = 1.0;
      fixtureDef.friction = 0.5;
      fixtureDef.restitution = 0.5;
      fixtureDef.shape = polygonShape;
      var body=world.CreateBody(bodyDef);
      body.CreateFixture(fixtureDef);
   }
   
   function debugDraw(){
      var debugDraw = new b2DebugDraw();
      debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
      debugDraw.SetDrawScale(30.0);
      debugDraw.SetFillAlpha(0.5);
      debugDraw.SetLineThickness(1.0);
      debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
      world.SetDebugDraw(debugDraw);
   }
   
   function update() {
      world.Step(1/60,10,10);
        world.DrawDebugData();
        for(var b = world.m_bodyList; b != null; b = b.m_next){
         if(b.GetUserData()){
            context.save();

            context.translate(b.GetPosition().x*worldScale,b.GetPosition().y*worldScale);
            context.rotate(b.GetAngle());
            context.drawImage(b.GetUserData(),-b.GetUserData().width/2,-b.GetUserData().height/2);
            
            // putImageData is diferend from drawImage?! maybe it has to go at the createBox data?
            var idata = Filters.filterImage(Filters.grayscale, b.GetUserData());         
            context.putImageData(idata,-b.GetUserData().width/2,-b.GetUserData().height/2);

            context.restore();
         }
      }
        world.ClearForces();
   };
   
   //http://js-tut.aardon.de/js-tut/tutorial/position.html
   function getElementPosition(element) {
      var elem=element, tagname="", x=0, y=0;
      while((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
         y += elem.offsetTop;
         x += elem.offsetLeft;
         tagname = elem.tagName.toUpperCase();
         if(tagname == "BODY"){
            elem=0;
         }
         if(typeof(elem) == "object"){
            if(typeof(elem.offsetParent) == "object"){
               elem = elem.offsetParent;
            }
         }
      }
      return {x: x, y: y};
   }

}; 
</script>


Thanks!

Original thread: https://box2d.org/forum/viewtopic.php?t=9802