smiley
Step 1
Create an HTML file Index.html and inside that add a canvas, give it a width of 400 and height of 200.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>
  5. HTML CANVAS
  6. </title> <!—a reference to the an external js file-->
  7. <script src="script.js"></script>
  8. </head>
  9. <body onload="bodyLoad()">
  10. <canvas id="mycanvas" width="400" height="200" onmousewheel="myfunction2();"></canvas>
  11. </body>
  12. </html>
Step 2
The next step is to create a JavaScript file “script.js”. In that JavaScript, file create a function myFace.
To get the element reference of Canvas we can use its Id “myCanvas”. Pass it in the GetElementById function as a parameter that is present in the global variable document.
  1. function myFace() {
  2. var c = document.getElementById("mycanvas");
  3. //to draw a surface on canvas element pass 2d as an argument in the getContext function
  4. var draw = c.getContext("2d");
  5. //to start a new path invoke BeginPath function.
  6. //Call this function when you want to create a new path
  7. draw.beginPath();
  8. //to specify a color or style for your canvas use fillStyle property
  9. draw.fillStyle = "yellow";
  10. //to create a full circle invoke the arc method and in that method
  11. //pass the value for x and y, radius, start point,
  12. draw.arc(75, 75, 50, 0, Math.PI * 2, true);
  13. //to close the path invoke the closePath function
  14. draw.closePath();
  15. //invoke fill function to fill the canvas with a circle and in that circle a color of yellow
  16. draw.fill();
  17. }
Call this function when the body content loads, in other words on:
  1. <body onload="myfunction()">
  2. <body onload="myFace()">
  3. <p>Scroll your mouse over my face</p>
  4. <canvas id="mycanvas" width="400" height="200"></canvas>
  5. </body>
Save and open the HTML file using a web browser and the output will look like the following:
output
So, we have created a smiley face.
Step 3
The next thing we need to do is to add eyes to this face.
  1. function myEye() {
  2. var c = document.getElementById("mycanvas");
  3. var eye = c.getContext("2d");
  4. //moveTo function is used to move the starting point of the canvas to a new point
  5. //x value is 55 and y value is 50
  6. eye.moveTo(55, 50);
  7. //from the new point, begin a new path
  8. eye.beginPath();
  9. //fill the style with color black
  10. eye.fillStyle = "black";
  11. //left round eye
  12. eye.arc(50, 50, 4, 0, Math.PI * 2, true);
  13. eye.closePath();
  14. eye.fill();
  15. //move to the new sub path from the current point and create a right eye
  16. eye.moveTo(103, 49);
  17. eye.beginPath();
  18. eye.fillStyle = "black";
  19. //right round eye
  20. eye.arc(100, 50, 4, 0, Math.PI * 2, true);
  21. eye.closePath();
  22. eye.fill();
  23. }
Create a new function and add myFace and myEye functions to it.
  1. function bodyLoad() {
  2. myFace();
  3. myEye();
  4. }
Pass this bodyLoad function in the onload event of the body element.
  1. <body onload="bodyLoad()">
Refresh the page.
Refresh the page
Step 4
The final step is to add a smile.
Create a new function, mySmile and add the following:
  1. function mySmile() {
  2. var c = document.getElementById("mycanvas");
  3. var smile = c.getContext("2d");
  4. //105 means x it will go the left side and 75 means y it will go upward of //downward
  5. smile.moveTo(105, 75);
  6. smile.beginPath();
  7. smile.strokeStyle = "red";
  8. smile.arc(75, 75, 30, 0, Math.PI, false);
  9. //to draw a half rounded circle with a line stroke we can invoke the stroke function
  10. smile.stroke();
  11. }
  12. Add mySmile function in bodyLoad function.
  13. function bodyLoad() {
  14. myFace();
  15. myEye();
  16. mySmile();
  17. }
Save and run it.
Save and run it
To learn more about Canvas Rendering click here.
I hope you like it. Thank you.