33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
const dashKeys = {
|
|
offset: "stroke-dashoffset",
|
|
array: "stroke-dasharray",
|
|
};
|
|
const camelKeys = {
|
|
offset: "strokeDashoffset",
|
|
array: "strokeDasharray",
|
|
};
|
|
/**
|
|
* Build SVG path properties. Uses the path's measured length to convert
|
|
* our custom pathLength, pathSpacing and pathOffset into stroke-dashoffset
|
|
* and stroke-dasharray attributes.
|
|
*
|
|
* This function is mutative to reduce per-frame GC.
|
|
*
|
|
* Note: We use unitless values for stroke-dasharray and stroke-dashoffset
|
|
* because Safari incorrectly scales px values when the page is zoomed.
|
|
*/
|
|
function buildSVGPath(attrs, length, spacing = 1, offset = 0, useDashCase = true) {
|
|
// Normalise path length by setting SVG attribute pathLength to 1
|
|
attrs.pathLength = 1;
|
|
// We use dash case when setting attributes directly to the DOM node and camel case
|
|
// when defining props on a React component.
|
|
const keys = useDashCase ? dashKeys : camelKeys;
|
|
// Build the dash offset (unitless to avoid Safari zoom bug)
|
|
attrs[keys.offset] = `${-offset}`;
|
|
// Build the dash array (unitless to avoid Safari zoom bug)
|
|
attrs[keys.array] = `${length} ${spacing}`;
|
|
}
|
|
|
|
export { buildSVGPath };
|
|
//# sourceMappingURL=path.mjs.map
|