how to get the pixel data from a canvas filter it and create a 2 canvas.

how to get the pixel data from a canvas filter it and create a 2 canvas.

Hello,


My question is how can I get the values of the pixels in a canvas stored in a 1 dimension array.

I have been able to know the values of the pixel of a photo and transform them in canvas.

It seems stupid but I cannot find on the web how to get the data of a canvas.

In the example here I load a first canvas from images=> OK

Now I want to get the data from the first canvas to filter it and create a second canvas.

Can some one help me please?


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title> EE Q_28383720 </title>
  5. <script src="http://code.jquery.com/jquery.min.js"></script>
  6. <style>

  7. .icone
  8. {
  9. margin-left:0px;
  10. }


  11. .icone-1 { background-position: left top }
  12. .icone-2 { background-position: 14.2857% top }
  13. .icone-3 { background-position: 28.5714% top }
  14. .icone-4 { background-position: 42.8571% top }
  15. .icone-5 { background-position: 57.1428% top }
  16. .icone-6 { background-position: 71.4285% top }
  17. .icone-7 { background-position: 85.7142% top }
  18. .icone-8 { background-position: right top }





  19. ul
  20. {
  21. padding:0px;
  22. border:3px solid blue;
  23. display: inline-block;
  24. }

  25. .icone li {
  26.   margin:auto;
  27.   text-align:center; /* non*/
  28.   vertical-align:bottom;
  29.   padding:0px;
  30.   width: 75px;
  31.   height: 100px;
  32.   border-radius:16px;
  33.   border: solid white 5px;
  34.   color: white;
  35.   opacity: 1;
  36.   background-image: url("http://www.davidinlove.com/img/sprite_menu_DT.jpg");
  37.   background-repeat: no-repeat;
  38.   display: inline-block;
  39.   list-style-type:none;
  40. }

  41. .menuli
  42. {
  43. display: inline-block;
  44. margin-top: 60px;
  45. padding: auto;
  46. width: 99%;
  47. text-align:center;
  48. border-radius:5px;
  49. bottom: 0;
  50. background-color: rgb(24,24,24); /* Pour les anciens navigateurs */
  51. background-color: rgba(24,24,24,0.6);
  52. }
  53. </style>
  54. </head>
  55. <body>
  56.   <div id="id-menu" class="c-menu">
  57.     SPRITE<br/>
  58.     <ul class="icone">
  59.       <a href="#" id = "IMG1" ><li class="icone-1" id = "IMG1child" ><div class="menuli">Draw the <br\>eiffel tower</div></li></a>
  60.       <a href="#" id = "IMG2" ><li class="icone-2" id = "IMG2child" ></li></a>
  61.       <a href="#" id = "IMG3" ><li class="icone-3" id = "IMG3child" ></li></a>
  62.       <a href="#" id = "IMG4" ><li class="icone-4" id = "IMG4child" ></li></a>
  63.       <a href="#" id = "IMG5" ><li class="icone-5" id = "IMG5child" ></li></a>
  64.       <a href="#" id = "IMG6" ><li class="icone-6" id = "IMG6child" ></li></a>
  65.       <a href="#" id = "IMG7" ><li class="icone-7" id = "IMG7child" ></li></a>
  66.       <a href="#" id = "IMG8" ><li class="icone-8" id = "IMG8child" ></li></a>
  67.       </ul>
  68.     </ul>
  69.     </div>

  70. <hr>
  71. CANVAS FROM PICTURE</br>
  72. <canvas id="myCanvas_OUT0" width="75" height="100"></canvas>

  73. </br>
  74. FILTERING EFFECT ON CANVAS</br>
  75. <canvas id="myCanvas_OUT21" width="75" height="100"></canvas>


  76. <script>




  77. function custompos(p, isY, cnv, img) {
  78.   var ti, tc;
  79.   if (isY) {
  80.     ti = img.height;
  81.     tc = 100; // cnv.height;
  82.     p = p.replace('top', '0').replace('bottom', '100%'); // ti - tc
  83.   } else {
  84.     ti = img.width;
  85.     tc = 75; // cnv.width;
  86.     p = p.replace('left', '0').replace('right', '100%'); // ti - tc
  87.   }
  88.   if (p.substr(-1) == '%') {
  89.     p = Number(p.substr(0, p.length - 1)) * (ti - tc) / 100;
  90.   }
  91.   return p;
  92. }


  93. $("a").click( function(e) {
  94.   e.preventDefault();
  95.   img_srcout0="";
  96.   img_srcout0= $(this).children(":first").css('background-image').replace(/^url\((['"]?)(.*?)\1\).*$/i, "$2");
  97.   var bpos = $(this).children(":first").css('background-position');
  98.   elemout0 = document.getElementById("myCanvas_OUT0");
  99.   if (elemout0 && elemout0.getContext) 
  100.   {
  101.     contextout0 = elemout0.getContext("2d");
  102.     if (contextout0) 
  103.     {
  104.       imgout0=new Image();
  105.       imgout0.onload=function(){

  106.         // some extra code to get position, this is not completely generic!
  107.         var bposa = bpos.split(' ');
  108.         var bposx = custompos(bposa[0], false, elemout0, imgout0);
  109.         var bposy = custompos(bposa[1], true, elemout0, imgout0);
  110.         contextout0.drawImage(imgout0,-bposx,-bposy);

  111.         //imgdout0 = contextout0.getImageData(0, 0, 303,539);
  112.         //pixout0 = imgdout0.data;
  113.       };
  114.       imgout0.src=img_srcout0;
  115.     }
  116.   }
  117. });



  118. $("#myCanvas_OUT0").on("click", function(e) {  

  119. //GET THE DATA OF THE myCanvas_OUT0

  120. var canvas_out0 = $("#myCanvas_OUT0");
  121. var canvasout0Position = {
  122. x: canvas_out0.offset().left,
  123. y: canvas_out0.offset().top
  124. };


  125. var elemout0 = document.getElementById("myCanvas_OUT0");
  126. if (elemout0 && elemout0.getContext) 
  127. {
  128. var contextout0 = elemout0.getContext("2d");
  129. }

  130. //********** It seems here is the problem
  131. //I need to know how to get the array with the values of the pixels of the canvas
  132. //This codes seems OK to take the data from a photo but my website will process data of canvas
  133. var imgdout0 = contextout0.getImageData(0, 0, 150,200);
  134. var pixout0 = imgdout0.data;
  135. //Define the second canvas
  136. var canvas_out21 = $("#myCanvas_OUT21");
  137. var canvasout21Position = {
  138. x: canvas_out21.offset().left,
  139. y: canvas_out21.offset().top
  140. };
  141. var elemout21 = document.getElementById("myCanvas_OUT21");
  142. if (elemout21 && elemout21.getContext) 
  143. {
  144.  var contextout21 = elemout21.getContext("2d");
  145.  

  146. }
  147. var imgdout21 = contextout21.getImageData(0, 0, 150,200);
  148. var pixout21 = new Array();

  149. //FILTER THE DATA
  150. //This I do here in a simple example 
  151. for(j=0;j<pixout0.length;j+=4)
  152. pixout21[j]=pixout0[j]/2 ;
  153. pixout21[j+1]=pixout0[j+1]/2 ;
  154. pixout21[j+2]=pixout0[j+2]/2 ;
  155. pixout21[j+3]=255;
  156. }
  157. //DISPLAY DATA IN The myCanvas_OUT21
  158. contextout21.putImageData(imgdout21, 0, 0);
  159. });

  160. </script>

  161. </body>
  162. </html>

David  ( @webtecgeek   www.thecacaocafe.com )