Chapter 2 - Create a drawing board with Konva.js¶
In this chapter, you will install Konva.js as the drawing library and make your project a board for drawing.
Info
All the following steps will be executed in the frontend directory.
In a new terminal, you can switch to the frontend directory with the following command.
| In a terminal, execute the following command(s). | |
|---|---|
Steps¶
Install Konva.js¶
Install Konva.js with the following command.
| In a terminal, execute the following command(s). | |
|---|---|
Create the Sketch component¶
To create a canvas and draw in your web application, you could simply implement it in the index.tsx file. But the complexity of this drawing application will soon begin to grow bigger and bigger. So it's better to separate some parts of your code. And how convenient! React proposes something. Something called Components.
As Next.js documentation explains:
Quote
The user interface can be broken down into smaller building blocks called Components.
Components allow you to build self-contained, reusable snippets of code. If you think of components as LEGO bricks, you can take these individual bricks and combine them together to form larger structures. If you need to update a piece of the UI, you can update the specific component or brick.
This modularity allows your code to be more maintainable as it grows because you can easily add, update, and delete components without touching the rest of our application.
Before implementing the Sketch Component, we need to think the data structure of a drawing. What is a drawing? It's an array of array of points. We could represent it like that:
But for Konva.js a line is not represented with x and y, it's just an array of number, and the one in odd indexes are x and the one in even indexes are y. So the data will be presented like that.
The Sketch component implements a State in which the data of line will be stored. Konva needs the data to be in a State to be responsive and react when a user move the mouse over the canvas.
As this project uses TypeScript, so you can define this kind of data with types to ensure code correctness:
- The
LineDatatype will be an array of number (number[]) - The
LinesDatatype will be an array ofLineData(LineData[]) - The
Statetype is an alias for theLinesDatatype for terminology purposes
Now, implement the Sketch component.
- Initialize the
Statewith an empty array[]and return a getter (to read the state) and a setter (to write in the state). - Create a reference that will persist for the full lifetime of the component to know if we're currently drawing or not. It would also be possible to use a
useStatefor this, but it would be less performant because it would trigger a re-render every time the value changes. - Function that convert a mouse event into a point.
- Add a new line with the coordinates of the first point in the
State. - Add the coordinates of a new point in the last line of the
State. - When the user press the mouse down, it starts drawing a new line.
- When the user moves the mouse, and if it's already drawing, it continues to draw a line.
- This is the component where the drawing are displayed.
- Iterate over the
linesarray and get each line individually. - This is the component that draws lines.
Konva.js can only be run on a browser. As Next.js can process data on the server side (without a browser) with SSR (Server Side Rendering), you need to import the Sketch component dynamically explicitly disabling SSR.
Update the main page to use the Sketch component without SSR.
| frontend/src/pages/index.tsx | |
|---|---|
- SSR is disabled for this component, it will only be available on the browser.
Check the results¶
Your working directory should looks like this.
Start the application and access http://localhost:3000. You should see a blank page.
If you draw on the board with your mouse and left click, you should be able to draw red lines to your wish!
Summary¶
Congrats! You have a Next.js + Konva.js application you can draw on it!
Go further¶
Are you able to change the color and the size of the line? Expand the next component to see the answer!
Show me the answer!
Update the Sketch to set the color and the size of the lines. The color can be any valid Web colors.