Flash:当渐变填充在其父级边界之外绘制时会消失吗?

发布于 2024-10-03 14:52:14 字数 1017 浏览 4 评论 0原文

我尝试使用 beginGradientFilldrawRect 在 Flash 中绘制渐变,但是当绘制的矩形部分超出父级边界时,不会绘制渐变根本不。

例如,考虑下面的代码:

function testGradient():void {
    var g:Graphics = container.graphics;
    var width:Number = container.width;
    var height:Number = container.height;
    var y:Number = 0;
    var x:Number = 0;
    var ratios:Array = [255 * y / height, 255 * (y + height) / height];
    g.beginGradientFill(GradientType.LINEAR, [0xFF, 0xFF], [0.6, 0], 
                        ratios, null);
    g.lineStyle(1, 0xFF0000);
    g.drawRect(x, y, width, height);
    g.endFill(); 
}

当绘制的矩形位于 container 的边界内时,一切正常:

workinggradient

但是,如果矩形位于容器边界之外,则根本不会绘制渐变。例如,如果代码更改为:

...
var x:Number = 10;
var y:Number = 10;
...

那么渐变消失:

brokengradient

缺少绘制绘制所需的数学运算框在父级边界内(并修复渐变,使其看起来正确),有什么方法可以处理这个问题吗?

I'm trying to draw a gradient in Flash using beginGradientFill and drawRect, but when the rect being drawn is partially outside the bounds of the parent, the gradient isn't drawn at all.

For example, consider the code below:

function testGradient():void {
    var g:Graphics = container.graphics;
    var width:Number = container.width;
    var height:Number = container.height;
    var y:Number = 0;
    var x:Number = 0;
    var ratios:Array = [255 * y / height, 255 * (y + height) / height];
    g.beginGradientFill(GradientType.LINEAR, [0xFF, 0xFF], [0.6, 0], 
                        ratios, null);
    g.lineStyle(1, 0xFF0000);
    g.drawRect(x, y, width, height);
    g.endFill(); 
}

When the rectangle being drawn lies within the bounds of container, everything works:

working gradient

However, if the rectangle lies outside of the bounds of the container, the gradient isn't drawn at all. For example, if the code is changed to:

...
var x:Number = 10;
var y:Number = 10;
...

Then the gradient disappears:

broken gradient

Short of doing the math required to draw the box inside the bounds of the parent (and fixing up the gradient so it looks correct), is there any way to deal with this?

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

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

发布评论

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

评论(1

々眼睛长脚气 2024-10-10 14:52:14

我认为您的问题是您在比率计算中使用了 x/y 位置,这可能会导致值超出范围。

使用比率计算,当 x/y 都为零时,您会得到一个很好的平滑渐变:

ratios = [0,255]

但是,例如,当您将 x/y 设置为 10 时,您的比率数组变为

ratios = [25.5,280.5]

这是无效的。 ratios 需要是 0-255 之间的线性整数系列。超出界限会导致变量被忽略。

如果您希望框的渐变保持一致(无论其 x/y 位置如何),请对您的 ratios 变量进行硬编码,或者对您在比率数组中输入的值进行一些边界检查,以确保它们在 0- 之间255.

请记住,在图形对象上调用drawRect 时,x/y/width/height 是相对于您正在操作的graphics 对象的对象。

希望有帮助。

I think your problem is that you are using the x/y positions in your ratio calculations, and that can lead to out-of-bounds values.

using your ratio calc, when x/y are both zero, then you get a nice smooth gradient:

ratios = [0,255]

however, when you, for instance, set x/y to 10, your ratios array becomes

ratios = [25.5,280.5]

which is invalid. ratios needs to be a linear series of integers from 0-255. Exceeding the bounds cause the variable to be ignored.

Either hardcode your ratios var if you want consistant gradients for you box regardless of its x/y position, or do some bounds checking on the values you enter in the ratio array to make sure they are between 0-255.

keep in mind, that when calling drawRect on a graphics object, the x/y/width/height are relative to the object whose graphics object you are manipulating.

hope that helps.

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