gameify. Image

An image for use in sprites and other places.

Constructor

new Image(pathopt)

Parameters:
NameTypeAttributesDescription
pathString<optional>

The image filepath. (Can also be a dataURI). If not specified, the image is created with no texture

Example
let playerImage = new gameify.Image("images/player.png");

Members

loaded :Boolean

If the image is loaded

Type:
  • Boolean

opacity :Number

The opaciy of the image

Type:
  • Number
Default Value
  • 1

(readonly) path

The image filepath. Modifying this will not do anything.

tileData :Object

If the image was derived from a tileset, details about where it came from. Mostly used for serialization. See example below for schema.

Type:
  • Object
Example
{   // tileData schema:
    tileset: Tileset,
    position: { x: Number, y: Number }
    size: { x: Number, y: Number }
    collisionShape: gameify.shapes.Shape OR undefined,
    tags: [ String ]
}

Methods

changePath(path)

Change and load a new image path. Reset's the image's crop

Parameters:
NameTypeDescription
pathstring

The new image path

crop(x, y, width, height)

Crop the image

Parameters:
NameTypeDescription
xNumber

how much to crop of the left of the image

yNumber

how much to crop of the right of the image

widthNumber

how wide the resulting image should be

heightNumber

how tall the resulting image should be

draw(context, x, y, w, h, r)

Draw the image to a context

Parameters:
NameTypeDescription
contextCanvasRenderingContext2D

The canvas context to draw to

xNumber

The x coordinate to draw at

yNumber

The y coordinate to draw at

wNumber

Width

hNumber

Height

rNumber

Rotation, in degrees

fromJSON(data, ref) → {gameify.Image}

Creates a object from JSON data

Parameters:
NameTypeDescription
dataObject | Array

Serialized object data (from object.toJSON)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
gameify.Image

getCrop()

Get the image crop. Returns an object with x, y, width, and height properties.

onLoad(callback)

Set a function to be run when the image is loaded

Parameters:
NameTypeDescription
callbackfunction

The function to be called when the image is loaded.

toJSON(keyopt, ref) → {Object}

Convert the object to JSON

Parameters:
NameTypeAttributesDescription
keystring<optional>

Key object is stored under (unused, here for consistency with e.g. Date.toJSON, etc.)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
Object

uncrop()

Remove crop from the image