AS3放大和缩小鼠标点击时未注册的位置

时间:2012-11-05 21:54:21

标签: actionscript-3 flash flex image-zoom

我试图通过点击和双击鼠标事件来放大和缩小屏蔽鼠标平移图像。我让图像变焦,但它总是放大左边的注册点,而不是我点击的位置。我完全不知道如何对此进行编码,并且花了一整天的时间在互联网上试图找出它没有运气。我希望有人可以帮我解决这个问题!

import com.greensock.*;//Greensock Tweening Platform.

//Variables
var percX:Number;
var percY:Number;
var destX:Number;
var destY:Number;

//Image panned and masked
this.mask = mask_mc;
stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
function mousemove(e:MouseEvent) {
    if (mask_mc.hitTestPoint(stage.mouseX,stage.mouseY,false)) {
        if (imgLoader.width>mask_mc.width) {//Avoids Scrolling if image is under mask area width
            percX = mask_mc.mouseX/mask_mc.width;
        }
        if (imgLoader.height>mask_mc.height) {//Avoids Scrolling if image is under mask area height
            percY = mask_mc.mouseY/mask_mc.height;
        }
        destX = -(imgLoader.width-mask_mc.width)*percX;
        destY = -(imgLoader.height-mask_mc.height)*percY;
        TweenMax.to(imgLoader,.5,{x:destX,y:destY});
    }
}
//Add listeners for the imgLoader movie clip.
imgLoader.doubleClickEnabled = true;  
imgLoader.addEventListener(MouseEvent.CLICK, increaseSize);
imgLoader.addEventListener(MouseEvent.DOUBLE_CLICK, decreaseSize);

//This function increases the scale of the image
function increaseSize(event:MouseEvent):void{
TweenLite.to(imgLoader, 1, {scaleX:2, scaleY:2});
}

//This function decreases the scale of the image
function decreaseSize(event:MouseEvent):void{
TweenLite.to(imgLoader, 1, {scaleX:1, scaleY:1});
}

3 个答案:

答案 0 :(得分:2)

此答案源自here

添加此功能:

function scaleAroundMouse(objectToScale:DisplayObject, scaleAmount:Number, bounds:Rectangle = null, onComplete:Function = null):TweenLite {
    // scaling will be done relatively
    var relScaleX:Number = scaleAmount / objectToScale.scaleX;
    var relScaleY:Number = scaleAmount / objectToScale.scaleY;
    // map vector to centre point within parent scope

    var scalePoint:Point = objectToScale.localToGlobal( new Point(objectToScale.mouseX, objectToScale.mouseY));
    scalePoint = objectToScale.parent.globalToLocal( scalePoint );
    // current registered postion AB
    var AB:Point = new Point( objectToScale.x, objectToScale.y );
    // CB = AB - scalePoint, objectToScale vector that will scale as it runs from the centre
    var CB:Point = AB.subtract( scalePoint );
    CB.x *= relScaleX;
    CB.y *= relScaleY;
    // recaulate AB, objectToScale will be the adjusted position for the clip
    AB = scalePoint.add( CB );
    // set actual properties

    if(bounds){
     var limits:Rectangle = new Rectangle(
        bounds.x + (bounds.width - (objectToScale.width * relScaleX)),
        bounds.y + (bounds.height - (objectToScale.height * relScaleY)),
        (objectToScale.width * relScaleX) - bounds.width,
        (objectToScale.height * relScaleY) - bounds.height
     );

     if(AB.x < limits.x) AB.x = limits.x;
     if(AB.x > limits.x + limits.width) AB.x = limits.x + limits.width;
     if(AB.y < limits.y) AB.y = limits.y;
     if(AB.y > limits.y + limits.height) AB.y = limits.y + limits.height;       
    }

    return TweenLite.to(objectToScale,1,{onComplete: onComplete, scaleX: scaleAmount, scaleY: scaleAmount, x: AB.x, y: AB.y);
}

然后将您的大小调整功能更新为:

function increaseSize(event:MouseEvent):void{
    stopMouseMove();
    scaleAroundMouse(imgLoader, 2, null, resumeMouseMove);
}

function decreaseSize(event:MouseEvent):void{
    stopMouseMove();
    scaleAroundMouse(imgLoader, 1, null, resumeMouseMove);
}

function stopMouseMove():void {
   stage.removeEventListener(MouseEvent.MOUSE_MOVE,mousemove);
}

function resumeMouseMove():void {
   stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
}

我还在函数中添加了一个bounds参数。如果您不希望内容的边缘在蒙版中可见,则此选项非常有用。因此,如果您可以通过将掩码的边界传递给函数来使用它:

scaleAroundMouse(imgLoader, 1, myMask.getBounds(this));

答案 1 :(得分:0)

此示例使用缩放效果类,可帮助您实现所需的缩放效果http://graphics-geek.blogspot.com/2010/06/video-image-zoom-effect-in-flex-4.html

答案 2 :(得分:0)

var mat:Matrix=new Matrix();
mat.translate(-p.x,-p.y);
mat.scale(desiredScale,desiredScale);
mat.translate(p.x,p.y);
yourObject.transform.matrix=mat;

这取自我一个月前发布的问题。你可以看到它here。虽然我最终没有使用那个特定的片段(我实际上已经发布了LondongDrugs_MediaServ脚本的修改版本),但它会起作用并且更容易理解和实现。