Skip to content
On this page

Sprite Shape

TIP

This class is a descendant of Shape it also inherits all the options that Shape provides.

TIP

This component also supports a return function

To create a sprite with fcanvas, we can instantiate a Sprite object.

In addition, this shape also provides a few other parameters:

Require Options

NameTypeDescription
imageMayBeRef<CanvasImageSource | string>This parameter accepts the same data type as image of Image
animationsMayBeRef<Record<string, AnimationFrames | number[]>>animation map
animationMayBeRef<keyof animations>animation key

Inherit Shape

NameTypeDescription
xMayBeRef<number>offset x
yMayBeRef<number>offset y
ts
interface AnimationFrames {
  frames: number[]
  frameIndex?: number
  frameRate?: number
}

Optional Options

NameTypeDefaultDescription
widthMayBeRef<number>autowidth that the image displays. this will be disabled if height does not exist
heightMayBeRef<number>auto height that the image displays. this will be disabled if width does not exist
frameIndexMayBeRef<number>0Animation frame index
frameRateMayBeRef<boolean>17Animation frame rate
infiniteMayBeRef<boolean>trueAnimation will repeat right after the end
fixedSizeMayBeRef<boolean>trueset a fixed size for this element. if =false it will continuously resize to fit the frame. this reduces the performance of removable memory

Demo

ts
import { Stage, Layer, Sprite, watch, loadImage } from "fcanvas"

const stage = new Stage({ height: 300 }).mount("#app")
const layer = new Layer().addTo(stage)

const animations = {
  idle: [
    // x, y, width, height (4 frames)
    2, 2, 70, 119, 71, 2, 74, 119, 146, 2, 81, 119, 226, 2, 76, 119
  ],
  punch: [
    // x, y, width, height (4 frames)
    2, 138, 74, 122, 76, 138, 84, 122, 346, 138, 120, 122
  ]
}

const blob = new Sprite({
  x: 0,
  y: 0,
  image: await loadImage("https://konvajs.org/assets/blob-sprite.png"),
  animation: "idle",
  animations,
  frameRate: 7,
  frameIndex: 0
})
// add the shape to the layer
layer.add(blob)
// add the layer to the stage
stage.add(layer)
// start sprite animation
blob.start()

window.addEventListener("click", () => {
  blob.animation = "punch"
  const watcher = watch(blob.currentFrameIndex, (index) => {
    if (index === 2) {
      setTimeout(() => {
        watcher()
        blob.$.animation = "idle"
      }, 1000 / 7)
    }
  })
})

Released under the MIT License.