2
0

QuadraticCurve.js 1.8 KB

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