Jump to content

imageData.data.indexOf() doesn't work


Recommended Posts

Hello,I want to make a script to check if there is anything drawn on a canvas.I tried to use this script:

[/font][font=arial,helvetica,sans-serif]var ctx = document.getElementById("canvas").getContext("2d");[/font][font=arial,helvetica,sans-serif]/* Some other code, including some drawing code[/font][font=arial,helvetica,sans-serif]All drawings are black, with alpha value 255 */[/font] [font=arial,helvetica,sans-serif]var a = ctx.[color=#000000][size=1]getImageData(0,0,room.width,room.height)[/size][/color].data.indexOf(255); // to see if anything is drawn[/font][font=arial,helvetica,sans-serif]

But for some reason it didn't work.I have also tested some other array methods, but they didn't work either on the imageData.dataIs there some way to make this work?Using a for loop instead would probably be to slow. Michiel

Edited by troido
Link to post
Share on other sites

are you sure imageData is a property of the context object? you might need getImageData() instead.i'm not sure how to use indexOf on an ArrayBuffer, but the loop might be worth a try:

var canvas = document.getElementById("canvas");var ctx = canvas.getContext("2d");var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); var data = imageData.data;var dataLength = data.length; var alphaFound = false;for (var i=0; i<dataLength; i+=4){if (data[i] == 255){alphaFound = true;break;}}

Link to post
Share on other sites

Yes, i meant getImageData(...).In the code I use I had this, but i made a mistake in the post.I corrected the post now. The loop will probably work (if var i starts as 3 instead of 0) but i wanted to know if there is also an solution that works a little faster.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...