Skip to content

Add stroke_dasharray to Style struct #18

@Oddball777

Description

@Oddball777

I think this could be a very useful and very simple addition. I've succeeded at implementing it by:

  • adding a new field in the Style struct
  • adding new behaviour in the Default and Display implementations for Style
  • adding a new with_stroke_dasharray function to the Svg impl.

I haven't really checked if that's all that's necessary but the feature seems pretty simple to add and it seems to work pretty well. I'll put the modified code in the comments. If this is something that you'd like implemented, I think it could also be worth considering adding more options like stroke line-cap and stroke-linejoin. If the Style struct grows too big, maybe some of the stroke parameters could be bundled together into a struct of their own. Dashes are something that's really lacking in most svg crates in Rust. I can submit a PR if you think this is a good idea.

Here's an example of what some shapes with a dashed stroke look like (generated with the modified Style struct):

use geo::{Coord, Point, Triangle};
use geo_svg::{Color, ToSvg};

let point = geo::Point::new(0.0, 0.0);
    let triangle = geo::Triangle::new(
        Coord { x: -20, y: 10 },
        Coord { x: 0, y: -10 },
        Coord { x: 20, y: 10 },
    );

    let svg_point = point
        .to_svg()
        .with_radius(40.0)
        .with_fill_opacity(0.0)
        .with_stroke_color(Color::Named("blue"))
        .with_stroke_width(1.0)
        .with_stroke_dasharray(vec![2.0, 1.0, 3.0, 1.0]); // 2 units on, 1 unit off, 3 units on, 1 unit off

    let svg_triangle = triangle
        .to_svg()
        .with_fill_opacity(0.5)
        .with_color(Color::Named("red"))
        .with_stroke_width(0.3)
        .with_stroke_dasharray(vec![1.0, 1.0]); // 1 unit on, 1 unit off

    let svg = svg_point.and(svg_triangle);

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions