handwritin: Grow and shrink the canvas without refreshing

This commit is contained in:
2025-06-23 21:50:10 +02:00
parent 61669e15bd
commit eaf0c3cb55
2 changed files with 33 additions and 26 deletions

View File

@ -18,6 +18,7 @@ const CHUNK_SIZE: usize = 64;
/// Rasterize onto a resizeable canvas.
#[derive(Default)]
pub struct CanvasRasterizer {
image_size: [usize; 2],
tiles: HashMap<[usize; 2], Tile>,
}
@ -49,12 +50,30 @@ impl Tile {
}
impl CanvasRasterizer {
//pub fn clear(&mut self) {
// self.image = ColorImage::new(self.image.size, Color32::TRANSPARENT);
// self.texture_is_dirty = true;
//}
pub fn set_size(&mut self, width: usize, height: usize) {
self.image_size = [width, height];
self.populate_tiles();
}
pub fn clear(&mut self) {
log::error!("clearing all tiles");
self.tiles.clear();
self.populate_tiles();
}
pub fn clear_and_set_size(&mut self, width: usize, height: usize) {
self.image_size = [width, height];
self.clear();
}
fn populate_tiles(&mut self) {
let [width, height] = self.image_size;
// discard tiles that are out of bounds
self.tiles.retain(|_, tile| {
tile.bounding_box.x_from <= width && tile.bounding_box.y_from <= height
});
let chunk = |max: usize| {
(0..)
.step_by(CHUNK_SIZE)
@ -62,6 +81,7 @@ impl CanvasRasterizer {
.enumerate()
};
// create new tiles where we need them
for (xi, _x) in chunk(width) {
for (yi, _y) in chunk(height) {
self.tiles
@ -71,13 +91,6 @@ impl CanvasRasterizer {
}
}
pub fn clear_and_set_size(&mut self, width: usize, height: usize) {
log::error!("clearing all tiles");
self.tiles.clear();
self.set_size(width, height);
}
pub fn rasterize<'a>(
&mut self,
point_to_pixel: TSTransform,