@pixi/core
Version:
Core PixiJS
106 lines (101 loc) • 3.6 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var settings = require('@pixi/settings');
var utils = require('@pixi/utils');
var BaseImageResource = require('./BaseImageResource.js');
const _SVGResource = class extends BaseImageResource.BaseImageResource {
constructor(sourceBase64, options) {
options = options || {};
super(settings.settings.ADAPTER.createCanvas());
this._width = 0;
this._height = 0;
this.svg = sourceBase64;
this.scale = options.scale || 1;
this._overrideWidth = options.width;
this._overrideHeight = options.height;
this._resolve = null;
this._crossorigin = options.crossorigin;
this._load = null;
if (options.autoLoad !== false) {
this.load();
}
}
load() {
if (this._load) {
return this._load;
}
this._load = new Promise((resolve) => {
this._resolve = () => {
this.resize(this.source.width, this.source.height);
resolve(this);
};
if (_SVGResource.SVG_XML.test(this.svg.trim())) {
if (!btoa) {
throw new Error("Your browser doesn't support base64 conversions.");
}
this.svg = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(this.svg)))}`;
}
this._loadSvg();
});
return this._load;
}
_loadSvg() {
const tempImage = new Image();
BaseImageResource.BaseImageResource.crossOrigin(tempImage, this.svg, this._crossorigin);
tempImage.src = this.svg;
tempImage.onerror = (event) => {
if (!this._resolve) {
return;
}
tempImage.onerror = null;
this.onError.emit(event);
};
tempImage.onload = () => {
if (!this._resolve) {
return;
}
const svgWidth = tempImage.width;
const svgHeight = tempImage.height;
if (!svgWidth || !svgHeight) {
throw new Error("The SVG image must have width and height defined (in pixels), canvas API needs them.");
}
let width = svgWidth * this.scale;
let height = svgHeight * this.scale;
if (this._overrideWidth || this._overrideHeight) {
width = this._overrideWidth || this._overrideHeight / svgHeight * svgWidth;
height = this._overrideHeight || this._overrideWidth / svgWidth * svgHeight;
}
width = Math.round(width);
height = Math.round(height);
const canvas = this.source;
canvas.width = width;
canvas.height = height;
canvas._pixiId = `canvas_${utils.uid()}`;
canvas.getContext("2d").drawImage(tempImage, 0, 0, svgWidth, svgHeight, 0, 0, width, height);
this._resolve();
this._resolve = null;
};
}
static getSize(svgString) {
const sizeMatch = _SVGResource.SVG_SIZE.exec(svgString);
const size = {};
if (sizeMatch) {
size[sizeMatch[1]] = Math.round(parseFloat(sizeMatch[3]));
size[sizeMatch[5]] = Math.round(parseFloat(sizeMatch[7]));
}
return size;
}
dispose() {
super.dispose();
this._resolve = null;
this._crossorigin = null;
}
static test(source, extension) {
return extension === "svg" || typeof source === "string" && source.startsWith("data:image/svg+xml") || typeof source === "string" && _SVGResource.SVG_XML.test(source);
}
};
let SVGResource = _SVGResource;
SVGResource.SVG_XML = /^(<\?xml[^?]+\?>)?\s*(<!--[^(-->)]*-->)?\s*\<svg/m;
SVGResource.SVG_SIZE = /<svg[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*>/i;
exports.SVGResource = SVGResource;
//# sourceMappingURL=SVGResource.js.map
;