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

196
Views
Get sprite sheet background position?

I have a table where each cell background is populated by sprites from a sprite sheet when the user clicks the cell which applies a class.

#design_container td {
  border: solid 1px #333;
  border-width: 0 1px 1px 0;
  width: 15px;
  height: 15px;
  background-image: url("../img/material_sprite.png");
  background-repeat: no-repeat;
}
.material_58 {
  background-position: -192px -48px;
}

Now I am using html5 canvas to make a download image. How can I get the background-position of the cells? I tried(and failed)

let x = document.querySelector('.material_58').style.backgroundPosition;
console.log(x); // returned nothing/blank

And also to no avail:

let tmp = window.getComputedStyle(x,null).backgroundPosition.trim().split(/\s+/);

I assume they may be getting the background position of the actual element, not the position of the sprite sheet. I'm trying to get

background-position: -192px -48px;

If something like that doesn't work maybe something like get the actual class description and parse it? Thank you for your help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The issue with your approach is that you're referencing a nonexistent style attribute on the element. What you want is the computed style:

const element = document.querySelector('.material_58');
const bgpos = getComputedStyle(element).backgroundPosition;

console.log(bgpos);
.material_58 {
  background-position: -192px -48px;
}
<div class="material_58"></div>

It would work the way you have it if the background position were inline:

console.log(
  document.querySelector('.inline-style')
    .style
    .backgroundPosition
);
<div class="inline-style" style="background-position: -192px -48px"></div>

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