如何在 Raphael 文本对象上应用多个旋转变换?
我有一个 Raphaël 文本对象,我想围绕一定距离外的轴旋转,并相应地旋转文本,使其保持水平。我知道使用 SVG 转换矩阵可以做到这一点,但 Raphaël 的作者已经指出它们不会很快成为工具包的一部分。以下是我想做的一些代码:
txt_obj = paper.text(100, 100, "Hello!");
txt_obj.rotate(90, 100, 200); // rotate text so it is sideways at (200,200)
txt_obj.rotate(-90); // rotate text back to horizontal
不幸的是,第二个旋转命令消除了第一个旋转命令完成的平移和旋转。
如果我直接使用 SVG,我可以这样做:
<g transform="rotate(90, 100, 200)">
<text x="100" y="100" transform="rotate(-90, 100, 100)">Hello!</text>
</g>
但是,我不相信 Raphaël 支持 svg g
元素。
理想情况下,我希望为该操作设置动画,但现在我将一步一步地进行。
I have a Raphaël text object that I would like to rotate around an axis some distance away and also rotate the text accordingly so it stays horizontal. I know this is possible using SVG transformation matrices, but the author of Raphaël has stated that they won't be a part of the toolkit anytime soon. Here's some code of what I'd like to do:
txt_obj = paper.text(100, 100, "Hello!");
txt_obj.rotate(90, 100, 200); // rotate text so it is sideways at (200,200)
txt_obj.rotate(-90); // rotate text back to horizontal
Unfortunately, the second rotate command obliterates the translation and rotation accomplished by the first.
If I were doing straight SVG I could do something like this:
<g transform="rotate(90, 100, 200)">
<text x="100" y="100" transform="rotate(-90, 100, 100)">Hello!</text>
</g>
However, I don't believe that Raphaël supports the svg g
element.
Ideally I'd like to animate the operation, but I'll take it one step at a time for right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个老问题,但我只是努力解决它,所以我想我会分享我的答案。
您可以直接访问 SVG 对象,甚至在 Raphael 调用中也是如此:
或
第二个是我们需要的。使用 Raphael 旋转文本的麻烦在于它所做的只是在 SVG 中设置变换:
对 .rotate() 的多次调用会覆盖以前的设置,因此最终在 SVG 变换中只有一次调用旋转。但是我们可以通过直接访问属性来添加我们自己的转换。变换似乎是以相反的顺序执行的(或者其他什么——老实说,我并没有想太多),所以如果你想让它先进行,你可以把额外的旋转放在最后:
This is an old question but I just fought my way through it so I thought I'd share my answer.
You can access the SVG object directly, even inside your Raphael calls, thusly:
or
The second one is what gets us what we need. The trouble with rotating text with Raphael is all it does is set the transform in the SVG:
Multiple calls to .rotate() overwrite previous settings so finally there's only one call to rotate in the SVG transform. But we can add our own transforms by accessing the attribute directly. Transforms seem to be performed in reverse order (or something -- I honestly haven't thought about it too hard) so you put your additional rotation last if you want it to go first: