Create and animate hand-drawn annotations on a web page
npm install rough-notationA small JavaScript library to create and animate annotations on a web page.
Rough Notation uses RoughJS to create a hand-drawn look and feel. Elements can be annotated in a number of different styles. Animation duration can be configured, or just turned off.
Rough Notation is 3.83kb in size when gzipped.
Visit website to see it in action and check out the source code for the website
You can add rough-notation to your project via npm
```
npm install --save rough-notation
Or load the ES module directly
`html`
Or load the IIFE version which created a RoughNotation object in your scope.
`html`
Create an annotation object by passing the element to annotate, and a config to describe the annotation style. show()
Once you have the annotation object, you can call on it to show the annotation
`javascript
import { annotate } from 'rough-notation';
// Or using unpkg
// import { annotate } from 'https://unpkg.com/rough-notation?module';
const e = document.querySelector('#myElement');
const annotation = annotate(e, { type: 'underline' });
annotation.show();
`
Note: This will add an SVG element as a sibling to the element, which may be troublesome in certain situations like in a rough-notation provides a way to order the animation of annotations by creating an annotation-group. Pass the list of annotations to create a group. When show is called on the group, the annotations are animated in order. const a1 = annotate(document.querySelector('#e1'), { type: 'underline' }); const ag = annotationGroup([a3, a1, a2]); When you create an annotation object, you pass in a config. The config only has one mandatory field, which is the #### type * __underline__: This style creates a sketchy underline below an element. #### animate #### animationDuration #### color #### strokeWidth #### padding #### multiline #### iterations #### brackets #### rtl When you call the #### isShowing(): boolean #### show() *Note: to reanimate the annotation, call #### hide() #### remove() #### Updating styles Note: the type of the annotation cannot be changed. Create a new annotation for that. When you call the #### show() #### hide() Others have created handy Rough Notation wrappers for multiple libraries and frameworks: - React Rough Notation Become a financial contributor and help us sustain our community. [Contribute] #### Individuals #### Organizations Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]. You may want to create an inner or for the content to annotate.`Annotation Group
javascript`
import { annotate, annotationGroup } from 'rough-notation';
const a2 = annotate(document.querySelector('#e3'), { type: 'box' });
const a3 = annotate(document.querySelector('#e3'), { type: 'circle' });
ag.show();typeLive examples
I have created some basic examples on Glitch for you to remix and play with the code:Configuring the Annotation
of the annotation. But you can configure the annotation in many ways. true
This is a mandatory field. It sets the annotation style. Following are the list of supported annotation types:
* __box__: This style draws a box around the element.
* __circle__: This style draws a circle around the element.
* __highlight__: This style creates a highlight effect as if marked by a highlighter.
* __strike-through__: This style draws horizontal lines through the element.
* __crossed-off__: This style draws an 'X' across the element.
__bracket__: This style draws a bracket around an element, usually a paragraph of text. By default on the right side, but can be configured to any or all of left, right, top, bottom*.
Boolean property to turn on/off animation when annotating. Default value is .800ms
Duration of the animation in milliseconds. Default is .currentColor
String value representing the color of the annotation sketch. Default value is .1
Width of the annotation strokes. Default value is . 5
Padding between the element and roughly where the annotation is drawn. Default value is (in pixels).top
If you wish to specify different , left, right, bottom paddings, you can set the value to an array akin to CSS style padding [top, right, bottom, left] or just [top & bottom, left & right].true
This property only applies to inline text. To annotate multiline text (each line separately), set this property to . right
By default annotations are drawn in two iterations, e.g. when underlining, drawing from left to right and then back from right to left. Setting this property can let you configure the number of iterations.
Value could be a string or an array of strings, each string being one of these values: left, right, top, bottom. When drawing a bracket, this configures which side(s) of the element to bracket. Default value is .true
By default annotations are drawn from left to right. To start with right to left, set this property to . annotateAnnotation Object
function, you get back an annotation object, which has the following methods:hide()
Returns if the annotation is showing
Draws the annotation. If the annotation is set to animate (default), it will animate the drawing. If called again, it will re-render the annotation, updating any size or location changes. and then show() again.`
Hides the annotation if showing. This is not animated.
Unlinks the annotation from the element.
All the properties in the configuration are also exposed in this object. e.g. if you'd like to change the color, you can do that after the annotation has been drawn.javascript`
const e = document.querySelector('#myElement');
const annotation = annotate(e, { type: 'underline', color: 'red' });
annotation.show();
annotation.color = 'green';annotationGroup` function, you get back an annotation group object, which has the following methods:Annotation Group Object
Draws all the annotations in order. If the annotation is set to animate (default), it will animate the drawing.
Hides all the annotations if showing. This is not animated.Wrappers
- Svelte Rough Notation
- Vue Rough Notation
- Web Component Rough Notation
- Angular Rough NotationContributors
$3