1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
//! This crate provides a fast and effective way to interact with SVG's using WebAssembly. //! It is able to: //! * Declare shapes and styles for these shapes for later use //! * Render these shapes to the DOM using defintions //! * Automatically detect if two shapes are the same, so only one defintion will get added to the DOM //! * Declare named items for later adjustments //! //! # Note //! This crate is still under development, but most API calls for 1.0.0 are completed. //! If any bugs are found please submit a issue or a pull request at: //! [GitHub](https://github.com/coastalwhite/WasmSVGGraphics) //! //! # Roadmap //! * Create more options for shapes, and create a easier way to declare these //! * Create more styling options and make these dynamic with subshapes //! //! # Examples //! ## Basics (How to render a circle) //! ``` //! use wasm_svg_graphics::figures; //! use geom_2d::point::Point; //! use wasm_svg_graphics::renderer::Renderer; //! //! // Declare renderer (must be mutable) into a parent container //! let mut renderer = Renderer::new("svg_parent_id") //! .expect("Failed to create renderer!"); //! //! // Generate circle //! let circle = figures::preset::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, &Point::new(20, 20)); //! ``` //! //! As one can see, it's not that difficult render a circle to the svg //! //! ## Basics (How to render a custom shape) //! ``` //! use wasm_svg_graphics::figures::Figure; //! use wasm_svg_graphics::figures::shape::*; //! use wasm_svg_graphics::figures::circle::CircleProps; //! use wasm_svg_graphics::figures::path::PathProps; //! use wasm_svg_graphics::figures::sub_path::SubPath; //! use geom_2d::point::Point; //! use wasm_svg_graphics::renderer::Renderer; //! //! // Declare renderer (must be mutable) into a parent container //! let mut renderer = Renderer::new("svg_parent_id") //! .expect("Failed to create renderer!"); //! //! let style = ShapeStyle::new_from_default(); //! //! // Generate smiley //! let smiley = Figure::new(vec!( //! // Head //! (Shape::new(style, SubShape::Circle(CircleProps::new(20))), Point::new(0, 0)), //! //! // Left eye //! (Shape::new(style, SubShape::Circle(CircleProps::new(1))), Point::new(-3, -3)), //! //! // Right eye //! (Shape::new(style, SubShape::Circle(CircleProps::new(1))), Point::new(3, -3)), //! //! // Mouth //! (Shape::new(style, SubShape::Path(PathProps::new( //! Point::new(-5, 0), // Beginning point //! vec!(SubPath::new_bezier_curve( // Create a new curve //! Point::new(-2, 2), // Control point 1 //! Point::new(2, 2), // Control point 2 //! Point::new(5, 0) // Ending point //! )), //! false // Is path closed? //! ))), Point::new(0, 5)), //! )); //! //! // Render smiley (since it's the first time of rendering this shape, //! // the renderer will add the shape's definition) //! renderer.render(&smiley, &Point::new(25, 25)); //! ``` //! //! Declaring custom figures is maybe somewhat of a cumbersome tasks but most definitely worth it! //! //! ## Basics (How to render with custom style) //! Let's use the smiley example from before, but now color it yellow //! ``` //! use wasm_svg_graphics::figures::Figure; //! use wasm_svg_graphics::figures::shape::*; //! use wasm_svg_graphics::figures::circle::CircleProps; //! use wasm_svg_graphics::figures::path::PathProps; //! use wasm_svg_graphics::figures::sub_path::SubPath; //! use geom_2d::point::Point; //! use wasm_svg_graphics::renderer::Renderer; //! use wasm_svg_graphics::color::Color; //! //! // Declare renderer (must be mutable) into a parent container //! let mut renderer = Renderer::new("svg_parent_id") //! .expect("Failed to create renderer!"); //! //! // Create head style //! let mut yellow_stroke = ShapeStyle::new_from_default(); //! //! // Assign a yellow stroke color //! yellow_stroke.add_style( //! AttributeField::StrokeColor, //! Color::new(255,255,0).to_string() //! ); //! //! // Create eye style //! let mut black_fill = ShapeStyle::new_from_default(); //! //! // Assign a yellow stroke color //! black_fill.add_style( //! AttributeField::FillColor, //! Color::new(0,0,0).to_string() //! ); //! //! // Create head style //! let mut red_stroke = ShapeStyle::new_from_default(); //! //! // Assign a yellow stroke color //! red_stroke.add_style( //! AttributeField::StrokeColor, //! Color::new(255,0,0).to_string() //! ); //! //! // Generate smiley //! let smiley = Figure::new(vec!( //! // Head //! (Shape::new(yellow_stroke, SubShape::Circle(CircleProps::new(20))), Point::new(0, 0)), //! //! // Left eye //! (Shape::new(black_fill, SubShape::Circle(CircleProps::new(3))), Point::new(5, 5)), //! //! // Right eye //! (Shape::new(black_fill, SubShape::Circle(CircleProps::new(3))), Point::new(15, 5)), //! //! // Mouth //! (Shape::new(red_stroke, SubShape::Path(PathProps::new( //! Point::new(0, 0), // Beginning point //! vec!(SubPath::new_bezier_curve( // Create a new curve //! Point::new(10, 20), // Control point 1 //! Point::new(10, 20), // Control point 2 //! Point::new(15, 15) // Ending point //! )), //! false // Is path closed? //! ))), Point::new(5, 15)), //! )); //! //! // Render smiley (since it's the first time of rendering this shape, //! // the renderer will add the shape's definition) //! renderer.render(&smiley, &Point::new(25, 25)); //! ``` use crate::errors::RendererError; use crate::errors::DomError::*; use crate::errors::RendererError::*; /// Container for the actual renderer object, this includes all logic for adding items to the DOM and for detecting duplication pub mod renderer; /// Container for the figures, this includes all definitions for shapes and styling pub mod figures; /// Container with the color object, and all it's logic pub mod color; /// Container with all the errors, mostly used internally pub mod errors; const NAME_ID_PREFIX: &str = "named"; const SHAPE_ID_PREFIX: &str = "shape"; const SVG_NS: &str = "http://www.w3.org/2000/svg"; fn get_document() -> Result<web_sys::Document, RendererError> { let window = web_sys::window() .ok_or(Dom(NoWindow))?; window.document() .ok_or(Dom(NoDocument)) } fn create_element_ns(namespace: &str, name: &str) -> Result<web_sys::Element, RendererError> { get_document()? .create_element_ns ( Some(namespace), name ) .map_err(|_| Dom(UncreatableNSElement)) }