jquery 如何通过移动或拖动鼠标来旋转A帧中的框?

dfuffjeb  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(151)

如何通过移动或拖动鼠标来旋转A帧中的框?
我试着这样做:http://codepen.io/jordizle/pen/haIdo/https://jsfiddle.net/MadLittleMods/n6u6asza/
这是我的代码。

  1. <html>
  2. <head>
  3. <title>Rotation</title>
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  5. <script src="https://aframe.io/releases/0.4.0/aframe.min.js"></script>
  6. </head>
  7. <body>
  8. <a-scene>
  9. <a-assets>
  10. <a-image id="a" src="Background.jpg">
  11. </a-assets>
  12. <a-box id="b1" src="#a" position="2 2 -3"></a-box>
  13. <a-box id="b2" src="#a" position="-2 2 -3"></a-box>
  14. <a-camera position="0 0 0">
  15. <a-cursor></a-cursor>
  16. </a-camera>
  17. </a-scene>
  18. <script type="text/javascript">
  19. var box=document.querySelector('a-box');
  20. var isDragging=false;
  21. box.addEventListener('mousedown', function() {
  22. isDragging=true;
  23. });
  24. box.addEventListener('mousemove',function(event) {
  25. if(isDragging)
  26. {
  27. box.setAttribute('rotation', {x:event.clientX , y:event.clientY, z:0});
  28. }
  29. });
  30. box.addEventListener('mouseleave', function() {
  31. if(isDraggging)
  32. {
  33. isDragging=false;
  34. }
  35. });
  36. </script>
  37. </body>
  38. </html>

字符串

5gfr0r5j

5gfr0r5j1#

注册一个新的组件来控制拖动旋转怎么样?首先,你需要禁用look-controls,因为look-controls也使用拖动操作。

  1. <html>
  2. <head>
  3. <title>Rotation</title>
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  5. <script src="https://aframe.io/releases/0.4.0/aframe.min.js"></script>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. AFRAME.registerComponent('drag-rotate-component',{
  10. schema : { speed : {default:1}},
  11. init : function(){
  12. this.ifMouseDown = false;
  13. this.x_cord = 0;
  14. this.y_cord = 0;
  15. document.addEventListener('mousedown',this.OnDocumentMouseDown.bind(this));
  16. document.addEventListener('mouseup',this.OnDocumentMouseUp.bind(this));
  17. document.addEventListener('mousemove',this.OnDocumentMouseMove.bind(this));
  18. },
  19. OnDocumentMouseDown : function(event){
  20. this.ifMouseDown = true;
  21. this.x_cord = event.clientX;
  22. this.y_cord = event.clientY;
  23. },
  24. OnDocumentMouseUp : function(){
  25. this.ifMouseDown = false;
  26. },
  27. OnDocumentMouseMove : function(event)
  28. {
  29. if(this.ifMouseDown)
  30. {
  31. var temp_x = event.clientX-this.x_cord;
  32. var temp_y = event.clientY-this.y_cord;
  33. if(Math.abs(temp_y)<Math.abs(temp_x))
  34. {
  35. this.el.object3D.rotateY(temp_x*this.data.speed/1000);
  36. }
  37. else
  38. {
  39. this.el.object3D.rotateX(temp_y*this.data.speed/1000);
  40. }
  41. this.x_cord = event.clientX;
  42. this.y_cord = event.clientY;
  43. }
  44. }
  45. });
  46. </script>
  47. <a-scene>
  48. <a-box id="b1" color="green" position="2 2 -3" drag-rotate-component></a-box>
  49. <a-box id="b2" color="blue" position="-2 2 -3"></a-box>
  50. <a-camera position="0 0 0" look-controls="enabled:false">
  51. <a-cursor></a-cursor>
  52. </a-camera>
  53. </a-scene>
  54. </body>
  55. </html>

字符串

展开查看全部

相关问题