API Docs for:
Show:

gs.Bitmap Class

The bitmap class allows to load images from files or creating in-memory bitmap. It offers different methods to directly draw to the bitmap or recolor it. You can attach a bitmap to a gs.Sprite object to display it on screen.

All bitmaps which are loaded from image files are immutable by default to keep memory usage as low as possible. The pixel-data of an immutable bitmap cannot be changed and the bitmap cannot be drawn onto another bitmap. However, an immutable bitmap will be automatically turned into a mutable bitmap whenever you try to draw onto that bitmap or use it as a source-bitmap. A mutable bitmap may take the double/triple amount of memory compared to an immutable one.

You can make a bitmap immutable/mutable manually anytime using the makeMutable/makeImmutable methods. Notice that a conversion from immutable to mutable or vice-versa may be time-consuming.

Constructor

gs.Bitmap

(
  • width/filePath/image
  • [height/languageCode=null]
)

Parameters:

  • width/filePath/image Object
    • The width of the bitmap for in-memory creation OR the filePath to an image resource to load it from file OR an HTMLImageElement to create the bitmap from.
  • [height/languageCode=null] Number optional
    • The height of the bitmap OR the language code if the bitmap is created from filePath.

Methods

blt

(
  • x
  • y
  • srcBitmap
  • srcRect-
  • opacity
)

Blits/Draws the specified bitmap on this bitmap.

Parameters:

  • x Number
    • The x-coordinate of the position.
  • y Number
    • The y-coordinate of the position.
  • srcBitmap gs.Bitmap
    • The bitmap to blit/draw.
  • srcRect- gs.Rect

    The source-rectangle. Only that area will be blitted/drawn.

  • opacity Number
    • The opacity

changeHue

(
  • hue
)

Changes the HUE of the bitmap. Once the HUE was changed it cannot changed back by passing 0 for hue or something. The HUE change is always executed on the current bitmap-data.

Parameters:

  • hue Number
    • The HUE in degrees.

clear

()

Clears the bitmap.

clearRect

(
  • x
  • y
  • width
  • height
)

Clears the specified rectangle-area.

Parameters:

  • x Number
    • The x-coordinate of the rectangle.
  • y Number
    • The x-coordinate of the rectangle.
  • width Number
    • The width of the rectangle.
  • height Number
    • The height of the rectangle.

dispose

()

Disposes the bitmap and frees the memory. Should be called if the bitmap is no longer needed to free resources which are not handled by the garbage collector.

drawText

(
  • x
  • y
  • w
  • h
  • text
  • [hAlign=gs.Alignment.LEFT]
  • [vAlign=gs.Alignment.TOP]
)

Draws the specified text on the bitmap.

Parameters:

  • x Number
    • The x-coordinate of the text's position.
  • y Number
    • The y-coordinate of the text's position.
  • w Number
    • The width of text-box used to align the text.
  • h Number
    • The height of text-box used to align the text.
  • text String
    • The text to draw.
  • [hAlign=gs.Alignment.LEFT] gs.Alignment optional
    • The horizontal alignment of the text.
  • [vAlign=gs.Alignment.TOP] gs.Alignment optional
    • The vertical alignment of the text.

fillRect

(
  • x
  • y
  • width
  • height
  • color
)

Fills a rectangle-area with the specified color.

Parameters:

  • x Number
    • The x-coordinate of the rectangle.
  • y Number
    • The x-coordinate of the rectangle.
  • width Number
    • The width of the rectangle.
  • height Number
    • The height of the rectangle.
  • color gs.Color
    • The fill color.

getImageData

(
  • x
  • y
  • width
  • height
)
ImageData

Gets the image data of the specified rectangle-area.

Parameters:

  • x Number
    • The x-coordinate of the rectangle.
  • y Number
    • The x-coordinate of the rectangle.
  • width Number
    • The width of the rectangle.
  • height Number
    • The height of the rectangle.

Returns:

ImageData:

The image data of the specified rectangle-area.

getPixel

(
  • x
  • y
)
gs.Color

Gets the color of the pixel at the specified position.

Parameters:

  • x Number
    • The x-coordinate of the pixel to get
  • y Number
    • The y-coordinate of the pixel to get

Returns:

gs.Color:

The color of the pixel.

gradientFillRect

(
  • x
  • y
  • width
  • height
  • color1
  • color2
  • vertical
)

Fills a rectangle-area with a linear-gradient.

Parameters:

  • x Number
    • The x-coordinate of the rectangle.
  • y Number
    • The x-coordinate of the rectangle.
  • width Number
    • The width of the rectangle.
  • height Number
    • The height of the rectangle.
  • color1 gs.Color
    • The first gradient color.
  • color2 gs.Color
    • The second gradient color.
  • vertical Boolean
    • If true the gradient is vertical. Otherwise horizontal.

isPixelSet

(
  • x
  • y
)
Boolean

Checks if the pixel at the specified position has an alpha-value other than 0.

Parameters:

  • x Number
    • The x-coordinate of the pixel to check
  • y Number
    • The y-coordinate of the pixel to check

Returns:

Boolean:

If true the pixel is set(alpha > 0). Otherwise false

makeImmutable

()

Makes the bitmap immutable if it was mutable before. An immutable bitmap's pixel-data cannot be changed anymore and it cannot be drawn on other bitmaps but the memory consumption is much less. This action cannot be undone. You don't need to call this for bitmap's created from image files since those are immutable by default.

makeMutable

()

Makes the bitmap mutable if it was immutable before. An mutable bitmap's pixel-data can be changed anytime and it can be drawn on other mutable bitmaps but the memory consumption is much higher.

scale

(
  • factor
)

Scales the bitmap by the specified factor.

Parameters:

  • factor Number
    • The scale factor.

setImageData

(
  • imageData
  • x
  • y
)

Puts the image data at a specified point.

Parameters:

  • imageData ImageData
    • The image data to put.
  • x Number
    • The x-coordinate of the point.
  • y Number
    • The y-coordinate of the point.

stretchBlt

(
  • destRect
  • srcBitmap
  • srcRect-
  • opacity
)

Blits/Draws the specified bitmap on this bitmap and stretches it to fit into the specified dest-rect.

Parameters:

  • destRect gs.Rect
    • The destination-rectangle for the bitmap to draw. If smaller/larger than the actual bitmap-size then it is automatically scaled.
  • srcBitmap gs.Bitmap
    • The bitmap to blit/draw.
  • srcRect- gs.Rect

    The source-rectangle. Only that area will be blitted/drawn.

  • opacity Number
    • The opacity

textSize

(
  • text
)
gs.Size

Measures the size of the specified text.

Parameters:

  • text String
    • The text to measure

Returns:

gs.Size:

size - The size of the specified text.

Properties

font

gs.Font

The font used to draw text on the bitmap.

immutable

Boolean

Indicates if the bitmap is immutable. An immutable bitmap's pixel-data cannot be anymore after creation which is the default for bitmaps created from image files. For in-memory bitmaps, it defaults to false. You can change this property before the bitmap has been loaded to create an mutable bitmap. You can also check makeImmutable/makeMutable method to make a bitmap immutable/mutable anytime after creation.

loaded

Boolean

Indicates if the bitmap is loaded.