@@ -94,49 +94,63 @@ Start by installing:
94
94
#### Generating image files
95
95
96
96
``` js
97
- const {Canvas , loadImage } = require (' skia-canvas' ),
98
- rand = n => Math .floor (n * Math .random ()),
99
- fs = require (' fs' )
97
+ const {Canvas } = require (' skia-canvas' )
98
+
99
+ let canvas = new Canvas (400 , 400 ),
100
+ {width, height} = canvas,
101
+ ctx = canvas .getContext (" 2d" );
102
+
103
+ let sweep = ctx .createConicGradient (Math .PI * 1.2 , width/ 2 , height/ 2 )
104
+ sweep .addColorStop (0 , " red" )
105
+ sweep .addColorStop (0.25 , " orange" )
106
+ sweep .addColorStop (0.5 , " yellow" )
107
+ sweep .addColorStop (0.75 , " green" )
108
+ sweep .addColorStop (1 , " red" )
109
+ ctx .strokeStyle = sweep
110
+ ctx .lineWidth = 100
111
+ ctx .strokeRect (100 ,100 , 200 ,200 )
112
+
113
+ // render to multiple destinations using a background thread
114
+ async function render (){
115
+ // save a ‘retina’ image...
116
+ await canvas .saveAs (" rainbox.png" , {density: 2 })
117
+ // ...or use a shorthand for canvas.toBuffer("png")
118
+ let pngData = await canvas .png
119
+ // ...or embed it in a string
120
+ let pngEmbed = ` <img src="${ await canvas .toDataURL (" png" )} ">`
121
+ }
122
+ render ()
100
123
101
- let canvas = new Canvas (600 , 600 ),
102
- ctx = canvas .getContext (" 2d" ),
124
+ // ...or save the file synchronously from the main thread
125
+ canvas .saveAsSync (" rainbox.pdf" )
126
+ ```
127
+
128
+ #### Multi-page sequences
129
+
130
+ ``` js
131
+ const {Canvas } = require (' skia-canvas' )
132
+
133
+ let canvas = new Canvas (400 , 400 ),
103
134
{width, height} = canvas;
104
135
105
- // draw a sea of blurred dots filling the canvas
106
- ctx . filter = ' blur(12px) hue-rotate(20deg) '
107
- for ( let i = 0 ; i < 800 ; i ++ ){
108
- ctx .fillStyle = ` hsl( ${ rand ( 40 ) } deg, 80%, 50%) `
109
- ctx .beginPath ()
110
- ctx .arc (rand ( width), rand ( height), rand ( 20 ) + 5 , 0 , 2 * Math .PI )
136
+ for ( const color of [ ' orange ' , ' yellow ' , ' green ' , ' skyblue ' , ' purple ' ]){
137
+ ctx = canvas . newPage ()
138
+ ctx . fillStyle = color
139
+ ctx .fillRect ( 0 , 0 , width, height)
140
+ ctx .fillStyle = ' white '
141
+ ctx .arc (width/ 2 , height/ 2 , 40 , 0 , 2 * Math .PI )
111
142
ctx .fill ()
112
143
}
113
144
114
- // mask all of the dots that don't overlap with the text
115
- ctx .filter = ' none'
116
- ctx .globalCompositeOperation = ' destination-in'
117
- ctx .font = ' italic 480px Times, DejaVu Serif'
118
- ctx .textAlign = ' center'
119
- ctx .textBaseline = ' top'
120
- ctx .fillText (' ¶' , width/ 2 , 0 )
121
-
122
- // draw a background behind the clipped text
123
- ctx .globalCompositeOperation = ' destination-over'
124
- ctx .fillStyle = ' #182927'
125
- ctx .fillRect (0 ,0 , width,height)
126
-
127
- // render to files using a background thread
128
145
async function render (){
129
- // save the graphic...
130
- await canvas .saveAs (" pilcrow.png" )
131
- // ...or use a shorthand for canvas.toBuffer("png")
132
- let pngData = await canvas .png
133
- // ...or embed it in a string
134
- console .log (` <img src="${ await canvas .toDataURL (" png" )} ">` )
146
+ // save to a multi-page PDF file
147
+ await canvas .saveAs (" all-pages.pdf" )
148
+
149
+ // save to files named `page-01.png`, `page-02.png`, etc.
150
+ await canvas .saveAs (" page-{2}.png" )
135
151
}
136
152
render ()
137
153
138
- // ...or save the file synchronously from the main thread
139
- canvas .saveAsSync (" pilcrow.png" )
140
154
```
141
155
142
156
#### Rendering to a window
0 commit comments