This documentation describes calculating the area of complex SVG elements. There are three algorithms you can use.
All algorithms need the ID of the SVG root element. In the demo, it is tutorial_svg The algorithm will evaluate every element with class area-calculate.
tutorial_svg
area-calculate
<rect x="352" y="1649" width="774" height="178" fill="#90929C" class="area-calculate random-generate" areagroup="2"></rect>
This algorithm samples every point in the SVG and checks if it lies within the fill of an element. All such points are summed as the total area.
const areaCalculator = new svgAreaCalculation(); const area = areaCalculator.lazyStupidAreaCalculation('tutorial_svg');
This algorithm samples points from each SVG element and tests whether they fall within other elements to estimate visible area.
a1 * b1
a2 * b2
(8 / 10) * (a2 * b2)
a1 * b1 + 0.8 * a2 * b2
const areaCalculator = new svgAreaCalculation(); const area = areaCalculator.areaInSvgWithIntersection('tutorial_svg', numberOfRandomPoints);
This algorithm merges all SVG elements into one or more polygons (including gaps), computes area via the Shoelace formula, and subtracts gap areas.
const intersectElements = new svgAreaIntersection('tutorial_svg'); const area = intersectElements.polygonIntersectionInSvg(show, redraw, color, opacity, group);