Skip to Content
defold-typescript libraries
britzl

britzl/defold-screenshot/screenshot

Functions for capturing full-screen or partial screenshots.

  • GitHub: britzl/defold-screenshot — pinned to 1.13.0 (released while Defold 1.12.3 was current)
    1. Pick a release from britzl/defold-screenshot releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as screenshot from "screenshot"
  • Asset Portal: defold-screenshot

Functions

screenshot.png(x?: number, y?: number, w?: number, h?: number, callback?: function): LuaMultiReturn<[string, number, number]>

Capture a screenshot as PNG-encoded bytes. On native platforms this is synchronous unless a callback is provided. On HTML5, only the asynchronous callback form is supported.

-- fullscreen with callback
screenshot.png(function(self, image, w, h)
    -- do something with image
end)

-- partial with callback
screenshot.png(0, 0, 100, 200, function(self, image, w, h)
    -- do something with image
end)

-- fullscreen (native only)
local image, w, h = screenshot.png()

Parameters

  • x?: number — X coordinate (lower-left) for partial capture.
  • y?: number — Y coordinate (lower-left) for partial capture.
  • w?: number — Width for partial capture.
  • h?: number — Height for partial capture.
  • callback?: function — Called on post-render (or next frame on HTML5) with the PNG bytes.
    • self: object — The calling script instance.
    • image: string — PNG data as a Lua string.
    • width: number — Image width in pixels.
    • height: number — Image height in pixels.

Returns

  • image: string — PNG data as a Lua string (native only, synchronous form).
  • width: number
  • height: number

screenshot.buffer(x?: number, y?: number, w?: number, h?: number, callback?: function): LuaMultiReturn<[buffer, number, number]>

Capture a screenshot as a Defold buffer. On native platforms this is synchronous unless a callback is provided. On HTML5, only the asynchronous callback form is supported.

local buffer, w, h = screenshot.buffer()
resource.set_texture(go.get("cube#model", "texture0"), {
    type = resource.TEXTURE_TYPE_2D,
    width = w,
    height = h,
    format = resource.TEXTURE_FORMAT_RGBA
}, buffer)

Parameters

  • x?: number — X coordinate (lower-left) for partial capture.
  • y?: number — Y coordinate (lower-left) for partial capture.
  • w?: number — Width for partial capture.
  • h?: number — Height for partial capture.
  • callback?: function — Called on post-render (or next frame on HTML5) with the buffer.
    • self: object — The calling script instance.
    • buffer: buffer — Defold buffer containing RGBA pixels.
    • width: number — Image width in pixels.
    • height: number — Image height in pixels.

Returns

  • buffer: buffer — Defold buffer containing RGBA pixels (native only, synchronous form).
  • width: number
  • height: number

screenshot.pixels(x?: number, y?: number, w?: number, h?: number, callback?: function): LuaMultiReturn<[string, number, number]>

Capture a screenshot as raw RGBA pixels in a Lua string. On native platforms this is synchronous unless a callback is provided. On HTML5, only the asynchronous callback form is supported.

local pixels, w, h = screenshot.pixels()

Parameters

  • x?: number — X coordinate (lower-left) for partial capture.
  • y?: number — Y coordinate (lower-left) for partial capture.
  • w?: number — Width for partial capture.
  • h?: number — Height for partial capture.
  • callback?: function — Called on post-render (or next frame on HTML5) with the pixel data.
    • self: object — The calling script instance.
    • pixels: string — Raw RGBA bytes as a Lua string.
    • width: number — Image width in pixels.
    • height: number — Image height in pixels.

Returns

  • pixels: string — Raw RGBA bytes as a Lua string (native only, synchronous form).
  • width: number
  • height: number

screenshot.html5(callback: function)

HTML5-only alias for screenshot.png(). Callback form only.

Parameters

  • callback: function — Called on the next frame with PNG bytes.
    • self: object — The calling script instance.
    • image: string — PNG data as a Lua string.
    • width: number
    • height: number

On this page