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

127
Visualizações
Make canvas draw function smooth

So I have a canvas and I'm trying to implement a smooth drawing function. First I used lines instead of dots to solve the issue of spaces between drawing points when the user draws too fast. This is my Fiddle.

The core drawing function is in the loop:

if (state.mouse.on) {
    ctx.beginPath();
    ctx.strokeStyle = 'red';
    ctx.lineWidth = 2;
    ctx.moveTo(
      state.last.x ? state.last.x : state.mouse.x,
      state.last.y ? state.last.y : state.mouse.y
    );
    ctx.lineTo(state.mouse.x, state.mouse.y);
    ctx.stroke();

    state.last = { x: state.mouse.x, y: state.mouse.y };
  }

The problem is, if you draw too fast and curvy (i.e., try to draw a circle as fast as you can), you can see the lines are not "curvy" enough, of course, cause the loop runs as many time as the browser let's it, and it might no be enough to make lines smooth.

So, is there a way to solve this? I tried using the bezier function, and storing the last positions of the mouse then running the function but it only create wavy lines lol.

I'm not good at math, so I can't really speak but I think I need some kind of interpolating function?

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