UNPKG

@pixi/core

Version:
80 lines (79 loc) 4.27 kB
import { settings } from "@pixi/settings"; import { uid } from "@pixi/utils"; import { BaseImageResource } from "./BaseImageResource.mjs"; const _SVGResource = class _SVGResource2 extends BaseImageResource { /** * @param sourceBase64 - Base64 encoded SVG element or URL for SVG file. * @param {object} [options] - Options to use * @param {number} [options.scale=1] - Scale to apply to SVG. Overridden by... * @param {number} [options.width] - Rasterize SVG this wide. Aspect ratio preserved if height not specified. * @param {number} [options.height] - Rasterize SVG this high. Aspect ratio preserved if width not specified. * @param {boolean} [options.autoLoad=true] - Start loading right away. */ constructor(sourceBase64, options) { options = options || {}, super(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(); } load() { return this._load ? this._load : (this._load = new Promise((resolve) => { if (this._resolve = () => { this.update(), resolve(this); }, _SVGResource2.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(); }), this._load); } /** Loads an SVG image from `imageUrl` or `data URL`. */ _loadSvg() { const tempImage = new Image(); BaseImageResource.crossOrigin(tempImage, this.svg, this._crossorigin), tempImage.src = this.svg, tempImage.onerror = (event) => { this._resolve && (tempImage.onerror = null, this.onError.emit(event)); }, tempImage.onload = () => { if (!this._resolve) return; const svgWidth = tempImage.width, 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, height = svgHeight * this.scale; (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_${uid()}`, canvas.getContext("2d").drawImage(tempImage, 0, 0, svgWidth, svgHeight, 0, 0, width, height), this._resolve(), this._resolve = null; }; } /** * Get size from an svg string using a regular expression. * @param svgString - a serialized svg element * @returns - image extension */ static getSize(svgString) { const sizeMatch = _SVGResource2.SVG_SIZE.exec(svgString), size = {}; return sizeMatch && (size[sizeMatch[1]] = Math.round(parseFloat(sizeMatch[3])), size[sizeMatch[5]] = Math.round(parseFloat(sizeMatch[7]))), size; } /** Destroys this texture. */ dispose() { super.dispose(), this._resolve = null, this._crossorigin = null; } /** * Used to auto-detect the type of resource. * @param {*} source - The source object * @param {string} extension - The extension of source, if set * @returns {boolean} - If the source is a SVG source or data file */ static test(source, extension) { return extension === "svg" || typeof source == "string" && source.startsWith("data:image/svg+xml") || typeof source == "string" && _SVGResource2.SVG_XML.test(source); } // eslint-disable-line max-len }; _SVGResource.SVG_XML = /^(<\?xml[^?]+\?>)?\s*(<!--[^(-->)]*-->)?\s*\<svg/m, /** * Regular expression for SVG size. * @example &lt;svg width="100" height="100"&gt;&lt;/svg&gt; * @readonly */ _SVGResource.SVG_SIZE = /<svg[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*>/i; let SVGResource = _SVGResource; export { SVGResource }; //# sourceMappingURL=SVGResource.mjs.map