HTML5 Canvas Modify Shape Color and set stroke width

In this article, you will learn about the implementation and use of an onclick event to modify the color of various figures and also see the implementation of stroke width on the mouseover event.
To fill a shape we can set the fill property when we instantiate a shape, or we can use the setFill() method.
To set a shape stroke and stroke width, we can set the stroke and strokeWidth properties when we instantiate a shape, or we can use the setStroke() and setStrokeWidth() methods.
First, we will create a triangle, circle, and pentagon with their borders. We will use the stoke(), to create the border. We will set the width of the border with strokeWidth and color of the border with strokeStyle.
For a triangle and a circle we will use the onclick event to change the color of the triangle and circle.
For the pentagon, we will use an onmouseovert event to change the color of the pentagon.
Example
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <style>
  7. body {
  8. margin: 0px;
  9. padding: 0px;
  10. }
  11. canvas {
  12. </style>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.3.1.min.js"></script>
  17. <script>
  18. var stage = new Kinetic.Stage({
  19. container: 'container',
  20. width: 578,
  21. height: 200
  22. });
  23. var layer = new Kinetic.Layer();
  24. var triangle = new Kinetic.RegularPolygon({
  25. x: 190,
  26. y: 120,
  27. sides: 3,
  28. radius: 80,
  29. fill: '#00D2FF',
  30. stroke: 'black',
  31. strokeWidth: 4
  32. });
  33. triangle.on('click', function () {
  34. var fill = this.getFill() == 'yellow' ? '#00D2FF' : 'yellow';
  35. this.setFill(fill);
  36. layer.draw();
  37. });
  38. layer.add(triangle);
  39. var circle = new Kinetic.Circle({
  40. x: 380,
  41. y: stage.getHeight() / 2,
  42. radius: 70,
  43. fill: 'red',
  44. stroke: 'black',
  45. strokeWidth: 4
  46. });
  47. circle.on('click', function () {
  48. var fill = this.getFill() == 'red' ? '#00d00f' : 'red';
  49. this.setFill(fill);
  50. layer.draw();
  51. });
  52. layer.add(circle);
  53. stage.add(layer);
  54. var pentagon = new Kinetic.RegularPolygon({
  55. x: stage.getWidth() / 2,
  56. y: stage.getHeight() / 2,
  57. sides: 5,
  58. radius: 70,
  59. fill: 'lightblue',
  60. stroke: 'black',
  61. strokeWidth: 3
  62. });
  63. pentagon.on('mouseover', function () {
  64. this.setStroke('blue');
  65. this.setStrokeWidth(20);
  66. layer.draw();
  67. });
  68. pentagon.on('mouseout', function () {
  69. this.setStroke('black');
  70. this.setStrokeWidth(4);
  71. layer.draw();
  72. });
  73. // add the shape to the layer
  74. layer.add(pentagon);
  75. // add the layer to the stage
  76. stage.add(layer);
  77. </script>
  78. </body>
  79. </html>
Output
Originally we have
color11.jpg
After the Onclick event the color of the triangle and circle change
color12.jpg
After the onMouseover event we finally get:
color13.jpg