not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.8 KiB

  1. /* Text mode will paste a text onto the canvas */
  2. define([], function () {
  3. function initGui() {
  4. PaintApp.elements.textButton = document.getElementById('text-button');
  5. PaintApp.paletteModesButtons.push(PaintApp.elements.textButton);
  6. new PaintApp.palettes.textPalette.TextPalette(PaintApp.elements.textButton, undefined);
  7. PaintApp.elements.textFontSelect = document.getElementById('text-font-select');
  8. PaintApp.elements.textInput = document.getElementById('text-input');
  9. PaintApp.elements.textButton.addEventListener('click', function () {
  10. PaintApp.paletteRemoveActiveClass();
  11. PaintApp.addActiveClassToElement(PaintApp.elements.textButton);
  12. PaintApp.switchMode('Text');
  13. });
  14. }
  15. var Text = {
  16. initGui: initGui,
  17. defaultSize: 25,
  18. fontFamily: 'Arial',
  19. onMouseDown: function (event) {
  20. PaintApp.handleCurrentFloatingElement();
  21. text = PaintApp.elements.textInput.value;
  22. if (!text) {
  23. return;
  24. }
  25. /* We create a floating element with the text where the user clicked */
  26. var element = document.createElement('span');
  27. element.innerHTML = text;
  28. element.style.position = 'absolute';
  29. element.style.padding = '0px';
  30. element.size = text.length + 1;
  31. element.style.whiteSpace = 'nowrap';
  32. element.style.fontFamily = PaintApp.modes.Text.fontFamily;
  33. element.style.width = 'auto';
  34. element.style.lineHeight = PaintApp.modes.Text.lineHeight;
  35. element.style.fontSize = PaintApp.modes.Text.defaultSize + 'px';
  36. element.style.opacity = '0.5';
  37. element.style.borderRadius = '0px';
  38. element.style.border = '5px dotted #500';
  39. element.style.color = PaintApp.data.color.fill;
  40. var left = event.point.x + 'px';
  41. var top = event.point.y + 55 + 'px';
  42. element.style.left = left;
  43. element.style.top = top;
  44. element.style.verticalAlign = 'bottom';
  45. document.body.appendChild(element);
  46. element.style.left = parseInt(left) - element.getBoundingClientRect().width / 2 + 'px';
  47. element.style.top = parseInt(top) - element.getBoundingClientRect().height / 2 + 'px';
  48. /* Storing a floating element */
  49. PaintApp.data.currentElement = {
  50. type: 'text',
  51. element: element,
  52. startPoint: {
  53. x: parseInt(element.style.left) + element.getBoundingClientRect().width / 2,
  54. y: parseInt(element.style.top) + element.getBoundingClientRect().height / 2
  55. }
  56. };
  57. },
  58. onMouseDrag: function (event) {
  59. if (!PaintApp.data.currentElement) {
  60. return;
  61. }
  62. /* We change the size of the text with the mouse drag*/
  63. var distanceX = Math.abs(event.point.x - PaintApp.data.currentElement.startPoint.x);
  64. var distanceY = Math.abs(event.point.y - PaintApp.data.currentElement.startPoint.y + 55);
  65. if (distanceX > distanceY) {
  66. distance = distanceX;
  67. } else {
  68. distance = distanceY;
  69. }
  70. PaintApp.data.currentElement.element.style.fontSize = PaintApp.modes.Text.defaultSize + distance + 'px';
  71. PaintApp.data.currentElement.element.style.left = PaintApp.data.currentElement.startPoint.x - PaintApp.data.currentElement.element.getBoundingClientRect().width / 2 + 'px';
  72. PaintApp.data.currentElement.element.style.top = PaintApp.data.currentElement.startPoint.y - PaintApp.data.currentElement.element.getBoundingClientRect().height / 2 + 'px';
  73. },
  74. onMouseUp: function (event) {
  75. if (!PaintApp.data.currentElement) {
  76. return;
  77. }
  78. var txt = PaintApp.data.currentElement.element.innerHTML;
  79. var top = PaintApp.data.currentElement.element.getBoundingClientRect().top - 55 + PaintApp.data.currentElement.element.getBoundingClientRect().height;
  80. /* We draw the text inside the canvas */
  81. var ctx = PaintApp.elements.canvas.getContext('2d');
  82. ctx.font = PaintApp.data.currentElement.element.style.fontSize + ' ' + PaintApp.modes.Text.fontFamily;
  83. ctx.fillStyle = PaintApp.data.color.fill;
  84. ctx.textAlign = 'start';
  85. ctx.fillText(txt, 5 + PaintApp.data.currentElement.element.getBoundingClientRect().left, top);
  86. /* If the activity is shared we send the element to everyone */
  87. if (PaintApp.data.isShared) {
  88. PaintApp.collaboration.sendMessage({
  89. action: 'text',
  90. data: {
  91. font: PaintApp.data.currentElement.element.style.fontSize + ' ' + PaintApp.modes.Text.fontFamily,
  92. fillStyle: PaintApp.data.color.fill,
  93. textAlign: 'start',
  94. text: txt,
  95. left: 5 + PaintApp.data.currentElement.element.getBoundingClientRect().left,
  96. top: top
  97. }
  98. });
  99. }
  100. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  101. PaintApp.data.currentElement = undefined;
  102. PaintApp.saveCanvas();
  103. }
  104. };
  105. return Text;
  106. });