{"id":1611,"date":"2024-04-16T07:06:02","date_gmt":"2024-04-16T01:36:02","guid":{"rendered":"http:\/\/codetheory.in\/?p=1611---2121a2b6-4375-4465-929e-cae49d719ac3"},"modified":"2024-04-16T07:06:02","modified_gmt":"2024-04-16T01:36:02","slug":"why-clearrect-might-not-be-clearing-canvas-pixels","status":"publish","type":"post","link":"https:\/\/codetheory.in\/why-clearrect-might-not-be-clearing-canvas-pixels\/","title":{"rendered":"Why clearRect Might Not be Clearing the Canvas Pixels"},"content":{"rendered":"

Sometimes the clearRect(x, y, width, height)<\/code> method on the canvas context might not erase the previous graphics drawn. This usually happens when we’re drawing paths using methods like lineTo()<\/code>, arc()<\/code>, rect()<\/code>, etc. and then stroking them with stroke()<\/code> or filling their content area using fill()<\/code>. Here’s<\/a> an example of what I’m trying to convey.<\/p>\n

<\/p>\n

The solution to this problem is actually quite simple but the issue itself can cause major headache if you’re unable to nail it. We basically have to call the beginPath()<\/code> method on the 2D context. This method will create a new path and all the drawing functions called later will be directed to it. If we do not call beginPath()<\/code> (and then preferably closing that using closePath()<\/code>), then all the drawing commands called will stack up in the memory and every time stroke()<\/code> or fill()<\/code> is called, it’ll draw all the graphic paths.<\/p>\n

The stacking up of all the previous path drawing functions due to not closing the paths (with beginPath()<\/code> and closePath()<\/code>) and hence not getting cleared (with clearRect()<\/code>) can lead to unpleasant results in animations. Here’s an image of what you can expect for a rect()<\/code> call with stroke()<\/code> in such a situation.<\/p>\n

\"HTML5<\/p>\n

Finally, here’s the fixed version<\/a> of the demo linked above:<\/p>\n

\r\n(function () {\r\n  var canvas = document.querySelector('canvas');\r\n  var ctx = canvas.getContext('2d');\r\n  \r\n  canvas.width = window.innerWidth;\r\n  canvas.height = window.innerHeight;\r\n  \r\n  ctx.beginPath(); \/\/\r\n  ctx.rect(50, 50, 100, 100);\r\n  ctx.stroke();\r\n  ctx.closePath();\r\n  \r\n  ctx.beginPath(); \/\/\r\n  ctx.moveTo(350, 50);\r\n  ctx.lineTo(400, 100);\r\n  ctx.stroke();\r\n  ctx.closePath();\r\n  \r\n  ctx.clearRect(0, 0, canvas.width, canvas.height);\r\n\r\n  ctx.beginPath(); \/\/\r\n  ctx.rect(100, 100, 100, 100);\r\n  ctx.stroke();\r\n  ctx.closePath();\r\n  \r\n  ctx.beginPath(); \/\/\r\n  ctx.moveTo(450, 50);\r\n  ctx.lineTo(500, 100);\r\n  ctx.stroke();\r\n  ctx.closePath();\r\n  \r\n  ctx.clearRect(0, 0, canvas.width, canvas.height);\r\n  \r\n  ctx.beginPath(); \/\/\r\n  ctx.rect(150, 150, 100, 100);\r\n  ctx.stroke();\r\n  ctx.closePath();\r\n  \r\n  ctx.beginPath(); \/\/\r\n  ctx.moveTo(550, 50);\r\n  ctx.lineTo(600, 100);\r\n  ctx.stroke();\r\n  ctx.closePath();\r\n  \r\n}());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Sometimes the clearRect(x, y, width, height) method on the canvas context might not erase the previous graphics drawn. This usually happens when we’re drawing paths using methods like lineTo(), arc(), rect(), etc. and then stroking them with stroke() or filling their content area using fill(). Here’s an example of what I’m trying to convey.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[14,15],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1611"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=1611"}],"version-history":[{"count":8,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1611\/revisions"}],"predecessor-version":[{"id":1620,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1611\/revisions\/1620"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}