source: imaps-frontend/node_modules/canvg/README.md@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[79a0317]1# canvg
2
3[![NPM version][npm]][npm-url]
4[![Dependencies status][deps]][deps-url]
5[![Build status][build]][build-url]
6[![Coverage status][coverage]][coverage-url]
7[![Dependabot badge][dependabot]][dependabot-url]
8[![Documentation badge][documentation]][documentation-url]
9
10[npm]: https://img.shields.io/npm/v/canvg.svg
11[npm-url]: https://npmjs.com/package/canvg
12
13[deps]: https://david-dm.org/canvg/canvg.svg
14[deps-url]: https://david-dm.org/canvg/canvg
15
16[build]: https://img.shields.io/github/workflow/status/canvg/canvg/CI.svg
17[build-url]: https://github.com/canvg/canvg/actions
18
19[coverage]: https://img.shields.io/coveralls/canvg/canvg.svg
20[coverage-url]: https://coveralls.io/r/canvg/canvg
21
22[dependabot]: https://api.dependabot.com/badges/status?host=github&repo=canvg/canvg
23[dependabot-url]: https://dependabot.com/
24
25[documentation]: https://img.shields.io/badge/API-Documentation-2b7489.svg
26[documentation-url]: https://canvg.github.io/canvg
27
28JavaScript SVG parser and renderer on Canvas. It takes the URL to the SVG file or the text of the SVG file, parses it in JavaScript and renders the result on Canvas.
29
30[Demo](https://canvg.github.io/canvg/demo/index.html)
31
32[Playground](https://jsfiddle.net/0q1vrjxk/)
33
34## Install
35
36```sh
37npm i canvg
38# or
39yarn add canvg
40```
41
42## Usage
43
44Basic module exports:
45
46```js
47export default Canvg;
48export {
49 presets
50};
51```
52
53[Description of all exports you can find in Documentation.](https://canvg.github.io/canvg/index.html)
54
55### Example
56
57```js
58import Canvg from 'canvg';
59
60let v = null;
61
62window.onload = async () => {
63 const canvas = document.querySelector('canvas');
64 const ctx = canvas.getContext('2d');
65
66 v = await Canvg.from(ctx, './svgs/1.svg');
67
68 // Start SVG rendering with animations and mouse handling.
69 v.start();
70};
71
72window.onbeforeunload = () => {
73 v.stop();
74};
75```
76
77<details>
78 <summary>
79 <b>OffscreenCanvas</b>
80 </summary>
81
82```js
83import Canvg, {
84 presets
85} from 'canvg';
86
87self.onmessage = async (event) => {
88 const {
89 width,
90 height,
91 svg
92 } = event.data;
93 const canvas = new OffscreenCanvas(width, height);
94 const ctx = canvas.getContext('2d');
95 const v = await Canvg.from(ctx, svg, presets.offscreen());
96
97 // Render only first frame, ignoring animations and mouse.
98 await v.render();
99
100 const blob = await canvas.convertToBlob();
101 const pngUrl = URL.createObjectURL(blob);
102
103 self.postMessage({
104 pngUrl
105 });
106};
107```
108
109[`OffscreenCanvas` browsers compatibility.](https://caniuse.com/offscreencanvas)
110
111</details>
112
113<details>
114 <summary>
115 <b>NodeJS</b>
116 </summary>
117
118```js
119import {
120 promises as fs
121} from 'fs';
122import {
123 DOMParser
124} from 'xmldom';
125import * as canvas from 'canvas';
126import fetch from 'node-fetch';
127import Canvg, {
128 presets
129} from 'canvg';
130
131const preset = presets.node({
132 DOMParser,
133 canvas,
134 fetch
135});
136
137(async (output, input) => {
138 const svg = await fs.readFile(input, 'utf8');
139 const canvas = preset.createCanvas(800, 600);
140 const ctx = canvas.getContext('2d');
141 const v = Canvg.fromString(ctx, svg, preset);
142
143 // Render only first frame, ignoring animations.
144 await v.render();
145
146 const png = canvas.toBuffer();
147
148 await fs.writeFile(output, png);
149
150})(
151 process.argv.pop(),
152 process.argv.pop()
153);
154```
155
156</details>
157
158<details>
159 <summary>
160 <b>Resize</b>
161 </summary>
162
163```js
164import Canvg, {
165 presets
166} from 'canvg';
167
168self.onmessage = async (event) => {
169 const {
170 width,
171 height,
172 svg
173 } = event.data;
174 const canvas = new OffscreenCanvas(width, height);
175 const ctx = canvas.getContext('2d');
176 const v = await Canvg.from(ctx, svg, presets.offscreen());
177
178 /**
179 * Resize SVG to fit in given size.
180 * @param width
181 * @param height
182 * @param preserveAspectRatio
183 */
184 v.resize(width, height, 'xMidYMid meet');
185
186 // Render only first frame, ignoring animations and mouse.
187 await v.render();
188
189 const blob = await canvas.convertToBlob();
190 const pngUrl = URL.createObjectURL(blob);
191
192 self.postMessage({
193 pngUrl
194 });
195};
196```
197
198</details>
199
200<details>
201 <summary>
202 <b>Browser</b>
203 </summary>
204
205```html
206<script type="text/javascript" src="https://unpkg.com/canvg@3.0.4/lib/umd.js"></script>
207<script type="text/javascript">
208window.onload = () => {
209 const canvas = document.querySelector('canvas');
210 const ctx = canvas.getContext('2d');
211
212 v = canvg.Canvg.fromString(ctx, '<svg width="600" height="600"><text x="50" y="50">Hello World!</text></svg>');
213
214 // Start SVG rendering with animations and mouse handling.
215 v.start();
216
217};
218</script>
219<canvas />
220```
221
222</details>
223
224### Options
225
226The third parameter of `new Canvg(...)`, `Canvg.from(...)` and `Canvg.fromString(...)` is options:
227
228```ts
229interface IOptions {
230 /**
231 * WHATWG-compatible `fetch` function.
232 */
233 fetch?: typeof fetch;
234 /**
235 * XML/HTML parser from string into DOM Document.
236 */
237 DOMParser?: typeof DOMParser;
238 /**
239 * Window object.
240 */
241 window?: Window;
242 /**
243 * Whether enable the redraw.
244 */
245 enableRedraw?: boolean;
246 /**
247 * Ignore mouse events.
248 */
249 ignoreMouse?: boolean;
250 /**
251 * Ignore animations.
252 */
253 ignoreAnimation?: boolean;
254 /**
255 * Does not try to resize canvas.
256 */
257 ignoreDimensions?: boolean;
258 /**
259 * Does not clear canvas.
260 */
261 ignoreClear?: boolean;
262 /**
263 * Scales horizontally to width.
264 */
265 scaleWidth?: number;
266 /**
267 * Scales vertically to height.
268 */
269 scaleHeight?: number;
270 /**
271 * Draws at a x offset.
272 */
273 offsetX?: number;
274 /**
275 * Draws at a y offset.
276 */
277 offsetY?: number;
278 /**
279 * Will call the function on every frame, if it returns true, will redraw.
280 */
281 forceRedraw?(): boolean;
282 /**
283 * Default `rem` size.
284 */
285 rootEmSize?: number;
286 /**
287 * Default `em` size.
288 */
289 emSize?: number;
290 /**
291 * Function to create new canvas.
292 */
293 createCanvas?: (width: number, height: number) => HTMLCanvasElement | OffscreenCanvas;
294 /**
295 * Function to create new image.
296 */
297 createImage?: (src: string, anonymousCrossOrigin?: boolean) => Promise<CanvasImageSource>;
298 /**
299 * Load images anonymously.
300 */
301 anonymousCrossOrigin?: boolean;
302}
303```
304
305#### Options presets
306
307There are two options presets:
308
309- `presets.offscreen()`: options for [`OffscreenCanvas`](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas);
310- `presets.node({ DOMParser, canvas, fetch })`: options for NodeJS with [`node-canvas`](https://github.com/Automattic/node-canvas).
311
312## What's implemented?
313
314The end goal is everything from the [SVG spec](http://www.w3.org/TR/SVG/). The majority of the rendering and animation is working. If you would like to see a feature implemented, don't hesitate to add it to the issues list, or better is to create pull request 😎
Note: See TracBrowser for help on using the repository browser.