Skip to content

Commit

Permalink
feat: implement image pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Mar 11, 2021
1 parent bf284d5 commit 2efbb18
Show file tree
Hide file tree
Showing 24 changed files with 1,464 additions and 52 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
depot_tools
skia
skia-c
index.d.ts
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
Binary file added __test__/canvas_createpattern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 38 additions & 2 deletions __test__/draw.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { promises } from 'fs'
import { join } from 'path'
import ava, { TestInterface } from 'ava'
import PNG from '@jimp/png'

import { createCanvas, Canvas, Image, Path2D, SKRSContext2D } from '../index'
import { createCanvas, Canvas, Image, ImageData, Path2D, SKRSContext2D, DOMMatrix } from '../index'
import { snapshotImage } from './image-snapshot'

const test = ava as TestInterface<{
canvas: Canvas
ctx: SKRSContext2D
}>

const png = PNG()

test.beforeEach((t) => {
const canvas = createCanvas(512, 512)
t.context.canvas = canvas
Expand Down Expand Up @@ -221,7 +224,40 @@ test('createLinearGradient', async (t) => {
await snapshotImage(t)
})

test.todo('createPattern')
test('createPattern-no-transform', async (t) => {
const { ctx } = t.context
const imageSrc = await promises.readFile(join(__dirname, 'canvas_createpattern.png'))
const image = new Image()
image.src = imageSrc
const pattern = ctx.createPattern(image, 'repeat')
ctx.fillStyle = pattern
ctx.fillRect(0, 0, 300, 300)
await snapshotImage(t)
})

test('createPattern-no-transform-imagedata', async (t) => {
const { ctx } = t.context
const imageSrc = await promises.readFile(join(__dirname, 'canvas_createpattern.png'))
const imageMeta = png.decoders['image/png'](imageSrc)
const imageData = new ImageData(new Uint8ClampedArray(imageMeta.data), imageMeta.width, imageMeta.height)
const pattern = ctx.createPattern(imageData, 'repeat')
ctx.fillStyle = pattern
ctx.fillRect(0, 0, 300, 300)
await snapshotImage(t)
})

test('createPattern-with-transform', async (t) => {
const { ctx } = t.context
const imageSrc = await promises.readFile(join(__dirname, 'canvas_createpattern.png'))
const image = new Image()
image.src = imageSrc
const pattern = ctx.createPattern(image, 'repeat')
const matrix = new DOMMatrix()
pattern.setTransform(matrix.rotate(-45).scale(1.5))
ctx.fillStyle = pattern
ctx.fillRect(0, 0, 300, 300)
await snapshotImage(t)
})

test('createRadialGradient', async (t) => {
const { ctx } = t.context
Expand Down
2 changes: 1 addition & 1 deletion __test__/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('should be able to create Image', (t) => {
t.notThrows(() => new Image())
})

test('shoule be able to set src with buffer', async (t) => {
test('should be able to set src with buffer', async (t) => {
const file = await loadImageFile()
t.notThrows(() => {
const image = new Image()
Expand Down
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/createPattern-no-transform.png
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.
Loading

1 comment on commit 2efbb18

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2efbb18 Previous: bf284d5 Ratio
Draw house#@napi-rs/skia 23 ops/sec (±1.29%) 24.2 ops/sec (±1.28%) 1.05
Draw house#node-canvas 18 ops/sec (±0.38%) 24.3 ops/sec (±0.98%) 1.35
Draw gradient#@napi-rs/skia 22 ops/sec (±0.22%) 23.4 ops/sec (±1.32%) 1.06
Draw gradient#node-canvas 17 ops/sec (±0.35%) 22.9 ops/sec (±1.12%) 1.35

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.