1 | "use strict";
|
2 | var settings = require("@pixi/settings"), utils = require("@pixi/utils"), BaseImageResource = require("./BaseImageResource.js");
|
3 | const _SVGResource = class _SVGResource2 extends BaseImageResource.BaseImageResource {
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | constructor(sourceBase64, options) {
|
13 | 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, options.autoLoad !== !1 && this.load();
|
14 | }
|
15 | load() {
|
16 | return this._load ? this._load : (this._load = new Promise((resolve) => {
|
17 | if (this._resolve = () => {
|
18 | this.update(), resolve(this);
|
19 | }, _SVGResource2.SVG_XML.test(this.svg.trim())) {
|
20 | if (!btoa)
|
21 | throw new Error("Your browser doesn't support base64 conversions.");
|
22 | this.svg = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(this.svg)))}`;
|
23 | }
|
24 | this._loadSvg();
|
25 | }), this._load);
|
26 | }
|
27 |
|
28 | _loadSvg() {
|
29 | const tempImage = new Image();
|
30 | BaseImageResource.BaseImageResource.crossOrigin(tempImage, this.svg, this._crossorigin), tempImage.src = this.svg, tempImage.onerror = (event) => {
|
31 | this._resolve && (tempImage.onerror = null, this.onError.emit(event));
|
32 | }, tempImage.onload = () => {
|
33 | if (!this._resolve)
|
34 | return;
|
35 | const svgWidth = tempImage.width, svgHeight = tempImage.height;
|
36 | if (!svgWidth || !svgHeight)
|
37 | throw new Error("The SVG image must have width and height defined (in pixels), canvas API needs them.");
|
38 | let width = svgWidth * this.scale, height = svgHeight * this.scale;
|
39 | (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);
|
40 | const canvas = this.source;
|
41 | 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;
|
42 | };
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | static getSize(svgString) {
|
50 | const sizeMatch = _SVGResource2.SVG_SIZE.exec(svgString), size = {};
|
51 | return sizeMatch && (size[sizeMatch[1]] = Math.round(parseFloat(sizeMatch[3])), size[sizeMatch[5]] = Math.round(parseFloat(sizeMatch[7]))), size;
|
52 | }
|
53 |
|
54 | dispose() {
|
55 | super.dispose(), this._resolve = null, this._crossorigin = null;
|
56 | }
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | static test(source, extension) {
|
64 | return extension === "svg" || typeof source == "string" && source.startsWith("data:image/svg+xml") || typeof source == "string" && _SVGResource2.SVG_XML.test(source);
|
65 | }
|
66 |
|
67 | };
|
68 | _SVGResource.SVG_XML = /^(<\?xml[^?]+\?>)?\s*(<!--[^(-->)]*-->)?\s*\<svg/m, |
69 |
|
70 |
|
71 |
|
72 |
|
73 | _SVGResource.SVG_SIZE = /<svg[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*>/i;
|
74 | let SVGResource = _SVGResource;
|
75 | exports.SVGResource = SVGResource;
|
76 |
|