【发布时间】:2020-01-02 01:27:46
【问题描述】:
我正在使用收到的数据在 SVG 中重新创建一个矩形。只要矩形不旋转,我就可以正确绘制矩形。当它旋转时,它位于错误的位置。
矩形值为:
left: 27
top: 30
width: 100
height: 100
localBoundsWidth: 100
localBoundsHeight: 100
这是未旋转的矩形(位置正确):
.Rectangle_1 {
position: absolute;
overflow: visible;
width: 100px;
height: 100px;
left: 27px;
top: 30px;
}
.Line_1 {
overflow: visible;
position: absolute;
top: 30.5px;
left: 0.5px;
width: 225px;
height: 1px;
}
.Line_2 {
overflow: visible;
position: absolute;
top: 0.5px;
left: 27.5px;
width: 1px;
height: 197px;
}
<div>
<svg class="Rectangle_1">
<rect fill="rgba(255,93,93,1)" id="Rectangle_1" rx="0" ry="0" x="0" y="0" width="100" height="100">
</rect>
</svg>
<svg class="Line_1">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_1" d="M 0 0 L 225 0">
</path>
</svg>
<svg class="Line_2">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_2" d="M 0 0 L 0 197">
</path>
</svg>
</div>
这是它的样子(期望的结果)。我已经手动调整了位置:
#Rectangle_1 {
transform: matrix(0.7660,0.6427,-0.6427,0.7660,64.28,0.5);
}
.Rectangle_1 {
position: absolute;
overflow: visible;
width: 140.883px;
height: 140.883px;
left: 6.558px;
top: 9.558px;
}
.Line_1 {
overflow: visible;
position: absolute;
top: 30.5px;
left: 0.5px;
width: 225px;
height: 1px;
}
.Line_2 {
overflow: visible;
position: absolute;
top: 0.5px;
left: 27.5px;
width: 1px;
height: 197px;
}
.Line_5 {
overflow: visible;
position: absolute;
top: 0.5px;
left: 70.5px;
width: 1px;
height: 9px;
}
.Line_6 {
overflow: visible;
position: absolute;
top: 86.5px;
left: 0.5px;
width: 6px;
height: 1px;
}
<div>
<svg class="Rectangle_1">
<rect fill="rgba(255,93,93,1)" id="Rectangle_1" rx="0" ry="0" x="0" y="0" width="100" height="100">
</rect>
</svg>
<svg class="Line_1">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_1" d="M 0 0 L 225 0">
</path>
</svg>
<svg class="Line_2">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_2" d="M 0 0 L 0 197">
</path>
</svg>
<svg class="Line_5">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_5" d="M 0 9 L 0 0">
</path>
</svg>
<svg class="Line_6">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_6" d="M 6 0 L 0 0">
</path>
</svg>
</div>
当矩形旋转时,我无法在正确的位置绘制它,因为我收到的值是矩形围绕其中心旋转后的 绘制边界。
所以原始未旋转的矩形值为:
left: 27
top: 30
width: 100
height: 100
localBoundsWidth: 100
localBoundsHeight: 100
当它旋转 40 度时,值为:
left: 6.56
top: 9.56
width: 140
height: 140
transform: matrix(0.766,0.6428,-0.6428,0.766,70.8372,9.5584)
rotation: 40
localBoundsWidth: 100
localBoundsHeight: 100
如果重要的话,其父坐标空间的左上角是:
x: 70.83
y: 9.56
当我尝试使用上述信息(旋转值)创建旋转矩形时,矩形的位置不正确。
#Rectangle_1 {
transform: rotate(40deg);
}
.Rectangle_1 {
position: absolute;
overflow: visible;
width: 100px;
height: 100px;
left: 6.56px;
top: 9.56px;
}
.Line_1 {
overflow: visible;
position: absolute;
top: 30.5px;
left: 0.5px;
width: 225px;
height: 1px;
}
.Line_2 {
overflow: visible;
position: absolute;
top: 0.5px;
left: 27.5px;
width: 1px;
height: 197px;
}
<div>
<svg class="Rectangle_1">
<rect fill="rgba(255,93,93,1)" id="Rectangle_1" rx="0" ry="0" x="0" y="0" width="100" height="100">
</rect>
</svg>
<svg class="Line_1">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_1" d="M 0 0 L 225 0">
</path>
</svg>
<svg class="Line_2">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_2" d="M 0 0 L 0 197">
</path>
</svg>
</div>
当我尝试使用矩阵值时,它的位置也不正确:
#Rectangle_1 {
transform: matrix(0.766,0.6428,-0.6428,0.766,70.8372,9.5584);
}
.Rectangle_1 {
position: absolute;
overflow: visible;
width: 100px;
height: 100px;
left: 6.56px;
top: 9.56px;
}
.Line_1 {
overflow: visible;
position: absolute;
top: 30.5px;
left: 0.5px;
width: 225px;
height: 1px;
}
.Line_2 {
overflow: visible;
position: absolute;
top: 0.5px;
left: 27.5px;
width: 1px;
height: 197px;
}
.Line_5 {
overflow: visible;
position: absolute;
top: 0.5px;
left: 70.5px;
width: 1px;
height: 9px;
}
.Line_6 {
overflow: visible;
position: absolute;
top: 86.5px;
left: 0.5px;
width: 6px;
height: 1px;
}
<div>
<svg class="Rectangle_1">
<rect fill="rgba(255,93,93,1)" id="Rectangle_1" rx="0" ry="0" x="0" y="0" width="100" height="100">
</rect>
</svg>
<svg class="Line_1">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_1" d="M 0 0 L 225 0">
</path>
</svg>
<svg class="Line_2">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_2" d="M 0 0 L 0 197">
</path>
</svg>
<svg class="Line_5">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_5" d="M 0 9 L 0 0">
</path>
</svg>
<svg class="Line_6">
<path stroke="rgba(112,112,112,1)" stroke-width="1px" stroke-linejoin="miter" stroke-linecap="butt" stroke-miterlimit="4" shape-rendering="auto" id="Line_6" d="M 6 0 L 0 0">
</path>
</svg>
</div>
我只有以下信息可用于正确旋转矩形(旋转值):
left: 6.56
top: 9.56
width: 140
height: 140
transform: matrix(0.766,0.6428,-0.6428,0.766,70.8372,9.5584)
rotation: 40
localBoundsWidth: 100
localBoundsHeight: 100
它的父坐标空间的左上角是:
x: 70.83
y: 9.56
是否可以仅使用上述信息(旋转值)将矩形放置在正确的位置?
或者是否可以仅使用上述信息取消旋转矩形以获得原始顶部和左侧值?
仅供参考,矩形已围绕其中心点旋转而不是左上角。
【问题讨论】:
-
所以你的意思是你只能使用left、top、width、height、transform和rotation来把它放到你想要的地方?我想我也意味着您只能更改#Rectangle_1 CSS 对吗?
-
@Keith 是的,但我必须使用的顶部和左侧值是对象旋转后的 x 和 y。
-
#Rectangle_1 必须有 x=70.83 和 y=9.56 ?
-
@Keith 我已将问题更新为更清晰
-
我只是好奇你不必转换:旋转 .Rectangle_1 而不是 #Rectange_1 吗?
标签: javascript css svg css-transforms