Struct wasm_svg_graphics::renderer::Renderer[][src]

pub struct Renderer { /* fields omitted */ }
Expand description

Container object used to interact with the SVG Object Keeps track of definitions and dom root id

Implementations

Create new renderer object

Arguments

  • dom_root_id - The HTMl Attribute ID of the parent element of to be created SVG

Creates renderer object from svg element

Note

  • This function will panic if the root of the svg_elem is not a TagName::Svg

Arguments

  • dom_root_id - The HTMl Attribute ID of the parent element of to be created SVG
  • svg_elem - The svg element to from the renderer from

Render figure at a location (this will automatically add a definition when needed)

Arguments

  • figure - Figure object, used when adding to the dom
  • location - the location where to add the figure

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render(circle, (20.0, 20.0));

Render a named figure at a location (this will automatically add a definition when needed) One can use named-figures for later adjustments, e.g. updating the location of the figure, hiding/showing the figure

Arguments

  • name - Name to use for later reference
  • figure - Figure object, used when adding to the dom
  • location - the location where to add the figure

Panics

Cannot create duplicate names or declare a name more than once. When an attempt is made to declare a name more than once, a panic will occur. One is able to check if a name already exists with the does_name_exist method.

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", circle, (10.0, 10.0));

// --snip

// Updates the named figure's location to (20,20)
renderer.move_named("named_circle", (20.0, 20.0));

Render figure from a previously added definition at a location (this will automatically add a definition when needed)

Arguments

  • figure_id - 8 byte hash of the figure used when adding to the dom
  • location - the location where to add the figure

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

let circle_id = renderer.define_render(circle);

// Render circle
renderer.render_id(circle_id, (20.0, 20.0));

Render named figure from a previously added definition at a location (this will automatically add a definition when needed)

Arguments

  • name - Name to use for later reference
  • figure_id - 8 byte hash of the figure used when adding to the dom
  • location - the location where to add the figure

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

let circle_id = renderer.define_render(circle);

// Render circle
renderer.render_named_id("named_circle", circle_id, (20.0, 20.0));

// --snip

// Updates the Circle's location
renderer.move_named("named_circle", (25.0, 25.0));

Define a figure and return it’s hash, this hash can later be used for rendering

Arguments

  • figure - Figure object, used when adding to the dom

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Define the render
let circle_id = renderer.define_render(circle);

// Render circle
renderer.render_id(circle_id, (20.0, 20.0));

Clears all elements within the SVG element and clears all internal definitions. Basically reinits the renderer.

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", SVGDefault::circle(10), (10.0, 10.0));

// --snip

// Reinit the Renderer object
renderer.clear();

// Renders the circle with "named_circle" name again.
// This would normally panic, since a name is redeclared,
// but since the renderer is cleared, it will not. :)
renderer.render_named("named_circle", SVGDefault::circle(10), (20.0, 20.0));

Clears all figures/containers within a named container, but does not clear up definitions.

Arguments

  • container_name - The name of the container to clear

Panics

Will panic when a name passed in is in use by a pure figure. For pure figures use delete_named or hide_named instead.

Note

Most of the time the update_named method is a better alternative. Also have a look at hide_named and move_named

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Adds the named container 'named_container' to the svg root
renderer.create_named_container("named_container", "root");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.append_to_container("named_container", SVGDefault::circle(10), (10.0, 10.0));

// Now the container contains the circle figure

// --snip

// Clear the named container
renderer.clear_named_container("named_container");

// Now the container is empty again

// Render circle in the named_container.
// Since definitions were not cleared, it will use a previous definition.
// This saves some processing time in this case.
renderer.append_to_container("named_container", SVGDefault::circle(10), (20.0, 20.0));

// Now the container contains the circle at a different position

Updates a named container or figure to either contain the passed figure or become the passed figure, respectively.

Arguments

  • name - The name of either a named container or a named figure
  • figure - Figure object, used when adding to the dom
  • location - the location where to add the figure

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Adds the named container 'named_container' to the svg root
renderer.create_named_container("named_container", "root");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.append_to_container("named_container", SVGDefault::circle(10), (10.0, 10.0));

// Now the container contains the circle figure

// --snip

// Update the contents of the named container
renderer.update_named("named_container", SVGDefault::circle(10), (20.0, 20.0));

// Now the container contains the circle at a different position

Updates a named container or figure to either contain the passed figure or become the passed figure from the id, respectively.

Arguments

  • name - The name of either a named container or a named figure
  • figure_id - id of Figure definition used when adding to the dom, defined using define_render
  • location - the location where to add the figure

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Adds the named container 'named_container' to the svg root
renderer.create_named_container("named_container", "root");

let circle_id = renderer.define_render(SVGDefault::circle(10));

// Render circle
renderer.append_to_container("named_container", SVGDefault::circle(10), (10.0, 10.0));

// Now the container contains the circle figure

// --snip

// Update the contents of the named container
renderer.update_named_with_id("named_container", circle_id, (20.0, 20.0));

// Now the container contains the circle at a different position

Hides a named item in the DOM, this can be undone by the show_named method.

Arguments

  • name - Name of item to hide

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", circle, (10.0, 10.0));

// --snip

// Hides the named figure
renderer.hide_named("named_circle");

Shows a named item in the DOM, this undoes the hide_named method.

Arguments

  • name - Name of item to show

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", circle, (10.0, 10.0));

// Hides the named figure
renderer.hide_named("named_circle");

// --snip

// Show the named figure again
renderer.show_named("named_circle");

Appends a figure to a named container

Arguments

  • name - The name of either a named container
  • figure - Figure object, used when adding to the dom
  • location - the location where to add the figure

Panics

Will panic when a name passed in is in use by a pure figure.

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Adds the named container 'named_container' to the svg root
renderer.create_named_container("named_container", "root");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.append_to_container("named_container", circle, (10.0, 10.0));

// Now the container contains the circle figure

Appends a figure from id to a named container

Arguments

  • name - The name of either a named container
  • figure_id - id of Figure definition used when adding to the dom, defined using define_render
  • location - the location where to add the figure

Panics

Will panic when a name passed in is in use by a pure figure.

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Adds the named container 'named_container' to the svg root
renderer.create_named_container("named_container", "root");

let circle_id = renderer.define_render(SVGDefault::circle(10));

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.append_to_container_with_id("named_container", circle_id, (10.0, 10.0));

// Now the container contains the circle figure

Deletes a named item from the DOM and from internal entries. But will not delete definitions

Arguments

  • name - Name of item to delete

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", SVGDefault::circle(10), (10.0, 10.0));

// --snip

// Delete the named figure
renderer.delete_named("named_circle");

// Renders the circle with "named_circle" name again.
// This would normally panic, since a name is redeclared,
// but since the named figure is deleted, it will not. :)
renderer.render_named("named_circle", SVGDefault::circle(10), (20.0, 20.0));

Will return if a certain name exists and therefore cannot be used for a declaration.

Arguments

  • ‘name’ - Name to check

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Will be set to false
let does_named_circle_exist = renderer.does_name_exist("named_circle");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", SVGDefault::circle(10), (10.0, 10.0));

// Will be set to true
let does_named_circle_exist = renderer.does_name_exist("named_circle");

// Delete the named figure
renderer.delete_named("named_circle");

// Will be set to false
let does_named_circle_exist = renderer.does_name_exist("named_circle");

// Renders the circle with "named_circle" name again.
// This would normally panic, since a name is redeclared,
// but since the named figure is deleted, it will not. :)
renderer.render_named("named_circle", SVGDefault::circle(10), (20.0, 20.0));

// Will be set to true
let does_named_circle_exist = renderer.does_name_exist("named_circle");

Creates a new named container in the parent

Arguments

  • name - Name of the named container used for later reference
  • parent - Name a container, which to use as parent (“root” is used for the SVG root)

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Adds the named container 'named_container' to the svg root
renderer.create_named_container("named_container", "root");

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.append_to_container("named_container", circle, (10.0, 10.0));

Moves a named figure to a given location

Arguments

  • name - Name of the named figure to move
  • loc - Location to move the figure to

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", circle, (10.0, 10.0));

// --snip

// Moves the named figure to a new location
renderer.move_named("named_circle", (5.0, 5.0));

Will return whether a given name is used for a named container, instead of a pure figure

Arguments

  • name - Name to check

Note

Will output false if the name is not in use

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer (must be mutable)
let mut renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Generate circle
let circle = SVGDefault::circle(10);

// Render circle (since it's the first time of rendering this shape,
// the renderer will add the shape's definition)
renderer.render_named("named_circle", circle, (10.0, 10.0));

// Create a named container
renderer.create_named_container("named_container", "root");

println!("{}", renderer.is_container("named_circle")); // false
println!("{}", renderer.is_container("named_container")); // true
println!("{}", renderer.is_container("not_in_use_name")); // false

Adjusts the viewbox of the svg

Arguments

  • x - The top-left x-coordinate of the viewbox
  • y - The top-left y-coordinate of the viewbox
  • width - The width of the viewbox
  • height - The height of the viewbox

Note

By default this is set to DEFAULT_VIEWBOX.

Examples

use wasm_svg_graphics::prelude::*;

// Declare renderer
let renderer = SVGRenderer::new("svg_parent_id")
    .expect("Failed to create renderer!");

// Adjust the viewbox
renderer.adjust_viewbox(0, 0, 50, 50);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.