Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are svg images harder to render for a browser than bitmaps?

So I am making this horizontal scroll site which has a ton of images. I planned on using svgs for the entire site, but with only 20-30 svg images of medium to high complexity used in the page, and chrome already seems to be showing som jank and high paint times for scroll (and firefox is even worse, though safari seems to do a lot better).

Scroll timeline

Long frame times

View the site (scroll works on mac only, windows users can use arrow keys)

My question is, if I were to use pngs instead of svgs, would it reduce the paint times and hence the jank? Why is the browser struggling with only around 20 odd svg images?

like image 853
Shubham Kanodia Avatar asked Oct 27 '25 16:10

Shubham Kanodia


2 Answers

As was my doubt, the problem turned out to be something completely different. Browsers are more than capable of handling multiple vector images. But what they aren't good at (and understandably so) is at redrawing those images very often.

Problem

My long horizontal scroll site was quite wide (30,000px). I had a background-color property applied to one of lower z-index'ed div's to represent the sky throughout the scroll site. I didn't want the sky to stretch the entire 30,000px since it essentially didn't change much, and hence gave it viewport width and height, with:

position:fixed;

Not a very smart move. Turns out that this property was causing my document layer to be repainted on every frame. Initially I though it was normal for browsers to do so on scroll, since Robby Leonardi's site, which I used as reference also repainted every frame.

Solution

Thanks to this article by one of the chrome dev tools developers, I set aside conventional wisdom, and make the sky layer

position:absolute;

and stretched it the entire site width, and boom! The paint rectangles were gone. And the scroll performance was smoother than butter.

Other solutions I tried

  • Hiding elements not near the viewport to make painting lighter, as suggested by @philipp, but didn't yeild any appreciable difference. Also, it felt super-hacky, and it wasn't targeting the root cause of the problem.

  • I tried modularizing my site into scenes and using translateZ(0) hack on each scene so that only the smaller scenes get repainted instead of the document. This actually helped quite a bit, and scroll was decent. Then, I gave all the svg images their own layer by using translateZ(0). I started getting FPS of around 60 now, but again, this wasn't the right way of doing things.

like image 169
Shubham Kanodia Avatar answered Oct 30 '25 05:10

Shubham Kanodia


I once had a similar thing. The SVG was 10 or more times as wide as the one shown above, it contained ~20k elements and was about 3MB in size. The only Thing what brought back performance (since it was a jump and run game) was an algorithm which was able to find all elements whose bounding box overlapped the viewport. With this I could use display: none; to hide everything what is invisible.

That reduced the amout of visible elements to ~150 per frame and the game ran again fluently.

I used a balanced binary tree (avl tree) and a one dimensional range query, since the height of the viewport was always the same as the image.

Good luck!

[EDIT]

Forgot to leave something like an anwer. From my Experience are large/huge SVG Graphics a bottleneck in rendering, especially if there is a lot of scripting happening. If you do not need any Interactivity with the elements from the Graphic, so nothing than a large Background Image, I would recommend to use a Tile map, based on PNG images, that is the standard way in Jump'n'Run games with huges »worlds«, so you can gain perfomance in two Points:

  1. Rendering is faster,
  2. You can »lazy ajax load« tiles, depending on visibility, to prevent users to download the »whole world« at startup.

Additinally you could use something like PIXI.js to render with WebGL, what will push the performance drastically and with it comes support for Tilemaps and Spritesheets.

If you insist on the advantages of Vector Grafics (Scaling, Interactivity) than you need to find a way to hide as much elements as possible to keep the frame rate high.

like image 37
philipp Avatar answered Oct 30 '25 07:10

philipp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!