Simulate graph states in the browser
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

59 líneas
2.0KB

  1. var mouse = {};
  2. var interaction = {};
  3. interaction.raycaster = new THREE.Raycaster();
  4. interaction.xyplane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  5. // Gets a reference to the node nearest to the mouse cursor
  6. interaction.nearestNode = function() {
  7. this.raycaster.setFromCamera(mouse, camera);
  8. for (var i = 0; i < nodeGeometry.vertices.length; ++i) {
  9. var v = nodeGeometry.vertices[i];
  10. if (this.raycaster.ray.distanceSqToPoint(v) < 0.01) {
  11. return i;
  12. }
  13. }
  14. return undefined;
  15. };
  16. // Find out: what is the mouse pointing at?
  17. interaction.checkIntersections = function() {
  18. var new_selection = nearestNode();
  19. if (new_selection != this.selection) {
  20. this.selection = new_selection;
  21. info.className = this.selection ? "visible" : "hidden";
  22. info.innerHTML = this.selection ? nodeGeometry.labels[new_selection] : info.innerHTML;
  23. render();
  24. }
  25. };
  26. // Update the mouse position tracker
  27. interaction.onMouseMove = function(event) {
  28. mouse.wasClick = false;
  29. mouse.absx = event.clientX;
  30. mouse.absy = event.clientY;
  31. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  32. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  33. //w = 200; //h = 15; //info.style.top = mouse.absy - h - 40 + "px"; //info.style.left = mouse.absx - w / 2 + "px"; //checkIntersections();
  34. };
  35. // Try to add a qubit at the current mouse position
  36. interaction.addQubitAtMouse = function(event) {
  37. this.raycaster.setFromCamera(mouse, camera);
  38. var intersection = this.raycaster.ray.intersectPlane(this.plane);
  39. intersection.x = Math.round(intersection.x);
  40. intersection.y = Math.round(intersection.y);
  41. abj.add_node(Object.keys(vops).length, {
  42. "position": intersection
  43. });
  44. updateScene();
  45. }
  46. interaction.bind = function() {
  47. var el = renderer.domElement;
  48. el.addEventListener("mousedown", this.onMouseDown);
  49. el.addEventListener("mouseup", this.onMouseDown);
  50. el.addEventListener("mousemove", this.onMouseMove);
  51. };