Search code examples
actionscript-3graphicslinestrokelinestyle

ActionScript Aligning Graphics Line Style Stroke?


is it possible to align the stroke of a graphic with actionscript? for example, the following code creates a black rounded rect with a grey stroke that is automatically centre aligned.

var t:Sprite = new Sprite();
t.graphics.lineStyle(5, 0x555555);
t.graphics.beginFill(0, 1);
t.graphics.drawRoundRect(25, 25, 200, 75, 25, 25);
t.graphics.endFill();

the lineStyle function doesn't seem to offer any built-in functionality for aligning the stroke. in Adobe Illustrator, you can align a stroke to be either centre (half in/half out of the fill), inside (bordering within the fill) or outside. (bordering outside the fill).


Solution

  • This is not supported in Flash (even in the GUI). You would have to modify the drawRoundRect parameters to simulate this effect.

    var strokeWidth:Number = 5;
    var strokeAlign:String = 'outer';
    var t:Sprite = new Sprite();
    t.graphics.lineStyle(strokeWidth, 0x555555);
    t.graphics.beginFill(0, 1);
    if (strokeAlign == 'outer') {
        t.graphics.drawRoundRect(25 - strokeWidth / 2, 25 - strokeWidth / 2, 200 + strokeWidth, 75 + strokeWidth, 25 + strokeWidth / 2, 25 + strokeWidth / 2);
    } else if (strokeAlign == 'inner') {
        t.graphics.drawRoundRect(25 + strokeWidth / 2, 25 + strokeWidth / 2, 200 - strokeWidth, 75 - strokeWidth, 25 - strokeWidth / 2, 25 - strokeWidth / 2);
    } else {
        t.graphics.drawRoundRect(25, 25, 200, 75, 25, 25);
    }
    t.graphics.endFill();