Skip to content

Commit

Permalink
test: add drawImage snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind committed Jan 18, 2021
1 parent 41c1906 commit faca57b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
16 changes: 13 additions & 3 deletions __test__/draw.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { promises } from 'fs'
import { join } from 'path'
import ava, { TestInterface } from 'ava'

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

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

test.beforeEach((t) => {
Expand Down Expand Up @@ -230,7 +232,15 @@ test('createRadialGradient', async (t) => {
await snapshotImage(t)
})

test.todo('drawImage')
test('drawImage', async (t) => {
const { ctx } = t.context
const filePath = './snapshots/drawImage.png'
const file = await promises.readFile(join(__dirname, filePath))
const image = new Image()
image.src = file
ctx.drawImage(image, 0, 0)
await snapshotImage(t)
})

test('ellipse', async (t) => {
const { ctx } = t.context
Expand Down
Binary file added __test__/snapshots/drawImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 23 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export type Canvas = HTMLCanvasElement & {
png(): Promise<Buffer>
}

export function createCanvas(width: number, height: number): Canvas

export interface DOMMatrix2DInit {
a: number
b: number
Expand Down Expand Up @@ -64,3 +58,26 @@ export class Path2D {
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void
rect(x: number, y: number, w: number, h: number): void
}

export interface SKRSContext2D extends Omit<CanvasRenderingContext2D, 'drawImage'> {
drawImage(image: Image, dx: number, dy: number): void
drawImage(image: Image, dx: number, dy: number, dw: number, dh: number): void
drawImage(
image: Image,
sx: number,
sy: number,
sw: number,
sh: number,
dx: number,
dy: number,
dw: number,
dh: number,
): void
}

export interface Canvas extends Omit<HTMLCanvasElement, 'getContext'> {
getContext(contextId: '2d'): SKRSContext2D
png(): Promise<Buffer>
}

export function createCanvas(width: number, height: number): Canvas

0 comments on commit faca57b

Please sign in to comment.