1

I want to clear the canvas and then draw new shape on click of same button at once. "Draw shape" is the button I am using which draws shape of given parameters. Now when again I select some other shape without reloading the page, and click on the button again, it should clear existing shape and show only new one. But it is overwriting new shape over existing shape. Can somebody specify what wrong i am doing?

Code:

<!DOCTYPE html>
<html>
  <title>Blue Orchids School</title>
  <head align="centre"><b>Blue Orchids School</b></head>
  <body>
    <button onclick="draw()" type="submit">Draw shape</button><br><br>
    <canvas id="myCanvas" width="600" height="400" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.</canvas>
    <p id="details"></p>
    <script>
      function draw() {
        rendershape("Circle", 4, 22, 5, "Red")
        rendershape("Circle", 4, 220, 5, "Red")
      }
      function rendershape(shape, width, xcoordinate, ycoordinate, colour)
      {   
        var c=document.getElementById("myCanvas");
        var ctx=c.getContext("2d");
        ctx.clearRect(0,0,c.width,c.height);
        
        //To draw a circle
        if(shape=='Circle'||shape=='circle')
        {
            ctx.lineWidth=width;
            ctx.arc(xcoordinate,ycoordinate,50,0,2*Math.PI);
            ctx.fillStyle= colour;
            ctx.fill();
            ctx.stroke();
        }

        document.getElementById("details").innerHTML += 'Shape: '  + shape + '<br>' + 'Colour : '+ colour + '<br>' +' X-coordinate: '+ xcoordinate + '<br>' +' Y-coordinate: '+ ycoordinate + '<br>' + ' Width: '+ width;
      }
    </script>
  </body>
</html>

6
  • Where is the canvas DOM element? It looks like you haven't included all your HTML code. Does the variable c, where you grab the canvas element, console log out correctly? Commented Apr 8, 2016 at 16:26
  • I have added it. While copy pasting code here, it ain't showing that, it should be in plase of "Your browser does not support the HTML5 canvas tag. " It is as: <canvas id="myCanvas" width="600" height="400" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas> And yes, console.log is showing the c variable Commented Apr 9, 2016 at 4:57
  • Possible duplicate of clearRect function doesn't clear the canvas Commented Apr 9, 2016 at 11:05
  • In the future, please provide a minimal testcase that reproduces a problem (see my edit to your post). Most of the time I spent answering this was removing unneeded parts of the code. Commented Apr 9, 2016 at 11:09
  • @Nickolay: Thanks for the edit, will keep this in mind. Commented Apr 11, 2016 at 4:27

2 Answers 2

0

You need to add ctx.beginPath() after clearRect(). This is a good explanation: Why clearRect Might Not be Clearing the Canvas Pixels.

If we do not call beginPath() (and then preferably closing that using closePath()), then all the drawing commands called will stack up in the memory and every time stroke() or fill() is called, it’ll draw all the graphic paths.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like a typo case.

ctx.clearRect(0,0,c.widht,c.height);

Change it to:

ctx.clearRect(0,0,c.width,c.height);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.