一楼完整源码
<style>
#mydiv {
margin: 30px auto;
width: fit-content;
height: fit-content;
outline: 1px dashed gray;
}
#tbox {
width: 720px;
height: 300px;
padding: 10px;
tab-size: 4;
}
.tMid { text-align: center; }
</style>
<h2>效果:</h2>
<div id="mydiv">
<svg width="400" height="400" viewBox="-200 -200 400 400" xmlns="http://www.w3.org/2000/svg">
<defs>
<g id="circle">
<circle cx="0" cy="0" r="8" fill="skyblue" stroke="none"/>
</g>
</defs>
<use href="#circle"/>
</svg>
</div>
<h2>SVG代码:</h2>
<p class="tMid"><textarea id="tbox"></textarea></p>
<script>
var msvg = mydiv.querySelector('svg');
mkCircles = (circles, columns, target) => {
let angle = 360 / columns,
sR = target.getAttribute('width') / 2,
outstr = '';
Array(columns).fill('').forEach((_,a) => {
Array(circles).fill('').forEach((_,b) => {
let xx = sR / circles;
outstr += `\t<use href="#circle" transform="rotate(${angle * a}) translate(${b * xx + xx - 10})"/>\n`;
});
});
target.innerHTML += outstr;
};
mkCircles(4,12,msvg);
tbox.value = msvg.outerHTML;
</script>
|