Skip to content

Commit

Permalink
fix: svg image transparent background should be preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jun 2, 2022
1 parent 26fdc3c commit 531bde8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
31 changes: 28 additions & 3 deletions __test__/image.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { promises } from 'fs'
import { promises as fs } from 'fs'
import { join } from 'path'
import test from 'ava'

import { Image } from '../index'
import { createCanvas, Image } from '../index'

import { snapshotImage } from './image-snapshot'

async function loadImageFile() {
return await promises.readFile(join(__dirname, '../example/simple.png'))
return await fs.readFile(join(__dirname, '../example/simple.png'))
}

test('should be able to create Image', (t) => {
Expand Down Expand Up @@ -63,3 +65,26 @@ test('properties should be readonly', (t) => {
// @ts-expect-error
t.throws(() => (image.complete = true), expectation)
})

test('svg-transparent-background', async (t) => {
const image = new Image()
image.src = await fs.readFile(join(__dirname, '..', 'example', 'resize-svg.svg'))

const w = 1000
const h = 1000

// resize SVG
image.width = w / 2
image.height = h / 2

// 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.fillStyle = 'pink'
ctx.fillRect(0, 0, w, h)
ctx.drawImage(image, 250, 250)

await snapshotImage(t, { canvas })
})
Binary file modified __test__/snapshots/filter-contrast-ff.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 added __test__/snapshots/svg-transparent-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ extern "C"
image_w = width;
image_h = height;
}
auto imageinfo = SkImageInfo::Make(image_w, image_h, kRGBA_8888_SkColorType, SkAlphaType::kOpaque_SkAlphaType, color_space);
auto imageinfo = SkImageInfo::Make(image_w, image_h, kRGBA_8888_SkColorType, SkAlphaType::kPremul_SkAlphaType, color_space);
auto bitmap = new SkBitmap();
bitmap->allocPixels(imageinfo);
auto sk_svg_canvas = new SkCanvas(*bitmap);
Expand Down

0 comments on commit 531bde8

Please sign in to comment.