topical media & game development

talk show tell print

graphic-canvas-util-pixastic-actions-invert.js / js



  /*
   * Pixastic Lib - Invert filter - v0.1.0
   * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
   * MIT License [http://www.opensource.org/licenses/mit-license.php]
   */
  
  Pixastic.Actions.invert = {
          process : function(params) {
                  if (Pixastic.Client.hasCanvasImageData()) {
                          var data = Pixastic.prepareData(params);
  
                          var invertAlpha = !!params.options.invertAlpha;
                          var rect = params.options.rect;
                          var w = rect.width;
                          var h = rect.height;
  
                          var w4 = w*4;
                          var y = h;
                          do {
                                  var offsetY = (y-1)*w4;
                                  var x = w;
                                  do {
                                          var offset = offsetY + (x-1)*4;
                                          data[offset] = 255 - data[offset];
                                          data[offset+1] = 255 - data[offset+1];
                                          data[offset+2] = 255 - data[offset+2];
                                          if (invertAlpha) data[offset+3] = 255 - data[offset+3];
                                  } while (--x);
                          } while (--y);
  
                          return true;
                  } else if (Pixastic.Client.isIE()) {
                          params.image.style.filter += " invert";
                          return true;
                  }
          },
          checkSupport : function() {
                  return (Pixastic.Client.hasCanvasImageData() || Pixastic.Client.isIE());
          }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.