Advertisement
  1. Code
  2. JavaScript

Manipulating HTML5 Canvas Using Konva: Part 5, Events

Scroll to top
This post is part of a series called Manipulating HTML5 Canvas Using Konva.
Manipulating HTML5 Canvas Using Konva: Part 4, Styling

If you have been following this series from the beginning, you should now be very comfortable with shapes, groups, and layers. You should also be able to easily draw some basic and complex shapes on the canvas using Konva. If you plan on using Konva to create some interactive app or games, learning how to bind events to different shapes on the stage is the next logical step.

In this tutorial, you will learn how to bind events to any shape using Konva. You will also learn about event delegation and propagation. Sometimes, you need might need to control the hit region of a shape as well as fire events programmatically. We will be discussing these two topics as well.

Binding Events to a Shape

You can bind different events to any shape created using Konva with the help of the on() method. All you have to do is pass the name of the event as the first parameter and the function to be executed when the event occurs as the second parameter. You can use Konva to detect mouseup, mousedown, mouseenter, mouseleave, mouseover, mousemove, click, and dblclick. Additionally, Konva allows you to detect wheel, dragstart, dragmove, and dragend events.

Here is an example that detects mousedown and mouseleave events on a regular polygon (hexagon). Similarly, the smaller circle is bound to the mouseover and mouseup events and the larger circle is bound to the mouseenter, mouseleave, and mousemove events.

1
var canvasWidth = 600;
2
var canvasHeight = 400;
3
4
var stage = new Konva.Stage({
5
  container: "example",
6
  width: canvasWidth,
7
  height: canvasHeight
8
});
9
10
var layerA = new Konva.Layer();
11
12
var polyA = new Konva.RegularPolygon({
13
  x: 125,
14
  y: 125,
15
  sides: 6,
16
  radius: 80,
17
  fill: "yellow",
18
  stroke: "black",
19
  strokeWidth: 5
20
});
21
22
var circA = new Konva.Circle({
23
  x: 275,
24
  y: 225,
25
  height: 100,
26
  fill: "orange",
27
  stroke: "black"
28
});
29
30
var circB = new Konva.Circle({
31
  x: 475,
32
  y: 275,
33
  radius: 100,
34
  fill: "red",
35
  stroke: "black"
36
});
37
38
layerA.add(polyA, circA, circB);
39
40
stage.add(layerA);
41
42
polyA.on("mousedown", function() {
43
  polyA.sides(polyA.sides() + 1);
44
  layerA.draw();
45
});
46
47
polyA.on("mouseleave", function() {
48
  var totalSides = polyA.sides();
49
  if(totalSides > 3) {
50
    polyA.sides(polyA.sides() - 1);
51
  }
52
  layerA.draw();
53
});
54
55
circA.on("mouseover", function() {
56
  circA.strokeWidth(10);
57
  layerA.draw();
58
});
59
60
circA.on("mouseup", function() {
61
  circA.strokeWidth(5);
62
  layerA.draw();
63
});
64
65
circB.on("mouseenter", function() {
66
  stage.container().style.cursor = "crosshair";
67
});
68
69
circB.on("mouseleave", function() {
70
  stage.container().style.cursor = "default";
71
});
72
73
circB.on("mousemove", function() {
74
  var pointerPos = stage.getPointerPosition();
75
  var r = pointerPos.x % 255;
76
  var g = pointerPos.y % 255;
77
  circB.fill("rgb(" + r + ", " + g + ", 100)");
78
  layerA.draw();
79
});

If a user presses any mouse button while the cursor is inside the regular polygon, we increase the number of sides of the polygon by 1. The sides() method can be used without a parameter to get the number of sides for a polygon or used with one parameter to set the number of sides for a polygon. You can also get the number of sides using getSides() and set the number of sides using setSides(). The sides of the polygon are reduced by one whenever the mouse cursor leaves the polygon.

For the smaller circle, the mouseover event is used to set the stroke width value to 10. The mouseup event changes the stroke width value to 5. Keep in mind that the mouseup event has to occur inside the circle itself. For example, the stroke width will not change to 5 if you press the mouse button inside the circle and then release it only after the cursor is outside the circle.

In the case of the larger circle, we are using the mousemove event to change its fill color. We are also changing the cursor of the larger circle using stage.container().style.cursor whenever the cursor moves in and out of the circle.

One important thing that you should keep in mind is that you have to call the draw() method on the respective layer if the event listeners for any shape resulted in a change of attributes like fill color, stroke width, etc. Otherwise, the changes won't be reflected on the canvas.

You don't have to bind one event at a time to a shape. You can also pass in a space delimited string containing multiple event types to the on() method. This will bind all the events listed in the string to that particular shape.

Konva also supports corresponding mobile versions of all these events. For example, you can register touchstart, touchmove, touchend, tap, dbltap, dragstart, dragmove, and dragend using Konva on mobile devices.

You can also fire any of these events for a particular shape or shapes using the fire() method. Similarly, Konva allows you to fire custom events like throwStones.

Removing Event Listeners

You can remove any event listeners attached to a shape with the help of the off() method in Konva. You just have to specify the event name that you don't want to listen to.

You can also create multiple event bindings for a single shape. For example, let's say you have a circle and you want to increase the radius of the circle every time the mouse cursor goes over it, up to a certain limit. You might also want to change the fill color of the circle on every mouseover event. 

One option is to do both these tasks inside a single mouseover event listener and stop updating the radius later. Another option is to create two mouseover event listeners with different namespaces to identify them. This way, you will be able to increase the radius and change the fill color independently.

1
circA.on("mouseover.radius", function() {
2
  var curRadius = circA.radius();
3
  if(curRadius < 150) {
4
    circA.radius(curRadius + 5);
5
    layerA.draw();
6
  } else {
7
    circA.off('mouseover.radius');
8
  }
9
});
10
11
circA.on("mouseover.fillcolor", function() {
12
  var h = Math.floor(Math.random()*360);
13
  var color = "hsl(" + h + ", 60%, 60%)";
14
  circA.fill(color);
15
  layerA.draw();
16
});

You should note that I have added layerA.draw() inside both listeners. If you fail to add it inside the mouseover.fillcolor listener, the color will stop updating as soon as the radius becomes 150.

Instead of removing one event listener at a time, you can also stop listening to all events bound to a shape using the setListening() method. You can pass true and false to this method in order to turn event listeners on and off. Keep in mind that you will also have to redraw the hit graph of the affected layer by calling the drawHit() method right after you call setListening().

Event Delegation and Propagation

Instead of binding events directly to all the shapes present on a layer, you can also bind an event to the layer itself. After that, you can determine which shape triggered the event using the target property of the event object. This way, Konva allows you to effectively delegate events from the parent to its children.

Let's say you are listening to click events on a circle drawn on a layer in Konva. The same click event propagates to the containing group as well as the containing layer. This may or may not be the intended behavior. If you want to prevent the event from bubbling up inside a shape to the containing layer, you can set the cancelBubble property for the event object to true.

1
var canvasWidth = 600;
2
var canvasHeight = 400;
3
4
var stage = new Konva.Stage({
5
  container: "example",
6
  width: canvasWidth,
7
  height: canvasHeight
8
});
9
10
var layerA = new Konva.Layer();
11
12
var circA = new Konva.Circle({
13
  x: 300,
14
  y: 200,
15
  height: 100,
16
  fill: "orange",
17
  stroke: "black",
18
  name: "Orange Circle"
19
});
20
21
var starA = new Konva.Star({
22
  x: 125,
23
  y: 125,
24
  innerRadius: 25,
25
  outerRadius: 75,
26
  rotation: 90,
27
  fill: "blue",
28
  stroke: "black",
29
  name: "Blue Star"
30
});
31
32
var ringA = new Konva.Ring({
33
  x: 475,
34
  y: 275,
35
  innerRadius: 25,
36
  outerRadius: 75,
37
  fill: "brown",
38
  stroke: "black",
39
  name: "Brown Ring"
40
});
41
42
var textA = new Konva.Text({
43
  text: "",
44
  fontFamily: "Calibri",
45
  fontSize: 24,
46
  fill: "black",
47
  x: 10,
48
  y: 10
49
});
50
51
layerA.add(circA, starA, ringA, textA);
52
53
stage.add(layerA);
54
55
layerA.on("click", function(e) {
56
  var shapeName = e.target.attrs.name;
57
  textA.setText(shapeName);
58
  layerA.draw();
59
});

I have used the name property to assign a name to each of our shapes. The setText() method is then used to change the text inside textA to the name of the shape we just clicked.

Custom Hit Regions

In the above example, the ring registered a click on it when the click occurred between the inner and outer circle. What if you wanted to register the click inside the smaller circle as well? Konva allows you to define custom hit regions using the hitFunc property. This property accepts a function as its value, and this function is used to draw the custom hit region.

The following example shows you how to create custom hit regions. You should now be able to click in the area between the star spikes and still register a click. With the help of custom hit regions, you can make sure that your users don't have to click on exact locations to register a click event. This can result in a better user experience when dealing with smaller or more complex shapes.

1
var starA = new Konva.Star({
2
  x: 125,
3
  y: 125,
4
  innerRadius: 25,
5
  outerRadius: 75,
6
  rotation: 90,
7
  fill: "blue",
8
  stroke: "black",
9
  name: "Blue Star",
10
  hitFunc: function(context) {
11
    context.beginPath();
12
    context.arc(0, 0, this.getOuterRadius(), 0, Math.PI * 2, true);
13
    context.closePath();
14
    context.fillStrokeShape(this);
15
  }
16
});
17
18
var ringA = new Konva.Ring({
19
  x: 475,
20
  y: 275,
21
  innerRadius: 25,
22
  outerRadius: 75,
23
  fill: "brown",
24
  stroke: "black",
25
  name: "Brown Ring",
26
  hitFunc: function(context) {
27
    context.beginPath();
28
    context.arc(0, 0, this.getOuterRadius(), 0, Math.PI * 2, true);
29
    context.closePath();
30
    context.fillStrokeShape(this);
31
  }
32
});

Final Thoughts

In this tutorial, we have covered different mobile and desktop events that you can bind to any shape in Konva. You can attach these events one at a time or many of them at once. Konva also allows you to fire your own custom events programmatically using the fire() method. The last section of the tutorial showed you how to define your own hit regions in order to detect hits on an area that could be larger or smaller than the original shape.

Combining the knowledge of this tutorial with others in the series, you should now be able to draw a variety of shapes on the canvas and allow your users to interact with them. 

If you have any questions related to this tutorial, feel free to let me know in the comments.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.