- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
import { uid } from '@pixi/utils';
import { BaseImageResource } from './BaseImageResource';
import { settings } from '@pixi/settings';
import type { ISize } from '@pixi/math';
export interface ISVGResourceOptions
{
source?: string;
scale?: number;
width?: number;
height?: number;
autoLoad?: boolean;
crossorigin?: boolean | string;
}
/**
* Resource type for SVG elements and graphics.
* @memberof PIXI
*/
export class SVGResource extends BaseImageResource
{
/** Base64 encoded SVG element or URL for SVG file. */
public readonly svg: string;
/** The source scale to apply when rasterizing on load. */
public readonly scale: number;
/** A width override for rasterization on load. */
public readonly _overrideWidth: number;
/** A height override for rasterization on load. */
public readonly _overrideHeight: number;
/** Call when completely loaded. */
private _resolve: () => void;
/** Promise when loading */
private _load: Promise<SVGResource>;
/** Cross origin value to use */
private _crossorigin?: boolean | string;
/**
* @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: string, options?: ISVGResourceOptions)
{
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;
if (options.autoLoad !== false)
{
this.load();
}
}
load(): Promise<SVGResource>
{
if (this._load)
{
return this._load;
}
this._load = new Promise((resolve): void =>
{
// Save this until after load is finished
this._resolve = (): void =>
{
this.resize(this.source.width, this.source.height);
resolve(this);
};
// Convert SVG inline string to data-uri
if (SVGResource.SVG_XML.test(this.svg.trim()))
{
if (!btoa)
{
throw new Error('Your browser doesn\'t support base64 conversions.');
}
(this as any).svg = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(this.svg)))}`;
}
this._loadSvg();
});
return this._load;
}
/** Loads an SVG image from `imageUrl` or `data URL`. */
private _loadSvg(): void
{
const tempImage = new Image();
BaseImageResource.crossOrigin(tempImage, this.svg, this._crossorigin);
tempImage.src = this.svg;
tempImage.onerror = (event): void =>
{
if (!this._resolve)
{
return;
}
tempImage.onerror = null;
this.onError.emit(event);
};
tempImage.onload = (): void =>
{
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.');
}
// Set render size
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);
// Create a canvas element
const canvas = this.source as HTMLCanvasElement;
canvas.width = width;
canvas.height = height;
(canvas as any)._pixiId = `canvas_${uid()}`;
// Draw the Svg to the canvas
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?: string): ISize
{
const sizeMatch = SVGResource.SVG_SIZE.exec(svgString);
const size: any = {};
if (sizeMatch)
{
size[sizeMatch[1]] = Math.round(parseFloat(sizeMatch[3]));
size[sizeMatch[5]] = Math.round(parseFloat(sizeMatch[7]));
}
return size;
}
/** Destroys this texture. */
dispose(): void
{
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: unknown, extension?: string): boolean
{
// url file extension is SVG
return extension === 'svg'
// source is SVG data-uri
|| (typeof source === 'string' && (/^data:image\/svg\+xml(;(charset=utf8|utf8))?;base64/).test(source))
// source is SVG inline
|| (typeof source === 'string' && SVGResource.SVG_XML.test(source));
}
/**
* Regular expression for SVG XML document.
* @example <?xml version="1.0" encoding="utf-8" ?><!-- image/svg --><svg
* @readonly
*/
static SVG_XML = /^(<\?xml[^?]+\?>)?\s*(<!--[^(-->)]*-->)?\s*\<svg/m;
/**
* Regular expression for SVG size.
* @example <svg width="100" height="100"></svg>
* @readonly
*/
static SVG_SIZE = /<svg[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*>/i; // eslint-disable-line max-len
}