Discuss Scratch

rui_mac
Scratcher
61 posts

Getting the width and height of a sprite, at 100% magnification

We should have a way to get the width and height of the current costume of a sprite.
The value would be for the costume at 100% magnification, of course.
AarcaWhale
Scratcher
100+ posts

Getting the width and height of a sprite, at 100% magnification

There's already a way, underneath the name of the costume.
TheLogFather
Scratcher
1000+ posts

Getting the width and height of a sprite, at 100% magnification

If I did want to get the values within a project for some reason, I'd do it something like this…
define set w & h to costume dimensions; original position: (x) (y) // set this to run non-refresh
set [w v] to [480] // !!!!! NOTE !!!!! This all assumes the costume will be less...
set [h v] to [360] // ...than 480 pixels wide and 360 pixels high, and assumes...
go to x: (0) y: (0) // ...the costume is not touching edge when at (0,0).
repeat until <touching [edge v]?> // It also gets the w & h specific to the current orientation of the costume.
change x by (1)
end
change [w v] by ((0)-(x position))
set x to (0)
repeat until <touching [edge v]?>
change x by (-1)
end
change [w v] by (x position)
set x to (0)
repeat until <touching [edge v]?>
change y by (1)
end
change [h v] by ((0)-(y position))
set y to (0)
repeat until <touching [edge v]?>
change y by (-1)
end
change [h v] by (y position)
go to x: (x) y: (y) // put sprite back to where it was
(Hope I got that all right – but you should get the idea, at least…)

Use it like this:
set w & h to costume dimensions; original position: (x position) (y position) :: custom block
If the costume is actually larger than 480x360, then you could probably adapt the above script by making it shrink so it fits within the bounds of the screen (i.e. so it's not touching edge when at 0,0), and then use the final “size” to scale up the resulting values appropriately.

Hope that makes sense!

Last edited by TheLogFather (April 19, 2016 20:27:58)

rui_mac
Scratcher
61 posts

Getting the width and height of a sprite, at 100% magnification

Yes, I created a similar script. But it is cumbersome. Specially if I have to do it for many costumes.
I was trying to create a “print anywhere in the screen with proportional fonts” script.
And, with proportional fonts, the “print head” will advance in a variable way, depending on the width of the previously printed letter.
I may have to store the widths in a list
TheLogFather
Scratcher
1000+ posts

Getting the width and height of a sprite, at 100% magnification

rui_mac wrote:

Yes, I created a similar script. But it is cumbersome. Specially if I have to do it for many costumes.
I was trying to create a “print anywhere in the screen with proportional fonts” script.
And, with proportional fonts, the “print head” will advance in a variable way, depending on the width of the previously printed letter.
I may have to store the widths in a list
Ah yes, you definitely should do that. The best way is to precompute such things, since using “touching” every time is way too slow.

Also, when I played around with such things, I found it also helped to have different lists for different (small) font sizes, since the system font-rendering doesn't necessarily scale exactly at such scales (at least, it didn't for me with vector costumes – didn't try with bitmap, so that could be different…?)

rui_mac
Scratcher
61 posts

Getting the width and height of a sprite, at 100% magnification

Yeah, I will really need to store it all in a list. Well, it will be faster, once all the data is input
griffpatch_tutor
Scratcher
28 posts

Getting the width and height of a sprite, at 100% magnification

Here is a fast way to get and set the width/height of a sprite in pixels: https://scratch.mit.edu/projects/110878444/ (it also supports costumes bigger than the screen)

Last edited by griffpatch_tutor (May 23, 2016 11:49:56)

TheLogFather
Scratcher
1000+ posts

Getting the width and height of a sprite, at 100% magnification

griffpatch_tutor wrote:

Here is a fast way to get and set the width/height of a sprite in pixels: https://scratch.mit.edu/projects/110878444/ (it also supports costumes bigger than the screen)
Ah, that's a neat way to do it!

Interesting though… for some reason it'd never quite registered with me that it's always exactly 15 pixels. I'd always had it in my head that it was related to the actual size of the costume. Yet I can see from the source code that it grows the bounding box by 3 pixels (I guess that's to avoid problems with small costumes), and then makes it overlap the edge by 18 pixels.

portalpap
Scratcher
1 post

Getting the width and height of a sprite, at 100% magnification

I made a script you can just place in and it will put the costumes dimensions in a list
https://scratch.mit.edu/projects/703833312

Last edited by portalpap (June 11, 2022 00:05:30)

Purplefork
Scratcher
1 post

Getting the width and height of a sprite, at 100% magnification

portalpap wrote:

I made a script you can just place in and it will put the costumes dimensions in a list
https://scratch.mit.edu/projects/703833312

So I adapted portalpap's project to be similar to the project that griffpatch_tutor posted earlier in the thread (without having looked at the code in griffpatch_tutor's project).

griffpatch_tutor wrote:

Here is a fast way to get and set the width/height of a sprite in pixels: https://scratch.mit.edu/projects/110878444/ (it also supports costumes bigger than the screen)

Strangely enough, I'm having the exact opposite issue to what griffpatch_tutor wrote in his description about the drawn bounding box being too large on some sprites: my drawn bounding boxes are too small! It's most noticeable with the 4 costume and the 7 costume, but I've noticed that it happens with each one. Does anyone know why this might be happening? https://scratch.mit.edu/projects/709706903/

Last edited by Purplefork (June 28, 2022 01:50:37)

TheRealHeavy_Lvy
Scratcher
1 post

Getting the width and height of a sprite, at 100% magnification

AarcaWhale wrote:

There's already a way, underneath the name of the costume.
He means to use a block to get the width and height
gfall219
Scratcher
24 posts

Getting the width and height of a sprite, at 100% magnification

AarcaWhale wrote:

There's already a way, underneath the name of the costume.
But witch side is the width and height
gfall219
Scratcher
24 posts

Getting the width and height of a sprite, at 100% magnification

griffpatch_tutor wrote:

Here is a fast way to get and set the width/height of a sprite in pixels: https://scratch.mit.edu/projects/110878444/ (it also supports costumes bigger than the screen)
It helped thanks I am making one of your games and I am a fan I am making terraria

Last edited by gfall219 (Feb. 18, 2024 19:25:47)

Dreamhorn_Camps
Scratcher
1 post

Getting the width and height of a sprite, at 100% magnification

gfall219 wrote:

AarcaWhale wrote:

There's already a way, underneath the name of the costume.
But witch side is the width and height

The second one the the height, the first one is the width.

Powered by DjangoBB