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

134
Visualizações
Draw rectangle using coordinates of length edge and width on Html Canvas

I am leaning to work with canvas objects. I have the following coordinates: A: [x1,y1] B: [x2,y2] and a width which is a number. I want to draw a rectangle on the canvas using these information. I know that the output can be two rectangles both sides of the AB. One way can be to find two lines that are perpendicular to the AB and then from each point find another BC and AD points using width value. Is there any better solutions for that? Thanks

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

0

You don't need width if you know the vertices of both ends of a rectangle. Width and height can be obtained by subtracting the vertices of the square. Please refer to the following code.

const CANVAS_WIDTH = 400;
const CANVAS_HEIGHT = 300;
const canvas = Object.assign(document.createElement('canvas'), {width: CANVAS_WIDTH, height: CANVAS_HEIGHT});
const ctx = canvas.getContext('2d');

const A = [10, 30];
const B = [120, 250];

const drawRectByCorner = (ctx, corner1, corner2) => {
  const [x1, y1] = corner1;
  const [x2, y2] = corner2;
  ctx.fillRect(x1, y1, x2 - x1, y2 - y1); // x, y, width, height
};

drawRectByCorner(ctx, A, B);

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