React Konva

19.2.3 · active · verified Sun Apr 19

React Konva provides declarative and reactive bindings for integrating the Konva 2D canvas framework into React applications. It allows developers to use Konva's rich set of shapes, layers, and stage components as standard React components, enabling a familiar declarative JSX syntax for canvas drawing. The library is currently at version 19.2.3, frequently releasing patch and minor updates to ensure compatibility with the latest React and Konva versions. Its primary differentiator is leveraging React's component model and data flow for high-performance canvas rendering, abstracting away direct DOM manipulation. It is built on top of the Konva framework, meaning users primarily need to understand Konva's API and concepts to effectively use React Konva.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a basic Konva stage with a layer, a text element, and a clickable colored rectangle using React state and event handlers.

import React, { useState } from 'react';
import { render } from 'react-dom';
import { Stage, Layer, Rect, Text } from 'react-konva';
import Konva from 'konva';

const ColoredRect = () => {
  const [color, setColor] = useState('green');

  const handleClick = () => {
    setColor(Konva.Util.getRandomColor());
  };

  return <Rect x={20} y={20} width={50} height={50} fill={color} shadowBlur={5} onClick={handleClick} />;
};

const App = () => {
  return (
    <Stage width={window.innerWidth} height={window.innerHeight}>
      <Layer>
        <Text text="Try click on rect" />
        <ColoredRect />
      </Layer>
    </Stage>
  );
};

render(<App />, document.getElementById('root'));

view raw JSON →