-
Notifications
You must be signed in to change notification settings - Fork 609
/
Canvg.ts
205 lines (184 loc) · 4.15 KB
/
Canvg.ts
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
import { RenderingContext2D } from './types'
import { IParserOptions, Parser } from './Parser'
import {
IScreenOptions,
IScreenStartOptions,
Screen
} from './Screen'
import {
IDocumentOptions,
Document,
SVGElement
} from './Document'
type DOMDocument = typeof window.document
export interface IOptions extends IParserOptions,
IScreenOptions,
IScreenStartOptions,
IDocumentOptions {}
/**
* SVG renderer on canvas.
*/
export class Canvg {
/**
* Create Canvg instance from SVG source string or URL.
* @param ctx - Rendering context.
* @param svg - SVG source string or URL.
* @param options - Rendering options.
* @returns Canvg instance.
*/
static async from(
ctx: RenderingContext2D,
svg: string,
options: IOptions = {}
) {
const parser = new Parser(options)
const svgDocument = await parser.parse(svg)
return new Canvg(ctx, svgDocument, options)
}
/**
* Create Canvg instance from SVG source string.
* @param ctx - Rendering context.
* @param svg - SVG source string.
* @param options - Rendering options.
* @returns Canvg instance.
*/
static fromString(
ctx: RenderingContext2D,
svg: string,
options: IOptions = {}
) {
const parser = new Parser(options)
const svgDocument = parser.parseFromString(svg)
return new Canvg(ctx, svgDocument, options)
}
/**
* XML/HTML parser instance.
*/
readonly parser: Parser
/**
* Screen instance.
*/
readonly screen: Screen
/**
* Canvg Document.
*/
readonly document: Document
private readonly documentElement: SVGElement
private readonly options: IOptions
/**
* Main constructor.
* @param ctx - Rendering context.
* @param svg - SVG Document.
* @param options - Rendering options.
*/
constructor(
ctx: RenderingContext2D,
svg: DOMDocument,
options: IOptions = {}
) {
this.parser = new Parser(options)
this.screen = new Screen(ctx, options)
this.options = options
const document = new Document(this, options)
const documentElement = document.createDocumentElement(svg)
this.document = document
this.documentElement = documentElement
}
/**
* Create new Canvg instance with inherited options.
* @param ctx - Rendering context.
* @param svg - SVG source string or URL.
* @param options - Rendering options.
* @returns Canvg instance.
*/
fork(
ctx: RenderingContext2D,
svg: string,
options: IOptions = {}
) {
return Canvg.from(ctx, svg, {
...this.options,
...options
})
}
/**
* Create new Canvg instance with inherited options.
* @param ctx - Rendering context.
* @param svg - SVG source string.
* @param options - Rendering options.
* @returns Canvg instance.
*/
forkString(
ctx: RenderingContext2D,
svg: string,
options: IOptions = {}
) {
return Canvg.fromString(ctx, svg, {
...this.options,
...options
})
}
/**
* Document is ready promise.
* @returns Ready promise.
*/
ready() {
return this.screen.ready()
}
/**
* Document is ready value.
* @returns Is ready or not.
*/
isReady() {
return this.screen.isReady()
}
/**
* Render only first frame, ignoring animations and mouse.
* @param options - Rendering options.
*/
async render(options: IScreenStartOptions = {}) {
this.start({
enableRedraw: true,
ignoreAnimation: true,
ignoreMouse: true,
...options
})
await this.ready()
this.stop()
}
/**
* Start rendering.
* @param options - Render options.
*/
start(options: IScreenStartOptions = {}) {
const {
documentElement,
screen,
options: baseOptions
} = this
screen.start(documentElement, {
enableRedraw: true,
...baseOptions,
...options
})
}
/**
* Stop rendering.
*/
stop() {
this.screen.stop()
}
/**
* Resize SVG to fit in given size.
* @param width
* @param height
* @param preserveAspectRatio
*/
resize(
width: number,
height = width,
preserveAspectRatio: boolean|string = false
) {
this.documentElement.resize(width, height, preserveAspectRatio)
}
}