Helpers.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {Circle} from "../objects/Circle.js";
  2. import {Object2D} from "../Object2D.js";
  3. /**
  4. * Class contains helper functions to create editing object tools.
  5. *
  6. * @class
  7. */
  8. function Helpers(){}
  9. /**
  10. * Create a rotation tool helper.
  11. *
  12. * When the object is dragged is changes the parent object rotation.
  13. *
  14. * @static
  15. */
  16. Helpers.rotateTool = function(object)
  17. {
  18. var tool = new Circle();
  19. tool.radius = 4;
  20. tool.layer = object.layer + 1;
  21. tool.onPointerDrag = function(pointer, viewport, delta)
  22. {
  23. object.rotation += delta.x * 1e-3;
  24. };
  25. object.add(tool);
  26. };
  27. /**
  28. * Create a box resize helper and attach it to an object to change the size of the object box.
  29. *
  30. * Each helper is positioned on one corner of the box, and the value of the corner is copied to the boxes as they are dragged.
  31. *
  32. * This method required to object to have a box property.
  33. *
  34. * @static
  35. */
  36. Helpers.boxResizeTool = function(object)
  37. {
  38. if(object.box === undefined)
  39. {
  40. console.warn("escher.js: Helpers.boxResizeTool(), object box property missing.");
  41. return;
  42. }
  43. function updateHelpers()
  44. {
  45. topRight.position.copy(object.box.min);
  46. bottomLeft.position.copy(object.box.max);
  47. topLeft.position.set(object.box.max.x, object.box.min.y);
  48. bottomRight.position.set(object.box.min.x, object.box.max.y);
  49. }
  50. var topRight = new Circle();
  51. topRight.radius = 4;
  52. topRight.layer = object.layer + 1;
  53. topRight.draggable = true;
  54. topRight.onPointerDrag = function(pointer, viewport, delta)
  55. {
  56. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  57. object.box.min.copy(topRight.position);
  58. updateHelpers();
  59. };
  60. object.add(topRight);
  61. var topLeft = new Circle();
  62. topLeft.radius = 4;
  63. topLeft.layer = object.layer + 1;
  64. topLeft.draggable = true;
  65. topLeft.onPointerDrag = function(pointer, viewport, delta)
  66. {
  67. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  68. object.box.max.x = topLeft.position.x;
  69. object.box.min.y = topLeft.position.y;
  70. updateHelpers();
  71. };
  72. object.add(topLeft);
  73. var bottomLeft = new Circle();
  74. bottomLeft.radius = 4;
  75. bottomLeft.layer = object.layer + 1;
  76. bottomLeft.draggable = true;
  77. bottomLeft.onPointerDrag = function(pointer, viewport, delta)
  78. {
  79. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  80. object.box.max.copy(bottomLeft.position);
  81. updateHelpers();
  82. };
  83. object.add(bottomLeft);
  84. var bottomRight = new Circle();
  85. bottomRight.radius = 4;
  86. bottomRight.layer = object.layer + 1;
  87. bottomRight.draggable = true;
  88. bottomRight.onPointerDrag = function(pointer, viewport, delta)
  89. {
  90. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  91. object.box.min.x = bottomRight.position.x;
  92. object.box.max.y = bottomRight.position.y;
  93. updateHelpers();
  94. };
  95. object.add(bottomRight);
  96. updateHelpers();
  97. };
  98. export {Helpers};