How do I check how much of a Canvas.AsJson is transparent?

Get help using Construct 2

Post » Wed Mar 04, 2015 11:59 pm

I'm struggling with the Canvas plugin to check in percentage how much of a Canvas is transparent loading the Canvas.AsJson expression in an Array every 1 second.
B
13
S
2
Posts: 27
Reputation: 952

Post » Thu Mar 05, 2015 8:03 pm

Here's a bit of info about the .asJson expression for the canvas:
viewtopic.php?f=147&t=118213&p=849241&hilit=canvas.asjson#p849241
And here's how to do it:

global number pixelCount = 0
global number percentClear = 0

every 1.0 seconds
--- array: load from canvas.asJSON
--- set pixelCount to 0
------ repeat canvas.width*canvas.height times
------ Array at (0,0,loopindex*4+3) = 0
--------- add 1 to pixelCount
--- set percentClear to pixelCount/canvas.width*canvas.height


In the array comparison instead of checking if the alpha is 0 you could for instance use <128 to count any pixel less than 50% trasparent.
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Fri Mar 06, 2015 12:09 am

Demo: http://yaserahmady.com/razor/

Thanks @R0J0hound but that was the exact same thing I've done, at least I think it was as I don't quite understand the textual representation of the events' flow.

The problem was apparently because
Code: Select all
pixelCount/canvas.width*canvas.height
gave me an incredibly off result. So I threw in some brackets
Code: Select all
pixelCount/(canvas.width*canvas.height)
and it magically worked. That's not what I learnt in elementary school. I'm sorry if I didn't explain thoroughly my problem.

Image

Could I ask you why this lags every time I do the check and why there are two Canvas.AsJSON?

Image

My event sheet.
Image
B
13
S
2
Posts: 27
Reputation: 952

Post » Fri Mar 06, 2015 2:04 am

Good catch with the parenthesis, multiplication and division are done left to right. I think I learned about that in basic algebra.

It's slow because between the .asJSON expression and loading it into an array it's a slow operation. I haven't really seen a way to make it faster.

There are two asJSON expressions because all objects have that expression to get a json version of their state. For canvas object they both do the same thing.
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], evillair, mongoose665, tarek2 and 59 guests