informatics-isi-edu / openseadragon-viewer

2D viewer with openseadragon
Apache License 2.0
5 stars 2 forks source link

Add text box drawing tool #102

Closed RFSH closed 1 year ago

RFSH commented 2 years ago

As the title suggests, we want to allow users to draw text on the image. The following are what we need to investigate as part of this:

  1. How should we position the text? What should be the default font size for different zoom levels?
  2. What should the controls to add a text box look like?
    • The most straightforward implementation is to popup an input box somewhere. That being said, we should investigate other methods first, as this is not an ideal/acceptable solution.
    • We could display an input overlay on top of the SVG and, after drawing, transform it into an SVG (Not worth pursuing since it can be easily broken).
    • We can show a box around an actual SVG text tag. The content changes based on user input, and we should display a blinking icon to mimic the browser's focus behavior.
    • We should show a textarea using foreignObject in the SVG. This way we're showing a fully functional text box to the users.
  3. User should be able to move the drawn text. Our solution for this should not be specific to the text, and after implementing this feature, users should be able to move any drawn shapes.
  4. Users should be able to change the text styles (font size, bold, italic, etc.)
RFSH commented 2 years ago

We had a discussion about this and the following is the list of tasks for this issue:

P.S. The selection is missing from this and we should most probably do that as a separate task for all the drawing tools.

nikhiilll commented 2 years ago

We need a functionality wherein the textarea should enable the users to write the text and should be converted into SVG text at the same position with respect to the image. Similarly, the users should be able to edit the text annotation by clicking on it and the SVG text should be replaced by a textarea. To achieve this functionality, I decided to use foreginObject to add HTML elements to the SVG itself.

Why foreignObject? I decided to go ahead with a foriegnObject instead of adding a textarea to an overlay because adding it to the overlay would increase the complexity in terms of switching back and forth between textarea and SVG text, placing the textarea at the proper position in the overlay and the click/drag functionality not working properly if the textarea might get covered by another overlay.

The logic currently is that we would add a foreignObject containing a div and append the textarea to this div. Adding a div as a wrapper would be helpful for the resizing and dragging functionality. This implementation works in the POC stage. We are able to append a textarea to the SVG and type the text as well.

Issues faced:

nikhiilll commented 2 years ago

To enable resizable textarea, I am using transparent divs that can be dragged to increase/decrease the size of the div containing the textarea. 3 divs are being used:

  1. The div on the right side of the textarea div would enable horizontal resizing.
  2. The div on the bottom will enable vertical resizing.
  3. The div in the bottom-right corner will enable both.

Here is a working POC that I have created: CodePen

The issue that I'm currently facing is similar to the previous input issue. There are OpenSeadragon handlers defined to handle the click, drag, mouseup and mousedown functionality. These need to be disabled when either of the divs mentioned above are being used to resize the textarea and enabled again. The same would be the scenario for moving functionality. We will need to disable and enable the handlers dynamically. I'm working on this.

RFSH commented 1 year ago

The latest changes in text-annotation-tool branch include the new textbox drawing tool. In this comment we will keep track of the missing features/bugs. We should not merge this branch with master until all the listed issues are resolved.