Skill: Read the mouse, touch, and pen
Read mouse, touch, and pen input through BLIT386's unified pointer API with up to four slots. Use for clicking, dragging, painting, aiming, scrolling, or...
Written for blit386 1.8.0. The rest of blit386.dev documents the newest engine, so an API you find there may be missing from a game pinned to 1.8.0.
Mouse, touch, and pen share one "pointer" API with up to four slots. Slot 0 is the mouse or first touch.
When to use
Use for clicking, dragging, painting, aiming, scrolling, or touch controls.
How to do it
configure() {
return {
// Needed so BT.pointerScrollDelta works and the page does not scroll over the canvas.
isCapturingPointerScroll: true,
};
}
update() {
if (BT.isPointerActive(0)) {
const p = BT.pointerPos(0); // a Vector2i in screen pixels
const move = BT.pointerDelta(0); // how far it moved this frame
if (BT.isDown(BT.BTN_POINTER_A, 0)) this.paintAt(p); // primary button held
if (BT.isPressed(BT.BTN_POINTER_A, 0)) this.startDrag(p);
if (BT.isReleased(BT.BTN_POINTER_A, 0)) this.endDrag(p, move); // move as flick velocity
}
const wheel = BT.pointerScrollDelta; // a getter; vertical wheel movement this frame (pixels)
this.zoom += wheel;
}Key calls
BT.pointerPos(slot?)/BT.pointerDelta(slot?)- methods, returnVector2i.BT.pointerPosTo(out, slot?)/BT.pointerDeltaTo(out, slot?)- methods, engine 1.7.0+; write into an existingVector2iand return it, instead of making a new one. The destination comes first. Reach for these only inside a hot loop - see thekeep-it-fastskill.BT.isPointerActive(slot?)- method; is a pointer present in that slot.BT.nativeScreenToDisplayPos(clientX, clientY)- method, engine 1.8.0+; converts a DOM event'sclientX/clientYto display pixels, ornullbefore init or for a zero-size canvas. Only for DOM events the engine does not wrap, such as a file dropped on the canvas - for the mouse and touch,pointerPosalready is in display pixels.BT.pointerScrollDelta- getter; vertical wheel delta this frame (requiresisCapturingPointerScroll: true).- Buttons:
BT.BTN_POINTER_A(primary),BTN_POINTER_B/C/D,BTN_POINTER_ANY, used withBT.isDown/isPressed/isReleased. BT.hideCursor()/BT.showCursor()- methods; hide or restore the OS cursor over the canvas.
Notes
- Slots: 0 = mouse or first touch; 1-3 = extra touches or pens (multi-touch).
- Read in
update(). Positions are already in your screen's pixel coordinates. A click that lands on a fast display between twoupdate()steps waits for the next one (engine 1.8.0+), soBT.isPressed(BT.BTN_POINTER_A)inupdate()never misses it - there is nothing to work around. pointerDeltamakes a good drag velocity - release while moving to "flick".- Wheel capture is opt-in: set
isCapturingPointerScroll: trueinconfigure()when you map the mouse wheel. Without it the host page scrolls normally andBT.pointerScrollDeltastays0. - The same flag gates touch scrolling past the canvas: off means
touch-action: pan-y(phones can tap-hold-scroll the page); on meanstouch-action: noneso the game owns the gesture.
See docs/input.md.
Skill: Read the keyboard
Read keyboard keys and face buttons in update(). Use for keyboard movement, jumping, menus, typed text, or remapping keys, "press any key" screens...
Skill: Run the game
Start the BLIT386 dev server and open the game in a browser. Use this whenever the user wants to run, start, launch, or preview the game, see what it looks...