0

All I need is to get the state of a single pixel on the canvas but I've noticed that it's horribly slow.

Here's my JS

var state = ctx.getImageData(x,y,1,1).data;
state = 'rgba(' + state[0] + ',' + state[1] + ',' + state[2] + ',' + state[3] + ')';
return state;

Is there an alternative to getting the state of a single pixel on the canvas?

1
  • No, there is not other method (that I know of). What you could try, is copying only that single pixel to another 1x1px canvas first (using drawImage), and then only get the imagedata of that … but whether this is gonna be any “faster” isn’t a given. Commented Feb 21, 2015 at 0:55

1 Answer 1

1

So how is this magic pixel changing?

Since you're doing the programming, you should likely be able to notice the activities that would change that pixel.

For example if you (or the user) are moving something across that pixel which changes that pixel, then you would be able to predict the path of any object over the pixel (or notice the user is moving something over that pixel).

Anyway, if .getImageData is dragging down your performance, my suggestion would be to set up a .requestAnimationFrame loop and check for changes on that pixel at larger intervals. If you're checking every 1/60th of a second, maybe just check every 5/60th of a second.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.