I am working on a JavaScript animation library and ran into a problem: All values are usually returned in pixels using this default function:
window.getComputedStyle(element).getPropertyValue(property);
However, when getting the value for background-position like so:
window.getComputedStyle(element).getPropertyValue('background-position');
The result is 50% 50% (background-position: center). How can I convert the values to pixels? I wrote the following function, but it gives me the wrong result, the reason being is that percentages on background-position are also relative to the image size.
var pixelsX = (parseFloat(percentX) / 100 * element.offsetWidth) + 'px';
var pixelsY = (parseFloat(percentY) / 100 * element.offsetHeight) + 'px';
I also cannot get the size using Image() since the calculation has to happen in real time and I cannot wait for the image to load.
Thanks!