Skip to content

Instantly share code, notes, and snippets.

@tangert
Last active April 22, 2024 23:32
Show Gist options
  • Save tangert/8f9e5bc68329228a48956be1f06c2157 to your computer and use it in GitHub Desktop.
Save tangert/8f9e5bc68329228a48956be1f06c2157 to your computer and use it in GitHub Desktop.
SVG Solidity Library
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "./Utils.sol";
// Core SVG utilitiy library which helps us construct
// onchain SVG's with a simple, web-like API.
library svg {
/* MAIN ELEMENTS */
function g(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("g", _props, _children);
}
function path(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("path", _props, _children);
}
function text(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("text", _props, _children);
}
function line(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("line", _props, _children);
}
function circle(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("circle", _props, _children);
}
function rect(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("rect", _props, _children);
}
function filter(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("filter", _props, _children);
}
/* GRADIENTS */
function radialGradient(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("radialGradient", _props, _children);
}
function linearGradient(string memory _props, string memory _children)
internal
pure
returns (string memory)
{
return el("linearGradient", _props, _children);
}
function gradientStop(
uint256 offset,
string memory stopColor,
string memory _props
) internal pure returns (string memory) {
return
el(
"stop",
string.concat(
prop("stop-color", stopColor),
" ",
prop("offset", string.concat(utils.uint2str(offset), "%")),
" ",
_props
),
utils.NULL
);
}
function animateTransform(string memory _props)
internal
pure
returns (string memory)
{
return el("animateTransform", _props, utils.NULL);
}
/* COMMON */
// A generic element, can be used to construct any SVG (or HTML) element
function el(
string memory _tag,
string memory _props,
string memory _children
) internal pure returns (string memory) {
return
string.concat(
"<",
_tag,
" ",
_props,
">",
_children,
"</",
_tag,
">"
);
}
// an SVG attribute
function prop(string memory _key, string memory _val)
internal
pure
returns (string memory)
{
return string.concat(_key, "=", '"', _val, '" ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment