17
17
18
18
---
19
19
20
+ ## Live Demo
21
+
22
+ [ ![ Edit on Stackblitz] ( https://developer.stackblitz.com/img/open_in_stackblitz.svg )] ( https://stackblitz.com/edit/canvas-constructor-vite?file=main.js )
23
+
20
24
## Installation
21
25
22
26
This module requires one of the following packages to be installed for Node.js:
@@ -35,8 +39,8 @@ How to use it:
35
39
36
40
``` js
37
41
const { Canvas } = require (' canvas-constructor/napi-rs' );
38
- // or ` canvas-constructor/skia` if you are using `skia-canvas`
39
- // or ` canvas-constructor/cairo` if you are using `canvas`
42
+ // or ' canvas-constructor/skia' if you are using `skia-canvas`
43
+ // or ' canvas-constructor/cairo' if you are using `canvas`
40
44
41
45
new Canvas (300 , 300 )
42
46
.setColor (' #AEFD54' )
@@ -50,6 +54,7 @@ new Canvas(300, 300)
50
54
** Browser** :
51
55
52
56
``` html
57
+ <canvas id =" canvas" width =" 300" height =" 400" ></canvas >
53
58
<script type =" text/javascript" src =" https://unpkg.com/canvas-constructor" ></script >
54
59
<script type =" text/javascript" >
55
60
const canvasElement = document .getElementById (' canvas' );
@@ -62,17 +67,13 @@ new Canvas(300, 300)
62
67
</script >
63
68
```
64
69
65
- Alternatively, you can import ` canvas-constructor/browser ` .
70
+ Alternatively, you can import ` canvas-constructor/browser ` if you are using a bundler such as Vite, Webpack, or Rollup:
66
71
67
- - That will create a canvas with size of 300 pixels width, 300 pixels height.
68
- - Set the color to #AEFD54
69
- - Draw a rectangle with the previous color, covering all the pixels from (5, 5) to (290 + 5, 290 + 5)
70
- - Set the color to #FFAE23
71
- - Set the font size to 28 pixels with font Impact.
72
- - Write the text 'Hello World!' in the position (130, 150)
73
- - Return a buffer.
72
+ ``` js
73
+ import { Canvas } from ' canvas-constructor/browser' ;
74
+ ```
74
75
75
- Now, let's suppose we want to add images, we'll use ` Canvas. loadImage` , which works in both Node.js and browser :
76
+ Now, let's suppose we want to add images, we can use the ` loadImage ` function , which works in all supported environments :
76
77
77
78
``` js
78
79
const { Canvas , loadImage } = require (' canvas-constructor/napi-rs' );
@@ -90,15 +91,8 @@ async function createCanvas() {
90
91
}
91
92
```
92
93
93
- - That will create a canvas with size of 300 pixels width, 400 pixels height.
94
- - Draw an image, given a Buffer (the image from the images folder).
95
- - Set the color to #FFAE23
96
- - Set the font size to 28 pixels with font Impact.
97
- - Set the text alignment to center.
98
- - Write the text 'Kitten!' in the position (150, 370)
99
- - Return a buffer.
100
-
101
- And now, you have created an image with a kitten in the background and some centred text in the bottom of it.
94
+ And now, you have created an image with a kitten in the background and some centered text at the bottom of it.
102
95
103
96
If you experience issues with ` @napi-rs/canvas ` , ` skia-canvas ` , or ` canvas ` , please refer to their respective package repositories, this
104
- package is just a convenient wrapper for the three.
97
+ package is just a convenient wrapper that makes it easier to use the canvas in both Node.js and the browser. And does not modify the
98
+ behavior of the underlying canvas implementation.
0 commit comments