how to draw 3d cube patterns
Cube
Cards are a expert start for working with 3D transforms, but they only show off 3D in transition. To bear witness off 3D at rest, we'll have to create true 3D objects: prisms. We'll commencement with a cube.
The markup for the cube is like to the bill of fare. This fourth dimension nosotros need 6 child elements for all vi faces of the cube.
forepart
back
correct
left
pinnacle
bottom
Basic position and size styles set up the 6 faces on elevation of i another in the container.
front end
back
right
left
top
lesser
At present all the faces are placed on top of 1 another, ready to be rotated. .cube__face--top
and .cube__face--bottom
volition use rotateX()
so they are rotated around the vertical 10 axis.
forepart
dorsum
right
left
top
lesser
(Nosotros could remove the rotateY( 0deg)
style, as this transform has no upshot, but permit'south exit it in for consistency.)
Now that faces are rotated, only the front and back faces are visible. The 4 side faces are all perpendicular to the viewer, so they appear on-edge, well-nigh-invisible. To push them out to their appropriate sides, they need to be translated out from the center of their positions. Each side of the cube is 200px wide. From the cube's heart they'll need to exist translated out one-half that distance, 100px
.
front
right
back
left
top
lesser
Notation here that the translate
function comes later on the rotate
. The order of transform functions is meaningful. Each face is first rotated towards its position, and so translated outward in a carve up direction.
We take rendered a cube, but nosotros're non washed all the same.
Un-fuzzing 3D transformed text
Take another wait at the text "front" in the in a higher place cube. Information technology'south fuzzy.
3D transforms affect text rendering. When you apply a 3D transform, browsers accept a snap-shot of the element and and then re-render those pixels with 3D transforms applied. Equally such, fonts don't accept the same anti-aliasing given their transformed size.
font-size: 2.5em
transform: scale(2.v)
transform: perspective(500px) translateZ(250px)
For the sake of our users, 3D transforms should not misconstrue the interface. To resolve the baloney and restore pixel perfection to our cube, we can push dorsum the 3D object, then that the front confront will be positioned back at the Z origin.
front
right
back
left
top
bottom
Rotating the cube
To expose whatever face of the cube, nosotros'll need a fashion that rotates the cube to show that face up. The transform is the opposite of the corresponding face up. We toggle the necessary grade on the .cube
to utilize the advisable transform.
Find how the society of the transform functions has reversed. First nosotros push the object back with translateZ
, then we rotate information technology.
Finishing up, we tin can add a transition to animate the rotation between states.
forepart
back
correct
left
meridian
bottom
Edit this demo on CodePen
Side by side: Box →
Source: https://3dtransforms.desandro.com/cube
0 Response to "how to draw 3d cube patterns"
ارسال یک نظر