BezierCurve.js 2.5 KB

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