Helpers.js 2.8 KB

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