pub struct Transform {
pub local_transform: Transform3D<f32, UnknownUnit, UnknownUnit>,
pub world_transform: Transform3D<f32, UnknownUnit, UnknownUnit>,
pub origin: (f32, f32),
pub position_relative_to_parent: (f32, f32),
pub parent_container_camera_perspective: Option<Transform3D<f32, UnknownUnit, UnknownUnit>>,
}Fields§
§local_transform: Transform3D<f32, UnknownUnit, UnknownUnit>Local transform relative to parent
world_transform: Transform3D<f32, UnknownUnit, UnknownUnit>Fully composed world transform including all parent transforms (may include perspective)
origin: (f32, f32)Origin relative to the shape (pivot)
position_relative_to_parent: (f32, f32)Layout position relative to the parent
parent_container_camera_perspective: Option<Transform3D<f32, UnknownUnit, UnknownUnit>>Optional perspective matrix of the current element’s parent
Implementations§
Source§impl Transform
impl Transform
pub fn new() -> Self
Sourcepub fn compose(&mut self, parent: &Transform)
pub fn compose(&mut self, parent: &Transform)
Composes local transform with parent’s world transform, and stores the result as this transform’s world transform. Prent should be composed before calling this method. You can set up an empty transform for the root element.
pub fn compose_2(self, parent: &Transform) -> Self
pub fn set_origin(&mut self, ox: f32, oy: f32)
pub fn with_origin(self, ox: f32, oy: f32) -> Self
pub fn set_position_relative_to_parent(&mut self, x: f32, y: f32)
pub fn with_position_relative_to_parent(self, x: f32, y: f32) -> Self
Sourcepub fn set_parent_container_perspective(
&mut self,
distance: f32,
origin_x: f32,
origin_y: f32,
)
pub fn set_parent_container_perspective( &mut self, distance: f32, origin_x: f32, origin_y: f32, )
Sets the parent’s perspective parameters. In CSS this would be done on the parent element, but here we set it on the child for convenience.
Sourcepub fn with_parent_container_perspective(
self,
distance: f32,
origin_x: f32,
origin_y: f32,
) -> Self
pub fn with_parent_container_perspective( self, distance: f32, origin_x: f32, origin_y: f32, ) -> Self
Sets the parent’s perspective parameters. In CSS this would be done on the parent element, but here we set it on the child for convenience.
pub fn translate(&mut self, tx: f32, ty: f32)
pub fn then_translate(self, tx: f32, ty: f32) -> Self
pub fn translate_3d(&mut self, tx: f32, ty: f32, tz: f32)
pub fn then_translate_3d(self, tx: f32, ty: f32, tz: f32) -> Self
pub fn translate_x(&mut self, tx: f32)
pub fn then_translate_x(self, tx: f32) -> Self
pub fn translate_y(&mut self, ty: f32)
pub fn then_translate_y(self, ty: f32) -> Self
pub fn translate_z(&mut self, tz: f32)
pub fn then_translate_z(self, tz: f32) -> Self
pub fn translate_2d(&mut self, tx: f32, ty: f32)
pub fn then_translate_2d(self, tx: f32, ty: f32) -> Self
pub fn rotate_x_deg(degrees: f32) -> Self
pub fn rotate_x_rad(radians: f32) -> Self
pub fn then_rotate_x_deg(self, degrees: f32) -> Self
pub fn then_rotate_x_rad(self, radians: f32) -> Self
pub fn rotate_y_deg(degrees: f32) -> Self
pub fn rotate_y_rad(radians: f32) -> Self
pub fn then_rotate_y_deg(self, degrees: f32) -> Self
pub fn then_rotate_y_rad(self, radians: f32) -> Self
pub fn rotate_z_deg(degrees: f32) -> Self
pub fn rotate_z_rad(radians: f32) -> Self
pub fn then_rotate_z_deg(self, degrees: f32) -> Self
pub fn then_rotate_z_rad(self, radians: f32) -> Self
pub fn rotate(axis_x: f32, axis_y: f32, axis_z: f32, angle: Angle<f32>) -> Self
pub fn then_rotate( self, axis_x: f32, axis_y: f32, axis_z: f32, angle: Angle<f32>, ) -> Self
pub fn scale(sx: f32, sy: f32) -> Self
pub fn then_scale(self, sx: f32, sy: f32) -> Self
pub fn scale_3d(sx: f32, sy: f32, sz: f32) -> Self
pub fn then_scale_3d(self, sx: f32, sy: f32, sz: f32) -> Self
Sourcepub fn transform_local_point2d_to_world(&self, x: f32, y: f32) -> (f32, f32)
pub fn transform_local_point2d_to_world(&self, x: f32, y: f32) -> (f32, f32)
Transforms a local 2D point (x, y) to world coordinates using the composed world transform. Properly handles perspective transforms with homogeneous coordinates.
Sourcepub fn transform_world_point_to_local(
&self,
x: f32,
y: f32,
z: f32,
) -> Option<(f32, f32)>
pub fn transform_world_point_to_local( &self, x: f32, y: f32, z: f32, ) -> Option<(f32, f32)>
Transform a point from world space to local space (inverse transform). Returns None if the transform is not invertible. Useful for hit testing - convert mouse position to shape-local coordinates. Properly handles perspective transforms with homogeneous coordinates.
For perspective transforms, you need to provide the Z coordinate in world space. For hit testing 2D shapes at z=0 in local space, first transform local (0,0,0) to world to get the Z, then use that Z when inverse transforming mouse coordinates.
Sourcepub fn project_screen_point_to_local_2d(
&self,
screen_pos: (f32, f32),
) -> Option<(f32, f32)>
pub fn project_screen_point_to_local_2d( &self, screen_pos: (f32, f32), ) -> Option<(f32, f32)>
Convert world coordinates to local coordinates for hit testing arbitrary shapes.
This uses ray-casting similar to browsers: it casts a ray from the screen point perpendicular to the screen (parallel to the Z-axis) and finds where it intersects the transformed plane at z=0 in local space.
§Arguments
screen_pos- Screen/world coordinates (e.g., mouse position)
§Returns
Local coordinates (x, y) if the ray intersects the plane, None otherwise.
§Example
// For hit testing a path
if let Some((lx, ly)) = transform.project_screen_point_to_local_2d((mouse_x, mouse_y)) {
hit_test_path(&Point::new(lx, ly), path, FillRule::EvenOdd, 0.1)
} else {
false
}