Simulate graph states in the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mouse.js 1.8KB

7 jaren geleden
7 jaren geleden
7 jaren geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var mouse = {};
  2. mouse.wasClick = true;
  3. mouse.pressed = false;
  4. mouse.leniency = 4;
  5. mouse.raycaster = new THREE.Raycaster();
  6. mouse.onFreeMove = function() {
  7. console.log("Free move");
  8. };
  9. mouse.onDrag = function() {
  10. //console.log("Drag");
  11. };
  12. mouse.onClick = function() {
  13. console.log("Click");
  14. };
  15. mouse.onCtrlClick = function() {
  16. console.log("Ctrl-click");
  17. };
  18. mouse.onShiftClick = function() {
  19. console.log("Shift-click");
  20. };
  21. mouse.prepare = function() {
  22. var el = gui.renderer.domElement;
  23. el.addEventListener("mousedown", mouse.onDown);
  24. el.addEventListener("mouseup", mouse.onUp);
  25. el.addEventListener("mousemove", mouse.onMove);
  26. };
  27. mouse.onDown = function(event) {
  28. mouse.wasClick = true;
  29. mouse.pressed = true;
  30. mouse.startX = event.clientX;
  31. mouse.startY = event.clientY;
  32. };
  33. mouse.onUp = function(event) {
  34. mouse.pressed = false;
  35. if (!mouse.wasClick) {
  36. return;
  37. }
  38. if (event.ctrlKey) {
  39. mouse.onCtrlClick();
  40. } else if (event.shiftKey) {
  41. mouse.onShiftClick();
  42. } else {
  43. mouse.onClick();
  44. }
  45. };
  46. mouse.onMove = function(event) {
  47. // TODO: wasclick sux
  48. if (Math.abs(event.clientX - mouse.startX)>mouse.leniency || Math.abs(event.clientY - mouse.startY)>mouse.leniency){
  49. mouse.wasClick = false;
  50. }
  51. mouse.position_absolute = {
  52. x: event.clientX,
  53. y: event.clientY
  54. };
  55. mouse.position_relative = {
  56. x: (event.clientX / window.innerWidth) * 2 - 1,
  57. y: -(event.clientY / window.innerHeight) * 2 + 1
  58. };
  59. gui.setInfoPosition(mouse.position_absolute);
  60. mouse.raycaster.setFromCamera(mouse.position_relative, gui.camera);
  61. mouse.ray = mouse.raycaster.ray;
  62. if (mouse.pressed) {
  63. mouse.onDrag();
  64. } else {
  65. mouse.onFreeMove();
  66. }
  67. // Update editor
  68. editor.gridTimeOut = 0;
  69. };