Merge pull request #4 from Almamu/fix/image-scale-position
fix: scale up or down images that do not fit the screen
This commit is contained in:
@@ -535,6 +535,21 @@ impl Renderer {
|
|||||||
|
|
||||||
// Draw background
|
// Draw background
|
||||||
if let Some(ref background) = self.background {
|
if let Some(ref background) = self.background {
|
||||||
|
self.context.save().expect("Failed to save context");
|
||||||
|
let bg_width = background.width() as f64;
|
||||||
|
let bg_height = background.height() as f64;
|
||||||
|
let scale_x = self.width as f64 / bg_width;
|
||||||
|
let scale_y = self.height as f64 / bg_height;
|
||||||
|
|
||||||
|
// Preserve aspect ratio by using the larger scale factor (zoom to fill)
|
||||||
|
let scale = scale_x.max(scale_y);
|
||||||
|
|
||||||
|
// Calculate offsets to center the image
|
||||||
|
let offset_x = (self.width as f64 - bg_width * scale) / 2.0;
|
||||||
|
let offset_y = (self.height as f64 - bg_height * scale) / 2.0;
|
||||||
|
|
||||||
|
self.context.translate(offset_x, offset_y);
|
||||||
|
self.context.scale(scale, scale);
|
||||||
self.context.new_path();
|
self.context.new_path();
|
||||||
self.context
|
self.context
|
||||||
.set_source_surface(background, 0.0, 0.0)
|
.set_source_surface(background, 0.0, 0.0)
|
||||||
@@ -542,6 +557,7 @@ impl Renderer {
|
|||||||
self.context
|
self.context
|
||||||
.paint_with_alpha(self.fade_alpha)
|
.paint_with_alpha(self.fade_alpha)
|
||||||
.expect("Failed to paint");
|
.expect("Failed to paint");
|
||||||
|
self.context.restore().expect("Failed to restore context");
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.config.indicator {
|
if self.config.indicator {
|
||||||
|
|||||||
Reference in New Issue
Block a user