laya 下以光标为中心缩放对象

  private MouseWheel(e: Laya.Event) {
        console.log("event");
        let currentSp = e.target as Laya.Sprite;
        let parentSp = currentSp.parent as Laya.Sprite;
        console.log("before scale");
        console.log(parentSp.mouseX, parentSp.mouseY);
        this.Zoom(currentSp, parentSp.mouseX, parentSp.mouseY, e.delta);
    }

    private Zoom(sp: Laya.Sprite, x: number, y: number, delta: number) {
        let oldScale = sp.scaleX;
        let scale = delta > 0 ? 1.1:1/1.1;
        let newScale = sp.scaleX * scale;
        // newScale = newScale < 0.1 ? 0.1 : (newScale > 5 ? 5 : newScale);
        // 获取在缩放比例下,光标相对偏移位置
  
        sp.x = sp.x - (x - sp.x) * (newScale - oldScale) / oldScale;
        sp.y = sp.y - (y - sp.y) * (newScale - oldScale) / oldScale;

        sp.scale(newScale, newScale);
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券