https://github.com/vincentlaucsb/svg
A simple header-only library for generating SVG files from C++
https://github.com/vincentlaucsb/svg
cpp svg
Last synced: 12 months ago
JSON representation
A simple header-only library for generating SVG files from C++
- Host: GitHub
- URL: https://github.com/vincentlaucsb/svg
- Owner: vincentlaucsb
- Created: 2018-02-11T23:55:12.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-18T20:58:29.000Z (over 2 years ago)
- Last Synced: 2025-03-24T11:21:33.731Z (about 1 year ago)
- Topics: cpp, svg
- Language: C++
- Size: 393 KB
- Stars: 19
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SVG for C++
[](https://travis-ci.org/vincentlaucsb/svg) [](https://codecov.io/gh/vincentlaucsb/svg)
## Purpose
This a header-only library for generating SVG files from a simple C++ interface. It can also perform non-trivial tasks such as calculating a bounding box for an SVG's elements, or merging several graphics together.
[Want to see more? Read the documentation.](https://vincentlaucsb.github.io/svg/)
## Basic Usage
```
#include "svg.hpp"
#include
int main() {
SVG::SVG root;
// Basic CSS support
root.style("circle").set_attr("fill", "#000000")
.set_attr("stroke", "#000000");
root.style("rect#my_rectangle").set_attr("fill", "red");
// Method 1 of adding elements - add_child<>()
auto shapes = root.add_child();
auto rect = shapes->add_child("my_rectangle");
// Method 2 of adding elements - operator<<
*shapes << SVG::Circle(-100, -100, 100) << SVG::Circle(100, 100, 100);
// Reference elements by id, tag, class name, etc...
root.get_element_by_id("my_rectangle")
->set_attr("x", 20).set_attr("y", 20)
.set_attr("width", 40).set_attr("height", 40);
std::cout << "There are " << root.get_children().size() <<
" circles." << std::endl;
// Automatically scale width and height to fit elements
root.autoscale();
// Output our drawing
std::ofstream outfile("my_drawing.svg");
outfile << std::string(root);
}
```
### Output
```
<![CDATA[
circle {
fill: #000000;
stroke: #000000;
}
rect#my_rectangle {
fill: red;
}
]]>
```
## Simple Animations
This package supports creating basic animations via CSS keyframes via the frame_animate() function.