generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support gradient in fill/stroke style
- Loading branch information
1 parent
99ef1cd
commit 12e061c
Showing
7 changed files
with
135 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import b from 'benny' | ||
|
||
import { createCanvas, Canvas } from 'canvas' | ||
|
||
import { createCanvas as skiaCreateCanvas } from '../index' | ||
|
||
function drawGradient(factory: (width: number, height: number) => Canvas) { | ||
const canvas = factory(1024, 768) | ||
|
||
const ctx = canvas.getContext('2d')! | ||
|
||
const gradient = ctx.createLinearGradient(20, 0, 220, 0) | ||
|
||
// Add three color stops | ||
gradient.addColorStop(0, 'green') | ||
gradient.addColorStop(0.5, 'cyan') | ||
gradient.addColorStop(1, 'green') | ||
|
||
// Set the fill style and draw a rectangle | ||
ctx.fillStyle = gradient | ||
ctx.fillRect(20, 20, 200, 100) | ||
|
||
canvas.toBuffer('image/png') | ||
} | ||
|
||
export function gradient() { | ||
return b.suite( | ||
'Draw gradient', | ||
|
||
b.add('@napi-rs/skia', () => { | ||
// @ts-expect-error | ||
drawGradient(skiaCreateCanvas) | ||
}), | ||
|
||
b.add('node-canvas', () => { | ||
drawGradient(createCanvas) | ||
}), | ||
|
||
b.cycle(), | ||
b.complete(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import b from 'benny' | ||
|
||
import { createCanvas, Canvas } from 'canvas' | ||
|
||
import { createCanvas as skiaCreateCanvas } from '../index' | ||
|
||
function drawHouse(factory: (width: number, height: number) => Canvas) { | ||
const canvas = factory(1024, 768) | ||
|
||
const ctx = canvas.getContext('2d')! | ||
|
||
ctx.lineWidth = 10 | ||
ctx.strokeStyle = '#03a9f4' | ||
ctx.fillStyle = '#03a9f4' | ||
|
||
// Wall | ||
ctx.strokeRect(75, 140, 150, 110) | ||
|
||
// Door | ||
ctx.fillRect(130, 190, 40, 60) | ||
|
||
// Roof | ||
ctx.beginPath() | ||
ctx.moveTo(50, 140) | ||
ctx.lineTo(150, 60) | ||
ctx.lineTo(250, 140) | ||
ctx.closePath() | ||
ctx.stroke() | ||
|
||
canvas.toBuffer('image/png') | ||
} | ||
|
||
export function house() { | ||
return b.suite( | ||
'Draw house', | ||
|
||
b.add('@napi-rs/skia', () => { | ||
// @ts-expect-error | ||
drawHouse(skiaCreateCanvas) | ||
}), | ||
|
||
b.add('node-canvas', () => { | ||
drawHouse(createCanvas) | ||
}), | ||
|
||
b.cycle(), | ||
b.complete(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { promises } = require('fs') | ||
const { join } = require('path') | ||
|
||
const { createCanvas } = require('../index') | ||
|
||
const canvas = createCanvas(1024, 768) | ||
|
||
const ctx = canvas.getContext('2d') | ||
|
||
const gradient = ctx.createLinearGradient(20, 0, 220, 0) | ||
|
||
// Add three color stops | ||
gradient.addColorStop(0, 'green') | ||
gradient.addColorStop(0.5, 'cyan') | ||
gradient.addColorStop(1, 'green') | ||
|
||
// Set the fill style and draw a rectangle | ||
ctx.fillStyle = gradient | ||
ctx.fillRect(20, 20, 200, 100) | ||
|
||
canvas | ||
.png() | ||
.then((data) => promises.writeFile(join(__dirname, 'gradient.png'), data)) | ||
.catch((e) => { | ||
console.error(e) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12e061c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
Draw house#@napi-rs/skia
21
ops/sec (±0.47%
)21
ops/sec (±0.81%
)1
Draw house#node-canvas
20
ops/sec (±0.41%
)19
ops/sec (±1.73%
)0.95
Draw gradient#@napi-rs/skia
20
ops/sec (±1.45%
)Draw gradient#node-canvas
19
ops/sec (±0.9%
)This comment was automatically generated by workflow using github-action-benchmark.