marker.js 2 - Image Annotation
marker.js 2 is a JavaScript browser library designed to enable image annotation functionality within web applications. It allows users to mark up, highlight, and add comments to images directly in the browser, and then save or process the annotated results. While still supported for some time, this version (2.x, with current stable being 2.32.7) is officially deprecated in favor of marker.js 3, which is the focus of all future development. Releases for marker.js 2 were frequent, often several patch versions per month, but this cadence is expected to slow significantly as focus shifts to v3. Its key differentiator was providing a complete, out-of-the-box UI for image annotation with various tools.
Common errors
-
TypeError: MarkerArea is not a constructor
cause Attempting to import `MarkerArea` as a named export from `markerjs2` instead of accessing it via the `markerjs2` namespace object.fixChange `import { MarkerArea } from 'markerjs2';` to `import * as markerjs2 from 'markerjs2';` and then use `new markerjs2.MarkerArea(...)`. -
Uncaught (in promise) DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
cause Attempting to `toDataURL` an image that was loaded from a different origin without CORS headers, making the canvas 'tainted'.fixEnsure that images loaded from external origins are served with appropriate CORS (Cross-Origin Resource Sharing) headers, specifically `Access-Control-Allow-Origin: *` or your domain. Alternatively, proxy the images through your own server. -
ReferenceError: document is not defined
cause Trying to run `markerjs2` in a Node.js environment without a browser DOM available. `markerjs2` is a browser-side library.fixEnsure `markerjs2` code is executed only in a browser environment or within a testing framework that provides a mocked DOM (e.g., JSDOM).
Warnings
- deprecated marker.js 2 is officially deprecated. While still supported, all future development and new features will be focused on marker.js 3. Users are strongly advised to migrate to version 3 for long-term stability and access to the latest improvements.
- gotcha The default license for marker.js 2 is 'Linkware'. This means the UI will display a small link back to the marker.js 2 website, which must be retained unless an alternative commercial license is purchased.
- gotcha When importing using ESM syntax, the library provides a namespace object (e.g., `markerjs2`), and components like `MarkerArea` are properties of this object, not direct named exports. Attempting a direct named import will fail.
Install
-
npm install markerjs2 -
yarn add markerjs2 -
pnpm add markerjs2
Imports
- MarkerArea
import { MarkerArea } from 'markerjs2';import * as markerjs2 from 'markerjs2'; const markerArea = new markerjs2.MarkerArea(...);
- Type definitions
import { MarkerArea } from 'markerjs2';import type { MarkerArea } from 'markerjs2/types/MarkerArea'; - CommonJS (Node.js)
const { MarkerArea } = require('markerjs2');const markerjs2 = require('markerjs2'); const markerArea = new markerjs2.MarkerArea(...);
Quickstart
import * as markerjs2 from 'markerjs2';
// Imagine an image element exists in the DOM
document.body.innerHTML = '<img id="myimg" src="https://picsum.photos/800/600?random=1" alt="Image to annotate" style="max-width: 100%;" />';
const imgElement = document.getElementById('myimg');
if (imgElement) {
let markerArea = new markerjs2.MarkerArea(imgElement);
markerArea.addEventListener('render', (event) => {
// Replace the original image with the annotated one
// In a real app, you might upload event.dataUrl to a server
// or display it in a new element.
if (imgElement instanceof HTMLImageElement) {
imgElement.src = event.dataUrl;
console.log('Annotation rendered. Data URL length:', event.dataUrl.length);
}
});
// Open the marker.js UI after a short delay to ensure image loads
setTimeout(() => {
markerArea.show();
}, 500);
} else {
console.error('Image element with ID "myimg" not found.');
}