#atom

Dynamic Graphics Creation with Time-Based Controls

Core Idea: Konva provides two complementary animation approaches: frame-based animations through Konva.Animation for continuous updates and declarative tweens through Konva.Tween for property transitions, both optimized for canvas performance.

Key Elements

Implementation Examples

Frame-based Animation

// Create a frame-based animation
var anim = new Konva.Animation(function(frame) {
  // frame.time = total time elapsed in milliseconds
  // frame.timeDiff = time since last animation frame
  // frame.frameRate = current frame rate
  
  // Example: rotate a shape based on time
  var angleDiff = (frame.timeDiff * 90) / 1000; // 90 degrees per second
  circle.rotate(angleDiff);
}, layer);

// Start animation
anim.start();

// Stop animation
// anim.stop();

Property Tween

// Create a property tween
var tween = new Konva.Tween({
  node: rect,
  duration: 1, // seconds
  x: 300,
  y: 200,
  rotation: Math.PI * 2,
  opacity: 0.5,
  strokeWidth: 6,
  easing: Konva.Easings.EaseInOut
});

// Start the tween
tween.play();

// Alternative shorthand syntax
circle.to({
  duration: 0.5,
  fill: 'green',
  scale: {
    x: 1.5,
    y: 1.5
  },
  onFinish: function() {
    console.log('Animation complete');
  }
});

Connections

References

  1. Konva.js animation documentation: https://konvajs.org/docs/animations/Create_an_Animation.html
  2. Tween documentation: https://konvajs.org/docs/tweens/Linear_Easing.html

#animation #canvas #graphics #transitions #motion


Connections:


Sources: