Helpers.js 2.6 KB

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