Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
I cant get canvas to rotate around a certain point

I need to rotate an image or in this case a square around its center using canvas. If its at point (10,16) and I rotate the square 180 degrees I need that square to stay at (10,16) while facing downwards. The methods of which I have tried are in the attached code.

const canvas = document.getElementById("canvas")
const ctx = canvas.getContext("2d")
const size = 50
const pos = canvas.width/4
let rotateAmount = 0;

const tests = [
  // Test 1
  (rotation)=>{
    ctx.translate(pos/2+size, pos/2+size)    
    ctx.rotate(Math.PI / 180 * rotation)
    ctx.fillRect(pos, pos, size, size)
  },
  // Test 2
  (rotation)=>{
    ctx.translate(size/2+pos, size/2+pos)    
    ctx.rotate(Math.PI / 180 * rotation)
    ctx.fillRect(pos, pos, size, size)
  },
  // Test 3
  (rotation)=>{
    ctx.translate(pos/2, pos/2)    
    ctx.rotate(Math.PI / 180 * rotation)
    ctx.fillRect(pos, pos, size, size)
  },
  // Test 4
  (rotation)=>{
    ctx.translate(size/2, size/2)    
    ctx.rotate(Math.PI / 180 * rotation)
    ctx.fillRect(pos, pos, size, size)
  },
  // Test 5
  (rotation)=>{
    ctx.translate(pos + 0.5 * size, pos + 0.5 * size)    
    ctx.rotate(Math.PI / 180 * rotation)
    ctx.fillRect(pos, pos, size, size)
  }
]

window.onmousemove = (mouse) => {
    ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.clearRect(0, 0, canvas.width, canvas.height)
  rotateAmount++
  tests[document.getElementById("testMethod").value](rotateAmount)
}
canvas{
  border-color: violet;
  border-style: solid;
  border-width: 5px;
}
<p>
Move your mouse to rotate the square
</p>
<p>
Select your test method via the dropdown
</p>
<select id="testMethod">
  <option value="0">Test 1</option>
  <option value="1">Test 2</option>
  <option value="2">Test 3</option>
  <option value="3">Test 4</option>
  <option value="4">Test 5</option>
</select>
<button onclick="rotateAmount = 0">Reset Rotation</button>
<br>
<canvas id="canvas" width="250" height="250"></canvas>

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Found the answer to this while reviewing my question! It turns out you must translate back after rotating. The correct test is test 2 as it divides the size by 2 (so you are in the center of the square) and then adds the position (so you are at the square). Below is a working example with the retranslation.

const canvas = document.getElementById("canvas")
const ctx = canvas.getContext("2d")
const size = 50
const pos = canvas.width/4
let rotateAmount = 0;

const tests = [
  // Test 0
  (rotation)=>{
    ctx.translate(size/2+pos, size/2+pos)    
    ctx.rotate(Math.PI / 180 * rotation)
    ctx.translate(-size/2-pos, -size/2-pos)// Added Line
    ctx.fillRect(pos, pos, size, size)
  }
]

window.onmousemove = (mouse) => {
    ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.clearRect(0, 0, canvas.width, canvas.height)
  rotateAmount++
  tests[document.getElementById("testMethod").value](rotateAmount)
}
canvas{
  border-color: violet;
  border-style: solid;
  border-width: 5px;
}
<p>
Move your mouse to rotate the square
</p>
<p>
Select your test method via the dropdown
</p>
<select id="testMethod">
  <option value="0">Test</option>
</select>
<button onclick="rotateAmount = 0">Reset Rotation</button>
<br>
<canvas id="canvas" width="250" height="250"></canvas>

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!