英文:
Align multiple Paths inside SVG Group to center
问题
为我正在进行的项目,我必须动态生成多个由路径组成的圆。这些圆的大小在两个设定的数字之间随机(例如750 <-> 850)。我试图将这些圆垂直和水平居中到SVG组的中间,但我不太确定如何实现这一点。
这是负责生成路径的代码片段:
const svgViewportWidth = 900;
const svgViewportHeight = 1000;
const randomSize = generateRandomNumberInRange(750, 850);
return (
<path key={`path-${aIndex}`}
d={`M ${(svgViewportWidth - randomSize) / 2}, ${svgViewportHeight - (randomSize - 320)}
a 1,1 0 1,1 ${randomSize},0
a 1,1 0 1,1 -${randomSize},0z`} id={`path-${aIndex}`}></path>
)
上述片段的“完整输出”可能如下所示:
https://jsfiddle.net/c6phn8ue/
从jsfiddle中可以看到,圆在左下角相交。最终我想要实现的是使每个圆都在中心对齐,这意味着这些圆永远不会相交。
我假设我需要以某种方式获取SVG和圆之间的差距,然后使用它来对齐圆,但我对如何做到这一点感到有些困惑。
英文:
For a project i'm working on, i have to dynamically generate multiple circles, composed of paths.
These circles have random sizes between two set numbers (eg. 750 <-> 850).
I'm trying to vertically and horizontally center these circles to the middle of the SVG Group, but i'm not quite sure how i would achieve this.
Here is the code snippet responsible for generating the paths:
const svgViewportWidth = 900;
const svgViewportHeight = 1000;
const randomSize = generateRandomNumberInRange(750, 850);
return (
<path key={`path-${aIndex}`}
d={`M ${(svgViewportWidth - randomSize) / 2}, ${svgViewportHeight - (randomSize - 320)}
a 1,1 0 1,1 ${randomSize},0
a 1,1 0 1,1 -${randomSize},0z`} id={`path-${aIndex}`}></path>
)
"Complete output" of above snippet could look like this:
https://jsfiddle.net/c6phn8ue/
As can be seen in the fiddle, the circles cross each other on the bottom left corner. Ultimately what i'm trying to achieve is to have each circle aligned in the center, meaning that the circles would never cross each other.
I am assuming i'll somehow need to get the delta between the SVG and the circle and then align the circle using that, but i'm somewhat at a loss as to how to do that.
答案1
得分: 0
更自解释的变量名有助于理解代码。
随机数将变为直径 d
。
let d = generateRandomNumberInRange(750, 850);
因此半径将为 let r=d/2
。
第一个 M
x 坐标将始终位于 cx
。
let [cx, cy] = [svgViewportWidth/2, svgViewportHeight/2];
现在我们有了新 <path>
元素的所有必要坐标。
let newPath = `<path d="
M ${ cx } ${ cy-r }
a 1 1 0 1 1 0 ${d}
a 1 1 0 1 1 0 -${d} z "/>`;
英文:
It's easier to understand when you assign more self-explanatory variable names.
The random number will become the diameter d
let d = generateRandomNumberInRange(750, 850);
and consequently the radius let r=d/2
Th first M
x coordinate will always be at cx
let [cx, cy] = [svgViewportWidth/2, svgViewportHeight/2];
Now we have all necessary coordinates for the new <path>
element:
let newPath = `<path d="
M ${ cx } ${ cy-r }
a 1 1 0 1 1 0 ${d}
a 1 1 0 1 1 0 -${d} z "/>`;
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
const svgViewportWidth = 900;
const svgViewportHeight = 1000;
for (let i = 0; i < 5; i++) {
let [cx, cy] = [svgViewportWidth/2, svgViewportHeight/2];
let d = generateRandomNumberInRange(750, 850);
let r = d/2;
let newPath = `
<path d="
M ${ cx } ${ cy-r }
a 1 1 0 1 1 0 ${d}
a 1 1 0 1 1 0 -${d}
"/>`;
svgGroup.insertAdjacentHTML('beforeend', newPath);
}
function generateRandomNumberInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
<!-- language: lang-html -->
<svg viewBox="0 0 900 1000" fill="none" stroke="black" class="svg">
<g class="svgGroup" id="svgGroup">
</g>
</svg>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论