How to get the stage dimensions with StageScaleMod

2019-03-30 16:32发布

问题:

If you set the stage.scaleMode to StageScaleMode.SHOW_ALL, your swf may be scaled up or down, and may be padded on the top/bottom or left/right. However, stage.width + height always return the width and height as defined by your swf, and stage.scaleX + Y always return 1. As I understand it, resize events are not thrown. So how do I get the actual scale and dimensions?

I want them for two problems:

  1. I want to fill that padding on the top/bottom or left/right with something only if the user can see it.

  2. I am drawing vectors into bitmaps and want to properly scale it so it doesn't look jagged (or fuzzy with bitmap.smoothing). Flash seems to do the scaling correctly when you cacheAsBitmap=true, so how do I recreate this?

回答1:

In your case it would probably be easier to use StageScaleMode.NO_SCALE and to code the resizing yourself:

stage.addEventListener(Event.RESIZE, onStageResize);
private function onStageResize(event : Event) : void 
    {
    var stageW : int = stage.stageWidth;
    var stageH : int = stage.stageHeight;

    var contentW : int = yourVisibleContent.width;
    var contentH : int = yourVisibleContent.height;

    // resize it to fit
    var canvasAspectRatio : Number = stageW / stageH;
    var contentAspectRatio : Number = contentW / contentH;
    if(canvasAspectRatio > contentAspectRatio)
    {
        yourVisibleContent.height = stageH;
        yourVisibleContent.width = yourVisibleContent.height * contentAspectRatio;
    } else {

        yourVisibleContent.width = stageW;
        yourVisibleContent.height = yourVisibleContent.width / contentAspectRatio;
    }

    // center it:
    yourVisibleContent.x = (stageW - yourVisibleContent.width) / 2;
    yourVisibleContent.y = (stageH - yourVisibleContent.height) / 2;

    // fill remaining space with black:
    graphics.beginFill(0x000000);
    if(canvasAspectRatio > contentAspectRatio)
    {
        var horizontalEmptySpace : Number = stageW - yourVisibleContent.width;
        graphics.drawRect(0, 0, horizontalEmptySpace / 2, stageH);
        graphics.drawRect(stageW - horizontalEmptySpace / 2, 0, horizontalEmptySpace / 2, stageH);
    }else{
        var verticalEmptySpace : Number = stageH - yourVisibleContent.height;
        graphics.drawRect(0, 0, stageW, verticalEmptySpace / 2);
        graphics.drawRect(0, stageH - verticalEmptySpace / 2, stageW, verticalEmptySpace / 2);
    }

    // now you can also redraw your bitmaps with higher resolutions
    // it is easy to read the scale of your content with: yourVisibleContent.scaleX and yourVisibleContent.scaleY
}


回答2:

This is the code I used to fetch the dimensions during swf load and use them to scale bitmaps later, based on bjornson's answer above.

var actualScale :Number;
var actualStageWidth :Number;
var actualStageHeight :Number;

private function collectDimensions () :void
{
    stage.scaleMode = StageScaleMode.NO_SCALE;
    actualStageWidth = stage.stageWidth;
    actualStageHeight = stage.stageHeight;
    var contentWidth :Number = yourVisibleContent.width;
    var contentHeight :Number = yourVisibleContent.height;
    var canvasAspectRatio :Number = actualStageWidth / actualStageHeight;
    var contentAspectRatio :Number = contentWidth / contentHeight;
    if (canvasAspectRatio > contentAspectRatio) {
        actualScale = actualStageHeight / contentHeight;
    } else {
        actualScale = actualStageWidth / contentWidth;
    }
    stage.scaleMode = StageScaleMode.SHOW_ALL;
}

public function createBitmap (clip :MovieClip) :Bitmap
{
    var bitmapData :BitmapData = new BitmapData(clip.width, clip.height);
    var matrix :Matrix = new Matrix();
    matrix.scale(actualScale, actualScale);
    bitmapData.draw(clip, matrix);
    var bitmap :Bitmap = new Bitmap(bitmapData);
    bitmap.scaleX = bitmap.scaleY = 1/actualScale;
    bitmap.smoothing = true;
    return bitmap;
}