Skip to main content

DrawEngine

Struct DrawEngine 

Source
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

Source

pub fn new(width: u32, height: u32, pixel_ratio: f32) -> Self

Create a new engine with the given canvas dimensions and device pixel ratio.

Source

pub fn set_size(&mut self, width: u32, height: u32)

Update canvas dimensions (e.g. on window resize).

Source

pub fn load_document(&mut self, json: &str) -> bool

Load a document from a JSON string.

Source

pub fn save_document(&self) -> String

Serialize the current document to JSON.

Source

pub fn render(&self) -> Vec<u8>

Render the current state and return RGBA pixel data.

Source

pub fn render_width(&self) -> u32

Return the width of the rendered pixmap in physical pixels.

Source

pub fn render_height(&self) -> u32

Return the height of the rendered pixmap in physical pixels.

Source

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":..}]

Source

pub fn hit_test(&self, screen_x: f32, screen_y: f32) -> String

Hit test at screen coordinates. Returns element ID or empty string.

Source

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.

Source

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.

Source

pub fn set_viewport(&mut self, scroll_x: f64, scroll_y: f64, zoom: f64)

Set the viewport (scroll and zoom).

Source

pub fn screen_to_world(&self, sx: f64, sy: f64) -> String

Convert screen coordinates to world coordinates. Returns JSON {"x":...,"y":...}.

Source

pub fn scroll_x(&self) -> f64

Source

pub fn scroll_y(&self) -> f64

Source

pub fn zoom(&self) -> f64

Source

pub fn set_selection(&mut self, ids_json: &str)

Set the selected element IDs (JSON array of strings).

Source

pub fn get_selection(&self) -> String

Get the current selection as a JSON array of strings.

Source

pub fn set_selection_box(&mut self, x: f64, y: f64, w: f64, h: f64)

Set the rubber-band selection box (screen coordinates).

Source

pub fn clear_selection_box(&mut self)

Clear the rubber-band selection box.

Source

pub fn add_element(&mut self, json: &str) -> String

Add an element from JSON. Returns the element ID, or empty string on failure.

Source

pub fn remove_element(&mut self, id: &str) -> bool

Remove an element by ID. Returns true if the element existed.

Source

pub fn move_element(&mut self, id: &str, x: f64, y: f64)

Move an element to absolute position (x, y).

Source

pub fn resize_element(&mut self, id: &str, x: f64, y: f64, w: f64, h: f64)

Resize an element to the given bounds.

Source

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.

Source

pub fn get_element(&self, id: &str) -> String

Get an element as JSON, or empty string if not found.

Source

pub fn undo(&mut self) -> bool

Undo the last action. Returns true if something was undone.

Source

pub fn redo(&mut self) -> bool

Redo the last undone action. Returns true if something was redone.

Source

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.

Source

pub fn can_undo(&self) -> bool

Source

pub fn can_redo(&self) -> bool

Source

pub fn select_all(&mut self)

Select all elements (skipping bound text — they follow parent shapes).

Source

pub fn clear_selection(&mut self)

Clear the selection.

Source

pub fn add_to_selection(&mut self, id: &str)

Add an element to the selection.

Source

pub fn remove_from_selection(&mut self, id: &str)

Remove an element from the selection.

Source

pub fn is_selected(&self, id: &str) -> bool

Check if an element is selected.

Source

pub fn remove_elements(&mut self, ids_json: &str)

Remove multiple elements by ID (JSON array). Pushes a batch undo action.

Source

pub fn get_all_element_ids(&self) -> String

Get all element IDs as a JSON array.

Source

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.

Source

pub fn reorder_to_front(&mut self, id: &str)

Move an element to the front (top of draw order).

Source

pub fn reorder_to_back(&mut self, id: &str)

Move an element to the back (bottom of draw order).

Source

pub fn reorder_forward(&mut self, id: &str)

Move an element one position forward in draw order.

Source

pub fn reorder_backward(&mut self, id: &str)

Move an element one position backward in draw order.

Source

pub fn set_show_grid(&mut self, show: bool)

Show or hide the grid.

Source

pub fn get_document_json_for_save(&self) -> String

Get the full document JSON with updated modified_at for saving.

Source

pub fn set_document_id(&mut self, id: &str)

Source

pub fn document_id(&self) -> String

Source

pub fn set_created_at(&mut self, ts: &str)

Source

pub fn document_name(&self) -> String

Source

pub fn set_document_name(&mut self, name: &str)

Source

pub fn element_count(&self) -> usize

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.