Setting up Doka Image Editor with jQuery

For a quick start use the jQuery example project included in the product package as a guideline. It includes a normal, modal, and overlay editor example.

We only have to include the jquery-doka/doka.js file and the doka/doka.css file in our project and we're good to go.

Default implementation example

In the default example below we'll use the dokaDefault function to quickly add an image editor to another HTML element.

This creates a "default" editor that has all available plugins loaded and comes preset with all plugin default options and English locale. Each of these settings can be adjusted freely.

<!DOCTYPE html>

<link rel="stylesheet" href="jquery-doka/doka.css" />

<style>
    .my-editor {
        height: 600px;
    }
</style>

<div class="my-editor"></div>

<script src="jquery.js"></script>
<script src="jquery-doka/doka.js"></script>

<script>
    $('.my-editor').dokaDefault({
        // The source image to load
        src: './my-image.jpeg',

        // This will set a square crop aspect ratio
        imageCropAspectRatio: 1,
    });
</script>

Events

To handle the events fired by the editor we have to prepend each event with doka:

$('.my-editor').on('doka:load', (e) => {
    // Handle event
});

The jQuery API also allows creation of modal and overlay editors. Instead of the $(selector).doka syntax we can use the methods exported by the editor API as these are available on the $.fn.doka property. See the exports section for an overview of the available methods and properties.

This will open the editor in a modal. Note that this is not a default editor, to open a default editor use openDefaultEditor instead of openEditor.

$.fn.doka.openEditor({
    // This will set a square crop aspect ratio
    imageCropAspectRatio: 1,
});

Listening for events on the editor modal can be done like shown below.

const editor = $.fn.doka.openEditor({
    // Configuration options here
});

editor.on('load', (e) => {
    // Handle event
});

Advanced implementation example

If we want to create a custom editor, using a custom set of plugins, locale, and available options, then we can opt to use the $.fn.doka method.

We now have to set the available plugins, load the correct locale objects, and set the default plugin options. While a lot more verbose this does allow us to create a more optimal editor package as we're omitting unneeded logic.

<!DOCTYPE html>

<link rel="stylesheet" href="jquery-doka/doka.css" />

<style>
    .my-editor {
        height: 600px;
    }
</style>

<div class="my-editor"></div>

<script src="jquery.js"></script>
<script src="jquery-doka/doka.js"></script>

<script>
    const {
        // Get the default image reader and writer
        createDefaultImageReader,
        createDefaultImageWriter,

        // The method used to register the plugins
        setPlugins,

        // The plugins we want to use
        plugin_crop,
        plugin_finetune,
        plugin_annotate,

        // The user interface and plugin locale objects
        locale_en_gb,
        plugin_crop_locale_en_gb,
        plugin_finetune_locale_en_gb,
        plugin_annotate_locale_en_gb,

        // Because we use the annotate plugin we also need
        // to import the markup editor locale
        markup_editor_locale_en_gb,

        // Import the default configuration for the markup editor and finetune plugins
        markup_editor_defaults,
        plugin_finetune_defaults,
    } = $.fn.doka;

    // This registers the plugins with Doka Image Editor
    setPlugins(plugin_crop, plugin_finetune, plugin_filter);

    // Append the editor
    $('.my-editor').doka({
        // The source image to load
        src: './my-image.jpeg',

        // This will read the image data (required)
        imageReader: createDefaultImageReader(),

        // This will write the output image
        imageWriter: createDefaultImageWriter(),

        // The markup editor default options, tools, shape style controls
        ...markup_editor_defaults,

        // The finetune util controls
        ...plugin_finetune_defaults,

        // This will set a square crop aspect ratio
        imageCropAspectRatio: 1,

        // The icons and labels to use in the user interface (required)
        locale: {
            ...locale_en_gb,
            ...plugin_crop_locale_en_gb,
            ...plugin_finetune_locale_en_gb,
            ...plugin_annotate_locale_en_gb,
            ...markup_editor_locale_en_gb,
        },
    });
</script>

Next steps

With the editor set up, we can continue to configure the editor to our liking by adjusting the available configuration options