forked from thx/resvg-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const fs = require('fs').promises | ||
const { join } = require('path') | ||
const { performance } = require('perf_hooks') | ||
|
||
const { render } = require('../index') | ||
const sharp = require('sharp') | ||
const { createCanvas, Image } = require('@napi-rs/canvas') | ||
|
||
async function main() { | ||
const svg = await fs.readFile(join(__dirname, './anime_girl.svg')) | ||
const svgString = svg.toString('utf-8') | ||
const t0 = performance.now() | ||
const pngData = render(svgString, { | ||
fitTo: { | ||
mode: 'width', | ||
value: 2104, | ||
}, | ||
font: { | ||
loadSystemFonts: false, // It will be faster to disable loading system fonts. | ||
}, | ||
logLevel: 'off', | ||
}) | ||
const t1 = performance.now() | ||
console.log('✨ resvg-js done in', t1 - t0, 'ms') | ||
await fs.writeFile(join(__dirname, './out-resvg-js.png'), pngData) | ||
|
||
sharpToPng('example/anime_girl.svg', 2104) | ||
skrCanvas(svg, 2104, 1488) | ||
} | ||
|
||
async function sharpToPng(file, width) { | ||
const t0 = performance.now() | ||
await sharp(file, { | ||
density: 100, | ||
}) | ||
.resize(width) | ||
// .flatten({ background: '#fff' }) | ||
.toFile('example/out-sharp.png') | ||
const t1 = performance.now() | ||
console.log('✨ sharp done in', t1 - t0, 'ms') | ||
} | ||
|
||
async function skrCanvas(file, width, height) { | ||
const t0 = performance.now() | ||
|
||
const image = new Image() | ||
image.src = file | ||
|
||
const w = width | ||
const h = height | ||
|
||
// resize SVG | ||
image.width = w | ||
image.height = h | ||
|
||
// create a canvas of the same size as the image | ||
const canvas = createCanvas(w, h) | ||
const ctx = canvas.getContext('2d') | ||
|
||
// fill the canvas with the image | ||
ctx.drawImage(image, 0, 0) | ||
var pngData = await canvas.encode('png') | ||
const t1 = performance.now() | ||
|
||
console.log('✨ skr-canvas done in', t1 - t0, 'ms') | ||
|
||
await fs.writeFile(join(__dirname, './out-skr-canvas.png'), pngData) | ||
} | ||
|
||
main() |
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 |
---|---|---|
@@ -1,77 +1,33 @@ | ||
const fs = require('fs').promises | ||
const { promises } = require('fs') | ||
const { join } = require('path') | ||
const { performance } = require('perf_hooks') | ||
|
||
const { render } = require('../index') | ||
const sharp = require('sharp') | ||
const { createCanvas, Image } = require('@napi-rs/canvas') | ||
|
||
async function main() { | ||
// const svg = await fs.readFile(join(__dirname, './text.svg')) | ||
const svg = await fs.readFile(join(__dirname, './anime_girl.svg')) | ||
const svg = await promises.readFile(join(__dirname, './text.svg')) | ||
const svgString = svg.toString('utf-8') | ||
const t0 = performance.now() | ||
const pngData = render(svgString, { | ||
// background: 'rgba(238, 235, 230, .9)', | ||
background: 'rgba(238, 235, 230, .9)', | ||
fitTo: { | ||
mode: 'zoom', | ||
value: 2, | ||
mode: 'width', | ||
value: 1200, | ||
}, | ||
font: { | ||
loadSystemFonts: true, // It will be faster to disable loading system fonts. | ||
fontFiles: [ | ||
'./example/SourceHanSerifCN-Light-subset.ttf', | ||
], // Load custom fonts. | ||
fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'], // Load custom fonts. | ||
loadSystemFonts: false, // It will be faster to disable loading system fonts. | ||
defaultFontFamily: 'Source Han Serif CN Light', | ||
}, | ||
// imageRendering: 1, | ||
// shapeRendering: 2, | ||
logLevel: 'off', | ||
logLevel: 'debug', | ||
}) | ||
const t1 = performance.now() | ||
console.log('✨ resvg-js done in', t1 - t0, 'ms') | ||
await fs.writeFile(join(__dirname, './out-resvg-js.png'), pngData) | ||
|
||
sharpToPng('example/anime_girl.svg', 1052) | ||
skrCanvas(svg, 1052, 744) | ||
} | ||
|
||
async function sharpToPng(file, width) { | ||
const t0 = performance.now() | ||
await sharp(file, { | ||
density: 100, | ||
}) | ||
.resize(width) | ||
// .flatten({ background: '#fff' }) | ||
.toFile('example/out-sharp.png') | ||
const t1 = performance.now() | ||
console.log('✨ sharp done in', t1 - t0, 'ms') | ||
} | ||
|
||
async function skrCanvas(file, width, height) { | ||
const t0 = performance.now() | ||
|
||
const image = new Image() | ||
image.src = file | ||
|
||
const w = width | ||
const h = height | ||
|
||
// resize SVG | ||
image.width = w | ||
image.height = h | ||
|
||
// create a canvas of the same size as the image | ||
const canvas = createCanvas(w, h) | ||
const ctx = canvas.getContext('2d') | ||
|
||
// fill the canvas with the image | ||
ctx.drawImage(image, 0, 0) | ||
var pngData = await canvas.encode('png') | ||
const t1 = performance.now() | ||
|
||
console.log('✨ skr-canvas done in', t1 - t0, 'ms') | ||
// eslint-disable-next-line no-console | ||
console.log('✨ Done in', t1 - t0, 'ms') | ||
|
||
await fs.writeFile(join(__dirname, './out-skr-canvas.png'), pngData) | ||
await promises.writeFile(join(__dirname, './text-out.png'), pngData) | ||
} | ||
|
||
main() | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.