How can I change data-src attribut of a canvas in jquery

How can I change data-src attribut of a canvas in jquery

Hi,

In a previous post I've learned how to load a canvas from png.
It is great but now I feel not very pride because I have a technical problem I cannot solve.
I would like to load the data of different png one after one another.

My idea is to do a setinterval just specifying the data-src before execute for different images.

The problem is I don't find how to change data-src of canvas with JQUERY.

Here is my code.

Can some one just tell me where and what I must include to load for example testbis.png?


Thanks!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>ANIMATION with timer: a solution very difficult to apply</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <script src="http://code.jquery.com/jquery-2.1.0.js"></script>
  7. </script>
  8. </head>
  9. <body>
  10. <canvas id="myCanvas_IN" width="448" height="448" data-src="http://www.thecacaocafe.com/img/test.png">
  11. </canvas>
  12. <br>
  13. <script>

  14. $(function() {
  15. $("canvas[data-src]").each(function(){
  16. var img = new Image,
  17. canvas = this
  18. img.onload = function(){
  19. canvas.getContext("2d").drawImage(img, 0, 0);
  20. // at this point you have a loaded canvas, and can start playing with it.
  21. contextin = canvas.getContext("2d");
  22. imgdin = contextin.getImageData(0, 0, 448, 448);
  23. pixin = imgdin.data;
  24.  
  25.  
  26.  
  27. for (var i=0;i<pixin.length;i++)
  28. {
  29. pixin[i*4  ] = (pixin[i*4  ] < 125)?pixin[i*4  ]*2:pixin[i*4  ];
  30. pixin[i*4 +1 ] = pixin[i*4 +1 ]/2;
  31. pixin[i*4 +2 ] = pixin[i*4 +2 ]/2;
  32. pixin[i*4 +3 ] = 255;
  33. }
  34. contextin.putImageData(imgdin, 0, 0);
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. }
  42. img.src=$(canvas).data("src")
  43. })
  44. });

  45. </script>
  46. </body>
  47. </html>

David  ( @webtecgeek   www.thecacaocafe.com )