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

189
Views
Setting canvas size does not work with CSS but works with JS attribute, when using putImageData

Why does setting the canvas size with attributes in JS work correctly:

canvas = document.getElementById('canvas');
canvas.width = 200;
canvas.height = 200;
const ctx = canvas.getContext('2d');
const arr = new Uint8ClampedArray(40000);
for (let i = 0; i < arr.length; i += 4) { arr[i + 0] = 0; arr[i + 1] = 190; arr[i + 2] = 0; arr[i + 3] = 255; }
let imageData = new ImageData(arr, 100, 100);
ctx.putImageData(imageData, 0, 0, 0, 0, 100, 100);
#canvas { border: 1px solid black; }
<canvas id="canvas"></canvas>

whereas setting it directly with CSS totally fails with wrong aspect ratio? (it gives a rectangle instead of a square!)

canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const arr = new Uint8ClampedArray(40000);
for (let i = 0; i < arr.length; i += 4) { arr[i + 0] = 0; arr[i + 1] = 190; arr[i + 2] = 0; arr[i + 3] = 255; }
let imageData = new ImageData(arr, 100, 100);
ctx.putImageData(imageData, 0, 0, 0, 0, 100, 100);
#canvas { border: 1px solid black; height: 200px; width: 200px; }
<canvas id="canvas"></canvas>

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