diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 743cfde6f..78ff1f413 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -6,10 +6,13 @@ use crate::render::BlendMode; mod fills; mod images; +mod matrix; mod paths; mod renderable; + pub use fills::*; pub use images::*; +use matrix::*; pub use paths::*; #[derive(Debug, Clone, PartialEq)] @@ -21,29 +24,6 @@ pub enum Kind { pub type Color = skia::Color; -#[derive(Debug, Clone, Copy)] -pub struct Matrix { - pub a: f32, - pub b: f32, - pub c: f32, - pub d: f32, - pub e: f32, - pub f: f32, -} - -impl Matrix { - pub fn identity() -> Self { - Self { - a: 1., - b: 0., - c: 0., - d: 1., - e: 0., - f: 0., - } - } -} - #[derive(Debug, Clone)] #[allow(dead_code)] pub struct Shape { @@ -103,12 +83,7 @@ impl Shape { } pub fn set_transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) { - self.transform.a = a; - self.transform.b = b; - self.transform.c = c; - self.transform.d = d; - self.transform.e = e; - self.transform.f = f; + self.transform = Matrix::new(a, b, c, d, e, f); } pub fn set_opacity(&mut self, opacity: f32) { @@ -127,18 +102,6 @@ impl Shape { self.children.clear(); } - pub fn translation(&self) -> (f32, f32) { - (self.transform.e, self.transform.f) - } - - pub fn scale(&self) -> (f32, f32) { - (self.transform.a, self.transform.d) - } - - pub fn skew(&self) -> (f32, f32) { - (self.transform.c, self.transform.b) - } - pub fn fills(&self) -> std::slice::Iter { self.fills.iter() } diff --git a/render-wasm/src/shapes/matrix.rs b/render-wasm/src/shapes/matrix.rs new file mode 100644 index 000000000..bd353b910 --- /dev/null +++ b/render-wasm/src/shapes/matrix.rs @@ -0,0 +1,61 @@ +use skia_safe as skia; + +#[derive(Debug, Clone, Copy)] +pub struct Matrix { + pub a: f32, + pub b: f32, + pub c: f32, + pub d: f32, + pub e: f32, + pub f: f32, +} + +impl Matrix { + pub fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self { + Self { a, b, c, d, e, f } + } + + pub fn identity() -> Self { + Self { + a: 1., + b: 0., + c: 0., + d: 1., + e: 0., + f: 0., + } + } + + pub fn to_skia_matrix(&self) -> skia::Matrix { + let mut res = skia::Matrix::new_identity(); + + let (translate_x, translate_y) = self.translation(); + let (scale_x, scale_y) = self.scale(); + let (skew_x, skew_y) = self.skew(); + res.set_all( + scale_x, + skew_x, + translate_x, + skew_y, + scale_y, + translate_y, + 0., + 0., + 1., + ); + + res + } + + fn translation(&self) -> (f32, f32) { + (self.e, self.f) + } + + fn scale(&self) -> (f32, f32) { + (self.a, self.d) + } + + fn skew(&self) -> (f32, f32) { + (self.c, self.b) + } +} diff --git a/render-wasm/src/shapes/renderable.rs b/render-wasm/src/shapes/renderable.rs index fa1144779..ef124c272 100644 --- a/render-wasm/src/shapes/renderable.rs +++ b/render-wasm/src/shapes/renderable.rs @@ -7,21 +7,7 @@ use crate::render::{ImageStore, Renderable}; impl Renderable for Shape { fn render(&self, surface: &mut skia_safe::Surface, images: &ImageStore) -> Result<(), String> { - let mut transform = skia::Matrix::new_identity(); - let (translate_x, translate_y) = self.translation(); - let (scale_x, scale_y) = self.scale(); - let (skew_x, skew_y) = self.skew(); - transform.set_all( - scale_x, - skew_x, - translate_x, - skew_y, - scale_y, - translate_y, - 0., - 0., - 1., - ); + let transform = self.transform.to_skia_matrix(); // Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc let center = self.selrect.center();