Skip to content

Commit

Permalink
feat: properties in CanvasRenderContext2D
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 31, 2020
1 parent 55ae7c3 commit 12727a7
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 29 deletions.
97 changes: 94 additions & 3 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import test from 'ava'
import ava, { TestInterface } from 'ava'

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

test('should be able to createCanvas', (t) => {
t.notThrows(() => createCanvas(1920, 1080))
const test = ava as TestInterface<{
canvas: HTMLCanvasElement
ctx: CanvasRenderingContext2D
}>

test.beforeEach((t) => {
const canvas = createCanvas(1024, 768)
t.context.canvas = canvas
t.context.ctx = canvas.getContext('2d')!
})

test('should be able to create Path2D', (t) => {
Expand All @@ -16,3 +23,87 @@ test('should be able to create Path2D', (t) => {
)
t.notThrows(() => new Path2D(new Path2D()))
})

test('miterLimit state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.miterLimit, 10)
ctx.miterLimit = 20
t.is(ctx.miterLimit, 20)
})

test('globalAlpha state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.globalAlpha, 1)
ctx.globalAlpha = 0.2
t.is(ctx.globalAlpha, 0.2)
})

test('globalCompositeOperation state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.globalCompositeOperation, 'source-over')
ctx.globalCompositeOperation = 'xor'
t.is(ctx.globalCompositeOperation, 'xor')
})

test('lineCap state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineCap, 'butt')
ctx.lineCap = 'round'
t.is(ctx.lineCap, 'round')
})

test('lineDashOffset state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineDashOffset, 0)
ctx.lineDashOffset = 10
t.is(ctx.lineDashOffset, 10)
})

test('lineWidth state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineWidth, 1)
ctx.lineWidth = 10
t.is(ctx.lineWidth, 10)
})

test('fillStyle state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.fillStyle, '#000')
ctx.fillStyle = 'hotpink'
t.is(ctx.fillStyle, 'hotpink')
})

test('strokeStyle state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.strokeStyle, '#000')
ctx.strokeStyle = 'hotpink'
t.is(ctx.strokeStyle, 'hotpink')
})

test('shadowBlur state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowBlur, 0)
ctx.shadowBlur = 10
t.is(ctx.shadowBlur, 10)
})

test('shadowColor state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowColor, '#000000')
ctx.shadowColor = 'hotpink'
t.is(ctx.shadowColor, 'hotpink')
})

test('shadowOffsetX state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowOffsetX, 0)
ctx.shadowOffsetX = 10
t.is(ctx.shadowOffsetX, 10)
})

test('shadowOffsetY state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowOffsetY, 0)
ctx.shadowOffsetY = 10
t.is(ctx.shadowOffsetY, 10)
})
10 changes: 10 additions & 0 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,21 @@ extern "C"
PAINT_CAST->setStrokeCap((SkPaint::Cap)cap);
}

int skiac_paint_get_stroke_cap(skiac_paint *c_paint)
{
return PAINT_CAST->getStrokeCap();
}

void skiac_paint_set_stroke_join(skiac_paint *c_paint, int join)
{
PAINT_CAST->setStrokeJoin((SkPaint::Join)join);
}

int skiac_paint_get_stroke_join(skiac_paint *c_paint)
{
return PAINT_CAST->getStrokeJoin();
}

void skiac_paint_set_stroke_miter(skiac_paint *c_paint, float miter)
{
PAINT_CAST->setStrokeMiter(miter);
Expand Down
2 changes: 2 additions & 0 deletions skia-c/skia_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ extern "C"
void skiac_paint_set_stroke_width(skiac_paint *c_paint, float width);
float skiac_paint_get_stroke_width(skiac_paint *c_paint);
void skiac_paint_set_stroke_cap(skiac_paint *c_paint, int cap);
int skiac_paint_get_stroke_cap(skiac_paint *c_paint);
void skiac_paint_set_stroke_join(skiac_paint *c_paint, int join);
int skiac_paint_get_stroke_join(skiac_paint *c_paint);
void skiac_paint_set_stroke_miter(skiac_paint *c_paint, float miter);
float skiac_paint_get_stroke_miter(skiac_paint *c_paint);
void skiac_paint_set_path_effect(skiac_paint *c_paint, skiac_path_effect *c_path_effect);
Expand Down
Loading

1 comment on commit 12727a7

@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: 12727a7 Previous: 55ae7c3 Ratio
Draw house#@napi-rs/skia 22.5 ops/sec (±0.1%) 27 ops/sec (±0.04%) 1.20
Draw house#node-canvas 21.7 ops/sec (±0.35%) 26 ops/sec (±0.43%) 1.20
Draw gradient#@napi-rs/skia 21.5 ops/sec (±0.3%) 26 ops/sec (±0.09%) 1.21
Draw gradient#node-canvas 20.7 ops/sec (±0.56%) 25 ops/sec (±0.43%) 1.21

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

Please sign in to comment.