Flash AS3线性渐变旋转问题
这是我在这里发表的第一篇文章,所以我希望我一切顺利 - 并找到我的问题的答案。
在 Flash AS3 中,我在矩形中创建动态渐变。我需要渐变旋转 26 度。我的问题是旋转似乎根据矩形的宽度/高度而变化。无论我将其应用到什么尺寸的矩形,我都需要旋转为真正的 26 度。
这是我正在使用的代码:
var rect:Shape=new Shape();
this.addChild(rect);
rect.x=40;
rect.y=70;
var rectWidth:Number=200;
var rectHeight:Number=100;
drawShapes();
function drawShapes():void {
var mat:Matrix;
var colors:Array;
var alphas:Array;
var ratios:Array;
//We proceed to draw 'rect'.
mat=new Matrix();
colors=[0xFF0000, 0x00FF00, 0x001eff];
alphas=[1,1,1];
ratios=[0,120,255];
mat.createGradientBox(rectWidth,rectHeight,toRad(26));
rect.graphics.lineStyle();
rect.graphics.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,mat);
rect.graphics.drawRect(0,0,rectWidth,rectHeight);
rect.graphics.endFill();
}
function toRad(a:Number):Number {
return a*Math.PI/180;
}
This is my first post here so I hope I get everything right - and find an answer to my problem.
In Flash AS3 I'm creating a dynamic gradient in a rectangle. I need the gradient to have a rotation of 26 degrees. My problem is that the rotation seems to change based on the width/height of my rectangle. I need the rotation to be true 26 degrees no matter what size rectangle I apply it to.
Here's the code I'm using:
var rect:Shape=new Shape();
this.addChild(rect);
rect.x=40;
rect.y=70;
var rectWidth:Number=200;
var rectHeight:Number=100;
drawShapes();
function drawShapes():void {
var mat:Matrix;
var colors:Array;
var alphas:Array;
var ratios:Array;
//We proceed to draw 'rect'.
mat=new Matrix();
colors=[0xFF0000, 0x00FF00, 0x001eff];
alphas=[1,1,1];
ratios=[0,120,255];
mat.createGradientBox(rectWidth,rectHeight,toRad(26));
rect.graphics.lineStyle();
rect.graphics.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,mat);
rect.graphics.drawRect(0,0,rectWidth,rectHeight);
rect.graphics.endFill();
}
function toRad(a:Number):Number {
return a*Math.PI/180;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题在于这一行:
如果渐变框的尺寸发生变化,您的渐变也会发生变化。如果您希望渐变始终具有相同的角度,请保持相同的渐变框尺寸,无论矩形尺寸如何。为了使渐变角度为 26 度(或其他),我还会尝试创建一个方形渐变框,否则渐变线的角度和旋转角度将不相等。
试试这个代码,看看这是否是您正在寻找的:
您可以调整渐变框的宽度和高度,直到获得您想要的效果。希望这有帮助!
Your problem is in this line:
Your gradient will change if the dimensions of the gradient box change. If you want your gradient to have always the same angle, keep the same gradient box dimensions regardless of your rectangle dimensions. To get gradient to be at an angle of 26 degrees (or whatever) I would also try to create a square gradient box, otherwise the angle of the gradient lines and the angle of rotation won't be equal.
Try out this code if see if this is what you are looking for:
You can play around with the width and height of the gradient box until you get the effect you want. Hope this helps!