Sign in to follow this  
Drewrg

HTML5 Canvas Antialiasing

Recommended Posts

The Example.

Hello every body. I have a Canvas with red background. On it I have rows of boxes, which go exactly after each other. Each box begins exactly where previous box ends, and they all have same skew. So there should not be any gap between edges, but as you can see in the example there are gaps.

Does anybody knows why is this happening, and how to get rid of it.

 

console.clear();
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var world = {
  centerY: canvas.height / 2,
  centerX: canvas.width / 2
}
var rowCount = 14;
var box = {
  width: 20,
  height: 20,
  skew: 10
}

function drawBox(x, y, id) {
  ctx.save();
  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(x + box.width, y);
  ctx.lineTo(x + box.width + box.skew, y - 20);
  ctx.lineTo(x + box.skew, y - box.height);
  ctx.lineTo(x, y);
  ctx.closePath();
  ctx.clip();
  ctx.clearRect(0,0, canvas.width, canvas.height);
  ctx.fillStyle = id % 2 == 0 ? 'lightgray' : 'darkgray';
  ctx.fill();
  ctx.restore();

}

for (var i = 0; i < 112; i++) {
  var k = Math.floor(i / rowCount) * rowCount;
  console.log(k);
  drawBox(i * box.width - (k * box.width), Math.floor(i / rowCount) * box.height, i)
}
#canvas {
  background-color: red;
  width: 800px;
  height: 600px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <canvas id="canvas">

  </canvas>
</body>

</html>

Share this post


Link to post
Share on other sites

The problem is fill(), which always draws with antialiasing.

  replace it with
  ctx.fillRect(0,0, canvas.width, canvas.height);

It will only work in Chrome though, other browsers antialias the clip regions.
 

 

 

Share this post


Link to post
Share on other sites
45 minutes ago, Fatalist said:

The problem is fill(), which always draws with antialiasing.

  replace it with
  ctx.fillRect(0,0, canvas.width, canvas.height);

It will only work in Chrome though, other browsers antialias the clip regions.
 

 

 

Actually there will be drawImage() instead of fill(),
but drawImage() got the same issue,
Is there any idea, what can replace drawImage().

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.