W3C

CSS 3D Transforms Module Level 3

W3C Working Draft 20 March 2009

This version:
http://www.w3.org/TR/2009/WD-css3-3d-transforms-20090320
Latest version:
http://www.w3.org/TR/css3-3d-transforms
Previous version:
none
Editors:
Dean Jackson (Apple Inc)
David Hyatt (Apple Inc)
Chris Marrin (Apple Inc)

Abstract

CSS 3D Transforms extends CSS Transforms to allow elements rendered by CSS to be transformed in three-dimensional space.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-3d-transforms” in the subject, preferably like this: “[css3-3d-transforms] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This is the first public Working Draft of the “css3-3d-transforms” series.

Table of contents

1 Introduction

This section is not normative.

This is an enhancement to the CSS Transforms specification which provides transforms in three dimensions. It should be read in conjunction with that specification.

This coordinate space can be modified with the transform property. Using transform, elements can be translated, rotated and scaled in two or three dimensional space. A perspective transform can also be applied to give a sense of depth to the way elements are displayed. In three dimensions, a Z axis is added, with positive z values conceptually rising perpendicularly out of the window toward the user and negative z values falling into the window away from the user.

Any value other than ‘none’ for the transform results in the creation of both a stacking context and a containing block. The object acts as though position: relative has been specified, but also acts as a containing block for fixed positioned descendants. The position on the Z axis of a transformed element does not affect the order within a stacking context. With elements at the same z-index, objects are drawn in order of increasing z position.

Note that while transform uses a three-dimensional coordinate system, the elements themselves are not three-dimensional objects. Instead, they exist on a two-dimensional plane (a flat surface) and have no depth.

2 The transform Property

A 2D or 3D transformation is applied to an element through the transform property. This property contains a list of transform functions. The final transformation value for an element is obtained by performing a matrix concatenation of each entry in the list. The set of transform functions is similar to those allowed by SVG. There are additional functions to support 3D transformations.

Name: transform
Value: none | <transform-function> [ <transform-function> ]*
Initial: none
Applies to: block-level and inline-level elements
Inherited: no
Percentages: refer to the size of the element's box
Media: visual
Computed value: Same as specified value.

3 The transform-origin Property

The transform-origin property establishes the origin of transformation for an element. This property is applied by first translating the element by the negated value of the property, then applying the element's transform, then translating by the property value. This effectively moves the desired transformation origin of the element to (0,0,0) in the local coordinate system, then applies the element's transform, then moves the element back to its original position.

Name: transform-origin
Value: [ [ [ <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ]? ] <length> ] | [ [ [ left | center | right ] || [ top | center | bottom ] ] <length> ]
Initial: 50% 50% 0
Applies to: block-level and inline-level elements
Inherited: no
Percentages: refer to the size of the element's box
Media: visual
Computed value: For <length> the absolute value, otherwise a percentage

4 The transform-style Property

The transform-style property defines how nested elements are rendered in 3D space. If the transform-style is flat, all children of this element are rendered flattened into the 2D plane of the element. Therefore, rotating the element about the X or Y axes will cause children positioned at positive or negative Z positions to appear on the element's plane, rather than in front of or behind it. If the transform-style is preserve-3d, this flattening is not performed, so children maintain their position in 3D space.

This flattening takes place at each element, so preserving a hierarchy of elements in 3D space requires that each ancestor in the hierarchy have the value preserve-3d for transform-style. But since transform-style affects only an element's children, the leaf nodes in a hierarchy do not require the perspective style.

Name: transform-style
Value: flat | preserve-3d
Initial: flat
Applies to: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: Same as specified value.

For some effects, the author will have to ensure that an ancestor element to a subtree using preserve-3d has a transform-style of flat (or the default). Otherwise, the elements in the 3D tree may be located behind ancestor elements and, thus, invisible (hidden behind an ancestor's background).

Note that while ‘preserve-3d’ can be specified on an element, the effect may not be possible. Elements that have overflow set to hidden are unable to keep their children in 3D. In this case the element will behave as if the property was set to flat.

Does transform-style: preserve-3d need to establish a stacking context and containing block like transform does?

5 The perspective Property

The perspective property applies the same transform as the perspective(<number>) transform function, except that it applies only to the positioned or transformed children of the element, not to the transform on the element itself.

If the value is none, less than or equal to 0 no perspective transform is applied.

The use of this property with any value other than ‘none’ establishes a stacking context. It also establishes a containing block (somewhat similar to position:relative), just like the ‘transform’ property does.

Name: perspective
Value: none | <number>
Initial: none
Applies to: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: Same as specified value.

6 The perspective-origin Property

The perspective-origin property establishes the origin for the perspective property. It effectively sets the X and Y position at which the viewer appears to be looking at the children of the element.

Name: perspective-origin
Value: [ [ <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ]? ] | [ [ left | center | right ] || [ top | center | bottom ] ]
Initial: 50% 50%
Applies to: block-level and inline-level elements
Inherited: no
Percentages: refer to the size of the box itself
Media: visual
Computed value: Same as specified value.

7 The backface-visibility Property

The backface-visibility property determines whether or not the "back" side of a transformed element is visible when facing the viewer. With an identity transform, the front side of an element faces the viewer. Applying a rotation about Y of 180 degrees (for instance) would cause the back side of the element to face the viewer.

This property is useful when you place two elements back-to-back, as you would to create a playing card. Without this property, the front and back elements could switch places at times during an animation to flip the card. Another example is creating a box out of 6 elements, but where you want to see the inside faces of the box. This is useful when creating the backdrop for a 3 dimensional stage.

Name: backface-visibility
Value: visible | hidden
Initial: visible
Applies to: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: Same as specified value.

8 The Transformation Functions

The value of the transform property is a list of <transform-functions>, applied in the order provided. The individual transform functions are separated by whitespace. The following is a list of allowed transform functions. In this list the type <translation-value> is defined as a <length> or <percentage> value, and the <angle> type is defined by CSS Values and Units.

matrix(<number>, <number>, <number>, <number>, <number>, <number>)
specifies a 2D transformation in the form of a transformation matrix of six values. matrix(a,b,c,d,e,f) is equivalent to matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, e, f, 0, 1).
matrix3d(<number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>, <number>)
specifies a 3D transformation as a 4x4 homogeneous matrix of 16 values in column-major order.
translate(<translation-value>[, <translation-value>])
specifies a 2D translation by the vector [tx, ty], where tx is the first translation-value parameter and ty is the optional second translation-value parameter. If <ty> is not provided, ty has zero as a value.
translate3d(<translation-value>, <translation-value>, <translation-value>)
specifies a 3D translation by the vector [tx,ty,tz], with tx, ty and tz being the first, second and third translation-value parameters respectively.
translateX(<translation-value>)
specifies a translation by the given amount in the X direction.
translateY(<translation-value>)
specifies a translation by the given amount in the Y direction.
translateZ(<translation-value>)
specifies a translation by the given amount in the Z direction. Note that percentage values are not allowed in the translateZ translation-value, and if present are evaluated as 0.
scale(<number>[, <number>])
specifies a 2D scale operation by the [sx,sy] scaling vector described by the 2 parameters. If the second parameter is not provided, it is takes a value equal to the first.
scale3d(<number>, <number>, <number>)
specifies a 3D scale operation by the [sx,sy,sz] scaling vector described by the 3 parameters.
scaleX(<number>)
specifies a scale operation using the [sx,1,1] scaling vector, where sx is given as the parameter.
scaleY(<number>)
specifies a scale operation using the [1,sy,1] scaling vector, where sy is given as the parameter.
scaleZ(<number>)
specifies a scale operation using the [1,1,sz] scaling vector, where sz is given as the parameter.
rotate(<angle>)
specifies a 2D rotation by the angle specified in the parameter about the origin of the element, as defined by the transform-origin property.
rotate3d(<number>, <number>, <number>, <angle>)
specifies a clockwise 3D rotation by the angle specified in last parameter about the [x,y,z] direction vector described by the first 3 parameters. If the direction vector is not of unit length, it will be normalized. A direction vector that cannot be normalized, such as [0, 0, 0], will cause the rotation to not be applied. This function is equivalent to matrix3d(1 + (1-cos(angle))*(x*x-1), -z*sin(angle)+(1-cos(angle))*x*y, y*sin(angle)+(1-cos(angle))*x*z, 0, z*sin(angle)+(1-cos(angle))*x*y, 1 + (1-cos(angle))*(y*y-1), -x*sin(angle)+(1-cos(angle))*y*z, 0, -y*sin(angle)+(1-cos(angle))*x*z, x*sin(angle)+(1-cos(angle))*y*z, 1 + (1-cos(angle))*(z*z-1), 0, 0, 0, 0, 1).
rotateX(<angle>)
specifies a clockwise rotation by the given angle about the X axis.
rotateY(<angle>)
specifies a clockwise rotation by the given angle about the Y axis.
rotateZ(<angle>)
specifies a clockwise rotation by the given angle about the Z axis.
skewX(<angle>)
specifies a skew transformation along the X axis by the given angle.
skewY(<angle>)
specifies a skew transformation along the Y axis by the given angle.
skew(<angle> [, <angle>])
specifies a skew transformation along the X and Y axes. The first angle parameter specifies the skew on the X axis. The second angle parameter specifies the skew on the Y axis. If the second parameter is not given then a value of 0 is used for the Y angle (ie. no skew on the Y axis).
perspective(<number>)
specifies a perspective projection matrix. This matrix maps a viewing cube onto a pyramid whose base is infinitely far away from the viewer and whose peak represents the viewer's position. The viewable area is the region bounded by the four edges of the viewport (the portion of the browser window used for rendering the webpage between the viewer's position and a point at a distance of infinity from the viewer). The depth, given as the parameter to the function, represents the distance of the z=0 plane from the viewer. Lower values give a more flattened pyramid and therefore a more pronounced perspective effect. The value is given in pixels, so a value of 1000 gives a moderate amount of foreshortening and a value of 200 gives an extreme amount. The matrix is computed by starting with an identity matrix and replacing the value at row 3, column 4 with the value -1/depth. The value for depth must be greater than zero, otherwise the function is invalid.

9 Transitions and animations between transform values

When animating or transitioning the value of a transform property the rules described below are applied. The ‘from’ transform is the transform at the start of the transition or current keyframe. The ‘end’ transform is the transform at the end of the transition or current keyframe.

10 DOM Interfaces

This section describes the interfaces and functionality added to the DOM to support runtime access to the functionality described above.

10.1 10.1 CSSMatrix

Interface CSSMatrix

The CSSMatrix interface represents a 4x4 homogeneous matrix.

IDL Definition
  interface CSSMatrix {
      attribute float m11;
      attribute float m12;
      attribute float m13;
      attribute float m14;
      attribute float m21;
      attribute float m22;
      attribute float m23;
      attribute float m24;
      attribute float m31;
      attribute float m32;
      attribute float m33;
      attribute float m34;
      attribute float m41;
      attribute float m42;
      attribute float m43;
      attribute float m44;

      void        setMatrixValue(in DOMString string) raises(DOMException);
      CSSMatrix   multiply(in CSSMatrix secondMatrix);
      CSSMatrix   inverse() raises(DOMException);
      CSSMatrix   translate(in float x, in float y, in float z);
      CSSMatrix   scale(in float scaleX, in float scaleY, in float scaleZ);
      CSSMatrix   rotate(in float rotX, in float rotY, in float rotZ);
      CSSMatrix   rotateAxisAngle(in float x, in float y, in float z, in float angle);
  };

Attributes
m11-m44 of type float
Each of these attributes represents one of the values in the 4x4 matrix. For instance m12 represents the value in the 2nd column of the first row.
Methods
setMatrixValue
The setMatrixValue method replaces the existing matrix with one computed from parsing the passed string as though it had been assigned to the transform property in a CSS style rule.
Parameters
string of type DOMString
The string to parse.
No Return Value
Exceptions
SYNTAX_ERR
Thrown when the provided string can not be parsed into a CSSMatrix.
multiply
The multiply method returns a new CSSMatrix which is the result of this matrix multiplied by the passed matrix, with the passed matrix to the right. This matrix is not modified.
Parameters
secondMatrix of type CSSMatrix
The matrix to multipy.
Return Value
CSSMatrix
The result matrix.
No Exceptions
inverse
The inverse method returns a new matrix which is the inverse of this matrix. This matrix is not modified.
No Parameters
Return Value
CSSMatrix
The inverted matrix.
Exceptions
NOT_SUPPORTED_ERROR
Thrown when the CSSMatrix can not be inverted.
translate
The translate method returns a new matrix which is this matrix post multiplied by a translation matrix containing the passed values. If the z component is undefined, a 0 value is used in its place. This matrix is not modified.
Parameters
x of type float
The X component of the translation value.
y of type float
The Y component of the translation value.
z of type float
The (optional) Z component of the translation value.
Return Value
CSSMatrix
The result matrix.
No Exceptions
scale
The scale method returns a new matrix which is this matrix post multiplied by a scale matrix containing the passed values. If the z component is undefined, a 1 value is used in its place. If the y component is undefined, the x component value is used in its place. This matrix is not modified.
Parameters
scaleX of type float
The X component of the scale value.
scaleY of type float
The (optional) Y component of the scale value.
scaleZ of type float
The (optional) Z component of the scale value.
Return Value
CSSMatrix
The result matrix.
No Exceptions
rotate
The rotate method returns a new matrix which is this matrix post multiplied by each of 3 rotation matrices about the major axes, first X, then Y, then Z. If the y and z components are undefined, the x value is used to rotate the object about the z axis, as though the vector (0,0,x) were passed. All rotation values are in degrees. This matrix is not modified.
Parameters
rotX of type float
The X component of the rotation value, or the Z component if the rotY and rotZ parameters are undefined.
rotY of type float
The (optional) Y component of the rotation value.
rotZ of type float
The (optional) Z component of the rotation value.
Return Value
CSSMatrix
The result matrix.
No Exceptions
rotateAxisAngle
The rotateAxisAngle method returns a new matrix which is this matrix post multiplied by a rotation matrix with the given axis and angle. The right-hand rule is used to determine the direction of rotation. All rotation values are in degrees. This matrix is not modified.
Parameters
x of type float
The X component of the axis vector.
y of type float
The Y component of the axis vector.
z of type float
The Z component of the axis vector.
angle of type float
The angle of rotation about the axis vector, in degrees.
Return Value
CSSMatrix
The result matrix.
No Exceptions

10.2 10.2 CSSTransformValue

Interface CSSTransformValue

The CSSTransformValue interface represents one transform function in the transform property. The operationType defines which operation is represented. The object also contains a list of values, which are the parameters of the function, in the same order in which they appear in the transform functions.

IDL Definition
  interface CSSTransformValue : CSSValueList {

      // 2D OperationTypes
      const unsigned short CSS_TRANSLATE   = 1;
      const unsigned short CSS_TRANSLATEX  = 2;
      const unsigned short CSS_TRANSLATEY  = 3;
      const unsigned short CSS_ROTATE      = 4;
      const unsigned short CSS_SCALE       = 5;
      const unsigned short CSS_SCALEX      = 6;
      const unsigned short CSS_SCALEY      = 7;
      const unsigned short CSS_SKEW        = 8;
      const unsigned short CSS_SKEWX       = 9;
      const unsigned short CSS_SKEWY       = 10;
      const unsigned short CSS_MATRIX      = 11;

      // 3D OperationTypes
      const unsigned short CSS_TRANSLATEZ  = 12;
      const unsigned short CSS_TRANSLATE3D = 13;
      const unsigned short CSS_ROTATEX     = 14;
      const unsigned short CSS_ROTATEY     = 15;
      const unsigned short CSS_ROTATEZ     = 16;
      const unsigned short CSS_ROTATE3D    = 17;
      const unsigned short CSS_SCALEZ      = 18;
      const unsigned short CSS_SCALE3D     = 19;
      const unsigned short CSS_PERSPECTIVE = 20;
      const unsigned short CSS_MATRIX3D    = 21;

      attribute unsigned short operationType;

      CSSMatrix getCSSMatrix() raises(DOMException);
  };    
  
Attributes
operationType of type unsigned short
One of the listed operation types.
Methods
getCSSMatrix
The getCSSMatrix method returns a CSSMatrix object representing this transform.
No Parameters
Return Value
CSSMatrix
The result matrix.
Exceptions
NOT_SUPPORTED_ERROR
Thrown when the CSSTranformValue can not be converted into a CSSMatrix, such as when the CSSTransformValue contains percentage units and is being called on an object with unknown dimensions.

In addition to the interfaces listed above, the getComputedStyle method of the Window object has been updated. The transform property of the style object returned by getComputedStyle contains a single CSSTransformValue with a type of CSS_MATRIX3D. The 16 parameters represent the 4x4 matrix that is the result of applying the individual functions listed in the transform property.

11 References

Normative references

Other references

Property index

Property Values Initial Applies to Inh. Percentages Media
backface-visibility visible | hidden visible block-level and inline-level elements no N/A visual
perspective none | <number> none block-level and inline-level elements no N/A visual
perspective-origin [ [ <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ]? ] | [ [ left | center | right ] || [ top | center | bottom ] ] 50% 50% block-level and inline-level elements no refer to the size of the box itself visual
transform none | <transform-function> [ <transform-function> ]* none block-level and inline-level elements no refer to the size of the element's box visual
transform-origin [ [ [ <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ]? ] <length> ] | [ [ [ left | center | right ] || [ top | center | bottom ] ] <length> ] 50% 50% 0 block-level and inline-level elements no refer to the size of the element's box visual
transform-style flat | preserve-3d flat block-level and inline-level elements no N/A visual

Index