Fork me on GitHub

ABOUT:

IMGFX.js is a JQuery plug-in that easily replace any targeted images of your webpage by a JScanvas 2D animation

USAGE:

First load Jquery :

 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>

Grab the SOURCE then load imgfx.js:

 <script type="text/javascript" src="path-to-js/imgfx.min.js"></script>

To apply an effect to an image by id , class or tag or a just target <img/> element with the usual Jquery way:

<script type="text/javascript">
$("img").imgfx({parameters});///will apply to all images on the page
$(".a-class").imgfx({parameters});//will apply to all images with same class
$(".#an-id").imgfx({parameters});//will apply to image targetted by id
</script>



Some of the FX will not work on images that are not on the same server due to the same origin policy.
By the way it works when the remote server send the adequat CORS hearders.
By default the plug-in sets the .crossOrigin attribute of images to "Anonymous".

img.crossOrigin = "Anonymous"; 
You can read more about this issue here : https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image .




Parameters:


The plug-in have a lot off parameters that allow you to play with.

Some are common to different effects:

mode

mode:"tv"

This parameters allow you to choose the fx you want to use

blendMode

blendMode: "difference",

You can choose between all of this values:


normal | multiply | screen | overlay |  darken |
lighten | color-dodge | color-burn | hard-light | 
soft-light | difference | exclusion | hue | saturation | 
color | luminosity |clear | copy | destination | source-over |
destination-over | source-in | destination-in | 
source-out | destination-out | source-atop | 
destination-atop | xor | lighter

This parameters controls the CanvasRenderingContext2D.globalCompositeOperation property for the FX that uses it.
"tv","glitch","zoom","bumpingrect" and "randomsquares" makes use of it.
I think it's fun to play with.


canvassize

canvassize:2,

This parameter control the size of the canvas by dividing the original image size.

set it to 1 if you want to keep your original image size (and burn your visitor's CPU)


crossOrigin

canvassize:"Anonymous",

Can be usefull for CORS settings.


DEMO

Navigate with tabs and click the trigger fx to see what it does

For information the demo image is hosted on instagram and yes it's my cat.

The link to the image
https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/1962897_1082146021812843_1655689444_n.jpg


TV simulator

This FX try to simulate a bad synced analog tv.
This Fx can uses the following controls:


      //TV SIMULATOR SPECIFIC CONTROLS
      Themecolor: "#0f0",///DOMINANT COLOR
      noise: true,//ENNABLE NOISE LAYER
      grainsize: 1,//NOISE GRAIN SIZE
      desyncspeed: 5,//SYNC DISPLACEMENT SPEED
      syncup: true,//TRUE IMAGE MOVE UP FALSE IMAGE MOVE DOWN
      scanspacing: 22,//SPACE BETWEEN SCANLINES
      scanheight: 4,//SIZE OF SCANLINES
      scanspeed: 1,//SCANLINE DISPLACEMENT SPEED DEFAULT MOVE DOWN, NEGATIVE INT MOVE UP
      flickerseed: 10,//FLICKERING: LESS THE NUMBER MORE IT FLICKERS    
      latunsync: 0,//DISPLACE IMAGE RANDOMLY ON X AXIS
      sidenoise: "left", // DRAW LATERAL NOISE LAYER options:"left","right","both","none" 
      sidenoiseforce: 8,//MAX FORCE OF SIDENOISE


Exemple:


  $('#tv').imgfx({
  mode:"tv",
  Themecolor:"#0f0",
  desyncspeed: 1,
  scanspeed: 3,
  syncup: false
  });



Glitch

This FX is "Glitchy".
This Fx uses the following controls:

//LATERAL GLITCH SPECIFIC CONTROLS 
      glichforce: 80,
      glitchclear: true,
      glitchdisplayimage: true,

Exemple:


  $('#tv').imgfx({
  mode:"glitch"

  });



Base64

This FX encode the image to base64,randomise the string then print it back.
The whole encoding/randomising/decoding is too slow for requestAnimationFrame so it use setInterval instead.
The base64 randomiser is adapted from this Source because it's far more efficent than the one i wrote at first place.
This Fx uses the following controls:


      //BASE64GLITCH SPECIFIC CONTROLS
      base64glitchforce:0,//BASE64 DESTROY It's an array index : The array is set like this ["64","128","256","1024","2048"]
      base64glitchInterval:300,//

Exemple:


  $('#base64').imgfx({
  mode:"base64",
  base64glitchforce:0,
  base64glitchInterval:60

  });

Zoom

This FX Apply Zoom and rotation to the image
This Fx uses the following controls:


 ///ZOOM SPECIFIC CONTROLS
      zoomRotationSpeed:1, //rotation speed
      zoomSpeed:0.05, // zooming speed
      zoomMinScale:0.001,//min scale
      zoomMaxScale:15,//max scale
      zoomXframe:30,// clear canvas n number of frame


Exemple:


  $('#zoom').imgfx({
  mode:"zoom",
  blendMode:"darken",
 zoomRotationSpeed:3,
 zoomSpeed:0.005,
 zoomMinScale:0.0001,
 zoomMaxScale:15,
zoomXframe:5 

  });

Rotozoom

This FX is a rotozom
I stole and adapted this sourcecode.
This Fx uses the following controls:


     //ROTOZOOM SPECIFIC CONTROLS
      rotozoomSpeed:0.01,//ZOOMING SPEED 
      rotozoomRotateSpeed:0.2,//ROTATION SPEED [use max as 0.9999...]
      rotozoomMaxScale:5,//MAX ZOOM
      rotozoomMinScale:0.001,//MAX DE-ZOOM


Exemple:


  $('#rotozoom').imgfx({
  mode:"rotozoom",
    
  rotozoomSpeed:0.01,
  rotozoomRotateSpeed:0.2,
  rotozoomMaxScale:5,
  rotozoomMinScale:0.001

  });

Bumping rects

This FX draws random rectangles that bounces inside the canvas,becomeiteresting when you play with the blend modes.

This Fx uses the following controls:


     //BUMPING RECT SPECIFIC CONTROLS
     //THIS FX USE THE blendMode PARAMETER
      rectNum: 4,//HOW MANY BOUNCING SQUARES
      rectSquare: false,//SQUARES OR RECTANGLES
      rectSpeed: 6,//DISPLACEMENT SPEED
      rectClear: true,//CLEAR SCREEN EVERY FRAME?


Exemple:


  $('#bumpingrect').imgfx({
  mode:"bumpingrect",
  bledMode:"difference",
  rectNum: 4,
  rectSquare: false,
  rectSpeed: 6,
  rectClear: true
    

  });

Kaleidoscope

This FX draws a kaleidoscope.

I stole the sourcecode somewhere on a jsfiddle but I can't find it again...Sorry . This Fx uses the following controls:


    //KALEIDOSCOPE SPECIFIC CONTROLS
     thetaAngle:360,
     thetaDiv:30,



Exemple:


  $('#kaleidoscope').imgfx({
  mode:"kaleidoscope",
  
     thetaAngle:360,
     thetaDiv:30,


  });

Pixelate

This FX randomy pizelize the image.

This Fx uses the following controls:


   
      //PIXELLATE SPECIFIC CONTROLS
      pixelsize: 100,//MAX SIZE 
      squared: true,//SQUARED PIXELS pixel width = pixel height
      smooth: false,//ENNABLE SOOTHING
      



Exemple:


  $('#pixelate').imgfx({
  mode:"pixelate",
  pixelsize: 100,
  squared: true,
  smooth: false


  });

Random rect

This FX randomy draw the image with random scale and random origin.(Who much random)

This Fx uses the following controls:


   
         //RANDOMRECT SPECIFIC CONTROLS
     rdmMaxRect:20,///MAX NUMBER OF RECTANGLES
     rdmClearBeforeDraw:true///clear canvas every frame

      



Exemple:


  $('#randomrect').imgfx({
  mode:"randomrect",
  rdmMaxRect:20,
  rdmClearBeforeDraw:false

  });





All parameters list and default values

                   
      mode: "tv",//DIFFERENT MODES : "tv","glitch","zoom","rotozoom","pixelate","kaleid","bumpingrect","crazyrect",
      
      canvassize:2,//DEVIDE SIZE TO IMPROVE CANVAS PERFORMANCES The more you reduce the size the more you gain perf but you get bigger pixels

     
      blendMode: "difference",//DEFINE A globalCompositeOperation BLEND MODE FOR FX THAT USE IT

      crossOrigin:"Anonymous", //Define CORS param



      //TV SIMULATOR SPECIFIC CONTROLS
      noise: true,//ENNABLE NOISE LAYER
      grainsize: 1,//NOISE GRAIN SIZE
      desyncspeed: 5,//SYNC DISPLACEMENT SPEED
      syncup: true,//TRUE IMAGE MOVE UP FALSE IMAGE MOVE DOWN
      scanspacing: 22,//SPACE BETWEEN SCANLINES
      scanheight: 4,//SIZE OF SCANLINES
      scanspeed: 1,//SCANLINE DISPLACEMENT SPEED DEFAULT MOVE DOWN, NEGATIVE INT MOVE UP
      flickerseed: 10,//FLICKERING LESS THE NUMBER MORE IT FLICKERS    
      latunsync: 0,//DISPLACE IMAGE RANDOMLY ON X AXIS
      sidenoise: "left", // DRAW LATERAL NOISE LAYER options:"left","right","both","none" 
      sidenoiseforce: 8,//MAX FORCE OF SIDENOISE
      Themecolor: "#f00",//DEFINE A COLOR OF TV

      //BASE64GLITCH SPECIFIC CONTROLS
      base64glitchforce:0,//BASE64 DESTROY
      base64glitchInterval:300,//

      //LATERAL GLITCH SPECIFIC CONTROLS 
      glichforce: 80,
      glitchclear: true,
      glitchdisplayimage: true,

      //PIXELLATE SPECIFIC CONTROLS
      pixelsize: 100,//MAX SIZE 
      squared: true,//SQUARED PIXELS pixel width = pixel height
      smooth: false,//ENNABLE SOOTHING
      
      //BOUNCING RECT SPECIFIC CONTROLS
      //THIS FX USE THE blendMode PARAMETER
      rectNum: 4,//HOW MANY BOUNCING SQUARES
      rectSquare: false,//SQUARES OR RECTANGLES
      rectSpeed: 6,//DISPLACEMENT SPEED
      rectClear: true,//CLEAR SCREEN EVERY FRAME?


      ///ZOOM SPECIFIC CONTROLS
      zoomRotationSpeed:1,
      zoomSpeed:0.05,
      zoomMinScale:0.001,
      zoomMaxScale:15,
      zoomXframe:30,


     //ROTOZOOM SPECIFIC CONTROLS
      rotozoomSpeed:0.01,//ZOOMING SPEED 
      rotozoomRotateSpeed:0.2,//ROTATION SPEED [use max as 0.9999...]
      rotozoomMaxScale:5,//MAX ZOOM
      rotozoomMinScale:0.001,//MAX DE-ZOOM

     
     //KALEIDOSCOPE SPECIFIC CONTROLS
     thetaAngle:360,
     thetaDiv:30,

     //RANDOMRECT SPECIFIC CONTROLS
     rdmMaxRect:20,///MAX NUMBER OF RECTANGLES
     rdmClearBeforeDraw:true///clear canvas every frame                   
                   
                                        
                   

Help Wanted!!

This plug-in have some isues, and if you know well JQuery and would kindly like to help, this are the thing that i want to fix.
When you instantiate the plug-in a window.requestAnimationFrame or setInteval loop is triggered. If you remove the dynamicly generated canvas element from the DOM, those process are still running in background. I would like to add a .destroy() function that kill everything from an instance of the plug-in but don't know how to do that.
I know it's not really clear but if you ennable the javascript console (firebug or chrome dev tool) and trigger the TV simulator , I left a console log in the window.requestAnimationFrame that logs "im here". If you click Re-print image the loop is still running and still eating CPU. Question is how to kill this? (I know clearInterval() and cancelAnimationFrame() but dont know how to trigger it when output canvas is removed). Please have a look at the source. Every link to usefull documentation, or advice from clever coder can be helpfull !
Feel free to contact me via github, or my website.