Advanced Usage

I will introduce methods to customize MapNJ.

Tips
  • Please refer to "Design Source Setup" for information on Area, Label, AreaID, etc. ( Design Source Setup )
  • Options that control fill and stroke must be defined in the design source itself.

Options

This is a list of options that can be specified in the object passed as the second argument when creating an instance.

Sample: JavaScript
const mapnj = new('#your-mapnj', {
  activeAreaId: "foo",
  areaDefaultFillColors: {
    area1: "#123456",
    area2: "red"
  }
});

Global

Option NameTypeDefaultDescription
attributeTypeid | data-nameid
SVGにマッピングされた情報を読み取る属性を指定します。
Note
MapNJは操作対象のidもしくはdata-name属性の値を読み取ります。 デザインソフト上ではこれはレイヤー名に対応させることが可能です。Adobe Illustratorではidもしくはdata-nameを、Figmaではidにマッピングし書き出しを行うことが可能です。
attributeValueSeparator'-' | '_''-'
mapnjに渡す情報(attributeTypeで指定されている属性の値)をハイフンで区切るかアンダースコアで区切るかを指定します。
activeAreaIdString''
指定したAreaIDに対応したAreaがアクティブ化します。 空文字を指定した場合、Area全てが非アクティブ状態になります。

Area

Option NameTypeDefaultDescription
areaDefaultFillColorString#eee
全てのAreaのデフォルト時の塗り色を一括指定します。
Note
pages.advanced_usage.options.area.areaDefaultFillColor.tips
areaDefaultFillColorsObject{}
個々のAreaのデフォルト時の塗り色を指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはareaDefaultFillColorの指定を上書きします。
areaActiveFillColorString | undefinedundefined
全てのAreaのアクティブ時の塗り色を一括指定します。
Note
通常はデザイン時に指定した色がアクティブ色として割り当てられます。
areaActiveFillColorsObject{}
個々のAreaのアクティブ時の塗り色を指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはareaActiveFillColorの指定を上書きします。
areaDefaultStrokeColorString#fff
全てのAreaのデフォルト時の線色を一括指定します。
areaDefaultStrokeColorsObject{}
個々のAreaのデフォルト時の線色を個別指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはareaDefaultStrokeColorsの指定を上書きします。
areaActiveStrokeColorString | undefinedundefined
全てのAreaのアクティブ時の線色を一括指定します。
Note
通常はデザイン時に指定した色がアクティブ色として割り当てられます。
areaActiveStrokeColorsObject{}
個々のAreaのアクティブ時の線色を個別指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはareaActiveStrokeColorでの指定を上書きします。
transparentDefaultAreasBooleanfalse
全てのAreaのデフォルト時の塗り色と線色を透明にします。(またこの時、その他のエリアのカラー定義はすべて無効化されます。)
areaChangeSpeedNumber0.5
Areaの切り替わるスピードを指定します。(単位は秒)

Label

Option NameTypeDefaultDescription
labelDefaultFillColorString#555
全てのLabelのデフォルト時の塗り色を一括指定します。
labelDefaultFillColorsObject{}
個々のLabelのデフォルト時の塗り色を個別指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったLabelはlabelDefaultFillColorでの指定を上書きします。
labelActiveFillColorString#555
全てのLabelのアクティブ時の塗り色を一括指定します。
labelActiveFillColorsObject{}
個々のLabelのアクティブ時の線色を個別指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはlabelActiveFillColorでの指定を上書きします。
labelDefaultStrokeColorString#fff
全てのLabelのデフォルト時の線色を一括指定します。
labelDefaultStrokeColorsObject{}
個々のLabelのデフォルト時の線色を個別指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはlabelDefaultStrokeColorでの指定を上書きします。
labelActiveStrokeColorString | undefinedundefined
全てのLabelのアクティブ時の線色を一括指定します。
Note
通常はデザイン時に指定した色がアクティブ色として割り当てられます。
labelActiveStrokeColorsObject{}
個々のLabelのアクティブ時の線色を個別指定します。
Note
areaIDをキーに、cssカラーをバリューに指定します。ここで指定があったAreaはlabelActiveStrokeColorでの指定を上書きします。
transparentDefaultLabelsBooleanfalse
全てのLabelのデフォルト時の塗り色と線色を透明にします。(またこの時、その他のエリアのカラー定義はすべて無効化されます。)
labelDispAnim'static' | 'fadeIn' | 'fadeInUp' | 'fadeInRight' | 'fadeInDown' | 'fadeInLeft'fadeIn
初期化時のLabelの表示エフェクトを指定します。
noEventLabelsArray[]
Labelをクリックした時のイベントを無効にします。無効にしたいAreaID名を列挙します。
Note
通常Labelはクリックすると、対応するAreaをアクティブ化するイベントが割り当てられています。

Content

Tips
Content refers to the parts included in the Option Parts, which will be explained later.
Option NameTypeDefaultDescription
contentChangeSpeedNumber0.5
コンテンツ(data-mapnj="area-content")の切り替わるスピードを指定します。(単位は秒)

Background

Option NameTypeDefaultDescription
bgDefaultImageString''
全てのAreaが非アクティブの時にマップの背景に表示される画像のパス指定します。
bgImagesObject{}
Areaアクティブ時にマップの背景に表示される画像をAreaIdをキー、画像パスをバリューとし個別指定します。
bgBrightnessNumber100
背景画像の明るさを指定します。(単位はパーセント)
bgChangeSpeedNumber0.5
背景画像の切り替わるスピードを指定します。(単位は秒)

Option Parts

In addition to the basic parts, it is possible to define optional parts. Optional parts assign specific data attributes to HTML tag elements.

Sample: HTML

<!-- Click to make SVG active. -->
<button data-mapnj="selector" data-area-id="foo">Active Foo</button>

<!-- Click to make all SVGs default. -->
<button data-mapnj="reset-selector">Reset</button>

<!-- Appears when A is active. -->
<div data-mapnj="content" data-area-id="foo"></div>

<div>
  <svg id="mapnj-area-foo"></svg>
  <svg id="mapnj-area-bar"></svg>
</div>
Parts NameData AttributesDescription
Selectordata-mapnj="selector" & data-area-id="[AreaID]"
クリック時、data-area-idと一致するAreaIDを持つAreaをアクティブ化させます。
※ data-area-idに空文字を指定すると、後述のarea-reset-selectorと同じ結果になります。
ResetSelectordata-mapnj="reset-selector"
クリック時、全てのAreaを非アクティブ化させます。
Contentdata-mapnj="content" & data-area-id="[AreaID]"
ここで指定したdata-area-idと現在アクティブ化しているAreaのIDが一致していた場合、画面に表示されます。
※ data-area-idに空文字を指定すると、全てのAreaが非アクティブの時に表示されるコンテンツになります。

Events

By using the "on" method of the instance, it is possible to define functions that are executed at specific timings.

Sample: JavaScript
const mapnj = new('#your-mapnj');

mapnj.on('AREA_CHANGE', (mapnj) => {
  console.log('instance:', mapnj);
  console.log('actiev area is :', mapnj.state.activeAreaId);
})
Event NameArgumentsDescription
AREA_CHANGEmapnj
選択中のエリアが変更された直後に実行されます。また選択中のエリアが無くなった場合にも実行されます。
AREA_CLICKmapnj
いずれかのエリアがクリックされた直後に実行されます。
AREA_MOUSEOVERmapnj
いずれかのAreaがマウスオーバーされた直後に実行されます。
AREA_MOUSEOUTmapnj
いずれかのAreaがマウスアウトされた直後に実行されます。
AREA_LABEL_CLICKmapnj
いずれかのLabelがクリックされた直後に実行されます。
AREA_LABEL_MOUSEOVERmapnj
いずれかのLabelがマウスオーバーされた直後に実行されます。
AREA_LABEL_MOUSEOUTmapnj
いずれかのLabelがマウスアウトされた直後に実行されます。
AREA_SELECTOR_CLICKmapnj
いずれかのArea Selectorがクリックされた直後に実行されます。
AREA_SELECTOR_MOUSEOVERmapnj
いずれかのArea Selectorがマウスオーバーされた直後に実行されます。
AREA_SELECTOR_MOUSEOUTmapnj
いずれかのArea Selectorがマウスアウトされた直後に実行されます。
AREA_RESET_SELECTOR_CLICKmapnj
いずれかのArea Reset Selectorがクリックされた直後に実行されます。

CSS Integration

Design classes are added to the HTML tag element specified as the first argument during instance creation, depending on the state. By utilizing these design classes, it is also possible to create custom design parts that work in conjunction with MapNJ.

NameDescription
--mapnj-is-active-area_true
いずれかのAreaがアクティブ化している時に付加されます。
--mapnj-is-active-area_false
いずれのAreaもアクティブ化していない時に付加されます。
--mapnj-active-area_[AreaID]
アクティブ化されているAreaがある場合にそのAreaIDを最後尾文字列とし、付加されます。
--mapnj-hover-area_[AreaID]
マウスホバーされているAreaがある場合にそのAreaIDを最後尾文字列とし、付加されます。

Programmatic API

MapNJ v1 exposes its selection state directly. You can select, reset, read, and subscribe from your own code.

Sample: JavaScript
const mapnj = new MapNJ('#your-mapnj');

// select / deselect from your own code
mapnj.selectArea('shibuya');
mapnj.reset();

// read the current state
console.log(mapnj.activeAreaId); // also: hoverAreaId, prevActiveAreaId
console.log(mapnj.getState()); // { activeAreaId, prevActiveAreaId, hoverAreaId }

// subscribe to any state change (returns an unsubscribe function)
const off = mapnj.subscribe((state) => {
  console.log('active:', state.activeAreaId);
});
off();
NameReturnsDescription
selectArea(areaId)void
Selects an area by id. Observers (contents, backgrounds, colors) are notified the same way as when an external selector is clicked.
reset()void
Deselects the current area, the same way as a reset selector click.
getState()MapNJState
Returns a snapshot of the current state (activeAreaId, prevActiveAreaId, hoverAreaId).
subscribe(listener)() => void
Subscribes to any state change regardless of the action type. Returns an unsubscribe function.
on(action, callback)() => void
The existing event API. In v1 the action name is type-checked (TypeScript) and the call returns an unsubscribe function.
activeAreaId / hoverAreaId / prevActiveAreaIdstring
Read-only getters for the current selection and hover state.

React Adapter

A React adapter ships as the subpath export mapnj/react (React 18+, optional peer dependency). The useMapNJ() hook creates the instance when the ref is attached, keeps the state in sync with React, and destroys the instance on unmount (StrictMode-safe).

Sample: React
import { useMapNJ } from 'mapnj/react';

function TouristMap() {
  const { ref, state, selectArea, reset } = useMapNJ({
    areaActiveFillColor: '#e8b4a0',
  });

  return (
    <div>
      <div ref={ref}>{/* inline SVG with mapnj-area-* ids */}</div>
      <p>Active area: {state.activeAreaId || 'none'}</p>
      <button onClick={() => selectArea('shibuya')}>Select Shibuya</button>
      <button onClick={reset}>Reset</button>
    </div>
  );
}
NameReturnsDescription
ref(node) => void
Callback ref. Attach it to the container element that wraps your inline SVG.
mapnjMapNJ | null
The underlying MapNJ instance (null before mount). Use it for the full core API.
stateMapNJState
The current selection / hover state. Updates trigger React re-renders.
selectArea(areaId)void
Selects an area programmatically.
reset()void
Deselects the current area.

CSS Hooks (Data Attributes)

In addition to the existing --mapnj-* classes, MapNJ v1 exposes the state as data attributes, which are easier to target with CSS attribute selectors.

Sample: CSS
/* container: which area is active / hovered */
#your-mapnj[data-mapnj-active-area='shibuya'] .legend-shibuya {
  font-weight: bold;
}

/* each area / label: current state */
#your-mapnj [data-mapnj-state='active'] {
  filter: drop-shadow(0 2px 6px rgb(0 0 0 / 0.3));
}
NameDescription
data-mapnj-active-area="[AreaID]"
Set on the container while an area is selected. Removed when nothing is selected.
data-mapnj-hover-area="[AreaID]"
Set on the container while an area is hovered (mouse / pen only).
data-mapnj-state="active | default"
Set on each area and label. Reflects whether the part is currently highlighted.

Accessibility & Interaction

MapNJ v1 treats interaction quality as a default, not an option:

  • Areas and clickable labels are focusable buttons (role="button", tabindex="0") and respond to Enter / Space.
  • The active area exposes aria-pressed="true", so assistive technology can follow the selection.
  • Hover uses Pointer Events. Touch devices no longer produce a stuck hover state.
  • When the OS "reduce motion" setting is on, transitions and label entrance animations are skipped.

Migration from 0.x

If you are upgrading from the 0.x line, here is what changed:

  • The package is now ESM-first (ESM + CJS + IIFE). If you imported from mapnj/lib/..., switch to the package root import.
  • The CDN file dist/MapNJ.min.js keeps its path — existing embeds continue to work.
  • Hover events keep their names (AREA_MOUSEOVER etc.) but fire from Pointer Events; touch does not emit them.
  • on() now returns an unsubscribe function, and invalid event names are rejected at compile time in TypeScript.
© 2024 Masaaki Sumimoto.All rights reserved.