BezierCurve.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /**
  29. * Create a bezier curve helper, to edit the bezier curve anchor points.
  30. *
  31. * Helper objects are added to the parent of the curve object.
  32. *
  33. * @static
  34. * @param {BezierCurve} object Object to create the helper for.
  35. */
  36. BezierCurve.curveHelper = function(object)
  37. {
  38. var fromCp = new Circle();
  39. fromCp.radius = 3;
  40. fromCp.layer = object.layer + 1;
  41. fromCp.draggable = true;
  42. fromCp.onPointerDrag = function(pointer, viewport, delta)
  43. {
  44. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  45. object.fromCp.copy(fromCp.position);
  46. };
  47. object.parent.add(fromCp);
  48. var fromLine = new Line();
  49. fromLine.from = object.from;
  50. fromLine.to = object.fromCp;
  51. object.parent.add(fromLine);
  52. var toCp = new Circle();
  53. toCp.radius = 3;
  54. toCp.layer = object.layer + 1;
  55. toCp.draggable = true;
  56. toCp.onPointerDrag = function(pointer, viewport, delta)
  57. {
  58. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  59. object.toCp.copy(toCp.position);
  60. };
  61. object.parent.add(toCp);
  62. var toLine = new Line();
  63. toLine.from = object.to;
  64. toLine.to = object.toCp;
  65. object.parent.add(toLine);
  66. };
  67. BezierCurve.prototype.draw = function(context, viewport, canvas)
  68. {
  69. context.beginPath();
  70. context.moveTo(this.from.x, this.from.y);
  71. context.bezierCurveTo(this.fromCp.x, this.fromCp.y, this.toCp.x, this.toCp.y, this.to.x, this.to.y);
  72. context.stroke();
  73. };
  74. export {BezierCurve};