简体   繁体   English

使用AS3放大和缩小

[英]Zoom in and zoom out using AS3

you all know: "right click -> zoom in or out" in flash file, well, I need to do this but using, for example, clicking a button. 你们都知道:“右键单击 - >放大或缩小”在flash文件中,好吧,我需要这样做但是使用,例如,单击按钮。

Is this possible using only AS3 code? 这只能使用AS3代码吗?

Thx! 谢谢!

你必须将所有图形放在一个精灵中,例如“场景”,然后修改它的比例......

Actually if you want to scale/zoomin/zoomout a stage or video object or anything else, multiply and divide should be used instead. 实际上,如果你想缩放/缩放/缩小舞台或视频对象或其他任何东西,应该使用乘法和除法。 Like this (this is what I used when I needed to create zoomin/zoomout functionality for web camera - digital zoom of course using scale functionality): 像这样(这是我在需要为网络摄像头创建缩放/缩放功能时使用的 - 当然使用缩放功能进行数字缩放):

Assume that: 假使,假设:


video_width = 640;
video_height = 480;

stage.scaleMode     = StageScaleMode.NO_SCALE; 
stage.align         = StageAlign.TOP_LEFT;
stage.stageWidth    = video_width;
stage.stageHeight   = video_height;


camera = Camera.getCamera();
// some other params

video = new Video( video_width, video_height );
video.attachCamera(camera);
addChild(video);

// to mirror webcam output:
video.scaleX = -1;
video.scaleY = 1;
video.x = video_width;
video.y = 0;


public function zoom(action:String = 'reset')
{
      var step = 1.1, // adjust this multiplyer for faster zoom. Eg. 1.2, 1.5, etc.
          x_offset = 0, 
          y_offset = 0;

    if (action == 'in')
    {
        video.scaleX *= step;
        video.scaleY *= step;

        x_offset = (stage.stageWidth - video.width)/2;
        y_offset = (stage.stageHeight - video.height)/2;
                // to center video object on stage by X if mirror effect is used
        video.x = stage.stageWidth - x_offset; 
                // to center video object on stage by X - no mirror
                // video.x = x_offset;
        video.y = y_offset; // to center video object on stage by Y
    }
    else if (action == 'out')
    {
            video.scaleX /= step;
        video.scaleY /= step;

        x_offset = (stage.stageWidth - video.width)/2;
        y_offset = (stage.stageHeight - video.height)/2;
        video.x = stage.stageWidth - x_offset;
        video.y = y_offset;
    }
    else
    {
                // need to be mirrored
        video.scaleX = -1; 
                // no mirror
                 // video.scaleX = 1;
        video.scaleY = 1;
                // if mirror video output
        video.x = stage.stageWidth;
               // no mirroring used
               // video.x = 0;
        video.y = 0;
    }       
}

Now you use this by attaching this functions to your buttons: 现在通过将此功能附加到按钮来使用它:


zoom('in');
zoom('out'); 
zoom('reset'); // reset zoom

Not as far as I know. 不是我所知道的。 Curious to see other answers though. 很想看到其他答案。

One hacky thing that comes to mind is, maybe you could use javascript to control a div containing your swf so that the div gets large(zoom in), but is displayed within the same 'scrollRect' rectangle. 想到的一个hacky事情是,也许你可以使用javascript来控制包含你的swf的div,以便div变大(放大),但是显示在同一个'scrollRect'矩形内。 you would call the javascript function to do that using ExternalInterface. 你可以调用javascript函数来使用ExternalInterface来做到这一点。

A quick'n'dirty test I did using flash's scrollPane component: 我使用flash的scrollPane组件做了一个快速的测试:

//zoomIn,zoomOut = button
//sp = scrollPane, scrollDrag = true
zoomIn.addEventListener(MouseEvent.CLICK, zoom);
zoomOut.addEventListener(MouseEvent.CLICK, zoom);

function zoom(event:MouseEvent) {
    var scale:Number = event.currentTarget == zoomIn ? 1 : -1;
    sp.content.scaleX += scale;
    sp.content.scaleY += scale;
    sp.update();
}

Then I've noticed you are using flex, so surely there must be aa container that will allow you a similar functionality. 然后我注意到你正在使用flex,所以肯定必须有一个允许你具有类似功能的容器。

HTH, George HTH,乔治

I would use scaleX and scaleY also, but simply with a number instead of George's solution with a var. 我也会使用scaleX和scaleY,但只是使用数字而不是George的解决方案和var。 So, something like 所以,像

zoomInButton.addEventListener(MouseEvent.MOUSE_DOWN, zoomIn);
zoomOutButton.addEventListener(MouseEvent.MOUSE_DOWN, zoomOut);

function zoomIn(e:MouseEvent) {
  //actions for zoom-in function
  myPicture.scaleX += 10;
  myPicture.scaleY += 10;
}
function zoomOut(e:MouseEvent) {
  //actions for zoom-out function
  myPicture.scaleX += 10;
  myPicture.scaleY += 10;
}

You might stumble upon another obstacle though, which is that the picture gets scaled from and to the upper left corner. 你可能会偶然发现另一个障碍,即图片从左上角缩放到左上角。 In that case try moving the picture with the scaling. 在这种情况下,尝试使用缩放移动图片。 Like 喜欢

function zoomIn(e:MouseEvent) {
  //actions for zoom-in function
  myPicture.scaleX += 10;
  myPicture.scaleY += 10;
  myPicture.x -= 5;
  myPicture.y -= 5;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM