开发者

Problem with getImageData function

开发者 https://www.devze.com 2023-03-19 03:05 出处:网络
So, I\'m using this function getImageData on the \"context\" variable that I\'ve made inside of the <script> part, and when I do something such as draw a rectangle then do ctx.getImageData.data[

So, I'm using this function getImageData on the "context" variable that I've made inside of the <script> part, and when I do something such as draw a rectangle then do ctx.getImageData.data[0] it will show the red value of that rectangle that I drew on the canvas. But, when I import an image and draw it onto the canvas, and try to use the getImageData.data[0] all I get is 0, which makes no sense, I'm not sure why it is not reading the image correctly. I've tried tutorials on this but they're all vague and only have segments written together.

So, when I draw the rectangle, its color value comes out just fine, but again, when I draw the image, even without drawing the rectangle on the canvas, 开发者_开发技巧I never get the value of that particular pixel on the image.

Can someone help me? Here's my currrent code:

<html>
<head>
<title>
Color Test :)
</title>
</head>

<body>

<canvas id = "ColorTest" width = "500" height = "500">
please don't use shitty browsers :)
</canvas>

<script>


//netscape.security.PrivilegeManage…
var canvas = document.getElementById("ColorTest")
, ctx = canvas.getContext("2d")
, image = new Image()

image.onload = function(){
ctx.drawImage(image,0,0)
}


image.src = 'Pikachu.gif'


ctx.fillStyle = "rgb(123,40,170)"
ctx.fillRect(0,0,100,100)



var imagedata = ctx.getImageData(0,0,100,100)
, spot = 0

while(imagedata.data[spot] == 0){
spot++
}

console.log(imagedata.data[0])


</script>
</body>
</html>


Does the following alert anything sensible?

image.onload = function() {
  ctx.drawImage(image, 0, 0);
  var id = ctx.getImageData(0,0,canvas.width,canvas.height);
  alert([id.data[0], id.data[1], id.data[2], id.data[3]].join(", "));
}


It could be that the image is transparent. Try with a single color non-transperent image.

What browser are you using? Using getImageData when loading an image from another domain is not allowed (security issue), and if I remember correctly there is some browsers that have problems determining the domain when running scripts localy

0

精彩评论

暂无评论...
验证码 换一张
取 消