I have a SVG generated hex grid. The hex grid is composed of polygons which are fed their points via PhP Array. The <polygon>
is followed by a <use>
element which points to a <symbol>
.
This code renders the hex grid correctly and the fill attribute works. However, when the use
element renders the symbol
it always puts it in the same location on the viewport.
The grid renders across the viewport in multiple locations, how do I keep the symbol
relative to the polygon
as its points change?
Sample Array code for points
$points = array("247.8,37.9 223.8,37.9 211.7,58.9 223.8,79.9 247.8,79.9 259.8,58.9",
,"247.8,79.9 223.8,79.9 211.7,100.9 223.8,121.9 247.8,121.9 259.8,100.9"
,"211.7,100.9 187.7,100.9 175.7,121.9 187.7,143 211.7,143 223.8,121.9"
,"211.7,143 187.7,143 175.7,164 187.7,185 211.7,185 223.8,164"
,"211.7,185 187.7,185 175.7,206 187.7,227 211.7,227 223.8,206"
,"211.7,227 187.7,227 175.7,248 187.7,269 211.7,269 223.8,248"
,"211.7,269 187.7,269 175.7,290 187.7,311.1 211.7,311.1 223.8,290"
,"211.7,311.1 187.7,311.1 175.7,332.1 187.7,353.1 211.7,353.1 223.8,332.1"
echo '<svg version="1.1" viewBox="0 0 2410 1192" x="0" y="0" preserveAspectRatio="none" width="100%">';
$hex = 0000;
//Symbol
echo '<symbol>
<path id="hills" style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:2.03431px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 234.27969,88.879347 c -2.45345,0.0538 -5.06817,2.298827 -7.87745,7.163223 l 15.63905,0.523621 c -2.40829,-4.9797 -4.981,-7.744138
-7.7616,-7.686754 z m 7.59808,8.001824 c -2.19129,0.03587 -4.68463,1.308966 -7.33231,4.522159 -3.83449,-1.16121 -7.72778,1.0103
-11.72419,9.24363 l 22.08548,0.14794 c -2.78806,-3.67548 -5.58581,-6.87639 -8.40893,-8.51612 l 13.63896,3.34992 c -0.79213,-4.08344
-3.93464,-8.819257 -8.25901,-8.747797 z" /></symbol>';
foreach ($points as $p) {
echo '<polygon class="st6" points="'.$p.' " ';
getFill($hydro,$landfill,$hex);
echo 'id="'.$hex.'" stroke="#000000" stroke-width="1"/>';
echo '<use xlink:href="#hills"/>';
$hex++;
}
echo '</svg>';
Sample Output (note the hills only rendered in one hex).
[Sample Hex Map] https://www.noxgeo.com/starfinder/galaxy/data/map-sample.pngSample SVG Output:
<svg version="1.1" viewBox="0 0 2410 1192" x="0" y="0" preserveAspectRatio="none" width="100%">
<symbol>
<path id="hills" style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:2.03431px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" d="m 234.27969,88.879347 c -2.45345,0.0538 -5.06817,2.298827 -7.87745,7.163223 l 15.63905,0.523621 c -2.40829,-4.9797 -4.981,-7.744138
-7.7616,-7.686754 z m 7.59808,8.001824 c -2.19129,0.03587 -4.68463,1.308966 -7.33231,4.522159 -3.83449,-1.16121 -7.72778,1.0103
-11.72419,9.24363 l 22.08548,0.14794 c -2.78806,-3.67548 -5.58581,-6.87639 -8.40893,-8.51612 l 13.63896,3.34992 c -0.79213,-4.08344
-3.93464,-8.819257 -8.25901,-8.747797 z"></path>
</symbol>
<polygon class="st6" points="247.8,37.9 223.8,37.9 211.7,58.9 223.8,79.9 247.8,79.9 259.8,58.9 " fill="#fdf898" id="0" stroke="#000000" stroke-width="1"></polygon><use xlink:href="#hills"></use><polygon class="st6" points="247.8,79.9 223.8,79.9 211.7,100.9 223.8,121.9 247.8,121.9 259.8,100.9 " fill="#66D3FA" id="1" stroke="#000000" stroke-width="1"></polygon>
<use xlink:href="#hills"></use>
<polygon class="st6" points="211.7,100.9 187.7,100.9 175.7,121.9 187.7,143 211.7,143 223.8,121.9 " fill="#fdf898" id="2" stroke="#000000" stroke-width="1"></polygon>
<use xlink:href="#hills"></use>
<polygon class="st6" points="211.7,143 187.7,143 175.7,164 187.7,185 211.7,185 223.8,164 " fill="#66D3FA" id="3" stroke="#000000" stroke-width="1"></polygon>
<use xlink:href="#hills"></use>
<polygon class="st6" points="211.7,185 187.7,185 175.7,206 187.7,227 211.7,227 223.8,206 " fill="#66D3FA" id="4" stroke="#000000" stroke-width="1"></polygon>
<use xlink:href="#hills"></use>
</svg>
2
Answers
Like mentioned in the comments it is mush easier to control if you know where each element is places. So, start off centering the path (both hexagon and hills etc. around 0,0). And then either group elements that need the same position or place them at the same spot (that depends on your use case) using transform/translate.
Never mind about the
<symbol>
element. You are better off just referencing the path elements and then give them a fill attribute on the<use>
element.Unfortunately we can’t align or distribute SVG elements in relation to each other as we can do with nested HTML elements (e.g via flex, grid or relative positions).
As explained by chrwahl you should first reposition your assets to facilitate the x/y translation.
In the following example I’ve centered the hexagon and the icon in a 48.1×42 viewBox:
You can retrieve the offsets from your polygon array by splitting each points attribute in an array of x and y coordinates.
We can get the top-Y coordinates from the first vertice and the left-X from the 3. one.
Due to the previous normalisation we can apply these values as translate offsets
Result
Flattened elements (avoiding
<use>
)Huge amounts of
<use>
elements impact the rendering performance so you may also write the<path>
elements directlyWhile this approach produces more markup it can be compressed quite efficiently with gzip because we only change the first 2 values in the relative path data.