overlapmedia / imagemapper

Adds SVG drawing capability (rectangles, circles, ellipses and polygons) on top of your image to let you make image maps.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

imagemapper logo

Create image maps. View image maps. Interact with image maps by event listeners. Create your next Design Collaboration Tool?

imagemapper provides both drawing and view mode interaction capabilities letting you enable features of your image map adapted to the context of the user.

  • Instantiated as an editor it adds SVG drawing capability (rectangles, circles, ellipses and polygons) on top of your image to let you make image maps. Shapes could be frozen (freeze method) to disallow deleting, resizing and moving.
  • Instantiated as a view you can't draw new shapes or change imported shapes, but all other features (eg. importing and event handlers) are still available.

Getting started

With Node.js

$ npm install @overlapmedia/imagemapper
// Use imagemapper.editor or editor
import imagemapper, { editor, view } from '@overlapmedia/imagemapper';

// Editor
const myEditor = imagemapper.editor('editor', {
  width: 800,
  height: 400,
  selectModeHandler: () => console.log('Editor is now in select mode'),
  componentDrawnHandler: (component, componentId) => {
    // Disabling changes on new components. If you are making a design collaboration tool you probably want
    // to do this on components returned by the import function (meaning all existing components you are importing)
    // and let all other components drawn by the user respond to changes.
    component.freeze();

    console.log(
      `Disabled selecting, deleting, resizing and moving on component with id ${componentId}`,
    );
  },
});
myEditor.loadImage('image.svg', 700, 350);
myEditor.on('mouseup', (e) => console.log('mouseup event', e));
myEditor.polygon(); // Let user draw polygons

// View
const myView = view('view', {
  width: 800,
  height: 400,
  viewClickHandler: (e, id) => console.log('User clicked on', id),
});
myView.loadImage('image.png', 700, 350);
myView.import(
  '{"idCounter":4,"components":[{"id":"rect_1","type":"rect","data":{"x":66,"y":36,"width":253,"height":148}},{"id":"polygon_2","type":"polygon","data":[{"x":376,"y":172},{"x":498,"y":291},{"x":625,"y":174},{"x":500,"y":57}]},{"id":"polygon_3","type":"polygon","data":[{"x":54,"y":249},{"x":234,"y":246},{"x":236,"y":225},{"x":415,"y":270},{"x":237,"y":313},{"x":235,"y":294},{"x":54,"y":292}]}]}',
);

From browser

<script src="https://cdn.jsdelivr.net/gh/overlapmedia/imagemapper@1.0.13/dist/imagemapper.min.js"></script>
<script>
    const { editor, view } = imagemapper;
    const myEditor = editor('editor-id');
    myEditor.rect(); // Let user draw rectangles
</script>

With React

If you want to use imagemapper in a React app, these examples might get you started.

Demo

Try out the demo of imagemapper here.

Backlog

  • feat: Support rotating shapes
  • feat: Import data with SVG attrs format (ref. #1)

API Reference

Example

import imagemapper from '@overlapmedia/imagemapper';
const editor = imagemapper.editor('editor-id');
editor.polygon();

Example

import { editor, view } from '@overlapmedia/imagemapper';
const myEditor = editor('editor-id');
myEditor.polygon();

imagemapper.editor ⇒ Editor

Editor

Kind: static constant of imagemapper

imagemapper.view ⇒ Editor

View

Kind: static constant of imagemapper
Returns: Editor - - an Editor constructor which does not add drawing capabilities

imagemapper~Editor(svgEl, [options], [style])

An Editor or View containing everything needed by the drawing/display board: DOM, event listeners, state and API functions.

Kind: inner method of imagemapper

Param Type Description
svgEl string | SVGElement the id of the SVG element to be created or the SVG element itself if it's already made
[options] object
[options.width] string if you let imagemapper create the SVGElement for you, you could specify width for it here
[options.height] string if you let imagemapper create the SVGElement for you, you could specify height for it here
[options.componentDrawnHandler] componentDrawnHandler function being called when finished drawing a valid component (eg. rectangle with width and height greater than 0 or polygon width at least three points), does not apply to importing
[options.selectModeHandler] selectModeHandler function being called when editor switches to select mode when eg. Esc keydown event or mousedown event on handle is causing it to leave draw mode
[options.viewClickHandler] viewClickHandler when using view this function will be called on click events from the shapes
[style] object see setStyle

editor.loadImage(path, [width], [height]) ⇒ Editor

Add an image element into the SVG element.

Kind: instance method of Editor

Param Type
path string
[width] number
[height] number

editor.setStyle(style) ⇒ Editor

Completely or partly set current style of components, handles, hovering etc.

Kind: instance method of Editor

Param Type
style object

Example

editor.setStyle({
  component: {
    fill: 'rgb(102, 102, 102)',
    stroke: 'rgb(51, 51, 51)',
  },
  componentHover: {
    off: {
      'stroke-width': 1,
      opacity: 0.5,
    },
    on: {
      'stroke-width': 2,
      opacity: 0.6,
    },
  },
  componentSelect: {
    off: {
      'stroke-dasharray': 'none',
      'stroke-linejoin': 'miter',
    },
    on: {
      'stroke-dasharray': '4 3',
      'stroke-linejoin': 'round',
    },
  },
  handle: {
    fill: 'rgb(255, 255, 255)',
    stroke: 'rgb(51, 51, 51)',
    'stroke-width': 1,
    opacity: 0.3,
  },
  handleHover: {
    opacity: 0.6,
  },
});

editor.rect()

Put editor in draw mode of rectangles.

Kind: instance method of Editor

editor.circle()

Put editor in draw mode of circles.

Kind: instance method of Editor

editor.ellipse()

Put editor in draw mode of ellipses.

Kind: instance method of Editor

editor.polygon()

Put editor in draw mode of polygons.

Kind: instance method of Editor

editor.selectMode()

Put editor in select mode.

Kind: instance method of Editor

editor.getComponentById(id) ⇒ Rectangle | Circle | Ellipse | Polygon

Kind: instance method of Editor

Param Type
id string

editor.selectComponent(component) ⇒ Rectangle | Circle | Ellipse | Polygon

Make programmatically selection of a component which basically enables its handles by making them visible. Please note that all components will be unselected when leaving select mode or leaving draw mode.

Kind: instance method of Editor

Param Type Description
component string | Rectangle | Circle | Ellipse | Polygon a component or a component id

editor.removeComponent(component) ⇒ Rectangle | Circle | Ellipse | Polygon

Remove a component (shape) from the editor or view.

Kind: instance method of Editor

Param Type Description
component string | Rectangle | Circle | Ellipse | Polygon a component or a component id

editor.on(eventTypes, handler) ⇒ Editor

Add event listener(s).

Kind: instance method of Editor

Param Type
eventTypes Array.<string>
handler handler

editor.off(eventTypes, handler) ⇒ Editor

Remove event listener(s).

Kind: instance method of Editor

Param Type
eventTypes Array.<string>
handler handler

editor.import(data, [idInterceptor]) ⇒ Array.<(Rectangle|Circle|Ellipse|Polygon)>

Import shapes from JSON.

Kind: instance method of Editor

Param Type Description
data string
[idInterceptor] idInterceptor function to change the imported id to avoid name conflicts, eg. in case user decides to import multiple times or import after drawing

Example

{
  "components": [{
    "id": "circle_1",
    "type": "circle",
    "data": {
      "x": 444,
      "y": 71,
      "width": 241,
      "height": 211
    }
  }]
}

Example

{
  "components": [{
    "id": "rect_1",
    "type": "rect",
    "data": {
      "x": 444,
      "y": 71,
      "width": 241,
      "height": 211
    }
  }]
}

Example

{
  "components": [{
    "id": "ellipse_1",
    "type": "ellipse",
    "data": {
      "x": 444,
      "y": 71,
      "width": 241,
      "height": 211
    }
  }]
}

Example

{
  "components": [{
    "id": "polygon_1",
    "type": "polygon",
    "data": [{
      "x": 603,
      "y": 114
    }, {
      "x": 625,
      "y": 203
    }, {
      "x": 699,
      "y": 124
    }]
  }]
}

editor.export([escape]) ⇒ string

Export drawn shapes as JSON.

Kind: instance method of Editor
Returns: string - - JSON data

Param Type Description
[escape] boolean whether double quotes should be escaped

Documented by jsdoc-to-markdown.

About

Adds SVG drawing capability (rectangles, circles, ellipses and polygons) on top of your image to let you make image maps.

License:MIT License


Languages

Language:TypeScript 99.6%Language:HTML 0.2%Language:JavaScript 0.2%