QuadraticCurve.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * Quadratic curve object draw as quadratic curve between two points.
  7. *
  8. * Quadratic 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 {Object2D}
  12. */
  13. function QuadraticCurve()
  14. {
  15. Line.call(this);
  16. /**
  17. * Control point of the quadratic curve used to control the curvature of the line between the from and to point.
  18. *
  19. * The curve is interpolated in the direction of the control point it defined the path of the curve.
  20. *
  21. * @type {Vector2}
  22. */
  23. this.controlPoint = new Vector2();
  24. }
  25. QuadraticCurve.prototype = Object.create(Line.prototype);
  26. QuadraticCurve.prototype.constructor = QuadraticCurve;
  27. QuadraticCurve.prototype.type = "QuadraticCurve";
  28. Object2D.register(QuadraticCurve, "QuadraticCurve");
  29. /**
  30. * Create a quadratic curve helper, to edit the curve control point.
  31. *
  32. * Helper objects are added to the parent of the curve object.
  33. *
  34. * @static
  35. * @param {QuadraticCurve} object Object to create the helper for.
  36. */
  37. QuadraticCurve.curveHelper = function(object)
  38. {
  39. var fromLine = new Line();
  40. fromLine.from = object.from;
  41. fromLine.to = object.controlPoint;
  42. object.parent.add(fromLine);
  43. var controlPoint = new Circle();
  44. controlPoint.radius = 3;
  45. controlPoint.layer = object.layer + 1;
  46. controlPoint.draggable = true;
  47. controlPoint.position = object.controlPoint;
  48. controlPoint.onPointerDrag = function(pointer, viewport, delta)
  49. {
  50. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  51. object.controlPoint.copy(controlPoint.position);
  52. };
  53. object.parent.add(controlPoint);
  54. var toLine = new Line();
  55. toLine.from = object.to;
  56. toLine.to = object.controlPoint;
  57. object.parent.add(toLine);
  58. };
  59. QuadraticCurve.prototype.draw = function(context, viewport, canvas)
  60. {
  61. context.beginPath();
  62. context.moveTo(this.from.x, this.from.y);
  63. context.quadraticCurveTo(this.controlPoint.x, this.controlPoint.y, this.to.x, this.to.y);
  64. context.stroke();
  65. };
  66. QuadraticCurve.prototype.serialize = function(recursive)
  67. {
  68. var data = Line.prototype.serialize.call(this, recursive);
  69. data.controlPoint = this.controlPoint.toArray();
  70. return data;
  71. };
  72. QuadraticCurve.prototype.parse = function(data, root)
  73. {
  74. Line.prototype.parse.call(this, data, root);
  75. this.controlPoint.fromArray(data.controlPoint);
  76. };
  77. export {QuadraticCurve};