如何使用StageScaleMode.SHOW_ALL获取舞台尺寸?

发布于 2024-10-27 09:43:14 字数 407 浏览 3 评论 0原文

如果将 stage.scaleMode 设置为 StageScaleMode.SHOW_ALL,则 swf 可能会放大或缩小,并且可能会在顶部/底部或左侧/右侧进行填充。但是,stage.width + height 始终返回 swf 定义的宽度和高度,而 stage.scaleX + Y 始终返回 1。据我了解,不会引发调整大小事件。那么如何获得实际的比例和尺寸呢?

我想要它们解决两个问题:

  1. 只有当用户可以看到它时,我才想用某些东西填充顶部/底部或左/右的填充。

  2. 我正在将矢量绘制为位图,并希望正确缩放它,这样它就不会看起来锯齿状(或使用 bitmap.smoothing 变得模糊)。当您cacheAsBitmap=true时,Flash似乎可以正确地进行缩放,那么我该如何重新创建它

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-11-03 09:43:14

在您的情况下,使用 StageScaleMode.NO_SCALE 并自行编码调整大小可能会更容易:

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
}

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
}
小情绪 2024-11-03 09:43:14

这是我用来在 swf 加载期间获取尺寸并在稍后根据 bjornson 的答案使用它们缩放位图的代码。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文