pxt-calliope/docs/reference/images/width.md

61 lines
1.4 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Width
2016-04-02 01:22:47 +02:00
The width function.
2016-03-26 00:47:20 +01:00
2016-04-16 00:02:26 +02:00
Get the width of an [Image](/reference/images/image) in columns.
2016-03-26 00:47:20 +01:00
2016-04-18 17:33:09 +02:00
```sig
images.createImage().width();
2016-03-26 00:47:20 +01:00
```
## Parameters
2016-03-26 00:47:20 +01:00
* none
## Returns
2016-03-26 00:47:20 +01:00
2017-03-16 15:57:41 +01:00
* [Number](/types/number) - the number of columns in a image. This function returns 5 if the image has 1 frame, 10 for 2 frames, 15 for 3 frames and so on. Divide the number of columns by 5 to find out how many frames an image has (see example below).
2016-03-26 00:47:20 +01:00
The following example gets the width of `img` and stores it in the `w` variable:
## ~hide
2016-03-26 00:47:20 +01:00
```blocks
2016-03-26 00:47:20 +01:00
let img = images.createImage(`
. . # . . . . . . .
. # . # . . . # . .
. . # . . . . . . .
. # . # . . . # . .
. . # . . . . . . .
`)
```
## ~
2016-03-26 00:47:20 +01:00
```typescript-ignore
2016-03-26 00:47:20 +01:00
let w = img.width()
```
## Example: show each frame
2016-03-26 00:47:20 +01:00
The following example uses the `width` function with a [for](/blocks/loops/for) loop to show each image frame on the screen:
2016-03-26 00:47:20 +01:00
```typescript
2016-03-26 00:47:20 +01:00
let img2 = images.createImage(`
. . # . . . # # # # . # # # .
. # # . . . . . . # . . . # .
. . # . . . . . # . . . # . .
. . # . . . . # . . . . . # .
. . # . . . # # # # . # # # .
`)
for (let i = 0; i < img2.width() / 5; i++) {
img2.showImage(i * 5)
basic.pause(1000)
}
```
## See also
2016-03-26 00:47:20 +01:00
2016-06-19 14:28:46 +02:00
[show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image), [show animation](/reference/basic/show-animation)
2016-03-26 00:47:20 +01:00