I am trying to know more about HTML canvas and JavaScript. Now I am trying to draw multiple oval shapes overlapping on each other and size will become smaller and smaller one after one. I can draw the shapes successfully.
This is my code
<canvas id="myCanvas" width="400" height="200" style="border:1px solid #000000;">
</canvas>
<script type="text/javascript">
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var center_x = 200;
var center_y = 100;
var width = 100;
var height = 200;
drawOvalShape(context, 200, 100, 100, 200);
drawOvalShape(context, 200, 100, 80, 180);
drawOvalShape(context, 200, 100, 60, 160);
drawOvalShape(context, 200, 100, 40, 140);
drawOvalShape(context, 200, 100, 20, 120);
function drawOvalShape(context, center_x, center_y, width, height){
context.ellipse(center_x, center_y, width, height, 90 * Math.PI/180, 0, 2 * Math.PI);
context.stroke();
}
</script>
When I run my code on the browser, it displays like this.

But the problem is why an extra line is included as below?

How can I get rid of that line?
You are missing the context.beginPath. Please see this JSFIddle: https://jsfiddle.net/zwcd7hcw/
function drawOvalShape(context, center_x, center_y, width, height){
context.beginPath()
context.ellipse(center_x, center_y, width, height, 90 * Math.PI/180, 0, 2 * Math.PI);
context.stroke();
}
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var center_x = 200;
var center_y = 100;
var width = 100;
var height = 200;
drawOvalShape(context, 200, 100, 100, 200);
drawOvalShape(context, 200, 100, 80, 180);
drawOvalShape(context, 200, 100, 60, 160);
drawOvalShape(context, 200, 100, 40, 140);
drawOvalShape(context, 200, 100, 20, 120);
function drawOvalShape(context, center_x, center_y, width, height){
context.beginPath()
context.ellipse(center_x, center_y, width, height, 90 * Math.PI/180, 0, 2 * Math.PI);
context.stroke();
}
<canvas id="myCanvas" width="400" height="200" style="border:1px solid #000000;">
</canvas>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With