troido Posted December 1, 2012 Share Posted December 1, 2012 (edited) 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 December 2, 2012 by troido Link to comment Share on other sites More sharing options...
JamesB Posted December 2, 2012 Share Posted December 2, 2012 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 comment Share on other sites More sharing options...
troido Posted December 2, 2012 Author Share Posted December 2, 2012 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 comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now