Adds a virtual joystick to litecanvas games.
NPM: npm i @litecanvas/plugin-joystick
CDN: https://unpkg.com/@litecanvas/plugin-joystick/dist/dist.js
import litecanvas from "litecanvas"
import pluginJoystick from "@litecanvas/plugin-joystick"
litecanvas({
loop: { init, update, draw },
})
use(pluginJoystick) // load the plugin
actor = {
x: CENTERX,
y: CENTERY,
speed: 100,
}
function update(dt) {
if (JOYSTICK.active) {
const force = min(JOYSTICK.force, 2)
actor.x += actor.speed * force * cos(JOYSTICK.angle) * dt
actor.y += actor.speed * force * sin(JOYSTICK.angle) * dt
}
}
function draw() {
cls(1)
circfill(actor.x, actor.y, 32, 6)
}
See this demo in litecanvas playground
For more details, check the demo.