Flash:当渐变填充在其父级边界之外绘制时会消失吗?
我尝试使用 beginGradientFill
和 drawRect
在 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
的边界内时,一切正常:
但是,如果矩形位于容器边界之外,则根本不会绘制渐变。例如,如果代码更改为:
...
var x:Number = 10;
var y:Number = 10;
...
那么渐变消失:
缺少绘制绘制所需的数学运算框在父级边界内(并修复渐变,使其看起来正确),有什么方法可以处理这个问题吗?
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:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的问题是您在比率计算中使用了 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 whosegraphics
object you are manipulating.hope that helps.