-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): Simplify API for anchoring elements above the canvas (#91)
* feat(editor): Simplify API for anchoring elements above the canvas * Remove commented out code * Add regression test * Commenting * Fix scroll correction logic * Fix formatting
- Loading branch information
1 parent
a30a435
commit 6bac2a3
Showing
6 changed files
with
252 additions
and
54 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
docs/doc-pages/guides/positioning-an-element-above-the-editor.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: Positioning an element above the editor | ||
category: Guides | ||
--- | ||
|
||
# Positioning an element above the editor | ||
|
||
The built-in `TextTool` tool renders a `<textarea>` above the editor at a particular location on the canvas. This guide demonstrates how to position custom HTML elements above the editor. | ||
|
||
Related APIs: | ||
|
||
- {@link js-draw!Editor.anchorElementToCanvas | Editor.anchorElementToCanvas}: Used to listen for changes in the viewport. | ||
- {@link @js-draw/math!Mat33.translation | Mat33.translation}: Lets us specify where to put the element. | ||
|
||
## Getting started: Creating the editor | ||
|
||
Let's start by creating an editor with a toolbar: | ||
|
||
```ts,runnable | ||
import { Editor } from 'js-draw'; | ||
// Adds the editor to document.body: | ||
const editor = new Editor(document.body); // 1 | ||
editor.addToolbar(); | ||
``` | ||
|
||
Running the above example should display an empty editor with the default toolbar. | ||
|
||
## Creating an element to be positioned | ||
|
||
We'll start by creating a `<button>` to position above the editor. Unlike the {@link js-draw!TextTool | TextTool}, we're using a `<button>` instead of a `<textarea>`: | ||
|
||
```ts,runnable | ||
---use-previous--- | ||
---visible--- | ||
const button = document.createElement('button'); | ||
button.textContent = 'Example!'; | ||
button.style.position = 'absolute'; | ||
``` | ||
|
||
## Attaching the element | ||
|
||
Finally, the element can be attached to the editor using `anchorElementToCanvas`: | ||
|
||
```ts,runnable | ||
---use-previous--- | ||
---visible--- | ||
import { Mat33, Vec2 } from '@js-draw/math'; | ||
const positioning = Mat33.translation(Vec2.of(10, 20)); | ||
const anchor = editor.anchorElementToCanvas(button, positioning); | ||
``` | ||
|
||
## Unattaching the element | ||
|
||
Later, the element can be removed from the editor with `anchor.remove()`. Let's do this when the button is clicked: | ||
|
||
```ts,runnable | ||
---use-previous--- | ||
---visible--- | ||
import { Mat33, Vec2 } from '@js-draw/math'; | ||
button.onclick = () => { | ||
anchor.remove(); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.