transformator
A Rust library for CSS-style 3D transform composition and inheritance. Compose hierarchical transforms with support for perspective, rotations, translations, scaling, and transform origins - just like CSS transforms work in browsers.
Features
- CSS-like transform composition: Chain transforms using familiar patterns (
translate,rotate,scale) - Hierarchical inheritance: Child transforms automatically inherit and compose with parent transforms
- Perspective support: Apply CSS-style perspective with customizable origin
- Hit testing: Project screen coordinates back to local space for accurate hit detection
- Optional serialization: Enable
serdesupport with theserializationfeature
Installation
[]
= "0.1"
# With serialization support
= { = "0.1", = ["serialization"] }
Usage
Basic Transform Composition
use Transform;
// Create a root transform (identity)
let root = new;
// Create a parent with position, perspective, origin, and rotation
let parent = new
.with_position_relative_to_parent
.with_parent_container_perspective
.with_origin // Rotate around center of 100x100 element
.then_rotate_x_deg
.compose_2;
// Create a child that inherits parent's transform
let child = new
.with_position_relative_to_parent
.compose_2;
// Transform local points to world coordinates
let world_pos = parent.transform_local_point2d_to_world;
Chaining Multiple Transforms
let transform = new
.with_position_relative_to_parent
.with_origin
.then_rotate_y_deg
.then_rotate_x_deg
.then_translate
.then_scale
.compose_2;
Hit Testing (Screen to Local Coordinates)
// Project mouse position to local coordinates for hit testing
if let Some = transform.project_screen_point_to_local_2d
Available Transform Methods
| Method | Description |
|---|---|
translate(x, y) / then_translate(x, y) |
2D translation |
translate_3d(x, y, z) / then_translate_3d(x, y, z) |
3D translation |
rotate_x_deg(deg) / then_rotate_x_deg(deg) |
Rotate around X axis |
rotate_y_deg(deg) / then_rotate_y_deg(deg) |
Rotate around Y axis |
rotate_z_deg(deg) / then_rotate_z_deg(deg) |
Rotate around Z axis |
scale(sx, sy) / then_scale(sx, sy) |
2D scaling |
scale_3d(sx, sy, sz) / then_scale_3d(sx, sy, sz) |
3D scaling |
with_origin(x, y) |
Set transform origin (pivot point) |
with_position_relative_to_parent(x, y) |
Set position relative to parent |
with_parent_container_perspective(dist, ox, oy) |
Set perspective |
compose(&parent) / compose_2(&parent) |
Compose with parent transform |
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.