1.18.0 • Published 2 months ago

js-draw v1.18.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

js-draw

NPM package | GitHub | Documentation | Try it!

npm.io

For example usage, see one of the examples or read the documentation.

API

To use js-draw,

Creating an Editor

With a bundler that supports importing .css files

To create a new Editor and add it as a child of document.body,

import Editor from 'js-draw';
import 'js-draw/styles';

const editor = new Editor(document.body);

The import js-draw/styles step requires a bundler that can import .css files. For example, webpack with css-loader.

With a bundler that doesn't support importing .css files

Import the pre-bundled version of the editor to apply CSS after loading the page.

import Editor from 'js-draw';
import 'js-draw/bundledStyles';

const editor = new Editor(document.body);

js-draw/bundledStyles is a version of the editor's stylesheets pre-processed by Webpack. As such, importing or including it with a <script src="..."></script> tag applies editor-specific CSS to the document.

Without a bundler

If you're not using a bundler, consider using the pre-bundled editor:

<!-- Replace 0.20.0 with the latest version of js-draw -->
<script src="https://cdn.jsdelivr.net/npm/js-draw@0.20.0/dist/bundle.js"></script>
<script>
    const editor = new jsdraw.Editor(document.body);
    editor.addToolbar();
    editor.getRootElement().style.height = '600px';
</script>

Note: To ensure the CDN-hosted version of js-draw hasn't been tampered with, consider including an integrity="..." attribute. Read more about using SRI with JSDelivr.

Adding a toolbar

To create a toolbar with the default tools:

const toolbar = editor.addToolbar();

Custom actions can be added to the toolbar. For example, to add a save button:

toolbar.addActionButton('Save', () => {
    const svgElem = editor.toSVG();
    console.log('The saved SVG:', svgElem.outerHTML);
});

or alternatively, with an icon,

toolbar.addActionButton({
  label: 'Save'
  icon: editor.icons.makeSaveIcon(),
}, () => {
    // Save content here.
});

Loading from an SVG

editor.loadFromSVG(`
    <svg
        viewBox="156 74 200 150"
        width="200" height="150"
    >
        <path d="M156,150Q190,190 209,217L213,215Q193,187 160,148M209,217Q212,218 236,178L232,176Q210,215 213,215M236,178Q240,171 307,95L305,93Q237,168 232,176M307,95Q312,90 329,78L327,74Q309,87 305,93" fill="#07a837"></path>
    </svg>
`);

Note: While js-draw supports a small subset of the SVG markup language, it tries to preserve unrecognised SVG elements.

For example, although js-draw doesn't support <circle/> elements,

<svg
    viewBox="156 74 200 150"
    width="200" height="150"
>
    <path d="M156,150Q190,190 209,217L213,215Q193,187 160,148M209,217Q212,218 236,178L232,176Q210,215 213,215M236,178Q240,171 307,95L305,93Q237,168 232,176M307,95Q312,90 329,78L327,74Q309,87 305,93" fill="#07a837"></path>
    <circle cx=200 cy=100 r=40 fill='red'/>
</svg>

renders as

screenshot of the image editor, displaying a green checkmark. The circle is invisible

but exports to

<svg viewBox="156 74 200 150" width="200" height="150" version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg"><g><path d="M156,150M156,150Q190,190 209,217L213,215Q193,187 160,148M209,217M209,217Q212,218 236,178L232,176Q210,215 213,215M236,178M236,178Q240,171 307,95L305,93Q237,168 232,176M307,95M307,95Q312,90 329,78L327,74Q309,87 305,93" fill="#07a837"></path></g><circle cx="200" cy="100" r="40" fill="red"></circle></svg>

which does contain the <circle/> element.

Settings/configuration

Disabling touchpad panning

Touchpad/mousewheel pan gestures can conflict with gestures used to scroll the document. To turn off touchpad pan gestures (and scrolling the editor with the mousewheel),

const editor = new Editor(document.body, {
    wheelEventsEnabled: false,
});

Alternatively, to only enable touchpad panning when the editor has focus,

const editor = new Editor(document.body, {
    wheelEventsEnabled: 'only-if-focused',
});

Localization

If a user's language is available in src/localizations/ (as determined by navigator.languages), that localization will be used.

To override the default language, use getLocalizationTable([ 'custom locale here' ]). For example,

const editor = new Editor(document.body, {
    // Force the Spanish (Español) localizaiton
    localization: getLocalizationTable([ 'es' ]),
});

See src/localization.ts for a list of strings that can be translated.

Many of the default strings in the editor might be overridden like this:

const editor = new Editor(document.body, {
    // Example partial Spanish localization
    localization: {
        // Not all translated strings need to be specified. If a string isn't given,
        // the English (default) localization will be used

        // Strings for the main editor interface
        // (see src/localization.ts)
        loading: (percentage: number) => `Cargando: ${percentage}%...`,
        imageEditor: 'Editor de dibujos',

        undoAnnouncement: (commandDescription: string) => `${commandDescription} fue deshecho`,
        redoAnnouncement: (commandDescription: string) => `${commandDescription} fue rehecho`,

        // Strings for the toolbar
        // (see src/toolbar/localization.ts)
        pen: 'Lapiz',
        eraser: 'Borrador',
        select: 'Selecciona',
        thicknessLabel: 'Tamaño: ',
        colorLabel: 'Color: ',

        ...
    },
});

Changing the editor's color theme

The editor's color theme is specified using CSS. Its default theme looks like this:

.imageEditorContainer {
    /* Deafult colors for the editor -- light mode */

    --primary-background-color: white;
    --primary-background-color-transparent: rgba(255, 255, 255, 0.5);
    --secondary-background-color: #faf;
    --primary-foreground-color: black;
    --secondary-foreground-color: black;
    --primary-shadow-color: rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
    .imageEditorContainer {
        /* Deafult colors for the editor -- dark mode */

        --primary-background-color: #151515;
        --primary-background-color-transparent: rgba(50, 50, 50, 0.5);
        --secondary-background-color: #607;
        --primary-foreground-color: white;
        --secondary-foreground-color: white;
        --primary-shadow-color: rgba(250, 250, 250, 0.5);
    }
}

To override it, use a more specific CSS selector to set the theme variables. For example,

body .imageEditorContainer {
    --primary-background-color: green;
    --primary-background-color-transparent: rgba(255, 240, 200, 0.5);
    --secondary-background-color: yellow;
    --primary-foreground-color: black;
    --secondary-foreground-color: black;
}

disables the dark theme and creates a theme that primarily uses yellow/green colors.

1.18.0

2 months ago

1.17.0

2 months ago

1.16.1

5 months ago

1.16.0

5 months ago

1.14.0

5 months ago

1.15.0

5 months ago

1.11.2

6 months ago

1.11.1

6 months ago

1.9.1

6 months ago

1.10.0

6 months ago

1.12.0

6 months ago

1.11.0

6 months ago

1.13.2

5 months ago

1.13.1

6 months ago

1.6.1

7 months ago

1.6.0

7 months ago

1.9.0

7 months ago

1.5.0

8 months ago

1.8.0

7 months ago

1.4.1

8 months ago

1.4.0

8 months ago

1.7.2

7 months ago

1.7.1

7 months ago

1.7.0

7 months ago

1.3.1

8 months ago

1.3.0

8 months ago

1.2.0

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

1.2.2

9 months ago

1.2.1

9 months ago

1.1.0

9 months ago

0.25.1

10 months ago

0.25.0

10 months ago

0.24.1

11 months ago

0.24.0

11 months ago

0.23.1

1 year ago

0.23.0

1 year ago

0.21.0

1 year ago

0.22.1

1 year ago

0.22.0

1 year ago

0.20.0

1 year ago

0.19.0

1 year ago

0.17.2

1 year ago

0.17.3

1 year ago

0.17.4

1 year ago

0.13.0

1 year ago

0.13.1

1 year ago

0.11.3

1 year ago

0.15.0

1 year ago

0.15.1

1 year ago

0.17.0

1 year ago

0.15.2

1 year ago

0.17.1

1 year ago

0.18.1

1 year ago

0.18.2

1 year ago

0.12.0

1 year ago

0.14.0

1 year ago

0.16.0

1 year ago

0.16.1

1 year ago

0.18.0

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.11.2

1 year ago

0.9.0

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.9.2

1 year ago

0.9.1

1 year ago

0.7.0

2 years ago

0.9.3

1 year ago

0.10.1

1 year ago

0.10.2

1 year ago

0.10.3

1 year ago

0.10.0

1 year ago

0.8.0

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago