Progressive Enhancement with WebGL and React


David Lindkvist

For the time being, it’s very accepted to mix creative technologies like WebGL with used HTML/CSS when building websites. In this publish, I’ll strive to stipulate how we at 14islands manner this trend to make particular the bellow is light on hand to as many traffic as attainable. All of these tactics were former on our contemporary firm web page

Recede back factual a couple of years and responsive web construct, innovative enhancement and mobile-first were your entire rage. Right here’s the object — they are light official practices, and factual because now we contain catch entry to to contemporary shining toys doesn’t mean we catch a free stream to easiest device websites for desktop monitors and the most contemporary browser abilities.

Image for post

Image for post

Each person loves a chilly loading animation — Having a preloader permits the browser to download all belongings sooner than exhibiting the score bellow. Large pictures are known to trigger creep as they are uploaded to the GPU for the principal time. For this operate, it’s accepted for WebGL apps to preload and pre-render textures sooner than exhibiting the score bellow.

Each person loves to scroll — Having easiest one lengthy page diagram the WebGL canvas can construct all objects for your entire web page on page load (all the diagram thru the preloader animation), and there are no requests to load contemporary pages or contemporary belongings to effort about.

Each person has a most up-to-date browser — Building for improved browsers and including polyfills for missing aspects. Some websites even lock the user out if the browser lacks enhance for a most up-to-date feature.

On memoir of these assumptions, it’s accepted to construct and performance all bellow utilizing Javascript (e.g. In most cases, text bellow is rendered easiest as WebGL textures, which makes the bellow essentially pictures.

In case you’re doing a chilly advertising campaign web page or interactive abilities, many of these assumptions will most likely be appropriate. On the other hand, for many initiatives, having easy catch entry to to the bellow, a excessive stage of accessibility and SEO are equally crucial to delivering a memorable creative abilities.

No longer all websites can catch the cash for a lengthy loading animation. Company contain brief attention spans, and it goes to impress conversion rates and sales.

We generally exercise Gatsby.js on our initiatives. It’s fast, has a actual ecosystem of plugins to crawl up accepted initiatives, and presents server-aspect rendering out of the field.

As any fan of static websites knows, server-aspect rendering is colossal for SEO and accessibility since the bellow could additionally be accessed without executing a bunch of Javascript. We also are making an strive to make particular text bellow is on hand even though WebGL isn’t supported.

If truth be told, our javascript heavy React and WebGL web page even renders without Javascript enhance: 😲

Image for post

Image for post

With Javascript enabled however no WebGL enhance, you catch some fancier fly states and the flexibility to play our showreel in a chilly murky cinema mode: 😎🍿

Image for post

Image for post

And if the browser supports WebGL, you catch your entire gooey blob magic: 🎩🐇

Image for post

Image for post

Sadly, some browsers will gladly screech that they enhance WebGL, however they are merely too outdated to scamper the code with actual performance.

There’s no actual manner to feature-detect powerful units. Some strive to measure the frame price over time or exercise a aggregate of diverse aspects, however none of them are very powerful. In our case, we merely nefarious it on camouflage camouflage size and mediate that the next camouflage camouflage corresponds to a more powerful tool (which could or will no longer be lawful).

We desire to exercise the Three.js WebGL library. The slither at which it updates and introduces breaking changes is mind-blowing. Any third-birthday celebration react library that tries to produce a 1:1 mapping of Three.js objects to React parts will presumably no longer be maintained for lengthy.

react-three-fiber takes a certain manner by hooking into the React reconciler and proxies JSX tags to Three.js objects. It doesn’t even care what version of Three we are utilizing, which makes it future proof. Having acknowledged that, there are positively pitfalls and ample performance gotchas for a future weblog publish. 😬

We created a framework on top of react-three-fiber that permits us to add WebGL functionality to any existing React ingredient on the score bellow.

Image for post

Image for post

At the core now we contain one world shared WebGL canvas that stays in between page loads:

export const wrapRootElement = ({ ingredient }) => (




Every UI ingredient can opt-in to exercise this world canvas utilizing a custom useCanvas() hook:

const Image = () => {

const ref = useRef()

// add webgl ingredient to world canvas while mounted




When the ingredient mounts, it tells the realm canvas to render . This will most likely also additionally automatically catch away the WebGL mesh and natty up resources when it unmounts.

This retains our code modular and hides the crucial aspects of the WebGL implementation from unprecedented UI parts.

The is in cost for hiding the accepted image ingredient and plot a WebGL mesh in its operate.

A huge living is to sync the mounted canvas parts with the scrollable HTML bellow. The methodology we exercise largely follows an manner that tracks “proxy” parts in the accepted page float and updates the WebGL scene positions to compare them.

Since Javascript is inevitably slower than native scrolling, this can construct the WebGL scene in actual fact feel laggy. The most traditional manner to repair here is to contain some easing on the WebGL operate change.

Right here’s an example of a WebGL image with easing subsequent to a pair accepted bellow. Look how the image without note feels more delicate than the DOM bellow thanks to the easing:

Image for post

Image for post

To repair this, most websites also introduce a virtual scroll on the HTML bellow with a matching easing operate. This makes your entire page stream with the identical feeling, and while it’s for determined much less performant than a local scroll, it light perceived as being smoother when all items stream at the identical scamper:

Image for post

Image for post

A virtual scroll diagram that we stream all HTML bellow with Javascript, and it’s a known source of many accessibility issues. For some initiatives, it’s value the commerce-off, while diverse initiatives could require the next stage of accessibility.

There are quite a couple of libraries for virtual/delicate scrolling on hand. We counsel you seize one which as a minimum retains the browser’s native scrollbar.

To be just appropriate, if load time and maximum tool enhance is your absolute most realistic precedence, you shouldn’t exercise WebGL at all. But for some websites (like ours), there is a actual center floor.

By including WebGL to existing HTML parts on the page, we can ruin up up the work greater. Some developers can give attention to building an impressive responsive web page layout, and the WebGL could additionally be added on top in a second iteration. Needless to remark, all the issues needs to be totally examined collectively, as there are many performance pitfalls when utilizing this device.

There are downsides to this manner. Loading challenging pictures could trigger scroll jank, and it becomes tougher to invent delicate page transitions when all the issues isn’t preloaded. Accessibility also takes a hit as a result of having virtual scrolling of the DOM.

Allow us to know whenever that that you might per chance contain solutions on learn how to toughen this even additional, we’d desire to hear more how others manner this!

Read More

Leave A Reply

Your email address will not be published.