Skip to main content

Load Simple HTML5 Canvas Stage from JSON Tutorial

To load a simple stage from JSON with Konva, we can use the Konva.Node.create() method. The create() method accepts a JSON string and container id as arguments.

import Konva from 'konva';

// JSON string from a previous save
const json = '{"attrs":{"width":400,"height":400},"className":"Stage","children":[{"attrs":{},"className":"Layer","children":[{"attrs":{"x":100,"y":100,"radius":50,"fill":"red","stroke":"black","strokeWidth":3},"className":"Circle"}]}]}';

// create node using json string
const stage = Konva.Node.create(json, 'container');

// you can keep adding events, etc
const circle = stage.findOne('Circle');
circle.on('click', () => {
  circle.fill(Konva.Util.getRandomColor());
  stage.find('Layer').first().draw();
});