AS3:在滑块更改时使用矩阵缩放精灵?

发布于 2024-11-25 09:53:29 字数 1176 浏览 1 评论 0原文

我有一个保存位图数据的精灵。 我想让用户能够使用滑块调整图像大小。 我正在使用下面的代码,正如您所看到的,问题在于缩放是相加的,因此图像很快就完全消失了。

我知道我必须以非累加的方式缩放它,只是不知道如何?

我试图通过:

var m:Matrix = userImageCopy.transform.matrix;

当 userImageCopy 保存原始图像时。这有助于缩放,但是每次缩放开始时,userImage 都会跳转到 userImageCopy 的位置。

有什么帮助吗?

function onSliderChange(evt:Event):void
    {
        trace( evt.target.value);
        //this will create a point object at the center of the display object

        var ptRotationPoint:Point = new Point(userImage.x + userImage.width / 2,userImage.y + userImage.height / 2);
        //call the function and pass in the object to be rotated, the amount to scale X and Y (sx, sy), and the point object we created
        scaleFromCenter(userImage, evt.target.value, evt.target.value, ptRotationPoint);
    }

    private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point)
    {
        var m:Matrix = userImage.transform.matrix;
        m.tx -=  ptScalePoint.x;
        m.ty -=  ptScalePoint.y;
        m.scale(sx, sy);
        m.tx += ptScalePoint.x;
        m.ty += ptScalePoint.y;
        ob.transform.matrix = m;
    }

I have a sprite that holds a bitmap data.
I want to give the user the ability to resize the image with a slider.
I am using the code beneath, as you can see the problem is that the scaling is additive so very quickly the image is gone totally.

I understand that i have to scale it in non additive way, just can not figure out how?

I tried to pass :

var m:Matrix = userImageCopy.transform.matrix;

when userImageCopy holds the original image. that helped for the scaling but then, each time the scaling started the userImage jumped to the position of the userImageCopy.

Any help?

function onSliderChange(evt:Event):void
    {
        trace( evt.target.value);
        //this will create a point object at the center of the display object

        var ptRotationPoint:Point = new Point(userImage.x + userImage.width / 2,userImage.y + userImage.height / 2);
        //call the function and pass in the object to be rotated, the amount to scale X and Y (sx, sy), and the point object we created
        scaleFromCenter(userImage, evt.target.value, evt.target.value, ptRotationPoint);
    }

    private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point)
    {
        var m:Matrix = userImage.transform.matrix;
        m.tx -=  ptScalePoint.x;
        m.ty -=  ptScalePoint.y;
        m.scale(sx, sy);
        m.tx += ptScalePoint.x;
        m.ty += ptScalePoint.y;
        ob.transform.matrix = m;
    }

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

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

发布评论

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

评论(1

乜一 2024-12-02 09:53:29

无需将中心值与当前 tx 和 ty 相结合,只需使用 Matrix.translate 方法直接设置它们即可:

private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point)
{
    var m:Matrix = new Matrix();
    m.translate(-ptScalePoint.x,-ptScalePoint.y);
    m.scale(sx, sy);
    m.translate(ptScalePoint.x,ptScalePoint.y);
    ob.transform.matrix = m;
}

Rather than combining your center values with the current tx and ty, just set them directly with the Matrix.translate method:

private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point)
{
    var m:Matrix = new Matrix();
    m.translate(-ptScalePoint.x,-ptScalePoint.y);
    m.scale(sx, sy);
    m.translate(ptScalePoint.x,ptScalePoint.y);
    ob.transform.matrix = m;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文