Force Hardware Acceleration in WebKit with translate3d

By  on  

Ever notice an odd flicker within WebKit-powered desktop and mobile browsers, or simply want to use hardware acceleration of a given device? There's a really neat trick you can use to force hardware acceleration!

The WebKit CSS

/* warp speed ahead */
.animClass {
	-webkit-transform: translate3d(0, 0, 0);
	/* more specific animation properties here */
}

The use of translate3d pushes CSS animations into hardware acceleration. Even if you're looking to do a basic 2d translation, use translate3d for more power! If your animation is still flickering after switching to the transform above, you can use a few little-known CSS properties to try to fix the problem:

.animClass {
	-webkit-backface-visibility: hidden;
	-webkit-perspective: 1000;
}

There you have it; more power, less flicker. Happy animating!

Recent Features

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    New MooTools Plugin:  ElementFilter

    My new MooTools plugin, ElementFilter, provides a great way for you to allow users to search through the text of any mix of elements. Simply provide a text input box and ElementFilter does the rest of the work. The XHTML I've used a list for this example...

  • By
    Comment Preview Using MooTools

    Comment previewing is an awesome addition to any blog. I've seen really simple comment previewing and some really complex comment previewing. The following is a tutorial on creating very basic comment previewing using MooTools. The XHTML You can set up your XHTML any way you'd like.

Discussion

  1. K Huehn

    The second set of styles doesn’t work in Chrome… there’s a bug that causes flickering and disappearing elements.

    • I believe this bug has been fixed in the new version of Chrome.

  2. I’ve been using a lot of David’s tips and snippets. Here’s my contribution:

    I’ve been working real hard on revamping my site with some fresh and came across some discoveries.
    @K HUEHN and Mr Walsh: there are some issues with Chrome and hardware acceleration. Sometimes it is amazing at speeding things up, but sometimes it is a nightmare as it slows everything down and causes flickering. One example is browser window resizing. For example, compare current page (no hardware acc) and http://davidwalsh.name/ – now try resizing them. Notice how the images on the right have refresh fps of about 1. That sucks. Now here is the code that fixes that:

    window.addEvent('resize', function(){
    $$('*[style*=-'+Browser.prefix+'-transform:translate3d(0, 0, 0)').setStyle('-'+Browser.prefix+'-transform', '');
    });
    

    What it does is simply kills all HW acceleration on window resize. You can use this code on any events that perform worse to roll back to good ol’ software way.

    • Sorry, forgot to mention that Browser.prefix needs this to work:


      Browser.prefix = (Browser.firefox) ? 'moz' : (Browser.safari || Browser.chrome) ? 'webkit' : (Browser.opera) ? 'o' : 'ms';

  3. Using translate3d creates strange behaviour in HTC mobile devices. I came across the problem when debugging a mobile site on an Android 2.3.5 HTC Desire. The translate3d on the parent element was preventing the keyboard from loading on a HTML form input element.

  4. I found out that it even helps with jQuery-animations. On my test-server i’m working on some jQuery slideUp and slideDown functions. In webkit I saw a weird shift in the text during animation. So tried your hack/ fix and works wihout wiers text shift at end of animation.

    I’ve done this by a LESS-mixin in 320 an Up-biolerplate;

    .webkitForceHardwareAcceleration() {
    	-webkit-transform: translate3d(0, 0, 0);
    	-webkit-backface-visibility: hidden;
    	-webkit-perspective: 1000;
    }
    
  5. It’s worth noting that using -webkit-backface-visibility: hidden can cause iPad browsers to crash. More info that I’ve found on this issue is [here](http://dontwakemeup.com/webkit-backface-visibility-and-browser-crashing/).

  6. Adam

    I’ve now had two difficult bugs that came out of this technique, both related to positioning. I think this is a very cool trick, but it does cause some unexpected problems. And, for what it’s worth, it did not solve the one very notable animation shake I came across in a company site.

  7. MC

    Hey, is anyone able to help me troubleshoot? I’m having this exact issue but can’t seem to fix it. I’ll pay

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!