The Transformation of the
transform
@DanCWilson
CSS Dev Conf 2017
Why are we talking about this single property?
It’s just one property...
It’s approaching a decade of existence...
It’s no longer the flashy new kid on the block...
It’s just something for animation...
Let’s get out of the way some of the the thoughts you possibly have as you find out this talk really is centered around a single property. Whew, ok that all hurt a little, but it’s good to get bias out of the way.
What does this property do again?
At a high level...
translateX(-20px)
rotate(25deg)
scale(.5)
skewY(-12deg)
rotateX(30deg)
But seriously... why talk about this?
We will get into several aspects of transform
Key benefits/effects and related properties
Stretch its limits
Dig into some of the math & 3D particulars
Key complications/frustrations
Potential of combining with newer CSS modules
What’s next for the spec
Friends... and at least a couple properties who put on appearances to be a friend but could be nicer and more authentic.
Think of it as a 10th anniversary retrospective, we're busting out the projector and looking at where it's been while sneakily talking about where we are going
The Key Effects
Change visual appearance without affecting flow layout
Relative to default position
The Key Effects
Animation (via transition and animation)
input {
transform: scale(1);
transition: transform 200ms ease-in-out;
animation: enter 175ms 0ms 1 ease-out both;
}
input:disabled {
transform: scale(.9)
}
@keyframes enter {
from {
scale(0);
}
}
The Beauty of Animating Transforms
Can be animated on separate thread: the compositor
The compositor arranges/combines layers
Animates outside the page’s layout flow
Comparison to Cel Animation (Brian Birtles)
The First Friend of the Transform: transform-origin
Position for the transformation origin
Default is 50% 50%
Evident for rotation, scaling, skewing
Bonus: It’s animatable
animatable but not in the ideal gets-off-main-thread way
The First Frenemy of the Transform: will-change
Intent: Clue the browser in that the element will change in the future
Trying to maximize the performance of actual moving elements
If everything is on its own layer you can have performance problems, too
The First Frenemy of the Transform: will-change
Safari can benefit from it
Firefox can benefit from it, but also can figure it out itself
Edge does not support it (and that is okay)
Chrome can benefit from it, and can also mess. you. up.
To Use or Not to Use: will-change
“I don't know... try it without, then add it if you need to” - @davidkpiano, 10/9/2017
Don’t put it on every element
Basically a stopgap solution
Test it
We haven’t even talked about multiple transforms
Multiple functions in a single transform property
Scale, rotate, translate, and skew any number of times
And now... the complication
How do we keep track of all of our transforms
Especially when different states might have different transforms
Which leads to the the question
Why are these transformation functions all crammed into one property?
The less mathy answer
Any number of transforms
Order matters
But I really want to talk a little more about math
Computer Graphics
Linear Algebra
Matrix Multiplication
But still... it’d be nice to have independent properties
Consider a button
Default state is translated vertically for a layout purpose
Hover state scales down a little
Active state rotates slightly
See the Pen Button Example: CSSDC by Dan Wilson (@danwilson ) on CodePen .
A first attempt
button {
transform: translateY(-150%)
}
button:hover {
transform: scale(.8)
}
button:active {
transform: rotate(-5deg)
}
Properties override... not append
Need to be explicit for all states
Change a value in one place, need to change everywhere
Maybe we are overreacting
It’s not the end of the world to be explicit
Compounds as more states are introduced
Complexity grows when working with updating transforms via JS reactively
So...
Independent Transform Properties to the Rescue!
Latest CSS Spec includes independent properties for translate, rotate, & scale
Change one type of transform without affecting a previously defined other type
Available in Chrome Canary behind Experimental Web Features flag
Independent Transform Properties Gotchas*
Any number of transforms
Order Matters
Independent Transform Properties Gotchas*
With independent properties, you get at most one transform per axis
translate: X Y
rotate: <angle>
scale: X Y
If you want more, you need to go back to the transform property
Spec details
Independent Transform Properties *Gotchas*
With independent properties, order will always be:
translate
rotate
scale
each function in the transform
Spec details
Wow, It’s so impressive what all options we have with transforms, and I was really getting excited about the prospect of these individual properties. But they seem a bit more limited in their viability than I expected, and it’s disappointing to hear they are only in Chrome Canary. Dan, isn’t there a solution to use today that has all the power of using as many transform combinations as I want - and in the order I want them - but also allows me the ability to change individual function values when I want them without having to know the state of all the others? And wait, am I really sure this is what I am thinking, or are you just allowing some time to rest your voice?
The Beauty of CSS Variables
Similar to Sass/Less Variables
In Firefox, Edge, Chrome, Opera, and Safari
Can help us get to a more flexible/powerful transform
An aside: Intro to CSS Variables
Use it like a Sass/Less variable
Can have a global variable and use it anywhere
:root {
--primary: #3489ab;
}
.branding {
color: var(--primary);
}
The Beauty of Custom Properties
Real power when you think of them as properties
Respect cascade and inheritance in CSS
Are straightforward to change via JavaScript
.branding {
--primary: #3489ab;
color: var(--primary);
}
.branding.subdued {
--primary: #24699b;
}
Individualize the transform property
Break it up with custom properties
.logo {
--translate-x: 80px;
--scale: .5;
transform:
translateX(var(--translate-x))
scale(var(--scale));
}
Individualize the transform property
Work with the scale and translate separately
.logo.secondary {
--scale: .75;
/* transform: translateX(80px) scale(.75)*/
}
.logo.tertiary {
--translate-x: 20px;
/* transform: translateX(20px) scale(.5)*/
}
.logo.fourthiary {
--translate-x: 10px;
--scale: .4;
/* transform: translateX(10px) scale(.4)*/
}
Individualize the transform property
Any number of transforms
Order Matters
.logo {
--translate-x: 80px;
--scale: .5;
--rotate: 10deg;
--scale-end: .75;
transform:
translateX(var(--translate-x))
scale(var(--scale))
rotate(var(--rotate))
scale(var(--scale-end));
}
Individualize the transform property
Transitions are applied when custom properties change
.logo {
--translate-x: 80px;
--scale: .5;
transform:
translateX(var(--translate-x))
scale(var(--scale));
transition: transform 200ms ease-in-out;
}
Individualizing properties examples
An aside: one more way to combine transforms the WAAPI
Web Animations API
Performantly animate transform and more from JS
Similar stylistically to CSS animations
Introducing a new way to build on transforms: composite
Currently available to try in Firefox Nightly: Demo
Potential to bring it into CSS , also
Let’s Transition to 3D
Thanks to iPhone Card Flipping
Rotate, translate, and scale around three axes
Elements won’t take a depth (still flat surfaces)
More to keep track when compared to 2D
Another friend of 3D Transforms: transform-style
flat vs preserve-3d
When nesting elements, will the element respect its parent’s 3D space
Or will the element be flattened into it
See the Pen Visual Reference: Transform-Style by Dan Wilson (@danwilson ) on CodePen .
Things to Remember
When you write transform: rotateX(39deg) and it doesn’t look right:
Set a perspective on the container
Make sure you preserve 3d if nesting
So what’s new with 3D?
Nothing.
Unless you look at other properties
The Problem with 3D Transforms
Everything is constructed of flat rectangles
An element will never have a thickness
We work around that by making boxes out of six divs/faces
The Beauty of Clip Path
Now more straightforward* to build other shapes like pyramids and dodecahedrons
See the Pen Pyramid from Cube by Dan Wilson (@danwilson ) on CodePen .
The Next Future
Houdini and Custom Properties Level 2
Define smarter Custom Properties
Future of CSS Slides (Tab Atkins)
Animating Custom Properties
Future:
Custom properties that can be computed similar to animateable properties
Spec details
.moving-picture {
transform: rotate(var(--angle));
animation: move-it 350ms ease both;
}
@keyframes move-it {
0% {
--angle: 0deg;
}
100% {
--angle: 100deg;
}
}
The future can be exciting but we have a lot today
Custom Properties bring some interesting options to the table today
As does thinking outside the box
Or thinking outside the transform alone
We will wrap by exploring other CSS modules to work with
Combine with other CSS Modules
Combine with other CSS Modules
Play with the Intersection Observer
Previous
Next