The World of CSS Transforms
joshwcomeau.com
joshwcomeau.com
When you have `transform: translateX(…) rotate(…);`, you’re saying “first rotate it about the origin, then translate it”. When you have `transform: rotate(…) translateX(…);`, you’re saying “first translate it, then rotate it about the origin”! — not the translated object’s origin! That’s why in the the second example the transformed object swings around the origin from a distance when you change the rotation angle.
I mean, sure, you can mentally do it left to right and track how the axes change, as the author did, but right to left is a lot simpler, and is what’s actually happening to the vectors behind the scenes.
For a bunch of scripting languages stacked on top of each other, with byzantine levels of backwards compatibility and alternative ways to define the same things, thats seriously impressive optimisation techniques in the browsers. And they are pretty consistent, too. Absolutely awesome.
Of course, I am sure it generates a lot of CO2 for all that effortless eyecandy.
Why?
Genuine question. A couple years ago I tried assessing the claim that websites (frontend, specifically) can meaningfully be tuned to reduce emissions [1]. Apparently it takes less than a dollar/year (2018) to charge a smartphone (0 to 100% daily) [2]. Given that fact, and assuming that energy usage from a mobile browser is in the same order of magnitude as a desktop browser + price is a decent stand-in for amount of emissions, it seems to me that any savings aimed at reducing emissions via "better" websites is negligible, even at web scale. I have a simple (maybe too simple?) spreadsheet to back this up [3].
[1] https://github.com/thegreenwebfoundation/lighthouse-plugin-g...
[2] https://www.zdnet.com/article/heres-how-much-it-costs-to-cha...
[3] https://docs.google.com/spreadsheets/d/18cNY8Xpabau3bMY5iEsB...
Any basis for this assumption?
Desktop processors are much less optimized for power consumption, and desktop browsers are also used differently with websites often time left running in the background tab, compared to the mobile phone.
But I'll do a similar analysis: how much does a constantly-used desktop PC emit? I'm seeing estimates of ~200 kg emissions/year from a desktop PC for ~8hr/day usage, which is ~0.4% of the average American's emissions (50,000 kg/year). Now you must consider some other things:
1) of those relatively small emissions, how much is due to energy that is used for baseline PC functions (OS, monitor, etc).
2) does idle time from a browser doing slightly less work translate to noticeably less power draw? I can see a difference from 100% to 80% CPU utilization, but what about 40% to 20%? is the relationship between cpu utilization and power draw linear?
3) I'd bet the largest offenders of sites in terms of power usage are complex webapps that have no meaningful room for performance improvements possible by a frontend developer: games, photo editors, video players. Compare to those, better CSS selectors seems small potatoes.
To be clear, my claim is that overall the savings here are negligible and efforts are far best spent elsewhere (extreme example: instead of a programmer trying to optimize even a heavily-trafficked site, they would have a larger impact if they spent their time ensuring their home, or the home of their friends and family, is not wasting energy).
Whereas had we been sticking to deliver just plain passive HTML markup users are after, we could've had solar-/light-powered devices with slow LCD displays and low-power RF transmission similar to old e-book readers by now (solar-powered pocket calculators were available in the 1970s already).
Would help with tracking, privacy, ad monopolies, and browser obsolescence as well.
Jokes aside it sounds like the gemini protocol is trying to fulfill what you're mentioning here
That doesn't stop some people (and even some goverments) from calling EVs "carbon neutral".
The resources modern browsers require to run are silly and often take more resources than all other applications combine, and small players have a hard time entering the field.
I don't know if there was ever a time when browsers weren't heavier to write than games - except back in the dark days when the web was but a baby.
The mini operating system is written in another language than the browser.
An analogy here would be writing such an operating system within the game engine, and then claiming the game engine that is only used to render the operating system is the operating system.
The real issue with browsers is the load of historical cruft they have to continue to support, as well as many new and duplicated technologies such as in this case C.S.S encroaching more and more upon the domain of browser scripting.
> I don't know if there was ever a time when browsers weren't heavier to write than games - except back in the dark days when the web was but a baby.
Is that really so? I would say that at the time game rendering technology was making vast advances in 3.d. rendering with clever hacks around the turn of the millennium, browsers were probably easier to write than these engines.
Game consoles at the time had hardware accelerated sprite scrolling; personal computers did not and had to redraw sprites manually to scroll but the games used some very interesting hacks to avoid having to redraw most of the screen and create the illusion of fluid movement by only redrawing parts of it.
Some of the absolute hacks to make games run on the Gameboy were even more impressive.
Maybe, but many people have optimized the browser layout engines to make them efficient. That probably means they're far more efficient that the sorts of UIs people write themselves, despite several layers of abstraction. One badly written block of code can waste a lot of energy.
I know CSS transforms relatively well, but there's just no substitute for a well-explained web page with interactive demos!
Another similarly laid out web page I find myself going to is a Flexbox tutorial with demos, and toggles that show the difference between flex properties. [0]
I've been making an app with React Native and react-native-svg and even though react-native-svg supports transformations, I've had to do a lot of transforms with trigonometry (i.e. rotating a shape around a center point, then getting the new bounding box, which requires things such as Math.atan2).
It's made me really appreciate 1. how much easier front end web development is and 2. how far we've gotten. I cannot tell you how much I appreciate web methods like getBoundingClientRect() or element.closest(), and how easy it is to make animations with CSS and move things around with transforms.
They used Flash. You can now do everything you could do in Flash, with CSS[0]. There's even a few GUI tools[1] for CSS where you don't have to figure out the code:
[0] Edit: And Javascript
AND JavaScript.
With Flash, you just needed to know Flash. With HTML5, you need to know a whole lot more about DOM manipulation. If you're animating SVGs, that has its own markup and learning curve.
Flash could also do 3D animation, which CSS cannot. You need to know WebGL and a library like three.js for that to be workable.
By far the best WYSIWIG animation tool I've used is Tumult Hype for Mac. It handles the creation of all the JS-based animation triggers through the GUI. I hope there will be a WebGL version of this that is as easy to use.
Saw a few Codepens over the years[0] where people managed to rotate 3D cubes, all done entirely with CSS. Of course if you needed to interact with the cube you would need JS, but 3D can be achieved with CSS alone, it's just a very hacky way of doing it.
An image of a cube in an HTML document will look like 3D without any transforms applied. However if you try to rotate that image on the Z-axis, it will warp, clearly showing that it is still a 2D object. There are other necessary aspects of 3D such as camera angles and dynamic light and shadows, but that's outside the scope of what you'd expect to be supported in the official CSS spec.
HTML and CSS don't support 3D shape manipulation and motion. WebGL is needed for that.
And SVG! ..and an almost fanatical devotion to the Pope.
Flash also felt more like a proper application framework. Web still feels like you're working with an extremely advanced word processor.
What sibling comments said, and also jQuery helper functions such as $.fade [1] or GSAP [2]
> current version: script.aculo.us 1.9.0 as of December 23, 2010.
Probably the main thing they're most useful for is animation, and people just used jQuery/JavaScript for that instead, which could do most of what you'd want, if a little clunky.
I hadn't clocked how useful it is that percentage for translate() means the element itself, not it's parent container for example.
This is the main problem with animations in CSS: they work as long as you don't have to reflow the document, and `transform: translate` is very good for that.
But try to slide a new item into a list (or animate as it's being removed), and you're in for a nasty surprise especially if you don't know the height of the element beforehand.
There's one thing that makes translate ridiculously powerful, though. Something totally unique in the CSS language.
When we use a percentage value in translate, that percentage refers to the element's own size, not the available space within the parent container.
Something I love to do with this is create "squishy" buttons that transform:scale(0.9) when under the :active pseudo-class (with a short transition set up for smooth animation). It's extra satisfying on touchscreens.
Thanks, Josh!
I'm guessing it still takes space, is in flow, and takes clicks?
I too was surprised to discover that the order of transform params matters. Until I swapped them I was getting a completely different and unwanted animation effect.
Golden rule of web dev: If you can do it in CSS, do it in CSS.
Nice! I've been using `position: absolute` + `bottom: 100%` for this. I'll be adding this trick to my toolbox.
- Firefox 90.0.2 (64-bit) (Intel) and macOS 11.5
- Firefox 90.0.2 (64-bit) (Apple Silicon) and macOS 11.5.1
Tried private window with no extensions as well and still no dice. All interactive demos. Nothing happens when I manipulate the sliders.
Interestingly this works fine when I click around the options [0]
[0] https://developer.mozilla.org/en-US/docs/Web/CSS/transform
Of course.
-webkit-transform, -o-transform, -ms-transform, -moz-transform, -e-transform, -some-other-bullshit-browser-transform, -khtml-transform, -html-transform ... welcome back to the 90's world of brower-specific-internet
I haven't heard any other reports, and it's working on my fully-updated iPhone hoping this was just a hiccup.