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?
- <!DOCTYPE html>
- <html>
- <head>
- <title> EE Q_28383720 </title>
- <script src="http://code.jquery.com/jquery.min.js"></script>
- <style>
- .icone
- {
- margin-left:0px;
- }
- .icone-1 { background-position: left top }
- .icone-2 { background-position: 14.2857% top }
- .icone-3 { background-position: 28.5714% top }
- .icone-4 { background-position: 42.8571% top }
- .icone-5 { background-position: 57.1428% top }
- .icone-6 { background-position: 71.4285% top }
- .icone-7 { background-position: 85.7142% top }
- .icone-8 { background-position: right top }
- ul
- {
- padding:0px;
- border:3px solid blue;
- display: inline-block;
- }
- .icone li {
- margin:auto;
- text-align:center; /* non*/
- vertical-align:bottom;
- padding:0px;
- width: 75px;
- height: 100px;
- border-radius:16px;
- border: solid white 5px;
- color: white;
- opacity: 1;
- background-image: url("http://www.davidinlove.com/img/sprite_menu_DT.jpg");
- background-repeat: no-repeat;
- display: inline-block;
- list-style-type:none;
- }
- .menuli
- {
- display: inline-block;
- margin-top: 60px;
- padding: auto;
- width: 99%;
- text-align:center;
- border-radius:5px;
- bottom: 0;
- background-color: rgb(24,24,24); /* Pour les anciens navigateurs */
- background-color: rgba(24,24,24,0.6);
- }
- </style>
- </head>
- <body>
- <div id="id-menu" class="c-menu">
- SPRITE<br/>
- <ul class="icone">
- <a href="#" id = "IMG1" ><li class="icone-1" id = "IMG1child" ><div class="menuli">Draw the <br\>eiffel tower</div></li></a>
- <a href="#" id = "IMG2" ><li class="icone-2" id = "IMG2child" ></li></a>
- <a href="#" id = "IMG3" ><li class="icone-3" id = "IMG3child" ></li></a>
- <a href="#" id = "IMG4" ><li class="icone-4" id = "IMG4child" ></li></a>
- <a href="#" id = "IMG5" ><li class="icone-5" id = "IMG5child" ></li></a>
- <a href="#" id = "IMG6" ><li class="icone-6" id = "IMG6child" ></li></a>
- <a href="#" id = "IMG7" ><li class="icone-7" id = "IMG7child" ></li></a>
- <a href="#" id = "IMG8" ><li class="icone-8" id = "IMG8child" ></li></a>
- </ul>
- </ul>
- </div>
- <hr>
- CANVAS FROM PICTURE</br>
- <canvas id="myCanvas_OUT0" width="75" height="100"></canvas>
- </br>
- FILTERING EFFECT ON CANVAS</br>
- <canvas id="myCanvas_OUT21" width="75" height="100"></canvas>
-
- <script>
- function custompos(p, isY, cnv, img) {
- var ti, tc;
- if (isY) {
- ti = img.height;
- tc = 100; // cnv.height;
- p = p.replace('top', '0').replace('bottom', '100%'); // ti - tc
- } else {
- ti = img.width;
- tc = 75; // cnv.width;
- p = p.replace('left', '0').replace('right', '100%'); // ti - tc
- }
- if (p.substr(-1) == '%') {
- p = Number(p.substr(0, p.length - 1)) * (ti - tc) / 100;
- }
- return p;
- }
- $("a").click( function(e) {
- e.preventDefault();
- img_srcout0="";
- img_srcout0= $(this).children(":first").css('background-image').replace(/^url\((['"]?)(.*?)\1\).*$/i, "$2");
- var bpos = $(this).children(":first").css('background-position');
- elemout0 = document.getElementById("myCanvas_OUT0");
- if (elemout0 && elemout0.getContext)
- {
- contextout0 = elemout0.getContext("2d");
- if (contextout0)
- {
- imgout0=new Image();
- imgout0.onload=function(){
- // some extra code to get position, this is not completely generic!
- var bposa = bpos.split(' ');
- var bposx = custompos(bposa[0], false, elemout0, imgout0);
- var bposy = custompos(bposa[1], true, elemout0, imgout0);
- contextout0.drawImage(imgout0,-bposx,-bposy);
- //imgdout0 = contextout0.getImageData(0, 0, 303,539);
- //pixout0 = imgdout0.data;
- };
- imgout0.src=img_srcout0;
- }
- }
- });
- $("#myCanvas_OUT0").on("click", function(e) {
- //GET THE DATA OF THE myCanvas_OUT0
- var canvas_out0 = $("#myCanvas_OUT0");
- var canvasout0Position = {
- x: canvas_out0.offset().left,
- y: canvas_out0.offset().top
- };
- var elemout0 = document.getElementById("myCanvas_OUT0");
- if (elemout0 && elemout0.getContext)
- {
- var contextout0 = elemout0.getContext("2d");
- }
- //********** It seems here is the problem
- //I need to know how to get the array with the values of the pixels of the canvas
- //This codes seems OK to take the data from a photo but my website will process data of canvas
-
- var imgdout0 = contextout0.getImageData(0, 0, 150,200);
- var pixout0 = imgdout0.data;
-
-
- //Define the second canvas
-
-
- var canvas_out21 = $("#myCanvas_OUT21");
- var canvasout21Position = {
- x: canvas_out21.offset().left,
- y: canvas_out21.offset().top
- };
- var elemout21 = document.getElementById("myCanvas_OUT21");
- if (elemout21 && elemout21.getContext)
- {
- var contextout21 = elemout21.getContext("2d");
-
-
- }
-
- var imgdout21 = contextout21.getImageData(0, 0, 150,200);
- var pixout21 = new Array();
-
- //FILTER THE DATA
- //This I do here in a simple example
-
- for(j=0;j<pixout0.length;j+=4)
- {
- pixout21[j]=pixout0[j]/2 ;
- pixout21[j+1]=pixout0[j+1]/2 ;
- pixout21[j+2]=pixout0[j+2]/2 ;
- pixout21[j+3]=255;
- }
-
-
-
- //DISPLAY DATA IN The myCanvas_OUT21
-
- contextout21.putImageData(imgdout21, 0, 0);
-
- });
- </script>
- </body>
- </html>
David ( @webtecgeek www.thecacaocafe.com )