wasm_svg_graphics/
default.rs

1//! Contains some easy and nice ways to create definitions and shapes to render
2
3use svg_definitions::prelude::*;
4
5/// Creates a default circle with a certain radius
6pub fn circle(radius: i32) -> SVGElem {
7    SVGElem::new(Tag::Circle)
8        .set(Attr::R, radius)
9        .set(Attr::Stroke, "#000000")
10        .set(Attr::StrokeWidth, 1)
11        .set(Attr::Fill, "transparent")
12        .set(Attr::Cx, 0)
13        .set(Attr::Cy, 0)
14}
15
16/// Creates a default rectangle with a certain width and height
17pub fn rect(width: i32, height: i32) -> SVGElem {
18    SVGElem::new(Tag::Rect)
19        .set(Attr::Width, width)
20        .set(Attr::Height, height)
21        .set(Attr::Stroke, "#000000")
22        .set(Attr::StrokeWidth, 1)
23        .set(Attr::Fill, "transparent")
24}
25
26/// Creates a default curve with control points 1 and 2 and an end point
27pub fn curve(
28    sx: i32,
29    sy: i32,
30    ex: i32,
31    ey: i32,
32    cx1: i32,
33    cy1: i32,
34    cx2: i32,
35    cy2: i32,
36) -> SVGElem {
37    SVGElem::new(Tag::Path).set(
38        Attr::D,
39        PathData::new().move_to(as_point_2d((sx, sy))).curve_to(
40            as_point_2d((ex, ey)),
41            as_point_2d((cx1, cy1)),
42            as_point_2d((cx2, cy2)),
43        ),
44    )
45}
46
47/// Creates a polygon from a vector of points
48pub fn polygon(points: Vec<(i32, i32)>) -> SVGElem {
49    let mut path_string = PathData::new().move_to(as_point_2d(points[0]));
50
51    points[1..]
52        .iter()
53        .for_each(|point| path_string = path_string.clone().line_to(as_point_2d(*point)));
54
55    SVGElem::new(Tag::Path)
56        .set(Attr::D, path_string)
57        .set(Attr::StrokeWidth, 1)
58        .set(Attr::Stroke, "#000000")
59}
60
61/// Sets the location of SVG elem (for circles use [set_circle_loc](#set_circle_loc))
62pub fn set_loc(elem: SVGElem, x: i32, y: i32) -> SVGElem {
63    elem.set(Attr::X, x).set(Attr::Y, y)
64}
65
66/// Sets the location of SVG Circle (for non circles use [set_loc](#set_loc))
67pub fn set_circle_loc(elem: SVGElem, x: i32, y: i32) -> SVGElem {
68    elem.set(Attr::Cx, x).set(Attr::Cy, y)
69}
70
71fn as_point_2d(point: (i32, i32)) -> Point2D {
72    (point.0 as f32, point.1 as f32)
73}