pub struct DrawEngine { /* private fields */ }Expand description
The main WASM-facing engine. Holds all state needed for rendering and interaction: document, renderer, viewport, selection, and undo history.
Implementations§
Source§impl DrawEngine
impl DrawEngine
Sourcepub fn new(width: u32, height: u32, pixel_ratio: f32) -> Self
pub fn new(width: u32, height: u32, pixel_ratio: f32) -> Self
Create a new engine with the given canvas dimensions and device pixel ratio.
Sourcepub fn set_size(&mut self, width: u32, height: u32)
pub fn set_size(&mut self, width: u32, height: u32)
Update canvas dimensions (e.g. on window resize).
Sourcepub fn load_document(&mut self, json: &str) -> bool
pub fn load_document(&mut self, json: &str) -> bool
Load a document from a JSON string.
Sourcepub fn save_document(&self) -> String
pub fn save_document(&self) -> String
Serialize the current document to JSON.
Sourcepub fn render_width(&self) -> u32
pub fn render_width(&self) -> u32
Return the width of the rendered pixmap in physical pixels.
Sourcepub fn render_height(&self) -> u32
pub fn render_height(&self) -> u32
Return the height of the rendered pixmap in physical pixels.
Sourcepub fn get_text_overlays(&self) -> String
pub fn get_text_overlays(&self) -> String
Get text overlay data for browser-native text rendering.
Returns JSON array of text elements with screen-space positions:
[{"x":..,"y":..,"text":"..","fontSize":..,"fontFamily":"..","align":"..","color":"..","opacity":..,"width":..,"height":..}]
Sourcepub fn hit_test(&self, screen_x: f32, screen_y: f32) -> String
pub fn hit_test(&self, screen_x: f32, screen_y: f32) -> String
Hit test at screen coordinates. Returns element ID or empty string.
Sourcepub fn hit_test_handle(&self, screen_x: f32, screen_y: f32) -> String
pub fn hit_test_handle(&self, screen_x: f32, screen_y: f32) -> String
Hit test for resize handles. Returns JSON {"id":"...","handle":"NorthWest"}
or empty string if no handle hit.
Sourcepub fn elements_in_rect(&self, x: f64, y: f64, w: f64, h: f64) -> String
pub fn elements_in_rect(&self, x: f64, y: f64, w: f64, h: f64) -> String
Get element IDs within a world-coordinate rectangle. Returns JSON array.
Sourcepub fn set_viewport(&mut self, scroll_x: f64, scroll_y: f64, zoom: f64)
pub fn set_viewport(&mut self, scroll_x: f64, scroll_y: f64, zoom: f64)
Set the viewport (scroll and zoom).
Sourcepub fn screen_to_world(&self, sx: f64, sy: f64) -> String
pub fn screen_to_world(&self, sx: f64, sy: f64) -> String
Convert screen coordinates to world coordinates. Returns JSON {"x":...,"y":...}.
pub fn scroll_x(&self) -> f64
pub fn scroll_y(&self) -> f64
pub fn zoom(&self) -> f64
Sourcepub fn set_selection(&mut self, ids_json: &str)
pub fn set_selection(&mut self, ids_json: &str)
Set the selected element IDs (JSON array of strings).
Sourcepub fn get_selection(&self) -> String
pub fn get_selection(&self) -> String
Get the current selection as a JSON array of strings.
Sourcepub fn set_selection_box(&mut self, x: f64, y: f64, w: f64, h: f64)
pub fn set_selection_box(&mut self, x: f64, y: f64, w: f64, h: f64)
Set the rubber-band selection box (screen coordinates).
Sourcepub fn clear_selection_box(&mut self)
pub fn clear_selection_box(&mut self)
Clear the rubber-band selection box.
Sourcepub fn add_element(&mut self, json: &str) -> String
pub fn add_element(&mut self, json: &str) -> String
Add an element from JSON. Returns the element ID, or empty string on failure.
Sourcepub fn remove_element(&mut self, id: &str) -> bool
pub fn remove_element(&mut self, id: &str) -> bool
Remove an element by ID. Returns true if the element existed.
Sourcepub fn move_element(&mut self, id: &str, x: f64, y: f64)
pub fn move_element(&mut self, id: &str, x: f64, y: f64)
Move an element to absolute position (x, y).
Sourcepub fn resize_element(&mut self, id: &str, x: f64, y: f64, w: f64, h: f64)
pub fn resize_element(&mut self, id: &str, x: f64, y: f64, w: f64, h: f64)
Resize an element to the given bounds.
Sourcepub fn update_element_style(&mut self, id: &str, style_json: &str) -> bool
pub fn update_element_style(&mut self, id: &str, style_json: &str) -> bool
Update an element’s style from JSON. The JSON should contain style fields (stroke, fill, font, opacity, etc.) to merge into the element.
Sourcepub fn get_element(&self, id: &str) -> String
pub fn get_element(&self, id: &str) -> String
Get an element as JSON, or empty string if not found.
Sourcepub fn redo(&mut self) -> bool
pub fn redo(&mut self) -> bool
Redo the last undone action. Returns true if something was redone.
Sourcepub fn push_action(&mut self, action_json: &str) -> bool
pub fn push_action(&mut self, action_json: &str) -> bool
Push a raw action for undo support. The action_json must match the Action enum serialization format.
pub fn can_undo(&self) -> bool
pub fn can_redo(&self) -> bool
Sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
Select all elements (skipping bound text — they follow parent shapes).
Sourcepub fn clear_selection(&mut self)
pub fn clear_selection(&mut self)
Clear the selection.
Sourcepub fn add_to_selection(&mut self, id: &str)
pub fn add_to_selection(&mut self, id: &str)
Add an element to the selection.
Sourcepub fn remove_from_selection(&mut self, id: &str)
pub fn remove_from_selection(&mut self, id: &str)
Remove an element from the selection.
Sourcepub fn is_selected(&self, id: &str) -> bool
pub fn is_selected(&self, id: &str) -> bool
Check if an element is selected.
Sourcepub fn remove_elements(&mut self, ids_json: &str)
pub fn remove_elements(&mut self, ids_json: &str)
Remove multiple elements by ID (JSON array). Pushes a batch undo action.
Sourcepub fn get_all_element_ids(&self) -> String
pub fn get_all_element_ids(&self) -> String
Get all element IDs as a JSON array.
Sourcepub fn get_elements_by_group(&self, group_id: &str) -> String
pub fn get_elements_by_group(&self, group_id: &str) -> String
Get elements with a specific group_id (for bound text). Returns JSON array of IDs.
Sourcepub fn reorder_to_front(&mut self, id: &str)
pub fn reorder_to_front(&mut self, id: &str)
Move an element to the front (top of draw order).
Sourcepub fn reorder_to_back(&mut self, id: &str)
pub fn reorder_to_back(&mut self, id: &str)
Move an element to the back (bottom of draw order).
Sourcepub fn reorder_forward(&mut self, id: &str)
pub fn reorder_forward(&mut self, id: &str)
Move an element one position forward in draw order.
Sourcepub fn reorder_backward(&mut self, id: &str)
pub fn reorder_backward(&mut self, id: &str)
Move an element one position backward in draw order.
Sourcepub fn set_show_grid(&mut self, show: bool)
pub fn set_show_grid(&mut self, show: bool)
Show or hide the grid.
Sourcepub fn get_document_json_for_save(&self) -> String
pub fn get_document_json_for_save(&self) -> String
Get the full document JSON with updated modified_at for saving.