BezierCurve.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Circle} from "./Circle.js";
  4. import {Line} from "./Line.js";
  5. /**
  6. * Bezier curve object draw as bezier curve between two points.
  7. *
  8. * @class
  9. * @extends {Object2D}
  10. */
  11. function BezierCurve()
  12. {
  13. Object2D.call(this);
  14. /**
  15. * Initial point of the curve.
  16. *
  17. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  18. */
  19. this.from = new Vector2();
  20. /**
  21. * Intial position control point, indicates the tangent of the bezier curve on the first point.
  22. */
  23. this.fromCp = new Vector2();
  24. /**
  25. * Final point of the curve.
  26. *
  27. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  28. */
  29. this.to = new Vector2();
  30. /**
  31. * Final position control point, indicates the tangent of the bezier curve on the last point.
  32. */
  33. this.toCp = new Vector2();
  34. /**
  35. * Dash line pattern to be used, if empty draws a solid line.
  36. *
  37. * Dash parttern is defined as the size of dashes as pairs of space with no line and with line.
  38. *
  39. * E.g if the daspattern is [1, 2] we get 1 point with line, 2 without line repeat infinitelly.
  40. */
  41. this.dashPattern = [5, 5];
  42. /**
  43. * Style of the object line.
  44. */
  45. this.strokeStyle = "#000000";
  46. /**
  47. * Line width of the line.
  48. */
  49. this.lineWidth = 1;
  50. }
  51. BezierCurve.prototype = Object.create(Object2D.prototype);
  52. /**
  53. * Create a bezier curve helper, to edit the bezier curve anchor points.
  54. *
  55. * @static
  56. */
  57. BezierCurve.curveHelper = function(object)
  58. {
  59. var fromCp = new Circle();
  60. fromCp.radius = 3;
  61. fromCp.layer = object.layer + 1;
  62. fromCp.draggable = true;
  63. fromCp.onPointerDrag = function(pointer, viewport, delta)
  64. {
  65. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  66. object.fromCp.copy(fromCp.position);
  67. };
  68. object.parent.add(fromCp);
  69. var fromLine = new Line();
  70. fromLine.from = object.from;
  71. fromLine.to = object.fromCp;
  72. object.parent.add(fromLine);
  73. var toCp = new Circle();
  74. toCp.radius = 3;
  75. toCp.layer = object.layer + 1;
  76. toCp.draggable = true;
  77. toCp.onPointerDrag = function(pointer, viewport, delta)
  78. {
  79. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  80. object.toCp.copy(toCp.position);
  81. };
  82. object.parent.add(toCp);
  83. var toLine = new Line();
  84. toLine.from = object.to;
  85. toLine.to = object.toCp;
  86. object.parent.add(toLine);
  87. };
  88. BezierCurve.prototype.draw = function(context, viewport, canvas)
  89. {
  90. context.lineWidth = this.lineWidth;
  91. context.strokeStyle = this.strokeStyle;
  92. context.setLineDash(this.dashPattern);
  93. context.beginPath();
  94. context.moveTo(this.from.x, this.from.y);
  95. context.bezierCurveTo(this.fromCp.x, this.fromCp.y, this.toCp.x, this.toCp.y, this.to.x, this.to.y);
  96. context.stroke();
  97. };
  98. export {BezierCurve};