4

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?

Sarah Northway
  • 1,029
  • 1
  • 14
  • 24

2 Answers2

3

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
}
Björn Kechel
  • 7,933
  • 3
  • 54
  • 57
  • Thank you for posting the code! It worked for my two requirements but caused havok with my existing mouse and positioning code. Rather than scale all of that as well, I decided to keep using SHOW_ALL, but briefly switch to NO_SCALE when the swf loads and use your code to collect the actual scale and dimensions. This means I can't listen for resizing, but the only side effect in my case is crappier looking bitmaps. – Sarah Northway Mar 29 '11 at 18:58
  • You should keep it fixed with NO_SCALE and just update your mouse and positioning code to use the localToGlobal and globalToLocal to convert from points on one object to coordinates on another object. If it messed up your code then your code was probably making assumptions about local coordinates being the same as global coordinates, which is wrong to begin with. – Triynko May 28 '13 at 18:26
1

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;
}
Sarah Northway
  • 1,029
  • 1
  • 14
  • 24