QuadraticCurve.js 1.8 KB

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