Skip to content

Using the Afterimage Directive ​

The Afterimage directive leaves fading, tinted copies of a sprite behind it while it moves. It gives a "time dilation" look to a dash, a haste buff or a knockback.

Basic Usage ​

html
<script>
  import { signal } from 'canvasengine';

  const dashing = signal(false);
</script>

<Sprite image="hero.png" x y afterimage={{ enabled: dashing }} />

Set dashing to true on a key press (for example in a keyDown handler of controls) and back to false on release. While it is true, a copy of the sprite's current frame is left every interval ms, so animated sprites ghost with their animation frames.

Options ​

OptionTypeDefaultDescription
enabledboolean (or signal)trueLeaves ghosts while true
intervalnumber45Time between two ghosts, in ms
durationnumber320Time for a ghost to fade out, in ms
alphanumber0.5Starting opacity of a ghost
tintnumber0x7fd0ffTint of the ghosts
blendModestring'add'Blend mode of the ghosts
minDistancenumber1Distance in px the sprite must have moved since the last ghost. 0 also ghosts a still sprite

Ghosts are placed right under the sprite in its parent, and removed when the directive is destroyed. It works on objects that have a texture (Sprite, animated sprites), not on a Container of several parts.