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

318
Visualizações
Get coordinates of the points in an ellipse in HTML5 Canvas

In HTML5 Canvas, I know I can draw an ellipse with CanvasRenderingContext2D.ellipse(), but how can I draw some points on the ellipse path?

I can draw points on a circular path using the following codes:

const canvas = document.getElementById('thumbCanvas');
const ctx = canvas.getContext('2d');
const distance = 200;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
var angle = 0;
for(i = 0; i < 36; i++) {
  angleInRadian = angle * Math.PI / 180;
  ctx.fillRect(centerX - imgWidth / 2 + distance * Math.cos(angleInRadian), centerY - imgHeight / 2 + distance * Math.sin(angleInRadian), 5, 5);
  angle += 10;
}

Screenshot

p.s. ignore the colors and the dot at the center.

But how to do this with an ellipse? I don't want to use ctx.scale() to distort the whole image.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

At the moment your little rectangles are drawn along a perfect circle because the distance from the center is always the same. If you want to draw an oval, you have to vary the distance horizontally and vertically.

For example:

const canvas = document.getElementById('thumbCanvas');
const ctx = canvas.getContext('2d');
const distanceX = 60;
const distanceY = 30;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
let imgWidth = 10;
let imgHeight = 10;
var angle = 0;
for (i = 0; i < 36; i++) {
  angleInRadian = angle * Math.PI / 180;
  ctx.fillRect(centerX - imgWidth / 2 + distanceX * Math.cos(angleInRadian), centerY - imgHeight / 2 + distanceY * Math.sin(angleInRadian), 5, 5);
  angle += 10;
}
<canvas id="thumbCanvas"></canvas>

about 4 years ago · Juan Pablo Isaza Relatório
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