Horje

How to Clear the canvas by setting the width attribute to 400px (using JavaScript)

Follow the Example.

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="200" style="border:1px solid">
Your browser does not support the HTML5 canvas tag.
</canvas>
<br>

<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillStyle = "#92B901";
ctx.fillRect(50, 50, 100, 100);

function clearCanvas() {
  c.width = 400;
}
</script>

<button onclick="clearCanvas()">Clear canvas</button>

</body>
</html>

Output should be:

How to Clear the canvas by setting the width attribute to 400px (using JavaScript)




Type:
html
Category:
Web Tutorial
Sub Category:
HTML Tag
Uploaded by:
Admin