I have a jQuery function changing the background-position property of three elements on mousemove and this seems to be causing some performance issues.
It should be noted that the background images of these elements are SVGs.
Example code:
$(window).on('mousemove', function(event) {
window.requestAnimationFrame(function() {
$banner.find('.pattern').each(function(key) {
var modifier = 20 * (key + 1);
$(this).css({
'background-position': (event.pageX / modifier)+'px '+(event.pageY / modifier)+'px'
});
});
});
});
See my working code here: https://codepen.io/thelevicole/project/full/DarVMY/
I am making use of window.requestAnimationFrame() and I also have will-change: background-position; css attribute on each element.
As you can probably tell, this effect is lagging. It seems to get worse on bigger window sizes.
I'm pretty sure the issue is caused by using SVGs for the background images instead of PNGs. The reason I am using SVGs is because of high pixel density screens.
If anyone can give some insight on how I can improve the FPS without having to use PNGs that would be great. Thanks.
$patterns = $banner.find('.pattern'); $(window).on('mousemove', function(event) { window.requestAnimationFrame(function() { $patterns.each(function(key) ...translate,scale,rotate, andopacity. Can you rewrite your JS to usetransform: translate()instead of repositioning backgrounds?