Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
Why do two lines drawn the same way render differently

I was toying around with the HTML Canvas in a 2D context and noticed that when rendering lines, they don't always render in the same way. I've got a "somehwat" minimal example below. Basically, both paths are rendered with the same strokeStyle and lineWidth using the moveTo and lineTo primitives. However, the bottom line of the hexagon and the top one of the triangle look differently (as shown in this screenshot).

Increasing the lineWidth does seem to fix it. But I want crisp yet thin lines, so after subsequently increasing the canvas size to a multiple of what it is rendered at on the page also doesn't fix this (e.g. set lineWidth to 4, canvas size to 1600x1600 and the canvas element's style size to 400px by 400px).

What's the cause of this behaviour, or what is this called? I thought it could be anti-aliasing, but why doesn't that affect both lines in the same way?

Once I know why this happens I can start by looking into a fix, but for now I don't even know what to search for :--).

<html>
    <head>
        <title>The Fuck</title>
    </head>
    <body>
​
    </body>
​
    <script>
        let canvas = document.createElement("canvas");
        let context = canvas.getContext("2d");
        document.body.appendChild(canvas);
        canvas.width = 400;
        canvas.height = 400;
        canvas.style.height = canvas.width + "px";
        canvas.style.width = canvas.height + "px";
        let lineWeight = 1;
        let strokeStyle = "black";
        let n = 6;
        let r = canvas.width / 4;
        let cx = canvas.width / 2;
        let cy = canvas.height / 2;
        let x = cx + r * Math.cos(0);
        let y = cy + r * Math.sin(0);
        context.save();
        context.strokeStyle = strokeStyle;
        context.lineWidth = lineWeight;
        context.moveTo(x, y);
        for (let i = Math.PI * 2 / n; i < Math.PI * 2; i += Math.PI * 2 / n) {
            x = cx + r * Math.cos(i);
            y = cy + r * Math.sin(i);
            context.lineTo(x, y);
        }
        context.closePath();
        context.stroke();
        context.restore();
        context.save();
        context.strokeStyle = strokeStyle;
        context.lineWidth = lineWeight;
        context.moveTo(canvas.width / 4, canvas.height * 3 / 4);
        context.lineTo(canvas.width * 3 / 4, canvas.height * 3 / 4);
        context.lineTo(canvas.width / 2, canvas.height * 7 / 8);
        context.closePath();
        context.stroke();
        context.restore();
    </script>
</html>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda