Skip to content

Commit

Permalink
Fix rasterize function not rendering egui windows
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Oct 12, 2023
1 parent 33c24a8 commit 7ed8c66
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
7 changes: 5 additions & 2 deletions examples/rasterize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui_skia::rasterize;
use egui_skia::{rasterize, RasterizeOptions};
use skia_safe::{EncodedImageFormat, Paint, Point};
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -30,7 +30,10 @@ pub fn main() {
});
});
},
None,
Some(RasterizeOptions {
pixels_per_point: 1.0,
frames_before_screenshot: 2
}),
);

let data = surface
Expand Down
22 changes: 16 additions & 6 deletions src/egui_skia.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use crate::painter::Painter;
use std::time::Duration;

use egui::{Context, Pos2};
use skia_safe::{Canvas, Surface};
use std::time::Duration;

use crate::painter::Painter;

pub struct RasterizeOptions {
pub pixels_per_point: f32,
/// The number of frames to render before a screenshot is taken.
/// Default is 2, so egui will be able to display windows
pub frames_before_screenshot: usize,
}

impl Default for RasterizeOptions {
fn default() -> Self {
Self {
pixels_per_point: 1.0,
frames_before_screenshot: 2,
}
}
}
Expand All @@ -27,10 +33,13 @@ pub fn rasterize(

pub fn draw_onto_surface(
surface: &mut Surface,
ui: impl FnMut(&Context),
mut ui: impl FnMut(&Context),
options: Option<RasterizeOptions>,
) {
let RasterizeOptions { pixels_per_point } = options.unwrap_or_default();
let RasterizeOptions {
pixels_per_point,
frames_before_screenshot,
} = options.unwrap_or_default();
let mut backend = EguiSkia::new();

let input = egui::RawInput {
Expand All @@ -45,8 +54,9 @@ pub fn draw_onto_surface(
..Default::default()
};

backend.run(input, ui);

for _ in 0..frames_before_screenshot {
backend.run(input.clone(), &mut ui);
}
backend.paint(surface.canvas());
}

Expand Down

0 comments on commit 7ed8c66

Please sign in to comment.