Skip to content

Kaplay.Context

Types

drawLinesOptions

Options for drawing connected lines

type drawLinesOptions = {
pts: array<Kaplay.Vec2.t>,
pos?: Kaplay.Vec2.t,
color?: Kaplay.Types.color,
opacity?: float,
width?: float,
radius?: array<float>,
join?: Kaplay.Types.lineJoin,
cap?: Kaplay.Types.lineCap,
bias?: float,
miterLimit?: float,
}
Fields
pts array < Kaplay.Vec2.t >
pos option < Kaplay.Vec2.t >
color option < Kaplay.Types.color >
opacity option < float >
width option < float >
radius option < array >
join option < Kaplay.Types.lineJoin >
cap option < Kaplay.Types.lineCap >
bias option < float >
miterLimit option < float >

getOptions

type getOptions = {recursive?: bool, liveUpdate?: bool}
Fields
recursive option < bool >
liveUpdate option < bool >

htmlCanvasElement

type htmlCanvasElement

kaplayOptions

Initialize KAPLAY context. The starting point of all KAPLAY games.

type kaplayOptions = {
width?: int,
height?: int,
global?: bool,
background?: string,
scale?: float,
letterbox?: bool,
canvas?: htmlCanvasElement,
crisp?: bool,
pixelDensity?: float,
spriteAtlasPadding?: int,
}
Fields
width option < int >
height option < int >
global option < bool >
background option < string >
scale option < float >
letterbox option < bool >
canvas option < htmlCanvasElement >
crisp option < bool >
pixelDensity option < float >
spriteAtlasPadding option < int >

levelOptions

type levelOptions = {
tileWidth?: float,
tileHeight?: float,
tiles: Dict.t<unit => array<Kaplay.Types.comp>>,
}
Fields
tileWidth option < float >
tileHeight option < float >
tiles Dict.t < unit = >

loadSpriteAnimation

type loadSpriteAnimation = {
from?: int,
to?: int,
loop?: bool,
pingpong?: bool,
speed?: float,
frames?: array<int>,
}
Fields
from option < int >
to option < int >
loop option < bool >
pingpong option < bool >
speed option < float >
frames option < array >

loadSpriteOptions

type loadSpriteOptions = {
sliceX?: int,
sliceY?: int,
anims?: Dict.t<loadSpriteAnimation>,
anim?: string,
frames?: array<Kaplay.Types.quad>,
singular?: bool,
}
Fields
sliceX option < int >
sliceY option < int >
anims option < Dict.t >
anim option < string >
frames option < array >
singular option < bool >

playOptions

type playOptions = {seek?: float}
Fields
seek option < float >

t

type t = {
debug: Kaplay.Debug.t,
easings: Kaplay.Types.easingMap,
}
Fields

Values

add

let add: (t, array<Kaplay.Types.comp>) => 't
Parameters
t
Return type
array < Kaplay.Types.comp >

addLevel

let addLevel: (t, array<string>, levelOptions) => Level.t
Parameters
t
array < string >
Return type

center

let center: t => Kaplay.Vec2.t
Parameters
t
Return type

clamp

let clamp: (t, int, int, int) => int
Parameters
t
int
int
int
Return type
int

clampFloat

let clampFloat: (t, float, float, float) => float
Parameters
t
float
float
float
Return type
float

colorFromHex

let colorFromHex: (t, string) => Kaplay.Types.color
Parameters
t
string
Return type

colorFromRGB

let colorFromRGB: (t, int, int, int) => Kaplay.Types.color
Parameters
t
int
int
int
Return type

deg2rad

Convert degrees to radians

let deg2rad: (t, float) => float
Parameters
t
float
Return type
float

destroy

let destroy: (t, 't) => unit
Parameters
t
Return type
unit

drawLines

let drawLines: (t, drawLinesOptions) => unit
Parameters
t
Return type
unit

dt

Get the delta time in seconds since last frame.

let dt: t => float
Parameters
t
Return type
float

getCamPos

let getCamPos: t => Kaplay.Vec2.t
Parameters
t
Return type

getCursor

let getCursor: t => Kaplay.Types.cursor
Parameters
t
Return type

getGameObjects

let getGameObjects: (t, string, ~options: getOptions=?) => array<'t>
Parameters
t
string
option < getOptions >
Return type
array

getSprite

let getSprite: (t, string) => Kaplay.Asset.t<Kaplay.SpriteData.t>
Parameters
t
string

go

let go: (t, string, ~data: 'a=?) => unit
Parameters
t
string
option
Return type
unit

goWithData

go(context, sceneName, ~data=?)

Go to a scene with data passed to the scene.

let goWithData: (t, string, 'a) => unit
Parameters
t
string
Return type
unit

height

let height: t => float
Parameters
t
Return type
float

isKeyDown

let isKeyDown: (t, Kaplay.Types.key) => bool
Parameters
t
Return type
bool

kaplay

let kaplay: (~initOptions: kaplayOptions=?) => t
Parameters
option < kaplayOptions >
Return type
t

loadBean

let loadBean: (t, ~name: string=?) => unit
Parameters
t
option < string >
Return type
unit

loadMusic

Like loadSound(), but the audio is streamed and won't block loading. Use this for big audio files like background music.

let loadMusic: (t, string, string) => unit
Parameters
t
string
string
Return type
unit

loadShader

loadShader(Context.t, name, shader code)

let loadShader: (t, string, ~vert: string=?, ~frag: string=?) => unit
Parameters
t
string
option < string >
option < string >
Return type
unit

loadSound

Use for short sound effects, use loadMusic for background music.

let loadSound: (t, string, string) => unit
Parameters
t
string
string
Return type
unit

loadSprite

let loadSprite: (t, string, string, ~options: loadSpriteOptions=?) => unit
Parameters
t
string
string
option < loadSpriteOptions >
Return type
unit

loop

Run the function every n seconds.

let loop: (
t,
float,
unit => unit,
~maxLoops: int=?,
~waitFirst: bool=?,
) => Kaplay.TimerController.t
Parameters
t
float
unit
unit
option < int >
option < bool >

mathCircle

center, radius

let mathCircle: (t, Kaplay.Vec2.t, float) => Kaplay.Math.Shape.t
Parameters
t
float
Return type

mathPolygon

let mathPolygon: (t, array<Kaplay.Vec2.t>) => Kaplay.Math.Shape.t
Parameters
t
array < Kaplay.Vec2.t >
Return type

mathRect

let mathRect: (t, Kaplay.Vec2.t, float, float) => Kaplay.Math.Shape.t
Parameters
t
float
float
Return type

mousePos

Get current mouse position (without camera transform).

let mousePos: t => Kaplay.Vec2.t
Parameters
t
Return type

on

on(context, event, tag, (gameObject, arg) => unit)

Register an event on all game objs with certain tag.

let on: (
t,
~event: string,
~tag: string,
('t, 'arg) => unit,
) => unit
Parameters
t
string
string
unit
Return type
unit

onClick

let onClick: (t, unit => unit) => unit
Parameters
t
unit
unit
Return type
unit

onClickWithController

let onClickWithController: (t, unit => unit) => Kaplay.KEventController.t
Parameters
t
unit
unit

onClickWithTag

let onClickWithTag: (t, string, 't => unit) => Kaplay.KEventController.t
Parameters
t
string
unit

onKeyDown

Holding the key down

let onKeyDown: (t, Kaplay.Types.key => unit) => unit
Parameters
t
unit
Return type
unit

onKeyDownWithController

Holding the key down

let onKeyDownWithController: (t, Kaplay.Types.key => unit) => Kaplay.KEventController.t
Parameters
t
unit

onKeyPress

Hitting the key

let onKeyPress: (t, Kaplay.Types.key => unit) => unit
Parameters
t
unit
Return type
unit

onKeyPressWithController

Hitting the key

let onKeyPressWithController: (t, Kaplay.Types.key => unit) => Kaplay.KEventController.t
Parameters
t
unit

onKeyRelease

Lifting the key up

let onKeyRelease: (t, Kaplay.Types.key => unit) => unit
Parameters
t
unit
Return type
unit

onKeyReleaseWithController

Lifting the key up

let onKeyReleaseWithController: (t, Kaplay.Types.key => unit) => Kaplay.KEventController.t
Parameters
t
unit

onLoad

Register an event that runs when all assets finished loading.

let onLoad: (t, unit => unit) => unit
Parameters
t
unit
unit
Return type
unit

onMouseMove

onMouseMove(context, (pos, delta) => unit)

Register an event that runs whenever user moves the mouse.

let onMouseMove: (t, (Kaplay.Vec2.t, Kaplay.Vec2.t) => unit) => unit
Parameters
t
unit
Return type
unit

onMouseMoveWithController

onMouseMove(context, (pos, delta) => KEventController.t)

Register an event that runs whenever user moves the mouse.

let onMouseMoveWithController: (
t,
(Kaplay.Vec2.t, Kaplay.Vec2.t) => unit,
) => Kaplay.KEventController.t

onMousePress

Register an event that runs whenever user presses a mouse button.

let onMousePress: (t, Kaplay.Types.mouseButton => unit) => unit
Parameters
t
unit
Return type
unit

onMousePressWithController

Register an event that runs whenever user presses a mouse button.

let onMousePressWithController: (
t,
Kaplay.Types.mouseButton => unit,
) => Kaplay.KEventController.t

onMouseRelease

Register an event that runs whenever user releases a mouse button.

let onMouseRelease: (t, Kaplay.Types.mouseButton => unit) => unit
Parameters
t
unit
Return type
unit

onMouseReleaseWithController

Register an event that runs whenever user releases a mouse button.

let onMouseReleaseWithController: (
t,
Kaplay.Types.mouseButton => unit,
) => Kaplay.KEventController.t

onTouchEnd

let onTouchEnd: (t, (Kaplay.Vec2.t, Kaplay.Types.touch) => unit) => unit
Return type
unit

onTouchEndWithController

let onTouchEndWithController: (
t,
(Kaplay.Vec2.t, Kaplay.Types.touch) => unit,
) => Kaplay.KEventController.t

onTouchMove

let onTouchMove: (t, (Kaplay.Vec2.t, Kaplay.Types.touch) => unit) => unit
Return type
unit

onTouchMoveWithController

let onTouchMoveWithController: (
t,
(Kaplay.Vec2.t, Kaplay.Types.touch) => unit,
) => Kaplay.KEventController.t

onTouchStart

let onTouchStart: (t, (Kaplay.Vec2.t, Kaplay.Types.touch) => unit) => unit
Return type
unit

onTouchStartWithController

let onTouchStartWithController: (
t,
(Kaplay.Vec2.t, Kaplay.Types.touch) => unit,
) => Kaplay.KEventController.t

onUpdate

let onUpdate: (t, unit => unit) => unit
Parameters
t
unit
unit
Return type
unit

onUpdateWithController

let onUpdateWithController: (t, unit => unit) => Kaplay.KEventController.t
Parameters
t
unit
unit

onWithController

on(context, event, tag, (gameObject, arg) => unit)

Register an event on all game objs with certain tag.

let onWithController: (
t,
~event: string,
~tag: string,
('t, 'arg) => unit,
) => Kaplay.KEventController.t
Parameters
t
string
string
unit

play

let play: (t, string, ~options: playOptions=?) => Kaplay.AudioPlay.t
Parameters
t
string
option < playOptions >
Return type

quad

let quad: (t, float, float, float, float) => Kaplay.Types.quad
Parameters
t
float
float
float
float
Return type

rad2deg

Convert radians to degrees

let rad2deg: (t, float) => float
Parameters
t
float
Return type
float

randf

let randf: (t, float, float) => float
Parameters
t
float
float
Return type
float

randi

let randi: (t, int, int) => int
Parameters
t
int
int
Return type
int

scene

let scene: (t, string, 'a => unit) => unit
Parameters
t
string
unit
Return type
unit

setBackground

let setBackground: (t, Kaplay.Types.color) => unit
Parameters
t
Return type
unit

setCamPos

let setCamPos: (t, Kaplay.Vec2.t) => unit
Parameters
t
Return type
unit

setCursor

let setCursor: (t, Kaplay.Types.cursor) => unit
Return type
unit

setFullscreen

let setFullscreen: (t, bool) => unit
Parameters
t
bool
Return type
unit

setGravity

let setGravity: (t, float) => unit
Parameters
t
float
Return type
unit

tagComponent

let tag: string => Kaplay.Types.comp
Parameters
string
Return type

time

Get the total time in seconds since beginning.

let time: t => float
Parameters
t
Return type
float

toScreen

Transform a point from world position (relative to the root) to screen position (relative to the screen).

let toScreen: (t, Kaplay.Vec2.t) => Kaplay.Vec2.t
Parameters
t
Return type

toWorld

Transform a point from screen position (relative to the screen) to world position (relative to the root).

let mousePos = k->Context.mousePos
let worldPos = k->Context.toWorld(mousePos)
let toWorld: (t, Kaplay.Vec2.t) => Kaplay.Vec2.t
Parameters
t
Return type

tween

tween(context, from, to, duration in seconds, setValue, easeFunc=?) => unit

Useful to change a property of a Game Object over time.

k
->Context.tween(
  ~from=-15.,
  ~to_=0.,
  ~duration=0.5,
  ~setValue=Bird.setAngle(bird, ...),
)
let tween: (
t,
~from: 'v,
~to_: 'v,
~duration: float,
~setValue: 'v => unit,
~easeFunc: Kaplay.Types.easeFunc=?,
) => unit
Parameters
t
float
unit
Return type
unit

tweenWithController

tween(context, from, to, duration in seconds, setValue, easeFunc=?) => TweenController.t

Useful to change a property of a Game Object over time.

k
->Context.tween(
  ~from=-15.,
  ~to_=0.,
  ~duration=0.5,
  ~setValue=Bird.setAngle(bird, ...),
)
let tweenWithController: (
t,
~from: 'v,
~to_: 'v,
~duration: float,
~setValue: 'v => unit,
~easeFunc: Kaplay.Types.easeFunc=?,
) => Kaplay.TweenController.t
Parameters
t
float
unit

vec2

let vec2: (t, float, float) => Kaplay.Vec2.t
Parameters
t
float
float
Return type

vec2Down

let vec2Down: t => Kaplay.Vec2.t
Parameters
t
Return type

vec2FromXY

let vec2FromXY: (t, float) => Kaplay.Vec2.t
Parameters
t
float
Return type

vec2Left

let vec2Left: t => Kaplay.Vec2.t
Parameters
t
Return type

vec2One

let vec2One: t => Kaplay.Vec2.t
Parameters
t
Return type

vec2Right

let vec2Right: t => Kaplay.Vec2.t
Parameters
t
Return type

vec2Up

let vec2Up: t => Kaplay.Vec2.t
Parameters
t
Return type

vec2Zero

let vec2Zero: t => Kaplay.Vec2.t
Parameters
t
Return type

wait

wait(context, seconds, callback)

Run the function after n seconds.

let wait: (t, float, unit => unit) => unit
Parameters
t
float
unit
unit
Return type
unit

waitWithController

wait(context, seconds, callback)

Run the function after n seconds.

let waitWithController: (t, float, unit => unit) => Kaplay.TimerController.t
Parameters
t
float
unit
unit

width

let width: t => float
Parameters
t
Return type
float

Modules

Level

Types

t
type t

Values

spawn
let spawn: (t, array<Kaplay.Types.comp>, Kaplay.Vec2.t) => 't
Parameters
t
array < Kaplay.Types.comp >
Return type