Skip to content

Commit 1a20589

Browse files
committed
refactor: prepare pixel plugin to keep aspect ratio
1 parent f501170 commit 1a20589

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/sqip-plugin-pixels/src/sqip-plugin-pixels.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,24 @@ export default class PixelsPlugin extends SqipPlugin {
7676
.raw()
7777
.toBuffer({ resolveWithObject: true })
7878

79+
const pixelHeight = pixelSize // Math.floor(pixelSize * (info.height / info.width))
80+
7981
let column = 0
8082
let row = 0
8183

82-
const canvas = SVG().size(info.width * pixelSize, info.height * pixelSize)
84+
const newWidth = info.width * pixelSize
85+
const newHeight = info.height * pixelHeight
86+
87+
const canvas = SVG().size(newWidth, newHeight)
8388

8489
for (let i = 0; i < data.length; i += info.channels) {
8590
const red = data[i]
8691
const green = data[i + 1]
8792
const blue = data[i + 2]
8893
canvas
89-
.rect(1 * pixelSize, 1 * pixelSize)
94+
.rect(1 * pixelSize, 1 * pixelHeight)
9095
.attr({ fill: `rgb(${red},${green},${blue})` })
91-
.move(column * pixelSize, row * pixelSize)
96+
.move(column * pixelSize, row * pixelHeight)
9297
column++
9398
if (column >= info.width) {
9499
column = 0
@@ -98,6 +103,8 @@ export default class PixelsPlugin extends SqipPlugin {
98103

99104
metadata.type = 'svg'
100105
metadata.mimeType = 'image/svg'
106+
metadata.height = newHeight
107+
metadata.width = newWidth
101108

102109
return Buffer.from(canvas.svg())
103110
}

0 commit comments

Comments
 (0)