Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove dashed line from HTML context

To draw a dashed line in a canvas context, I use this

    var canvas = document.getElementById('canv');
    ctx = canvas.getContext('2d');        
    ctx.setLineDash([5]);

When I don't want to draw more dashed lines I do this.

    ctx.setLineDash([0]);

Removing the dashs works in desktop browsers, but this is not working in mobile Safari. Is there another way to remove the dashes and draw plain continious solid lines?

Thanks

like image 950
MobileCushion Avatar asked Sep 10 '25 15:09

MobileCushion


1 Answers

You can pass empty array. It also makes line solid.

ctx.setLineDash([])

const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");

ctx.setLineDash([5, 3]);
ctx.strokeRect(20, 20, 150, 100);


button.onclick = () => {
  ctx.clearRect(15, 15, 200, 200);
  ctx.setLineDash([]);
  ctx.strokeRect(20, 20, 150, 100);
}
<!DOCTYPE html>
<html>

<body>

  <button id='button'>Unset Dashed Line</button><br>
  <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>


</body>

</html>
like image 80
Denis Kreshikhin Avatar answered Sep 12 '25 06:09

Denis Kreshikhin