How can I use a SVG image as layer on OpenLayers-3

Nowadays, 2018, a example for Open Layers 4:

var svgComment = '<svg width="160" height="160" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" viewPort="0 0 160 160" class="svgClass">'
    + '<circle cx="30" cy="30" r="10" stroke="rgb(0, 191, 255)" stroke-width="1" fill="none" opacity="0.8">'
    + '<animate attributeType="CSS" attributeName="stroke-width" from="1" to="30" dur="0.5s" begin="0s" repeatCount="indefinite" />'
    + '<animate attributeType="CSS" attributeName="opacity" from="0.8" to="0.2" dur="0.5s" begin="0s" repeatCount="indefinite" />'
    + '</circle>'
    + '<circle cx="30" cy="30" r="10" fill="rgba(0,0,0,0.8)">'
    + '</circle>'
    + '</svg>';


//Test SVG
//var img = document.createElement('img');
//img.src=src;
//document.body.append(img);

 var commentStyle =  new ol.style.Style({
    image: new ol.style.Icon({
      src: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgComment)
    })
  });

var vectorSource = new ol.source.Vector({
  features: [
      new ol.Feature({
        geometry: new ol.geom.Point([0, 0])
      }) 
  ]
});

var vectorLayer = new ol.layer.Vector({
  name: 'Comments',
  style: commentStyle,
  source: vectorSource
});

//display the map
var rasterLayer = new ol.layer.Tile({
  source: new ol.source.TileJSON({
    url: 'https://api.tiles.mapbox.com/v3/mapbox.geography-class.json?secure',
    crossOrigin: ''
  })
});

var map = new ol.Map({
  layers: [rasterLayer, vectorLayer],
  target: document.getElementById('map'),
  view: new ol.View({
    center: [0, 0],
    zoom: 3
  })
});
<script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
<div id="map" class="map"></div>

see original post:

https://stackoverflow.com/a/48232790/2797243


@Alvin Lindstam's answers is almost there but doesn't show the full image on Chrome (or any on IE). Because the SVG doesn't have a fixed size the width and height need to be set in an imageLoadFunction

var extent = [0, 0, 512, 512];
var projection = new ol.proj.Projection({
  code: 'static-image',
  units: 'pixels',
  extent: extent
});

var map = new ol.Map({
  layers: [
    new ol.layer.Image({
      source: new ol.source.ImageStatic({
        url: 'https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg',
        projection: projection,
        imageExtent: extent,
        imageLoadFunction: function (image, src) {
           image.getImage().src = src;
           image.getImage().width = ol.extent.getWidth(extent);
           image.getImage().height = ol.extent.getHeight(extent);
        }
      })
    })
  ],
  target: 'map',
  view: new ol.View({
    projection: projection,
    center: ol.extent.getCenter(extent),
    zoom: 0
  })
});
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
<div id="map" class="map"></div>

You can not use the ol.source.Vector with svg files, but OL3 can display svg files as images.

The image stays sharp when zoomed.

I modified the official static image example, and replaced the png file with a svg file. See the runnable example below.

var extent = [0, 0, 512, 512];
var projection = new ol.proj.Projection({
  code: 'static-image',
  units: 'pixels',
  extent: extent
});

var map = new ol.Map({
  layers: [
    new ol.layer.Image({
      source: new ol.source.ImageStatic({
        url: 'https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg',
        projection: projection,
        imageExtent: extent
      })
    })
  ],
  target: 'map',
  view: new ol.View({
    projection: projection,
    center: ol.extent.getCenter(extent),
    zoom: 0
  })
});
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
<div id="map" class="map"></div>