NodeConnector.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {BezierCurve} from "../BezierCurve";
  2. /**
  3. * Node connector is used to connect a output of a node to a input of another node.
  4. *
  5. * Some nodes outputs might support multiple connections having an output connected to multiple inputs.
  6. *
  7. * Data always goes from the output node to a input node.
  8. *
  9. * @class NodeConnector
  10. */
  11. function NodeConnector()
  12. {
  13. BezierCurve.call(this);
  14. this.lineWidth = 2;
  15. /**
  16. * Origin output socket that is attached to a node.
  17. *
  18. * @type {NodeSocket}
  19. */
  20. this.outputSocket = null;
  21. /**
  22. * Destination input socket that is attached to a node.
  23. *
  24. * @type {NodeSocket}
  25. */
  26. this.inputSocket = null;
  27. }
  28. NodeConnector.prototype = Object.create(BezierCurve.prototype);
  29. NodeConnector.prototype.destroy = function()
  30. {
  31. BezierCurve.prototype.destroy.call(this);
  32. if(this.outputSocket !== null)
  33. {
  34. this.outputSocket.connector = null;
  35. }
  36. if(this.inputSocket !== null)
  37. {
  38. this.inputSocket.connector = null;
  39. }
  40. };
  41. NodeConnector.prototype.onUpdate = function()
  42. {
  43. if(this.outputSocket !== null)
  44. {
  45. this.from.copy(this.outputSocket.position);
  46. }
  47. if(this.inputSocket !== null)
  48. {
  49. this.to.copy(this.inputSocket.position);
  50. }
  51. // Center control points
  52. this.fromCp.copy(this.from);
  53. this.fromCp.add(this.to);
  54. this.fromCp.multiplyScalar(0.5);
  55. this.toCp.copy(this.fromCp);
  56. var curvature = 0.5;
  57. // Check vertical/horizontal distances
  58. var yDistance = this.to.y - this.from.y;
  59. var xDistance = this.to.x - this.from.x;
  60. // Apply a offset to the control points
  61. if(Math.abs(xDistance) > Math.abs(yDistance))
  62. {
  63. this.toCp.x += xDistance * curvature;
  64. this.fromCp.x -= xDistance * curvature;
  65. }
  66. else
  67. {
  68. this.toCp.y += yDistance * curvature;
  69. this.fromCp.y -= yDistance * curvature;
  70. }
  71. };
  72. export {NodeConnector};