function applyRoughStyling(gContainer, roughOptions) {
const roughSvg = roughJS.svg(gContainer);
gContainer.forEach((svg) => {
svg
.querySelectorAll(
"rect:not(clipPath *), circle:not(clipPath *), path:not(clipPath *), line:not(clipPath *)"
)
.forEach((element) => {
let roughElement;
const fill =
svg.getAttribute("fill") ||
element.getAttribute("fill") ||
"transparent";
const stroke =
svg.getAttribute("stroke") ||
element.getAttribute("stroke") ||
"black";
const clipPath = element.getAttribute("clip-path") || "";
const mergedOptions = {
...roughOptions,
fill: roughOptions.fill || fill,
stroke: roughOptions.stroke || stroke
};
switch (element.tagName.toLowerCase()) {
case "rect":
const x = parseFloat(element.getAttribute("x") || 0);
const y = parseFloat(element.getAttribute("y") || 0);
const width = parseFloat(element.getAttribute("width"));
const height = parseFloat(element.getAttribute("height"));
roughElement = roughSvg.rectangle(
x,
y,
width,
height,
mergedOptions
);
break;
case "circle":
const cx = parseFloat(element.getAttribute("cx"));
const cy = parseFloat(element.getAttribute("cy"));
const r = parseFloat(element.getAttribute("r"));
roughElement = roughSvg.circle(cx, cy, r * 2, mergedOptions);
break;
case "path":
roughElement = roughSvg.path(element.getAttribute("d"), {
...mergedOptions
});
break;
case "line":
const x1 = parseFloat(element.getAttribute("x1"));
const y1 = parseFloat(element.getAttribute("y1"));
const x2 = parseFloat(element.getAttribute("x2"));
const y2 = parseFloat(element.getAttribute("y2"));
roughElement = roughSvg.line(x1, y1, x2, y2, mergedOptions);
break;
}
if (roughElement) {
// Create a group to hold the rough element
// const group = document.createElementNS(
// "http://www.w3.org/2000/svg",
// "g"
// );
// element.parentNode.replaceChild(group, element);
// Apply clip-path to the group if it exists
if (clipPath) {
// group.setAttribute("clip-path", clipPath);
}
roughElement.setAttribute(
"transform",
element.getAttribute("transform") ?? ""
);
roughElement.setAttribute("class", "rough-elements-group");
// Replace the original element with the group
element.parentNode.insertBefore(roughElement, element);
// element.style.display = "none";
element.parentNode.removeChild(element);
}
});
});
}