Merge pull request #2725 from jguille2/svgLoading

Fixing svg loading
pull/95/head
Jens Mönig 2020-12-01 13:25:46 +01:00 zatwierdzone przez GitHub
commit 8ca886e0c8
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 40 dodań i 1 usunięć

Wyświetl plik

@ -2139,7 +2139,46 @@ IDE_Morph.prototype.droppedImage = function (aCanvas, name) {
};
IDE_Morph.prototype.droppedSVG = function (anImage, name) {
var costume = new SVG_Costume(anImage, name.split('.')[0]);
var myself,
viewBox, w, h,
svgNormalized,
headerLenght = anImage.src.search('base64') + 7, // usually 26 from "data:image/svg+xml;base64,"
svgStrEncoded = anImage.src.substring(headerLenght),
svgObj = new DOMParser().parseFromString(atob(svgStrEncoded), "image/svg+xml").firstElementChild;
name = name.split('.')[0];
// check svg 'width' and 'height' attributes and set them if needed
if (svgObj.attributes.getNamedItem("width") &&
svgObj.attributes.getNamedItem("height")) {
this.loadSVG(anImage, name);
} else {
// setting HTMLImageElement default values
w = '300';
h = '150';
// changing default values if viewBox attribute is set
if (svgObj.attributes.getNamedItem("viewBox")) {
viewBox = svgObj.attributes.getNamedItem('viewBox').value;
viewBox = viewBox.split(/[ ,]/).filter(item => item);
if (viewBox.length == 4) {
w = Math.ceil(viewBox[2]);
h = Math.ceil(viewBox[3]);
}
}
svgNormalized = new Image(w, h);
svgObj.setAttribute('width', w);
svgObj.setAttribute('height', h);
svgNormalized.src = 'data:image/svg+xml;base64,' +
btoa(new XMLSerializer().serializeToString(svgObj));
myself = this;
svgNormalized.onload = function () { myself.loadSVG(svgNormalized, name); }
}
};
IDE_Morph.prototype.loadSVG = function (anImage, name) {
var costume = new SVG_Costume(anImage, name);
this.currentSprite.addCostume(costume);
this.currentSprite.wearCostume(costume);
this.spriteBar.tabBar.tabTo('costumes');