Skip to content

Commit 1492d38

Browse files
committed
use simpler, more task-oriented example code
1 parent 6331b92 commit 1492d38

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

README.md

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,49 +94,63 @@ Start by installing:
9494
#### Generating image files
9595

9696
```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()
100123

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),
103134
{width, height} = canvas;
104135

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)
111142
ctx.fill()
112143
}
113144

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
128145
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")
135151
}
136152
render()
137153

138-
// ...or save the file synchronously from the main thread
139-
canvas.saveAsSync("pilcrow.png")
140154
```
141155

142156
#### Rendering to a window

0 commit comments

Comments
 (0)