Simulate graph states in the browser
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

interaction.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. };