BezierCurve.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Helper objects are added to the parent of the curve object.
  56. *
  57. * @static
  58. * @param {BezierCurve} object Object to create the helper for.
  59. */
  60. BezierCurve.curveHelper = function(object)
  61. {
  62. var fromCp = new Circle();
  63. fromCp.radius = 3;
  64. fromCp.layer = object.layer + 1;
  65. fromCp.draggable = true;
  66. fromCp.onPointerDrag = function(pointer, viewport, delta)
  67. {
  68. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  69. object.fromCp.copy(fromCp.position);
  70. };
  71. object.parent.add(fromCp);
  72. var fromLine = new Line();
  73. fromLine.from = object.from;
  74. fromLine.to = object.fromCp;
  75. object.parent.add(fromLine);
  76. var toCp = new Circle();
  77. toCp.radius = 3;
  78. toCp.layer = object.layer + 1;
  79. toCp.draggable = true;
  80. toCp.onPointerDrag = function(pointer, viewport, delta)
  81. {
  82. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  83. object.toCp.copy(toCp.position);
  84. };
  85. object.parent.add(toCp);
  86. var toLine = new Line();
  87. toLine.from = object.to;
  88. toLine.to = object.toCp;
  89. object.parent.add(toLine);
  90. };
  91. BezierCurve.prototype.draw = function(context, viewport, canvas)
  92. {
  93. context.lineWidth = this.lineWidth;
  94. context.strokeStyle = this.strokeStyle;
  95. context.setLineDash(this.dashPattern);
  96. context.beginPath();
  97. context.moveTo(this.from.x, this.from.y);
  98. context.bezierCurveTo(this.fromCp.x, this.fromCp.y, this.toCp.x, this.toCp.y, this.to.x, this.to.y);
  99. context.stroke();
  100. };
  101. export {BezierCurve};