Adjusting canvas view to fit a required region

I am writing an application that needs to map real-world coordinates to the screen space coordinate system of a canvas. I’ve been referencing the game of life example for how to scale and translate the canvas in response to user mouse events. That example uses members of the Grid struct to track the current scale multiplier and translation of the canvas, and reapplies these transformations every canvas::Program::draw call.

However, if I want my canvas to reflect a real-world coordinates region say starting at 0.0, 0.0 with a size of 2.0, 1.0, I’m not sure how to modify the scale and translation members to achieve this given the current shape of the canvas.

For reference, the relevant parts of the Grid struct look like:

pub struct Grid {
    ...
    translation: Vector,
    scale: f32,
    ...
}

and the place in canvas::Program::draw where these are applied looks like:

fn draw(&self, ...) -> Vec<Geometry> {
...
        frame.with_save(|frame| {
            frame.translate(center);
            frame.scale(self.scaling);
            frame.translate(self.translation);
            ...
        }
...
}

Any help or ideas are welcome! Thanks.

For anyone who may find this later, I was able to achieve what I wanted by maintaining a “Region” struct to record the visible region of my data that the canvas is displaying. The struct definition looks like:

struct Region {
    x: f32,
    y: f32,
    width: f32,
    height: f32
}

Then every time the canvas::Program::view trait method is called, you have to do the (simple) linear transformation between the visible Region and the current canvas extent and apply it with the frame.translate and frame.scale calls. Then when the update method sees a message for scaling or translating the view, it just modifies the visible Region instead and clears the cache.