How to get the stage dimensions with StageScaleMode.SHOW_ALL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 16:44:51

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
}

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!