如何使用 Raphael 在对象的内边缘创建渐变填充?

发布于 2024-12-09 20:20:55 字数 255 浏览 1 评论 0原文

我有一个动态生成的图表,它说明了适合容器内的项目范围(相对于项目的宽度和厚度)。我试图表明,靠近“拟合范围”边缘的项目可能不如靠近图表中间的项目那么合适。为此,我想用绿色填充我的形状,它的渐变在边缘周围变成黄色。该黄色区域的整个内边缘周围的厚度应均匀,如下图所示。我怎样才能和拉斐尔一起完成这个任务呢?我知道如何进行实心填充;梯度是我遇到困难的地方。预先感谢您的帮助!

渐变示例

I have a dynamically generated graph that illustrates the range of items that will fit within a container, relative to the item's width and thickness. I'm trying to show that the items near the edge of the "fit range" might not be as good of a fit as those closer to the middle of the graph. To do this I'd like to fill my shape with green, which has a gradient that turns to yellow around the edges. This yellow area should be of uniform thickness around the entire inside edge, as illustrated in the image below. How might I accomplish this with Raphael? I know how to do a solid fill; the gradient is where I'm having difficulty. Thanks in advance for your help!

Gradient Example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

泡沫很甜 2024-12-16 20:20:55

理论上,应该可以通过将图形切成四个三角形来做到这一点。

将图形切成四个矩形

然后,每个三角形都可以用渐变填充,该渐变主要是纯色,但在一端是渐变色变成你的边缘颜色。通过在渐变上设置直角,您可以使其看起来只有图表上的边缘具有不同的颜色。

边缘为另一种颜色的图形

我使用以下代码创建了上面的矩形。

var slice1 = paper.path("M200 200L100 100L300 100").attr({
    "fill": "90-#0f0:70-#ff0:95",
});

var slice2 = paper.path("M200 200L300 100L300 300").attr({
    "fill": "0-#0f0:70-#ff0:95",
});

var slice3 = paper.path("M200 200L300 300L100 300").attr({
    "fill": "270-#0f0:70-#ff0:95",
});

var slice4 = paper.path("M200 200L100 300L100 100").attr({
    "fill": "180-#0f0:70-#ff0:95",
});

不过,您的情况会稍微复杂一些。您必须首先找到图形的中间才能将其切成三角形。然后您需要找到每个渐变的角度。

In theory it should be possible to do this by slicing the graph into four triangles.

Slice the graph into four rectangles

Each triangle can then be filled with a gradient that is mostly your solid color but at one end it turns into your edge color. By setting the right angle on the gradient you can make it look like only the edges on the graph have a different color.

Graph with edges in another color

I've created the rectangle above using the following code.

var slice1 = paper.path("M200 200L100 100L300 100").attr({
    "fill": "90-#0f0:70-#ff0:95",
});

var slice2 = paper.path("M200 200L300 100L300 300").attr({
    "fill": "0-#0f0:70-#ff0:95",
});

var slice3 = paper.path("M200 200L300 300L100 300").attr({
    "fill": "270-#0f0:70-#ff0:95",
});

var slice4 = paper.path("M200 200L100 300L100 100").attr({
    "fill": "180-#0f0:70-#ff0:95",
});

Your case will be a bit more complex though. You will have to first find the middle of the graph to be able to slice it into triangles. Then you need to find the angle for each of the gradients.

半衬遮猫 2024-12-16 20:20:55

晚上,

事情并不像看起来那么容易。可能是由于如何在 VML 中实现它的问题。

我能做的最好的事情就是在原始对象后面使用目标对象的稍大的克隆,然后在其上使用渐变填充。

我在这个小提琴中制作了一个示例

希望有帮助。

Evening,

It's not as easy as it looks like it should be. Probably due to an issue with how to implement it in VML.

The best I was able to do is to use a slightly larger clone of your target object behind the original, and then use a gradient fill on it.

I've made an example in this fiddle

Hope that helps.

一身软味 2024-12-16 20:20:55

您可以在重叠的椭圆上使用径向渐变,但这会留下相当大的黄色角
要找到对象的中心,请使用旧的 getBBox()

You could use a radial gradient on an overllying ellipse but that will leave a rather large corner of yellow
To find the centre of your object use good old getBBox()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文