如何在 Raphael 文本对象上应用多个旋转变换?

发布于 2024-09-05 19:20:03 字数 764 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2024-09-12 19:20:03

这是一个老问题,但我只是努力解决它,所以我想我会分享我的答案。

您可以直接访问 SVG 对象,甚至在 Raphael 调用中也是如此:

this.node.getAttribute('x')

this.node.getAttribute('transform')

第二个是我们需要的。使用 Raphael 旋转文本的麻烦在于它所做的只是在 SVG 中设置变换:

<text x="600" y="606.240234375" text-anchor="middle" style="text-anchor: middle; 
font: normal normal normal 10px/normal Arial; font-family: Arial; font-weight: 900;
font-size: 18px; " font="10px "Arial"" stroke="none" fill="#000000" 
font-family="Arial" font-weight="900" font-size="18px" transform="rotate(45 600 600)">
<tspan style="-webkit-user-select: none; cursor: pointer; ">FOOBAR</tspan></text>

对 .rotate() 的多次调用会覆盖以前的设置,因此最终在 SVG 变换中只有一次调用旋转。但是我们可以通过直接访问属性来添加我们自己的转换。变换似乎是以相反的顺序执行的(或者其他什么——老实说,我并没有想太多),所以如果你想让它先进行,你可以把额外的旋转放在最后:

<script>
(function (raphael) {
  $(function () {
    var paper = raphael("raphael_div", 500, 500);

    upside_down_text = paper.text(100,100,'UPSIDE DOWN')
                            .attr('font-family','Arial')
                            .attr('font-size','24px');                           
    upside_down_text.rotate(25,250,250); // rotate using Raphael
    // But first we want to rotate 180 degrees.
    height = upside_down_text.getBBox().height;
    upside_down_text.node.setAttribute('transform',
                                       upside_down_text.node.getAttribute('transform')+
                                       ' rotate(180 '+
                                       upside_down_text.node.getAttribute('x')+
                                       ' '+
                                       (upside_down_text.node.getAttribute('y')-(height/2))+
                                       ')');
  });
})(Raphael.ninja());
</script>
<div id="raphael_div"></div>

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:

this.node.getAttribute('x')

or

this.node.getAttribute('transform')

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:

<text x="600" y="606.240234375" text-anchor="middle" style="text-anchor: middle; 
font: normal normal normal 10px/normal Arial; font-family: Arial; font-weight: 900;
font-size: 18px; " font="10px "Arial"" stroke="none" fill="#000000" 
font-family="Arial" font-weight="900" font-size="18px" transform="rotate(45 600 600)">
<tspan style="-webkit-user-select: none; cursor: pointer; ">FOOBAR</tspan></text>

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:

<script>
(function (raphael) {
  $(function () {
    var paper = raphael("raphael_div", 500, 500);

    upside_down_text = paper.text(100,100,'UPSIDE DOWN')
                            .attr('font-family','Arial')
                            .attr('font-size','24px');                           
    upside_down_text.rotate(25,250,250); // rotate using Raphael
    // But first we want to rotate 180 degrees.
    height = upside_down_text.getBBox().height;
    upside_down_text.node.setAttribute('transform',
                                       upside_down_text.node.getAttribute('transform')+
                                       ' rotate(180 '+
                                       upside_down_text.node.getAttribute('x')+
                                       ' '+
                                       (upside_down_text.node.getAttribute('y')-(height/2))+
                                       ')');
  });
})(Raphael.ninja());
</script>
<div id="raphael_div"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文