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

191
Visualizações
How to get TopLeft, TopRight, BottomLeft, BottomRight, and CentreTop position of Canvas Rect

Basically, I am trying to get the TopLeft, TopRight, BottomLeft, BottomRight, and TopMiddle positions for my rect that has been added to the canvas. Please show me in such a way that it applies to any size change that may occur to the rect. Not for the current specific size.

HTML:

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(20, 20, 150, 100);
ctx.stroke();
</script> 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What you need can be calculated using Element.getBoundingClientRect() and the drawing information of rect relative to the canvas.

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();

var rectLeft = 20;
var rectTop = 20;
var rectRight = 150;
var rectBottom = 100;

ctx.rect(rectLeft, rectTop, rectRight, rectBottom);
ctx.stroke();

var boundingRect = c.getBoundingClientRect();

//TopLeft, TopRight, BottomLeft, BottomRight, and TopMiddle
console.log("TopLeft", `(${boundingRect.top + rectTop},${boundingRect.left + rectLeft})`)
console.log("TopRight", `(${boundingRect.top + rectTop},${boundingRect.left + rectRight})`)
console.log("BottomLeft", `(${boundingRect.top + rectBottom},${boundingRect.left + rectLeft})`)
console.log("BottomRight", `(${boundingRect.top + rectBottom},${boundingRect.left + rectRight})`)
console.log("TopMiddle", `(${boundingRect.top + rectTop},${boundingRect.left + (rectLeft + rectRight)/2})`)
//console.log(boundingRect)
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</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