Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b15e01

Browse files
committed
feat: improve rendered svg
1 parent 44d2b68 commit 1b15e01

File tree

6 files changed

+18
-8
lines changed

6 files changed

+18
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ lib-cov
99
logs
1010
node_modules
1111
temp
12+
play/out.svg

play/run.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-console */
2-
import { renderANSI, renderUnicode, renderUnicodeCompact } from '../src'
2+
import fs from 'node:fs'
3+
import { renderANSI, renderSVG, renderUnicode, renderUnicodeCompact } from '../src'
34

45
const text = 'qrcode'
56

@@ -11,3 +12,5 @@ console.log()
1112

1213
console.log(renderUnicodeCompact(text))
1314
console.log()
15+
16+
fs.writeFileSync('./play/out.svg', renderSVG(text), 'utf8')

src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function addBorder(input: QrCodeGenerateResult, border = 1): QrCodeGenerateResul
4949
if (!border)
5050
return input
5151

52-
const { size, data } = input
52+
const { size } = input
5353
const newSize = size + border * 2
5454

5555
input.size = newSize

src/svg.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,29 @@ export function renderSVG(
1010
) {
1111
const result = encode(data, options)
1212
const {
13-
pixelSize = 5,
13+
pixelSize = 10,
1414
whiteColor = 'white',
1515
blackColor = 'black',
1616
} = options
1717
const height = result.size * pixelSize
1818
const width = result.size * pixelSize
1919

20-
let svg = `<svg xmlns="http://www.w3.org/2000/svg" height="${height}" width="${width}">`
20+
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}">`
21+
22+
const pathes: string[] = []
2123

2224
for (let row = 0; row < result.size; row++) {
2325
for (let col = 0; col < result.size; col++) {
2426
const x = col * pixelSize
2527
const y = row * pixelSize
26-
const fillColor = result.data[row][col] ? blackColor : whiteColor
27-
svg += `<rect x="${x}" y="${y}" width="${pixelSize}" height="${pixelSize}" fill="${fillColor}"/>`
28+
if (result.data[row][col])
29+
pathes.push(`M${x},${y}h${pixelSize}v${pixelSize}h-${pixelSize}z`)
2830
}
2931
}
32+
33+
svg += `<rect fill="${whiteColor}" width="${width}" height="${height}"/>`
34+
svg += `<path fill="${blackColor}" d="${pathes.join('')}"/>`
35+
3036
svg += '</svg>'
3137
return svg
3238
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface QrCodeGenerateSvgOptions extends QrCodeGenerateOptions {
101101
/**
102102
* Size of each pixel
103103
*
104-
* @default 5
104+
* @default 20
105105
*/
106106
pixelSize?: number
107107

test/output/out1.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)