CDN: https://unpkg.com/@litecanvas/utils/dist/actor.js
import litecanvas from "@litecanvas"
import { Actor } from "@litecanvas/utils"
let player, mySprite
litecanvas({
loop: { init, tapped, draw },
})
// lets create a image to our actor
mySprite = paint(3, 3, ["303", "030", "303"], { scale: 10 })
function init() {
// create a actor and pass its image
player = new Actor(mySprite)
}
function tapped(tapx, tapy) {
// update the actor position
player.x = tapx
player.y = tapy
}
function draw() {
cls(0)
// draw the actor sprite
player.draw()
}Set or get the actor position vector
player.pos.x = 100
player.pos.y = 200Note: The
player.posis a Vector instance.
Set or get the actor position X or Y
player.x = 100
player.y = 200Set or get the actor scale vector
// twice bigger
player.scale.x = 2
player.scale.y = 2// multiplies the scale (y is optional)
player.scaleBy(3)
// same as
player.scale.x *= 3
player.scale.y *= 3// sets the scale (y is optional)
player.scaleTo(3)
// same as
player.scale.x = 3
player.scale.y = 3If true the actor sprite is flipped horizontally. Default is false.
If true the actor sprite is flipped vertically. Default is false.
Set or get the actor anchor (origin). By default, the anchor is a vector (0, 0) (meaning anchor Top Left).
// example: actor position based on their center
player.anchor.x = 0.5
player.anchor.y = 0.5
// or...
import { ANCHOR_CENTER } from "@litecanvas/utils"
player.anchor = ANCHOR_CENTERNote: You can import and use the following constants to help you choose an actor anchor: ,
ANCHOR_TOP_LEFT(default),ANCHOR_TOP_RIGHT,ANCHOR_BOT_LEFT,ANCHOR_BOT_RIGHTorANCHOR_CENTER.
Set or get the actor angle (in degrees).
player.angle = 45Set or get the actor opacity (alpha).
player.opacity = 0.5 // 50% transparentActor#hidden
Set or get the actor hidden (boolean) state.
player.hidden = true // hides the actor image
player.hidden = false // display the actor imageGet (not set) the actor current width.
console.log(player.width) // => 30
player.scale.x = 1.5
console.log(player.width) // => 45Get (not set) the actor current height.
console.log(player.height) // => 30
player.scale.y = 2
console.log(player.height) // => 60Set or get the actor sprite image. Useful to make animations.
player.sprite = anotherSpriteThe actor sprite must be a
Image,ImageBitmap,HTMLCanvasElementorOffscreenCanvas.Remember, you can create images using the litecanvas' built-in
paint()function or loading a image.
Check taps (clicks and touchs).
function tapped(x, y) {
const pointArea = 8 // default = 1
if (player.in(x, y, pointArea)) {
console.log("tap!")
}
}Check collision between actors
const collided = player.col(anotherActor)