Website/slides/reveal.js-3.6.0/plugin/svginline/data-src-svg.js

38 lines
1.2 KiB
JavaScript

// Courtesy of...
// https://github.com/flying-sheep/revealjs-template/blob/master/src/data-src-svg.js
// as per
// https://github.com/hakimel/reveal.js/issues/1258
'use strict';
(function() {
var svgsToLoad = document.querySelectorAll('svg[data-src]');
console.log(svgsToLoad)
var loadSVGs = Array.map(svgsToLoad, function (svg) {
fetch(svg.getAttribute('data-src')).then(function (response) {
return response.text();
}).then(function (svgCode) {
var svgDoc = new DOMParser().parseFromString(svgCode, 'image/svg+xml');
var newSVG = svgDoc.documentElement;
for (var i = 0; i < svg.attributes.length; i++) {
var attr = svg.attributes[i];
newSVG.setAttribute(attr.name, attr.value);
}
svg.parentNode.replaceChild(newSVG, svg);
});
});
Promise.all(loadSVGs).then(function (val) {
var stretchSVGs = document.querySelectorAll('svg.stretch:not([preserveAspectRatio])');
Array.call(stretchSVGs, function (svg) {
if (!svg.hasAttribute('viewBox')) {
var w = svg.getAttribute('width');
var h = svg.getAttribute('height');
svg.setAttribute('viewBox', '0 0 ' + w + ' ' + h);
}
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
});
});
})()