1. 程式人生 > >laya 下以光標為中心縮放對象

laya 下以光標為中心縮放對象

private 比例 lds scale tar 中心 mouse pre alex

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

  

laya 下以光標為中心縮放對象