Skip to main content

HTML5 Canvas Batch Draw Tip

Update: this demo is not relevant with the new konva@8. In the new version, Konva is doing all batching draws automatically on any changes on canvas.

The demo may be still relevant if you use Konva.autoDrawEnabled = false.

In some situations, we may want to update a Konva shape as fast as possible, but without causing too many redraws. For example, if we want to update an element on the stage via mousemove, we don't want to redraw the layer with the draw() method, because the mousemove event could be fired hundreds of times per second, which would result in a forced frame rate of over a hundred frames per second. Often times this can cause jumpy animations because browsers simply can't handle excessive redraws.

For situations like this, it's much better to use the batchDraw() method which automatically hooks redraws into the Konva animation engine. No matter how many times you call batchDraw(), Konva will automatically limit the number of redraws per second based on the maximum number of frames per second that the browser can handle at any given point in time.

Instructions: Move your mouse over the stage to spin the rectangle

import Konva from 'konva';

// For demo purposes, disable auto draw
Konva.autoDrawEnabled = false;

const stage = new Konva.Stage({
  container: 'container',
  width: window.innerWidth,
  height: window.innerHeight,
});

const layer = new Konva.Layer();
stage.add(layer);

const rect = new Konva.Rect({
  x: stage.width() / 2 - 50,
  y: stage.height() / 2 - 25,
  width: 100,
  height: 50,
  fill: 'green',
  stroke: 'black',
  strokeWidth: 4,
});

layer.add(rect);
layer.draw();

stage.on('mousemove', () => {
  // rotate rectangle on mouse move
  rect.rotate(5);
  // use batchDraw instead of draw for better performance
  layer.batchDraw();
});