【问题标题】:Zoom in and zoom out using AS3使用 AS3 放大和缩小
【发布时间】:2010-04-07 21:49:28
【问题描述】:

你们都知道:在 Flash 文件中“右键单击 -> 放大或缩小”,好吧,我需要这样做,但使用例如单击按钮。

这是否可能只使用 AS3 代码?

谢谢!

【问题讨论】:

    标签: apache-flex actionscript-3 zooming out


    【解决方案1】:

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

    【讨论】:

    • 是的.. 可以在 Flex 中使用,但是... 我仍然没有使用原生 Flash 缩放。不知道有没有办法使用,暂时就用这个解决方案,thx。
    【解决方案2】:

    据我所知没有。不过很想看到其他答案。

    我想到的一个 hacky 事情是,也许你可以使用 javascript 控制包含您的 swf 的 div 以使 div 变大(放大), 但显示在同一个“scrollRect”矩形内。你会打电话给 使用 ExternalInterface 执行此操作的 javascript 函数。

    我使用 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();
    }
    

    然后我注意到您正在使用 flex,所以肯定有一个容器可以为您提供类似的功能。

    HTH, 乔治

    【讨论】:

      【解决方案3】:

      我也会使用 scaleX 和 scaleY,但只是使用数字而不是 George 的带 var 的解决方案。所以,像

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

      不过,您可能会遇到另一个障碍,那就是图片从左上角缩放到左上角。在这种情况下,请尝试使用缩放移动图片。喜欢

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

      【讨论】:

        【解决方案4】:

        实际上,如果您想缩放/放大/缩小舞台或视频对象或其他任何东西,则应使用乘法和除法。像这样(这是我在需要为网络摄像头创建放大/缩小功能时使用的 - 当然使用缩放功能的数字缩放):

        假设:

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

        现在您可以通过将此功能附加到您的按钮来使用它:

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

        【讨论】:

          猜你喜欢
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-28
          相关资源
          最近更新 更多