raphael.js - 自定义属性
是否可以为 raphael 元素定义自定义属性?
例如,
r.circle(25,50,10).attr({fill:'#b71e16', stroke:'#71140f', 'my_custom_attribute':'a value'});
我需要这个的原因是我想对一整套元素做一些相当复杂的动画,并且我想在某个地方存储每个元素的原始 y 坐标。
Is it possible to define a custom attribute for a raphael element?
e.g.
r.circle(25,50,10).attr({fill:'#b71e16', stroke:'#71140f', 'my_custom_attribute':'a value'});
Reason I need this is I want to do some quite complex animation on a whole set of elements and I want somewhere to store the original y coordinate for each one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的自定义属性是:
.attr()
和.animate()
)?1. 自定义数据存储和检索
我 99% 确信在 Raphael 中存储任意数据的官方预期方法是使用
.data ()
函数,例如请注意,从 Raphael 2.1 开始,这适用于元素,不适用于集合。当我需要将数据分配给一个集合时,我倾向于使用
for
循环来设置它,并使用someSet[0].data()
来获取它 - 有点麻烦,但它有效。令人烦恼的是
.data
的文档没有说什么根本不知道它的用途(在撰写本文时)...但是 jQuery 中的.data()
、HTML5 中的data-*
等等都有这个目的,像这样使用它是有效的,并且 SO 上的其他人谈论它打算像这样使用,所以我非常有信心这是将任意数据附加到 Raphael 对象的预期方法。2. 由
attr()
或animate()
触发的自定义函数如果您需要行为类似于 Raphael 属性的自定义属性 - 使用
更改时触发函数或转换attr
或animate
(有点像拉斐尔钩子) - 这就是 paper.customAttributes 用于。它定义了一个函数,只要为该paper
对象中的任何元素设置命名的自定义属性,就会执行该函数。返回对象应用于元素的标准属性。官方文档对此有一些非常有用的示例,这里是一个改编的示例:
请注意,每个 customAttribute 执行中的
this
是为其设置 attr 的 Raphael 对象。这意味着...3. 在浏览器中将自定义属性强制添加到 SVG 或 VML 标记中
Raphael 并不真正支持此操作,因此仅在您确实非常需要时才执行此操作。但是,如果您确实需要标记中的某些 Raphael 不支持的内容,您可以使用
attr
和animate 通过使用
paper.customAttributes
和element.node
(请注意,element.node
的文档几乎只是非常无用的“<一个href="http://raphaeljs.com/reference.html#Element.node" rel="nofollow noreferrer">不要弄乱它" - 你不应该弄乱它的原因是,它直接为您提供 SVG 或 VML 元素,这意味着 Raphael 不知道您对其所做的任何更改,这可能会使您的 Raphael 对象与其控制的元素不同步,除非您很小心,否则可能会破坏内容。 ,并使用类似的技术 这...)。这是一个设置 SVG 属性
dy
的示例(假设也使用了 jQuery,jQuery 不是必需的,但更方便),它允许您控制 Raphael 文本的行间距(注意 - 示例代码尚未在 VML/IE 中进行测试。如果在 VML 模式下不起作用,将会更新):Live jsfiddle 示例
Is the custom attribute you want:
.attr()
and.animate()
)?1. Custom data storage and retrieval
I'm 99% sure that the official, intended way to store arbitrary data in Raphael is to use the
.data()
function, e.g.Note that as of Raphael 2.1 this works for elements, not sets. When I need to assign data to a set I tend to set it with a
for
loop and get it withsomeSet[0].data()
- a bit of a cludge, but it works.Annoyingly the documentation for
.data
doesn't say anything at all about what it is for (at time of writing)... but.data()
in jQuery,data-*
in HTML5, etc etc all have this purpose, using it like this works, and others on SO talk about it being intended to be used it like this, so I'm pretty confident that this is the intended method for attaching arbitrary data to Raphael objects.2. Custom functions triggered by
attr()
oranimate()
If you need a custom attribute that behaves like Raphael attributes - triggering a function or transformation when changed using
attr
oranimate
(kind of like a Raphael hook) - that's what paper.customAttributes is for. It defines a function that is executed any time the named custom attr is set for any element in thatpaper
object. The return object is applied to the element's standard attributes.The offical docs have some pretty useful examples for this one, here's an adapted one:
Note that
this
within each customAttribute execution is the Raphael object for which the attr is being set. This means...3. Forcing custom attribute into the SVG or VML markup in the browser
Raphael doesn't really support this, so only do this if you really, really need to. But if you really do need something in the markup that Raphael just doesn't support, you can create a rudimentary control for manipulating it using
attr
andanimate
by usingpaper.customAttributes
andelement.node
(note that the documentation forelement.node
is pretty much just the highly unhelpful "Don't mess with it" - the reason you shouldn't mess with it is, it gives you the SVG or VML element directly, which means Raphael doesn't know about any of the changes you make to it, which may put your Raphael object out of sync with the element it controls, potentially breaking stuff. Unless you're careful, and use a technique like this...).Here's an example (assuming jQuery is also being used, jQuery isn't essential but is more convenient) that sets the SVG property
dy
, which allows you to control line spacing of Raphael text (note - example code not yet tested in VML/IE. will update if it doesn't work in VML mode):Live jsfiddle example
我认为你可以这样做:
那么
希望这会有所帮助。
I think you can do:
then
Hope this helps.
是的,您应该能够执行以下操作:
当然,标题是您要设置或创建的属性的名称,值应该是值。当然,所讨论的 raphael 元素将是 attr 的接收者。
Yes, you should be able to do the following:
Of course title is the name of the attribute you want to set or create and value should be the value. Of course the raphael element in question would be the reciever for attr.