Rough Notation

repository·master·Indexed 27 days ago

https://github.com/rough-stuff/rough-notation

A lightweight JavaScript library using RoughJS to create and animate hand-drawn, sketchy-style annotations on web elements. It supports various annotation types including underlines, boxes, circles, highlights, strike-throughs, crossed-off marks, and brackets. The library provides an API to control individual annotations via the annotate function and manage sequential animation sequences using annotationGroup.

Tokens
2K
Snippets
4
Records
12
Agent score
94%

What's inside rough-notation

  1. Install Rough Notation

    master

    You can install Rough Notation via npm, use the ES module directly from unpkg, or use the IIFE version which creates a RoughNotation object in your global scope.

    npm install --save rough-notation
    <!-- ES module -->
    <script type="module" src="https://unpkg.com/rough-notation?module"></script>
    
    <!-- IIFE version -->
    <script src="https://unpkg.com/rough-notation/lib/rough-notation.iife.js"></script>
  2. Create and show an annotation

    master

    Use the annotate function by passing the target DOM element and a configuration object. To make the annotation visible, call the .show() method on the returned annotation object.

    Note: Rough Notation adds an SVG element as a sibling to the target element. To avoid layout issues (e.g., inside a <table>), wrap your content in an inner <span> or <div> before annotating.

    import { annotate } from 'rough-notation';
    
    const e = document.querySelector('#myElement');
    const annotation = annotate(e, { type: 'underline' });
    annotation.show();
  3. Animate annotations in order using Annotation Groups

    master

    To control the sequence of animations, use annotationGroup. Pass an array of annotation objects to annotationGroup(). When you call .show() on the group, the annotations will animate one after another in the order they appear in the array.

    import { annotate, annotationGroup } from 'rough-notation';
    
    const a1 = annotate(document.querySelector('#e1'), { type: 'underline' });
    const a2 = annotate(document.querySelector('#e3'), { type: 'box' });
    const a3 = annotate(document.querySelector('#e3'), { type: 'circle' });
    
    const ag = annotationGroup([a3, a1, a2]);
    ag.show();
  4. Use the Annotation Group object API

    master

    The annotationGroup function returns a group object used to manage multiple annotations as a single unit.

    Methods:

    • show(): Draws all annotations in the group in the specified order.
    • hide(): Hides all annotations in the group immediately (not animated).
  5. Use the Annotation object API

    master

    The annotate function returns an annotation object. You can use its methods to control visibility or update its properties dynamically (except for type).

    Methods:

    • isShowing(): Returns true if the annotation is currently visible.
    • show(): Draws/animates the annotation. Calling this again re-renders it to match current element size/location. To re-animate, call hide() then show().
    • hide(): Hides the annotation immediately (not animated).
    • remove(): Unlinks the annotation from the element.

    Updating Styles: Properties like color can be updated after the annotation is created.

    const e = document.querySelector('#myElement');
    const annotation = annotate(e, { type: 'underline', color: 'red' });
    annotation.show();
    
    // Update color dynamically
    annotation.color = 'green';
  6. Configure RoughAnnotationConfig

    master

    When creating an annotation, you can provide a configuration object to define its visual style and behavior. The configuration is split into base styles and type-specific settings.

    Base Configuration Options:

    • animate: Boolean. Whether to animate the annotation (defaults to true).
    • animationDuration: Number. Duration of the animation in milliseconds (defaults to 1000).
    • color: String. The color of the annotation (defaults to currentColor).
    • strokeWidth: Number. The thickness of the stroke (default varies by type).
    • padding: RoughPadding. The spacing around the annotation. Can be a single number, a tuple of two numbers [topBottom, leftRight], or a full tuple [top, right, bottom, left]. Defaults to 5.
    • iterations: Number. How many times the animation repeats (defaults to 2).
    • brackets: BracketType | BracketType[]. Only applicable for bracket type. Defines which sides to annotate. Options: 'left', 'right', 'top', 'bottom'. Defaults to 'right'.

    Type-Specific Options:

    • type: One of 'underline', 'box', 'circle', 'highlight', 'strike-through', 'crossed-off', or 'bracket'.
    • multiline: Boolean. Whether the annotation should support multiple lines.
    • rtl: Boolean. Whether to use Right-to-Left text direction.
  7. Configure annotation styles

    master

    The annotate function accepts a configuration object. The type field is mandatory.

    Supported type values:

    • underline: Sketchy underline below the element.
    • box: A box around the element.
    • circle: A circle around the element.
    • highlight: A highlighter effect.
    • strike-through: Horizontal lines through the element.
    • crossed-off: An 'X' across the element.
    • bracket: Brackets around the element. Use the brackets option to specify sides.

    Configuration Options:

    • type (string, mandatory): The annotation style.
    • animate (boolean): Whether to animate the drawing. Default: true.
    • animationDuration (number): Duration in milliseconds. Default: 800.
    • color (string): Color of the sketch. Default: currentColor.
    • strokeWidth (number): Width of the strokes. Default: 1.
    • padding (number | array): Padding between element and annotation. Default: 5. Can be [top, right, bottom, left] or [top & bottom, left & right].
    • multiline (boolean): If true, annotates each line of inline text separately.
    • iterations (number): Number of times the annotation is drawn.
    • brackets (string | string[]): For type: 'bracket', specifies sides: left, right, top, bottom. Default: right.
    • rtl (boolean): If true, draws from right to left. Default: false.
  8. Manage a RoughAnnotation instance

    master

    A RoughAnnotation instance provides methods and properties to control the lifecycle and appearance of the annotation attached to an element.

    Methods

    • show(): Displays the annotation.
    • hide(): Removes the annotation from view.
    • remove(): Completely removes the annotation and its associated SVG from the DOM.
    • isShowing(): Returns true if the annotation is currently visible.

    Properties

    You can dynamically update the following properties, which will trigger a refresh of the annotation:

    • animate: Boolean to enable/disable animation.
    • animationDuration: The duration of the animation.
    • iterations: Number of times the animation should repeat.
    • color: The color of the annotation.
    • strokeWidth: The thickness of the annotation stroke.
    • padding: The padding around the annotated element.