Skip to content

Commit

Permalink
feat: support getContextAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind committed Jan 28, 2021
1 parent 17a8d01 commit 4c0586b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions __test__/draw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ test('fillRect', async (t) => {

test.todo('fillText')

test.todo('getContextAttributes')
test('getContextAttributes', (t) => {
const { ctx } = t.context
const attributes = ctx.getContextAttributes()
t.is(attributes.alpha, true)
t.is(attributes.desynchronized, false)
})

test('getImageData', async (t) => {
const { ctx } = t.context
Expand Down Expand Up @@ -314,7 +319,7 @@ test('isPointInPath', (t) => {
t.is(ctx.isPointInPath(path, 50, 1), true)

path.rect(40, 40, 20, 20)
t.is(ctx.isPointInPath(50, 50), true)
t.is(ctx.isPointInPath(path, 50, 50), true)
t.is(ctx.isPointInPath(path, 50, 50, 'nonzero'), true)
t.is(ctx.isPointInPath(path, 50, 50, 'evenodd'), false)
})
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface SKRSContext2D extends Omit<CanvasRenderingContext2D, 'drawImage
dw: number,
dh: number,
): void
getContextAttributes(): { alpha: boolean; desynchronized: boolean }
}

export interface Canvas extends Omit<HTMLCanvasElement, 'getContext'> {
Expand Down
9 changes: 9 additions & 0 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Context {
Property::new(&env, "createLinearGradient")?.with_method(create_linear_gradient),
Property::new(&env, "createRadialGradient")?.with_method(create_radial_gradient),
Property::new(&env, "drawImage")?.with_method(draw_image),
Property::new(&env, "getContextAttributes")?.with_method(get_context_attributes),
Property::new(&env, "isPointInPath")?.with_method(is_point_in_path),
Property::new(&env, "isPointInStroke")?.with_method(is_point_in_stroke),
Property::new(&env, "ellipse")?.with_method(ellipse),
Expand Down Expand Up @@ -685,6 +686,14 @@ fn draw_image(ctx: CallContext) -> Result<JsUndefined> {
ctx.env.get_undefined()
}

#[js_function]
fn get_context_attributes(ctx: CallContext) -> Result<JsObject> {
let mut obj = ctx.env.create_object()?;
obj.set_named_property("alpha", ctx.env.get_boolean(true)?)?;
obj.set_named_property("desynchronized", ctx.env.get_boolean(false)?)?;
Ok(obj)
}

#[js_function(4)]
fn is_point_in_path(ctx: CallContext) -> Result<JsBoolean> {
let this = ctx.this_unchecked::<JsObject>();
Expand Down

0 comments on commit 4c0586b

Please sign in to comment.