如何用Raphael创建渐变对象

tar*_*lla 15 javascript svg gradient object raphael

我试图使用Raphael JS图形库.我想使用属性梯度,它应该接受一个对象.文档说参考SVG规范.例如,我在SVG中找到了渐变对象

<linearGradient id="myFillGrad" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="5%" stop-color="red" />
<stop offset="95%" stop-color="blue" stop-opacity="0.5" />
</linearGradient>
Run Code Online (Sandbox Code Playgroud)

但我如何从我的JavaScript中引用它?

circle.attr("gradient", "myFillGrad"); 
Run Code Online (Sandbox Code Playgroud)

不起作用:)提前谢谢

Nat*_*ies 19

更新:重写最新的Raphael API:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Linear Gradient</title>
  <script src="http://raphaeljs.com/raphael.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
  <script type="text/javascript" charset="utf-8">
    var paper = Raphael(10, 10, 800, 600);
    var circle = paper.circle(150, 150, 150);
    circle.attr({
      "fill": "90-#f00:5-#00f:95",
      "fill-opacity": 0.5
    });
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

attr()可以在此处找到新API 的文档.


laf*_*cow 6

我不相信当前的raphael API允许你设置除最后一个之外的单个停止不透明度,它被赋予传递给"不透明度"attr的值,例如:

this.ellipse(x, y, r, r).attr({stroke: "none", fill: "r(.5,.1)#ccc-#ccc", opacity: 0})
Run Code Online (Sandbox Code Playgroud)

...最后一站的停止不透明度为0.为了更细粒度的控制,我将这个"case"添加到我的raphael.js中的属性解析开关中:

 case "opacityStops":
                            if (attrs.gradient) {
                                var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
                                if (gradient) {
                                    var stops = gradient.getElementsByTagName("stop");
                                    var opacs=value.split("-");
                                    for(var ii=0;ii<stops.length;ii++){
                                        stops[ii][setAttribute]("stop-opacity", opacs[ii]||"1");
                                    }
                                }
                                break;
                            }
Run Code Online (Sandbox Code Playgroud)

您还必须在"availableAttrs"对象中添加相应的条目,例如:

availableAttrs = {<other attrs here>..., opacityStops:"1"}
Run Code Online (Sandbox Code Playgroud)

创建一个具有不同不透明度的径向渐变的圆圈的调用将如下所示:

this.ellipse(x, y, r, r).attr({stroke: "none", fill: "r(.5,.5)#fff-#fff:70-#000", "opacityStops": "1-0-0.6"}
Run Code Online (Sandbox Code Playgroud)