Commit 20d936113ffbf3224e962f4ce2761a53a9ac48f2
1 parent
e64f5307
Keep aspect ratio with upload previews
Showing
1 changed file
with
19 additions
and
2 deletions
... | ... | @@ -21,11 +21,28 @@ pub(super) async fn upload_preview_logic( mut rx_canvas :broadcast::Receiver<Dom |
21 | 21 | let context = canvas |
22 | 22 | . get_context("2d").unwrap().unwrap() |
23 | 23 | . dyn_into::<CanvasRenderingContext2d>().unwrap(); |
24 | + let image_width = upload.bitmap().width() as f64; | |
25 | + let image_height = upload.bitmap().height() as f64; | |
26 | + let canvas_width = canvas.width() as f64; | |
27 | + let canvas_height = canvas.height() as f64; | |
28 | + | |
29 | + /* scale with aspect ratio */ | |
30 | + let (ox, oy, width, height) = if image_width > image_height { | |
31 | + let f = canvas_width / image_width; | |
32 | + let dest_height = image_height * f; | |
33 | + let o_y = (canvas_height - dest_height) / 2.0; | |
34 | + (0.0, o_y, canvas_width, dest_height) | |
35 | + } else { | |
36 | + let f = canvas_height / image_height; | |
37 | + let dest_width = image_width * f; | |
38 | + let o_x = (canvas_width - dest_width) / 2.0; | |
39 | + (o_x, 0.0, dest_width, canvas_height) | |
40 | + }; | |
41 | + | |
24 | 42 | context |
25 | 43 | . draw_image_with_image_bitmap_and_dw_and_dh( |
26 | 44 | &upload.bitmap() |
27 | - , 0.0, 0.0 | |
28 | - , canvas.width() as f64, canvas.height() as f64 ) | |
45 | + , ox, oy, width, height ) | |
29 | 46 | . unwrap(); |
30 | 47 | } |
31 | 48 | } | ... | ... |
Please
register
or
login
to post a comment