| Crates.io | svgwriter |
| lib.rs | svgwriter |
| version | 0.1.1 |
| created_at | 2022-09-25 14:38:50.919652+00 |
| updated_at | 2026-01-05 19:17:09.825902+00 |
| description | Typed SVG Writer |
| homepage | |
| repository | https://codeberg.org/msrd0/svgwriter |
| max_upload_size | |
| id | 673646 |
| size | 881,406 |
svgwriter is a typed library for writing correct SVG files. It includes SVG
specification and documentation from mdn.
use svgwriter::{
tags::{Path, TagWithPresentationAttributes as _},
Data, Graphic
};
let mut svg = Graphic::new();
let size = 100;
svg.set_width(size);
svg.set_height(size);
svg.set_view_box(format!("0 0 {size} {size}"));
// draw a heart, inspired by https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#example
let d = 40;
let padding = size / 2 - d;
let mut heart = Data::new();
heart
.move_to(padding, padding + d / 2)
.arc_by(d / 2, d / 2, 0, false, true, d, 0)
.arc_by(d / 2, d / 2, 0, false, true, d, 0)
.quad_by(0, d * 3 / 4, -d, d * 3 / 2)
.quad_by(-d,-d * 3 / 4, -d, -d * 3 / 2);
svg.push(
Path::new()
.with_d(heart)
.with_fill("#A919FA")
.with_fill_opacity(0.5)
.with_stroke("#A919FA")
.with_stroke_width(3)
);
// write the svg to a file
write!(file, "{}", svg.to_string());
This code produces the following image: