Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. JavaScript

Manipulating HTML5 Canvas Using Konva: Part 3, Complex Shapes and Sprites

Read Time:11 minsLanguages:
This post is part of a series called Manipulating HTML5 Canvas Using Konva.
Manipulating HTML5 Canvas Using Konva: Part 2, Basic Shapes
Manipulating HTML5 Canvas Using Konva: Part 4, Styling

In the second tutorial of this series, you learned how to draw basic shapes using Konva. You can combine the basic shapes one way or another to create stars, rings, arrows, clocks, or a hut. Konva also allows you to create some common complex shapes by using built-in functions provided by the library. 

In this tutorial, you will learn how to create stars and rings in Konva. After that, we will discuss how to write text using Konva and how to write it along a specific path. You will also learn how to draw images and sprites on a canvas using Konva.

Drawing Stars and Rings

Compared to a lot of other complex shapes that you can draw on the canvas, a star shape is one of the most common. With the right set of values, you can also turn pointy stars into simple badge-like shapes. Konva allows you to draw stars using the Konva.Star() object.

You can specify the number of spikes that a star should have using the numPoints property. You can control the size of those spikes using the innerRadius and outerRadius properties. A large difference in the value of these properties will make the stars more spiky. Setting the outer radius equal to the inner radius will create a regular polygon with the number of sides double the value of numPoints. Varying the values of numPoints, innerRadius, and outerRadius can result in some interesting shapes.

As usual, you can rotate and scale the star shapes that you create using the rotation, scale, scaleX, and scaleY properties. Similarly, you can control the opacity of a star using the opacity property and show or hide it with the help of the visible property.

Rings in Konva consist of a bigger solid circle and a smaller hollow circle laid over it. The radius of the inner circle is specified using the innerRadius property, and the radius of the outer circle is specified using the outerRadius property. The x and y properties control the position of the center of the star.

You can scale and rotate the rings that you create using the scaleX, scaleY, scale, and rotation properties. However, keep in mind that rotation won't seem to have any effect unless you have scaled the ring with different magnitudes in the x and y direction.

In the following example, I have overlaid multiple star and ring shapes together to create a nice pattern. The three stars and two rings in the pattern are concentric. Keep in mind that whenever you are trying to create something like this, always draw the biggest shape first. If I had added starA later to the layer, it would have covered all the smaller rings and stars, hiding them from the viewers.

Text and TextPath in Konva

You can write text on a canvas using the Konva.Text() object. When creating a text object, you can specify the value for font family, font size, font style, and font variant using the fontFamily, fontSize, fontStyle, and fontVariant properties. The default font family is Arial, and the default font size is 12. You can set the fontStyle property to normal, bold, or italic. Similarly, fontVariant can be set to either normal or smallcaps. 

The actual text that you want to write can be specified using the text property. If there is not enough space between lines in a multi-line comment, you can increase it using the lineHeight property.

The top-left point from which Konva should start writing the text is specified using the x and y properties. You can limit the width of the text using the width property. Any text that you write will be left aligned by default. You can align it to right or center using the align property.

In the above example, you should note that I have added textB to the layer after adding all other elements. This way we can make sure that the actual quote stays on top of all other shapes.

The text that you write on the canvas doesn't have to follow straight lines. You can also provide a path for the text to follow using the data property. The path is provided in the form of an SVG data string and can include commands to follow lines, curves, and arcs. 

One important thing you should remember is that the text which you want to write along a specific path has to be created using the Konva.TextPath() object. Here is an example that provides the path for the text to follow in the form of a Cubic Bezier curve.

Drawing Images and Sprites Using Konva

You should now be able to create a variety of shapes using the methods that we have discussed in this and the previous tutorial, but sometimes it makes more sense to directly use an image when trying to draw a graphic on the canvas. Konva allows you to draw images using the Konva.Image() object. 

The position of the top-left corner of an image is determined by the value of the x and y properties. Similarly, its width and height are specified using the width and height properties. The values of the width and height properties don't have to be equal to the actual dimensions of the image. You can also scale or rotate the image using the rotation, scale, scaleX, and scaleY properties.

It is worth noticing that in the above code, I instantiated the Konva.Image() object after the image had already loaded. Trying to instantiate the Konva.Image() object before the image has loaded will result in an error. The image of the field has been taken from Pixabay.

Konva also allows you to render sprites on the canvas with the help of the Konva.Sprite() object. The only difference is that this time you have to use the animation and animations keys in addition to the image key that we used earlier while rendering the image.

The animation key accepts a string that specifies the id of the animation to be played. The animations key accepts an object that stores the animation map for the sprite as its value. You can control the rate at which a sprite animation should be played using the frameRate property.

The width and height of our hero sprite are 80px and 94px respectively. I have used these values to tell Konva the position of a sprite that should be shown while playing a specific animation sequence. Each animation sequence has been given an id to identify what the hero is doing. This id is used later when you want to tell Konva which animation it should play. Just like the previous example, I have instantiated the hero after the image loads in order to avoid errors. 

The hero sprite sheet that we are using in the demo was created by tokka. I have also used the same image in the Crafty Beyond the Basics: Sprites tutorial to create a sprite animation. The tutorials in that series show you how to create a simple 2D game using Crafty.

Coming back to Konva, the following example shows a walking and jumping hero animation. The hero at the bottom has a higher frameRate value, which makes it appear as if the hero is running at superhuman speeds.

Final Thoughts

After completing the second and third tutorials, you should now be able to create a lot of shapes, patterns, and designs by yourself using Konva. The last section also showed you how to draw images and sprites on your canvas. This ought to cover all your drawing-related needs.

If you have any questions related to this tutorial, I would be glad to help. The next tutorial will teach you how to fill your shapes with gradients and how to apply shadows on anything that you draw on the canvas.

Did you find this post useful?
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.