The HTML5 Canvas is a powerful tool for embedding performant, interactive graphics in web apps. It has broad browser support and built-in APIs for drawing shapes, images and text. There are also a number of helper libraries built on top of Canvas like Konva that help with event handling and animations.
However, the Canvas API is entirely imperative. It uses methods like
ctx.fillRect(10, 10, 100, 100) and
ctx.fillStyle = ‘green’ which, to a React developer spoiled by the magic of reactive DOM, feel rather old fashioned. It’s easy to see complexity growing quickly when building complicated illustrations.
react-konva is a library for creating Canvas illustrations using React. It is built as a React wrapper over the Konva Canvas library, letting you interact with the Konva API through React component interfaces.
Let’s jump into the code to get a feel for how it works.
Don’t worry too much about Stage and Layer here, they’re the basic building blocks of a Konva layout. The important part is the
<Rect /> component. With
react-konva shapes are represented as components. You use props to describe the color, size, and other attributes of each shape. When props change,
react-konva updates the underlying canvas graphics. It’s just like working with DOM nodes, except elements are rendered to a Canvas!
Event Handling / Interactions
react-konva supports an event handling API similar to that in the DOM. You can attach handlers to shapes for clicks, drags, and other interaction events. It has built-in components for a host of figures like Circle, Ellipse, Wedge, Line, Arrow, RegularPolygon, and many more. You can also use pre-built components for rendering images and text to the canvas.
One of the reasons to use Canvas is that you can draw thousands of objects without incurring a memory penalty, since when you “draw” something, it doesn’t create an actual object in memory, but simply changes pixels on the canvas. This is a limitation of
react-konva, since the library creates React components to manage the lifecycle of each shape. If you draw hundreds of shapes on a canvas, there is a similar memory overhead to creating many DOM nodes in React. As such,
react-konva is not well suited to applications where you need to render a very large number of elements to a canvas.
If you need to do this, you can use the Konva API directly (without the React wrapper) like this:
Here, I built a simple demo in JSBin so you can get a feel for using
react-konva. Try changing the parameters of the <Star /> or add other shapes/interactions to the illustration.
To create a rectangle with Konva, we can instantiate a Konva.Rect() object. For a full list of attributes and methods, check out the Konva.Rect documentation. You can define corner radius for Konva.Rect. It can be simple number or array of numbers [topLeft, topRight, bottomRight, bottomLeft].
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens on your React app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app's performance, reporting with metrics like client CPU load, client memory usage, and more.
The LogRocket Redux middleware package adds an extra layer of visibility into your user sessions. LogRocket logs all actions and state from your Redux stores.
Modernize how you debug your React apps — start monitoring for free.