How do I detect transparent pixels with javascript

Get help using Construct 2

Post » Mon Oct 26, 2015 4:45 am

As far as I know there's no built-in way in C2 to determine whether a user has clicked on a transparent area of a sprite.
Is there a chance to insert a Browser.Exec.JS on the "On object clicked" event and capture the transparency value into a variable?
I'm thinking I can then trigger the action I want only if the value in the variable is <255.
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Mon Oct 26, 2015 8:05 am

Could you explain in what way this would be useful?

I know the canvas plugin by @Rojohound has the functionality to get rgbaAt any x,y
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Mon Oct 26, 2015 8:32 am

@LittleStain

I'm doing an application where several images are loaded in runtime and then put into animation frames of a single sprite.
I then create instances of that sprite and set each one into a different animation frame. (Thus, i have a bunch of images on the canvas)
Now, the collision polygon of each of these instances is its bounding box (can't change that on runtime).

All I want to do, is when I click on one of them (on a transparent area), ignore the click, check if there's another sprite instance under the mouse pointer, and fire a click event on that one instead.

Thanks for bringing into attention the canvas plugin. I'll give it a go!

EDIT: Maybe this is a good place for a request. @Ashley, how difficult would be to implement a feature (both in the editor and on runtime), where mouse events ignore completely transparent pixels on a sprite?
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Mon Oct 26, 2015 12:31 pm

This kind of feature is difficult to support since it requires fetching pixel data from the GPU, which typically stalls the rendering pipeline and results in awful performance. So because of that if at all possible you should not even try to do this, and look for an alternative solution. For example perhaps there could be some way to use an existing sprite with a collision polygon that can be used to test collisions instead?
Scirra Founder
B
399
S
236
G
89
Posts: 24,546
Reputation: 195,436

Post » Mon Oct 26, 2015 1:04 pm

@Ashley
Thanks a lot for the detailed reply! I truly understand now how difficult it is to support this feature. As an alternative, I have the option to display a list to select the sprites from and it seems that's the way I'm going to. Thanks again for your valuable time!
B
13
S
5
G
1
Posts: 116
Reputation: 1,805


Return to How do I....?

Who is online

Users browsing this forum: blackhornet and 25 guests