当前位置:   article > 正文

Laya鼠标点击移动旋转,滚轮缩放_laya 鼠标滚轮

laya 鼠标滚轮
function chromeInit(mod){
    var rotate = new Laya.Vector3(0,0,0);
    var lastPosition = new Laya.Vector2(0,0);
    var mousedown = false;

    Laya.stage.on(Laya.Event.MOUSE_DOWN,this, mouseDown);
    Laya.stage.on(Laya.Event.MOUSE_MOVE,this, mouseMove);
    Laya.stage.on(Laya.Event.MOUSE_UP,this, mouseUp)
	Laya.stage.on(Laya.Event.MOUSE_OUT,this, mouseUp);
	Laya.stage.on(Laya.Event.MOUSE_WHEEL,this,mouseWheel)

	function mouseDown(){
		lastPosition = new Laya.Vector2(Laya.stage.mouseX,Laya.stage.mouseY);
		mousedown = true;
	}

	function mouseMove(){
		if (mousedown) {
			//移动触碰点
			let deltaY = Laya.stage.mouseY - lastPosition.y;
			let deltaX = Laya.stage.mouseX - lastPosition.x;
			lastPosition.x = Laya.stage.mouseX;
			lastPosition.y = Laya.stage.mouseY;
			//根据移动的距离进行旋转
			rotate.setValue(0, 1 * deltaX / 2, 0);
			mod.transform.rotate(rotate,false,false);
		}
	}

	function mouseUp(){
		mousedown = false;
    }
	
	function mouseWheel(e){
		let currScale = mod.transform.scale;
		let newScale;
		if (e.delta > 0) {
			newScale = currScale.x + 0.1 > 2 ? 2 : (currScale.x + 0.1 < 0.2 ? 0.2 : (currScale.x + 0.1));
		} else {
			newScale = currScale.x - 0.1 > 2 ? 2 : (currScale.x - 0.1 < 0.2 ? 0.2 : (currScale.x - 0.1));
		}
		mod.transform.scale = new Laya.Vector3(newScale,newScale,newScale);
	}

    function offRotate(){
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this, mouseDown);
        Laya.stage.off(Laya.Event.MOUSE_MOVE,this, mouseMove);
        Laya.stage.off(Laya.Event.MOUSE_UP,this, mouseUp)
        Laya.stage.off(Laya.Event.MOUSE_OUT,this, mouseUp);
    }
    return offRotate;
}

export {
    chromeInit
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/214604
推荐阅读
  

闽ICP备14008679号