kopia lustrzana https://github.com/backface/turtlestitch
commit
8ca886e0c8
41
src/gui.js
41
src/gui.js
|
|
@ -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');
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue