SVG 坐标与变换矩阵
我想在矩形元素上实现像 svg-edit 这样的功能
- 旋转矩形
- 调整大小
- 拖动
旋转 SVG 矩形它工作正常,但是当我想调整矩形大小时它有问题。坐标不正确;我使用变换矩阵来旋转 targetelement.setAttribute(transform,rotate(45,cx,cy))
但是当元素旋转时,坐标也会移动。我还使用 inverse
函数来反转变换矩阵,它解决了问题,但它不适用于拖动功能。
I want to implement the functionality like svg-edit on rectangle element
- Rotate rectangle
- Resizing
- Drag
Rotating the SVG rectangle it works fine, but when I want to resize the rectangle it has a problem. The coordinates are not working right; I use the transform matrix to rotatetargetelement.setAttribute(transform,rotate(45,cx,cy))
but when the element has been rotated the coordinates are moved. I'm also using inverse
function to inverse the transform matrix it resolves the problem but its not working with drag function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经创建了一个我相信您在我的网站上描述的工作示例:
http://phrogz.net/svg/drag_under_transformation.xhtml
一般来说,您可以转换鼠标光标通过以下方式进入对象的本地空间:
创建
mousemove
事件处理程序:在该事件处理程序中,将鼠标坐标(以像素为单位)转换为 SVG 文档的全局空间:
将全局点转换为您拖动的对象的空间:
对于 Stack Overflow 的后人,这里是我的 SVG+XHTML 演示的完整源代码(以防我的网站宕机):
I have created a working example of what I believe you are describing on my site here:
http://phrogz.net/svg/drag_under_transformation.xhtml
In general, you convert the mouse cursor into the local space of an object by:
Creating a
mousemove
event handler:In that event handler, convert the mouse coordinates (in pixels) into the global space of your SVG document:
Convert the global point into the space of the object you are dragging:
For Stack Overflow posterity, here's the full source of my SVG+XHTML demo (in case my site is down):
对于使用 Chrome 的用户,请在
此处的更多信息后添加以下行:https://github。 com/cpettitt/dagre-d3/issues/202
For those who use Chrome please add the following lines after
More info here: https://github.com/cpettitt/dagre-d3/issues/202
虽然 Phrogz 答案非常好且完整,但仍然可能有对于某些人来说,已弃用的 SVGPoint 解决方案中的值可能是在此处简要介绍 DOMPoint 替代方案很有启发性。
抛开所有其他细节不谈,之前我们有:
我们现在可以这样做:
While Phrogz answer is very good and complete, and there still may be value in the deprecated SVGPoint solution for some, it may be instructive to present the DOMPoint alternative here in brief.
All other particulars aside, where before we had:
we can now do: