Image

Image Functions

loadImage

Async. Loads a graphics image from a disk file. Unless you want to draw a rectangular image, your image's background should be transparent, or you should specify the color of the image's background for the transparentColor parameter (see below). Images are cached, so if you load the same image with the same options more than once, it will reuse the originally loaded image for efficiency.

name
type
default
description

file*

object

-

the file that contains the image

rotate

number

0

the number of degrees to rotate the image counter-clockwise (use a negative number to rotate clockwise)

scale

number

1

the scaling (resizing) factor, where 1 means no scaling, 2 means draw the image at twice its usual size, 0.5 means draw the image at half its usual size, etc.

flipHorizontal

boolean

false

flip the image horizontally (across the Y axis)

flipVertical

boolean

false

flip the image vertically (across the X axis)

Returns

object an image that you can pass to the drawImage function (see below) to have drawn on the screen

world.soccerball = await loadImage(require("./soccer.jpg"))
world.soccerball = await loadImage(require("./soccer.jpg"), scale=0.5)

drawImage

Draws a graphic image on the screen

name
type
default
description

image*

object

-

the image to draw (must have been already loaded by the loadImage function)

x*

number

-

the x coordinate of the point on which to center the image

y*

number

-

the y coordinate of the point on which to center the image

rotate

number

0

the number of degrees to rotate the image counter-clockwise (use a negative number to rotate clockwise)

scale

number

1

the scaling (resizing) factor, where 1 means no scaling, 2 means draw the image at twice its usual size, 0.5 means draw the image at half its usual size, etc.

flipHorizontal

boolean

false

flip the image horizontally (across the Y axis)

flipVertical

boolean

false

flip the image vertically (across the X axis)

Returns

nothing

drawImage(world.soccerball, 300, 200)
drawImage(world.soccerball, 300, 200, 90)
drawImage(world.soccerball, 300, 200, 90, 0.75)
drawImage(world.soccerball, 300, 200, scale=0.75)
drawImage(world.soccerball, 300, 200, 45, 0.75, false, true)
drawImage(world.soccerball, 300, 200, flipHorizontal=true)

getImageWidth

Returns the width in pixels of a loaded image

name
type
default
description

image*

object

-

the image to get the width of

Returns

number The width in pixels

let width = getImageWidth(world.soccerball)

getImageHeight

Returns the height in pixels of a loaded image

name
type
default
description

image*

object

-

the image to get the height of

Returns

number The height in pixels

let height = getImageHeight(world.soccerball)

Last updated