Skip to content

Commit

Permalink
feat: add example/compare.js
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Oct 24, 2021
1 parent 6c7e89a commit f3ea4b8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 57 deletions.
70 changes: 70 additions & 0 deletions example/compare.js
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()
70 changes: 13 additions & 57 deletions example/index.js
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()
Binary file modified example/out-sharp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/out-skr-canvas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f3ea4b8

Please sign in to comment.