BezierCurve.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * Bezier curve data is composed of two anchor points, one for the start of the curve and one for the end of the curve.
  9. *
  10. * @class
  11. * @extends {Line}
  12. */
  13. function BezierCurve()
  14. {
  15. Line.call(this);
  16. /**
  17. * Initial position control point, indicates the tangent of the bezier curve on the first point.
  18. *
  19. * @type {Vector2}
  20. */
  21. this.fromCp = new Vector2();
  22. /**
  23. * Final position control point, indicates the tangent of the bezier curve on the last point.
  24. *
  25. * @type {Vector2}
  26. */
  27. this.toCp = new Vector2();
  28. }
  29. BezierCurve.prototype = Object.create(Line.prototype);
  30. BezierCurve.prototype.constructor = BezierCurve;
  31. BezierCurve.prototype.type = "BezierCurve";
  32. Object2D.register(BezierCurve, "BezierCurve");
  33. /**
  34. * Create a bezier curve helper, to edit the bezier curve anchor points.
  35. *
  36. * Helper objects are added to the parent of the curve object.
  37. *
  38. * @static
  39. * @param {BezierCurve} object Object to create the helper for.
  40. */
  41. BezierCurve.curveHelper = function(object)
  42. {
  43. var fromCp = new Circle();
  44. fromCp.radius = 3;
  45. fromCp.layer = object.layer + 1;
  46. fromCp.draggable = true;
  47. fromCp.onPointerDrag = function(pointer, viewport, delta)
  48. {
  49. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  50. object.fromCp.copy(fromCp.position);
  51. };
  52. object.parent.add(fromCp);
  53. var fromLine = new Line();
  54. fromLine.from = object.from;
  55. fromLine.to = object.fromCp;
  56. object.parent.add(fromLine);
  57. var toCp = new Circle();
  58. toCp.radius = 3;
  59. toCp.layer = object.layer + 1;
  60. toCp.draggable = true;
  61. toCp.onPointerDrag = function(pointer, viewport, delta)
  62. {
  63. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  64. object.toCp.copy(toCp.position);
  65. };
  66. object.parent.add(toCp);
  67. var toLine = new Line();
  68. toLine.from = object.to;
  69. toLine.to = object.toCp;
  70. object.parent.add(toLine);
  71. };
  72. BezierCurve.prototype.draw = function(context, viewport, canvas)
  73. {
  74. context.beginPath();
  75. context.moveTo(this.from.x, this.from.y);
  76. context.bezierCurveTo(this.fromCp.x, this.fromCp.y, this.toCp.x, this.toCp.y, this.to.x, this.to.y);
  77. context.stroke();
  78. };
  79. BezierCurve.prototype.serialize = function(recursive)
  80. {
  81. var data = Line.prototype.serialize.call(this, recursive);
  82. data.fromCp = this.fromCp.toArray();
  83. data.toCp = this.toCp.toArray();
  84. return data;
  85. };
  86. BezierCurve.prototype.parse = function(data, root)
  87. {
  88. Line.prototype.parse.call(this, data, root);
  89. this.fromCp.fromArray(data.fromCp);
  90. this.toCp.fromArray(data.toCp);
  91. };
  92. export {BezierCurve};