Skip to content

Commit

Permalink
Added a couple of examples of the loadImage function. (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrc03c committed Jan 21, 2024
1 parent 453fe67 commit a46682a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ You can simply attach a lambda layer by getting an ARN from [Canvas-Lambda-Layer
```js
const { promises } = require('fs')
const { join } = require('path')
const { createCanvas } = require('@napi-rs/canvas')
const { createCanvas, loadImage } = require('@napi-rs/canvas')

const canvas = createCanvas(300, 320)
const ctx = canvas.getContext('2d')
Expand All @@ -88,6 +88,21 @@ ctx.closePath()
ctx.stroke()

async function main() {
// load images from disk or from a URL
const catImage = await loadImage("path/to/cat.png")
const dogImage = await loadImage("https://example.com/path/to/dog.jpg")

ctx.drawImage(catImage, 0, 0, catImage.width, catImage.height)

ctx.drawImage(
dogImage,
canvas.width / 2,
canvas.height / 2,
dogImage.width,
dogImage.height
)

// export canvas as image
const pngData = await canvas.encode('png') // JPEG, AVIF and WebP are also supported
// encoding in libuv thread pool, non-blocking
await promises.writeFile(join(__dirname, 'simple.png'), pngData)
Expand Down

0 comments on commit a46682a

Please sign in to comment.