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

0 favourites
  • 4 posts
  • 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.

  • Here's a bit of info about the .asJson expression for the canvas:

    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.

  • Demo:

    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

    pixelCount/canvas.width*canvas.height[/code:u6nw2nwf] gave me an incredibly off result. So I threw in some brackets [code:u6nw2nwf]pixelCount/(canvas.width*canvas.height)[/code:u6nw2nwf] and it magically worked. That's not what I learnt in elementary school. I'm sorry if I didn't explain thoroughly my problem.
    Could I ask you why this lags every time I do the check and why there are two Canvas.AsJSON?
    My event sheet.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)