1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var settings = require('@pixi/settings');
|
6 | var utils = require('@pixi/utils');
|
7 | var BaseImageResource = require('./BaseImageResource.js');
|
8 |
|
9 | const _SVGResource = class extends BaseImageResource.BaseImageResource {
|
10 | constructor(sourceBase64, options) {
|
11 | options = options || {};
|
12 | super(settings.settings.ADAPTER.createCanvas());
|
13 | this._width = 0;
|
14 | this._height = 0;
|
15 | this.svg = sourceBase64;
|
16 | this.scale = options.scale || 1;
|
17 | this._overrideWidth = options.width;
|
18 | this._overrideHeight = options.height;
|
19 | this._resolve = null;
|
20 | this._crossorigin = options.crossorigin;
|
21 | this._load = null;
|
22 | if (options.autoLoad !== false) {
|
23 | this.load();
|
24 | }
|
25 | }
|
26 | load() {
|
27 | if (this._load) {
|
28 | return this._load;
|
29 | }
|
30 | this._load = new Promise((resolve) => {
|
31 | this._resolve = () => {
|
32 | this.resize(this.source.width, this.source.height);
|
33 | resolve(this);
|
34 | };
|
35 | if (_SVGResource.SVG_XML.test(this.svg.trim())) {
|
36 | if (!btoa) {
|
37 | throw new Error("Your browser doesn't support base64 conversions.");
|
38 | }
|
39 | this.svg = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(this.svg)))}`;
|
40 | }
|
41 | this._loadSvg();
|
42 | });
|
43 | return this._load;
|
44 | }
|
45 | _loadSvg() {
|
46 | const tempImage = new Image();
|
47 | BaseImageResource.BaseImageResource.crossOrigin(tempImage, this.svg, this._crossorigin);
|
48 | tempImage.src = this.svg;
|
49 | tempImage.onerror = (event) => {
|
50 | if (!this._resolve) {
|
51 | return;
|
52 | }
|
53 | tempImage.onerror = null;
|
54 | this.onError.emit(event);
|
55 | };
|
56 | tempImage.onload = () => {
|
57 | if (!this._resolve) {
|
58 | return;
|
59 | }
|
60 | const svgWidth = tempImage.width;
|
61 | const svgHeight = tempImage.height;
|
62 | if (!svgWidth || !svgHeight) {
|
63 | throw new Error("The SVG image must have width and height defined (in pixels), canvas API needs them.");
|
64 | }
|
65 | let width = svgWidth * this.scale;
|
66 | let height = svgHeight * this.scale;
|
67 | if (this._overrideWidth || this._overrideHeight) {
|
68 | width = this._overrideWidth || this._overrideHeight / svgHeight * svgWidth;
|
69 | height = this._overrideHeight || this._overrideWidth / svgWidth * svgHeight;
|
70 | }
|
71 | width = Math.round(width);
|
72 | height = Math.round(height);
|
73 | const canvas = this.source;
|
74 | canvas.width = width;
|
75 | canvas.height = height;
|
76 | canvas._pixiId = `canvas_${utils.uid()}`;
|
77 | canvas.getContext("2d").drawImage(tempImage, 0, 0, svgWidth, svgHeight, 0, 0, width, height);
|
78 | this._resolve();
|
79 | this._resolve = null;
|
80 | };
|
81 | }
|
82 | static getSize(svgString) {
|
83 | const sizeMatch = _SVGResource.SVG_SIZE.exec(svgString);
|
84 | const size = {};
|
85 | if (sizeMatch) {
|
86 | size[sizeMatch[1]] = Math.round(parseFloat(sizeMatch[3]));
|
87 | size[sizeMatch[5]] = Math.round(parseFloat(sizeMatch[7]));
|
88 | }
|
89 | return size;
|
90 | }
|
91 | dispose() {
|
92 | super.dispose();
|
93 | this._resolve = null;
|
94 | this._crossorigin = null;
|
95 | }
|
96 | static test(source, extension) {
|
97 | return extension === "svg" || typeof source === "string" && source.startsWith("data:image/svg+xml") || typeof source === "string" && _SVGResource.SVG_XML.test(source);
|
98 | }
|
99 | };
|
100 | let SVGResource = _SVGResource;
|
101 | SVGResource.SVG_XML = /^(<\?xml[^?]+\?>)?\s*(<!--[^(-->)]*-->)?\s*\<svg/m;
|
102 | SVGResource.SVG_SIZE = /<svg[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*>/i;
|
103 |
|
104 | exports.SVGResource = SVGResource;
|
105 |
|