mirror of
https://github.com/penpot/penpot.git
synced 2025-05-18 07:16:15 +02:00
🎉 Render wasm fill images
This commit is contained in:
parent
c688ae2e33
commit
0a3c6f38ef
7 changed files with 237 additions and 38 deletions
|
@ -2,6 +2,7 @@ use skia_safe as skia;
|
|||
|
||||
use super::Color;
|
||||
use crate::math;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Gradient {
|
||||
|
@ -40,10 +41,19 @@ impl Gradient {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ImageFill {
|
||||
pub id: Uuid,
|
||||
pub alpha: u8,
|
||||
pub height: f32,
|
||||
pub width: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Fill {
|
||||
Solid(Color),
|
||||
LinearGradient(Gradient),
|
||||
Image(ImageFill),
|
||||
}
|
||||
|
||||
impl Fill {
|
||||
|
@ -57,6 +67,15 @@ impl Fill {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn new_image_fill(id: Uuid, alpha: u8, height: f32, width: f32) -> Self {
|
||||
Self::Image(ImageFill {
|
||||
id,
|
||||
alpha,
|
||||
height,
|
||||
width,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_paint(&self, rect: &math::Rect) -> skia::Paint {
|
||||
match self {
|
||||
Self::Solid(color) => {
|
||||
|
@ -75,6 +94,14 @@ impl Fill {
|
|||
p.set_blend_mode(skia::BlendMode::SrcOver);
|
||||
p
|
||||
}
|
||||
Self::Image(image_fill) => {
|
||||
let mut p = skia::Paint::default();
|
||||
p.set_style(skia::PaintStyle::Fill);
|
||||
p.set_anti_alias(true);
|
||||
p.set_blend_mode(skia::BlendMode::SrcOver);
|
||||
p.set_alpha(image_fill.alpha);
|
||||
p
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
52
render-wasm/src/shapes/images.rs
Normal file
52
render-wasm/src/shapes/images.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use crate::math;
|
||||
use skia_safe as skia;
|
||||
|
||||
pub type Image = skia::Image;
|
||||
|
||||
pub fn draw_image_in_container(
|
||||
canvas: &skia::Canvas,
|
||||
image: &Image,
|
||||
size: (f32, f32),
|
||||
container: skia::Rect,
|
||||
paint: &skia::Paint,
|
||||
) {
|
||||
let width = size.0;
|
||||
let height = size.1;
|
||||
let image_aspect_ratio = width / height;
|
||||
|
||||
// Container size
|
||||
let container_width = container.width();
|
||||
let container_height = container.height();
|
||||
let container_aspect_ratio = container_width / container_height;
|
||||
|
||||
// Calculate scale to ensure the image covers the container
|
||||
let scale = if image_aspect_ratio > container_aspect_ratio {
|
||||
// Image is widther, scale based on height to cover container
|
||||
container_height / height
|
||||
} else {
|
||||
// Image is taller, scale based on width to cover container
|
||||
container_width / width
|
||||
};
|
||||
|
||||
// Scaled size of the image
|
||||
let scaled_width = width * scale;
|
||||
let scaled_height = height * scale;
|
||||
|
||||
// Calculate offset to center the image in the container
|
||||
let offset_x = container.left + (container_width - scaled_width) / 2.0;
|
||||
let offset_y = container.top + (container_height - scaled_height) / 2.0;
|
||||
|
||||
let dest_rect = math::Rect::from_xywh(offset_x, offset_y, scaled_width, scaled_height);
|
||||
|
||||
// Save the current canvas state
|
||||
canvas.save();
|
||||
|
||||
// Set the clipping rectangle to the container bounds
|
||||
canvas.clip_rect(container, skia::ClipOp::Intersect, true);
|
||||
|
||||
// Draw the image with the calculated destination rectangle
|
||||
canvas.draw_image_rect(image, None, dest_rect, &paint);
|
||||
|
||||
// Restore the canvas to remove the clipping
|
||||
canvas.restore();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue