gpt4 book ai didi

javascript - 实现点击拖动旋转3D对象 - vue.js

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:22 25 4
gpt4 key购买 nike

我有一个按钮,单击它可以激活旋转功能。之后,每次我将鼠标悬停在对象上时,对象都会按照我的意愿旋转。

现在我想更改它并通过单击对象并移动鼠标(同时仍按住鼠标按钮)使对象旋转。当我释放鼠标按钮时,我希望它停止旋转。

我的代码:

HTML:

<div class="model__3d" ref="panel3d">   
<a class="button" @click.prevent="rotatemouse()">360</a>

代码js:

rotatemouse() {

document.querySelector('.model__3d').addEventListener('mousedown', () =>{
document.onmousemove = handleMouseMove;
});

//document.onmouseup = function () {
document.querySelector('.model__3d').addEventListener('mouseup', () =>{
document.onmousemove = null;
});


//function handleMouseMove(event) {
document.querySelector('.model__3d').addEventListener('mousemove', (event) =>{
let eventDoc, doc, body;

event = event || window.event;

if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;

event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
}

if(maxX == 0){
maxX = event.pageX;
}

if(maxY == 0){
maxY = event.pageY;
}

if(event.pageX > maxX){
console.log("Right");
this.model.rotation.y = this.model.rotation.y + 0.08;
maxX = event.pageX;
}
else {
console.log("Left");
this.model.rotation.y = this.model.rotation.y - 0.08;
maxX = event.pageX;
}

if(event.pageY > maxY){
console.log("Up");
if(this.model.rotation.x < 0.32)
this.model.rotation.x = this.model.rotation.x + 0.08;

console.log(this.model.rotation.x);

maxY = event.pageY;
}
else {
console.log("Down");
if(this.model.rotation.x > -0.25)
this.model.rotation.x = this.model.rotation.x - 0.08;

console.log(this.model.rotation.x);
maxY = event.pageY;
}
});
}

谢谢!

最佳答案

您可以通过组合使用@mousemove, @mousedown, @mouseup

new Vue({
el: '#app',
data: {
captureToggle: false,
x: 0,
y: 0
},
methods: {
mo: function(evt) {
if (this.captureToggle) {
this.x = evt.x
this.y = evt.y
}
},
captureOn: function() {
this.captureToggle = true
},
captureOff: function() {
this.captureToggle = false
}
}
})
#app {
margin: 50px;
}

.mouseArea {
height: 100px;
width: 300px;
border: solid thin;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>
<div id="app">
<div class="mouseArea" @mousedown="captureOn" @mouseup="captureOff" @mousemove="mo"></div>
<div>x : {{x}}</div>
<div>y : {{y}}</div>
</div>

关于javascript - 实现点击拖动旋转3D对象 - vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48929712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com