51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
const SvgNode = require("../svg-node");
|
|
|
|
function simple_radial_gradient(id, color0, color1) {
|
|
let s0 = new SvgNode("stop", { offset: "0%", "stop-color": color0 });
|
|
let s1 = new SvgNode("stop", { offset: "100%", "stop-color": color1 });
|
|
return new SvgNode("radialGradient", { id }).append(s0, s1);
|
|
}
|
|
|
|
function radial_gradient(id, cx, cy, r) {
|
|
return new SvgNode("radialGradient", { id, cx, cy, r });
|
|
}
|
|
|
|
function stop(offset, color) {
|
|
return new SvgNode("stop", { offset, "stop-color": color });
|
|
}
|
|
|
|
/**
|
|
* Extracts from styles items which must be in defs section.
|
|
* @param {*} style
|
|
*/
|
|
function extract_style_defs(style) {
|
|
const skeys = Object.keys(style).filter((x) => x.startsWith("$"));
|
|
|
|
return skeys.map((skey) => extract_class_def(style[skey], skey.substring(1)))
|
|
}
|
|
|
|
/**
|
|
* Extracts from styles items which must be in defs section.
|
|
* @param {*} style
|
|
*/
|
|
function extract_class_def(cls, id) {
|
|
if (cls.type == "radial-gradient") {
|
|
const ckeys = Object.keys(cls);
|
|
const stops = ckeys
|
|
.filter((x) => x.endsWith("%"))
|
|
.map((x) => new SvgNode("stop", { offset: x, "stop-color": cls[x] }));
|
|
|
|
return new SvgNode("radialGradient", { id }, stops);
|
|
}
|
|
|
|
throw `Unknown system style ${cls.type}`
|
|
}
|
|
|
|
module.exports = {
|
|
simple_radial_gradient,
|
|
radial_gradient,
|
|
extract_style_defs,
|
|
};
|
|
|
|
module.exports.default = module.exports;
|