Skip to content

Instantly share code, notes, and snippets.

@david-mart
Last active May 23, 2020 22:04
Show Gist options
  • Save david-mart/5c38586f045b7823ba6de0a513b5a8fe to your computer and use it in GitHub Desktop.
Save david-mart/5c38586f045b7823ba6de0a513b5a8fe to your computer and use it in GitHub Desktop.
icon-url-generator.js
import Vue from 'vue'
import LocationIcon from './LocationIcon'
// create a constructor from a Vue component
const LocationIconConstructor = Vue.extend(LocationIcon)
export const getColoredIconUrl = (fillColor, strokeColor) => {
// create a Vue element with required props
const iconComponent = new LocationIconConstructor({ propsData: { fillColor, strokeColor } })
// mount the component shadow DOM
iconComponent.$mount()
// get icon DOM element
const iconDom = iconComponent.$el
// generate an html string from an element
const iconString = new XMLSerializer().serializeToString(iconComponent.$el)
// finally, generate a data url from a string
return 'data:image/svg+xml;charset=UTF-8;base64,' + btoa(iconString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment