html - 更改 svg 组元素中心

标签 html css svg

我想将 svg 组的中心平移到其左上角:

而不是:

+-----------------+       x-----------------+
|                 |       |                 |
|                 |       |                 |
|                 |       |                 |
|        x        | ----> |                 |
|                 |       |                 |
|                 |       |                 |
|                 |       |                 |
+-----------------+       +-----------------+

因此,对于以下 svg:

<svg width="900" height="900">
    <g transform="translate(200,300)">
        <rect width="400" height="500"></rect>
    </g>
</svg>

rect元素应该位于绝对位置:0, 50 ( 200 - 400 / 2300 - 500 / 2 )。

我知道我可以设置 xy属性 rect元素( <rect width="400" height="500" x="-200" y="-250"></rect> ),但这或多或少是一个黑客,因为 g 的真正中心。没有改变。

更改 <g> 的中心的正确方法是什么? SVG 元素?

rect {
    fill: red;
}
<svg width="900" height="900">
    <g transform="translate(200,300)">
        <rect width="400" height="500" x="-200" y="-250"></rect>
    </g>
</svg>

最佳答案

这可能适合您作为翻译吗?

rect {
    fill: red;
}
<svg width="900" height="900">
  <g transform="translate(200,300)">
    <g transform="translate(-200, -250)">
      <rect width="400" height="500"></rect>    
    </g>
  </g>
</svg>

关于html - 更改 svg 组元素中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26628289/

相关文章:

javascript - 如何在 d3 javascript 中为 SVG 文本元素分配唯一 ID

JavaScript 和 CSS 悬停问题

javascript - 检查其值存储在数组中的复选框

html - Chrome 报告 html5 缓存 list mime 类型不正确

javascript - 自定义 SharePoint 2013 日历日名称

SVG:公共(public)文档类型与其他 namespace 冲突

HTML:无法对齐表格单元格

javascript - CSS3 转换和 javascript 交互

javascript - 2 个带有响应滚动的固定图像的 div

javascript - 如何在 D3.js 中操作特定的 SVG 路径