Transparent background sprite...[Solved]

Get help using Construct 2

Post » Mon Nov 25, 2013 5:45 am

Basically I'm trying to take an image that has been canvas snapshotted and make the background of the image transparent after the snapshot takes place. The current background of the image that has been snapshotted is white, but I was wondering if anyone knew a way to make the white portions of an image transparent, either through webgl or another method?

Anyone got any ideas?

retrodude2013-11-25 19:07:37
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Nov 25, 2013 8:49 am

One way would be to send the snapshot URL to a server script with AJAX. there are many image libraries for PHP, which allow you to do such things. Then you can retrieve the URL of the new generated image when the ajax request completes, and invoke download

Maybe it is possible to do it on the client side only, but I don't know how
B
12
S
5
G
2
Posts: 132
Reputation: 2,637

Post » Mon Nov 25, 2013 8:54 am

It depends on how fast you need this to be. For example, you could use ROJOhound's Canvas plugin where you can paste the sprite to the canvas plugin, then loop through all pixels, get the color/alpha value with rgbaAt() and in case it's white replace the alpha with

Draw point (x,y) with "rgba(0, 0, 0, 0)"

Of course that wouldn't be very fast.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Mon Nov 25, 2013 1:37 pm

@tulamide

Wouldn't that be cpu intensive? The canvas plugin itself is very useful but I noticed it uses alot of memory to use a canvas object. It's fine if its slow, I'm just wondering how it would work performance wise?
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Nov 25, 2013 3:12 pm

@OlivierC

I'm reading on this now, although I would prefer a client-sided method since ajax isn't really fully supportive with C2 unless you know how to cross-domain or bypass all those permissions.retrodude2013-11-25 16:25:22
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Nov 25, 2013 3:46 pm

Does anyone know if the canvas snapshot can take an image but change all the white spots of an image to transparent?

I tried setting the canvas layer where the snapshot is taking place to transparent at "yes", but it seems when you load the canvas snapshot the image has a black background instead of a transparent one.

Lets say you have a background with rgb values at (255,255,255). So a white background. Is it possible to set pixels with (255,255,255) to transparent? Kinda like a direct conversion. If that makes sense.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Nov 25, 2013 6:00 pm

You could use the canvas plugin like tulamide said, but yes it would be pretty slow as looping over all the pixels of an image in general is a slow thing to do without a gpu shader.

You can take a snapshot with a transparent background. You just need to set the bottom layer to be transparent.
Here's my test: https://dl.dropboxusercontent.com/u/5426011/examples20/snapshot_transparent.capx
B
94
S
33
G
118
Posts: 5,394
Reputation: 75,849

Post » Mon Nov 25, 2013 6:33 pm

@R0J0hound

Much appreciated rojo! Without looking at your capx I'd probably be lost.. I did try this method, but one distinct thing I noticed from your capx file was that you had clear background set to "yes" which seems to make the transparent backgrounds event work. Without setting it to "yes" you get this funky transparent background that shows up in black. But performance-wise shouldn't it be off? retrodude2013-11-25 18:33:54
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Nov 25, 2013 6:54 pm

With it off the canvas isn't cleared so you'll have no transparency. First an foremost get something working and then you can look for shortcuts and speedups.R0J0hound2013-11-25 18:55:36
B
94
S
33
G
118
Posts: 5,394
Reputation: 75,849

Post » Mon Nov 25, 2013 7:07 pm

Well nonetheless, problem solved. I guess for smaller projects it barely affects your performance, so it works. Thanks rojo.

This thread can be closed.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 10 guests