Building An SVG Map With Vue.js

This article provides a simple example of using Vue.js to work with SVG graphics.

Building An SVG Map With Vue.js

Why Vue.js?

Vue.js provides practical ways to create component-based dynamic user interfaces where you can powerfully control and manage DOM elements. You can easily update and manage various SVG elements - dynamically - using only a small subset of the features. It also allows you to group related SVG elements and compose them.

For example we will use SVG of world continents.


The component will consist of two files, the main component will be called, for example, SvgMap.vue - this is our file in which we will keep the main SVG hierarchy.

<template lang="pug">
      :viewBox="`0 0 ${viewBoxW} ${viewBoxH}`"
      g(:transform="`translate(${translateL}, ${translateT}) scale(${scale})`" ref="g")

import SvgMapPath from './SvgMapPath';

export default {
  name: 'SvgMap',
  components: {
  data() {
    return {
      viewBoxW: 1140,
      viewBoxH: 580,
      translateL: -865,
      translateT: -810,
      scale: 1,


SvgMapPath.vue - component for one path.

<template lang="pug">
      :style="{fill: fill}"


export default {
  name: 'SvgMapPath',
  props: {
    id: String,
    title: String,
    d: String,
    fill: [String, Number],

After creating the main files, we’ll start working with the svg map file ,for example, we took the map, which is ideal and has all the information we need. Let’s create the WorldContinents.js file where we will store the data of the continents in JSON.

export const CONTINENTS = [
    id: 'EU',
    title: 'Europe',
    d: 'M358.9,97.6 L359.5,98.7 L359.2,100.1 L357.3,98.3 Z',

Let’s do v-for and pass the necessary parameters to SvgMapPath.

    v-for="path in CONTINENTS",

In total, the basis is ready, and it is possible to work with each continent as a separate component. As an example, you can add different events:

  • @mouseleave.native
  • @mouseenter.native
  • @mousemove.native

For example, lets change randomly with the mounted fill of the continent :create a random number function where you can set the minimum and maximum:

getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;

And one more function for changing colors, the easiest way to do color is through rgba, where the random number will act as opacity:

randomFill() {
    const alpha = this.getRandomArbitrary(30, 100);
    return `rgba(144,190,109, ${alpha}%`;

In the end, we got a pretty good base component for working with SVG cards.

Let’s summarize:

Vue.js gives you great opportunities to work with SVG, this is just a small example that shows how easy it is to make friends VUE with SVG and make a functional map.

comments powered by Disqus