This function checks if a canvas pixel is black.
This is very useful for making pixel-perfect collisions in a javascript game.
argument x and y are for the coordinates of said pixel.
argument c is for the canvas context.
| function(x,y,c){return window[c].getImageData(x,y,1,1).data[1]<20} |
| function(x,y,c){return window[c].getImageData(x,y,1,1).data[1]<20} |
| { | |
| "name": "PixCheck", | |
| "description": "This function checks if a certain canvas pixel is black", | |
| "keywords": [ | |
| "canvas", | |
| "collision", | |
| "pixel", | |
| "data", | |
| "html" | |
| ] | |
| } |
| <!DOCTYPE html> | |
| <title>Foo</title> | |
| <div>Expected value: <b>undefined</b></div> | |
| <div>Actual value: <b id="ret"></b></div> | |
| <script> | |
| // write a small example that shows off the API for your example | |
| // and tests it in one fell swoop. | |
| var myFunction = function(){ /* the code here should be identical to the entry. */ } | |
| document.getElementById( "ret" ).innerHTML = myFunction() | |
| </script> |
Doesn't this fail for all red, blue and magenta colored pixels? Also (I'm sure you know) it's very slow to call
getImageDataover and over again.