Load Img from URL,how to Scaled Down RESPECTING AspectRatio?

Discussion and feedback on Construct 2

Post » Tue Oct 13, 2015 3:51 pm

So the Sprite Object can Load Image from URL, now, for example it loads from the file chooser and load the image from your phone.

The sprite is a square and the picture is not a square, the "resize to image size" will expand the sprite beyond the size of the screen so NO.

But the "keep current size" option, even though the image fits into the square, is stretched.
Is there a way to scale it down RESPECTING aspect ratio ?
B
12
S
3
Posts: 288
Reputation: 1,694

Post » Wed Oct 14, 2015 11:51 am

You will have to do a little maths to resize the incoming image to fit within the square.

-Use the width and height of the new image to work out the aspect ratio.
-Work out if the image is portrait or landscape.
-then set the new image width and height to fit within the square
B
24
S
7
G
1
Posts: 186
Reputation: 2,716

Post » Wed Oct 14, 2015 2:36 pm

Let me see if I get this straight:

Step 1: Let the image load and let the sprite resize to fit the image.

Step 2: THEN retrieve the resulting width/height of Sprite.

Step 3: Resize the Sprite from there.

This might work !!! I will let you know ! Thank you !
B
12
S
3
Posts: 288
Reputation: 1,694

Post » Wed Oct 14, 2015 4:01 pm

That's it..
Load from the URL and create an event with the trigger 'On Image URL loaded' to initialise the resize process.
B
24
S
7
G
1
Posts: 186
Reputation: 2,716

Post » Wed Oct 19, 2016 11:19 am

@Toddler or @aquinn:

Could you please explain to me what I need to do in the "step 3"?
B
4
S
2
Posts: 125
Reputation: 850

Post » Thu Oct 20, 2016 12:29 pm

Ok. I saw that it is an old thread but anyone can help me with this issue?
B
4
S
2
Posts: 125
Reputation: 850

Post » Thu Oct 20, 2016 1:23 pm

Braus wrote:Could you please explain to me what I need to do in the "step 3"?


Say sprite is supposed to be no bigger than 256x256, use a couple of variables/globals to store new width and height based on calculation that sets the biggest side to 256 and the other to a proportionate size, then apply those to the sprite

newWidth = sprite.Width >= sprite.Height ? 256 : (sprite.Width/sprite.Height)*256
newHeight = sprite.Height >= sprite.Width ? 256 : (sprite.Height/sprite.Width)*256

sprite.Width = newWidth
sprite.Height = newHeight

EDIT: Did a quick example capx (r233)
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
33
S
9
G
7
Posts: 450
Reputation: 6,780

Post » Fri Oct 21, 2016 11:07 am

Awesome @OddConfection!

Thanks for all the instructions and for the capx too.

I'm having a problem to load PNG files. The image has a transparent background but the bg always is showed black. Do you know how can I fix it?
B
4
S
2
Posts: 125
Reputation: 850

Post » Fri Oct 21, 2016 4:44 pm

Braus wrote:I'm having a problem to load PNG files. The image has a transparent background but the bg always is showed black. Do you know how can I fix it?


I'm not sure what the problem is as I am able to load a PNG with a transparent background without problem. Have you tried different PNGs? Maybe there is a problem with the one you are testing.

A long shot, but I know early versions of Internet Explorer had an issue with PNG transparency, maybe it's a browser issue.
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
33
S
9
G
7
Posts: 450
Reputation: 6,780

Post » Fri Oct 21, 2016 4:49 pm

I will check it. Thanks for all.
B
4
S
2
Posts: 125
Reputation: 850


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 12 guests