JavaScript: Draw a diagram using moveto() function
JavaScript Drawing: Exercise-5 with Solution
Write a JavaScript program to draw the following diagram [use the moveto() function].
Sample output:
Sample Solution:
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Draw diagram using moveto() function</title>
</head>
<body onload="draw();">
<canvas id="canvas" width="250" height="250"></canvas>
</body>
</html>
JavaScript Code:
function draw()
{
var canvas = document.getElementById('canvas');
if (canvas.getContext)
{
var context = canvas.getContext('2d');
context.beginPath();
// Outer circle
context.arc(75,75,50,0,Math.PI*2,true);
context.moveTo(110,75);
// Mouth
context.arc(75,75,35,0,Math.PI,false);
// Lefy and Right eye
context.moveTo(55,65);
context.arc(60,65,5,0,Math.PI*2,true);
context.arc(90,65,5,0,Math.PI*2,true);
context.stroke();
}
}
Live Demo:
See the Pen javascript-drawing-exercise-5 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript program to draw the following right-angled triangle.
Next: Write a JavaScript program to draw the following diagram [diagonal, white to black circles].
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/javascript-exercises/javascript-drawing-exercise-5.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics