Skip to content

Commit

Permalink
fix: do nothing if lineTo parameters invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jun 2, 2022
1 parent 39f3fb1 commit 0ddeb7c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
11 changes: 11 additions & 0 deletions __test__/draw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,17 @@ test('lineTo', async (t) => {
await snapshotImage(t)
})

test('lineTo-with-invalid-point', async (t) => {
const { ctx } = t.context
ctx.beginPath() // Start a new path
ctx.lineTo(NaN, 100)
ctx.lineTo(50, 50)
ctx.lineTo(100, NaN)
ctx.lineTo(250, 100)
ctx.stroke()
await snapshotImage(t)
})

test('measureText', (t) => {
const { ctx } = t.context
ctx.font = '50px Iosevka Slab'
Expand Down
Binary file added __test__/snapshots/lineTo-with-invalid-point.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,10 +1326,16 @@ fn line_to(ctx: CallContext) -> Result<JsUndefined> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

let x = ctx.get::<JsNumber>(0)?.get_double()? as f32;
let y = ctx.get::<JsNumber>(1)?.get_double()? as f32;

context_2d.path.line_to(x, y);
if let Ok((x, y)) = ctx.get::<JsNumber>(0)?.get_double().and_then(|x| {
ctx
.get::<JsNumber>(1)?
.get_double()
.map(|y| (x as f32, y as f32))
}) {
if !x.is_nan() && !y.is_nan() {
context_2d.path.line_to(x, y);
}
}

ctx.env.get_undefined()
}
Expand Down

1 comment on commit 0ddeb7c

@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: 0ddeb7c Previous: 39f3fb1 Ratio
Draw house#skia-canvas 23 ops/sec (±0.4%) 18.9 ops/sec (±0.43%) 0.82
Draw house#node-canvas 26 ops/sec (±1.7%) 19.2 ops/sec (±0.59%) 0.74
Draw house#@napi-rs/skia 29 ops/sec (±1.06%) 21.3 ops/sec (±0.2%) 0.73
Draw gradient#skia-canvas 22 ops/sec (±1.13%) 18 ops/sec (±0.41%) 0.82
Draw gradient#node-canvas 25 ops/sec (±0.84%) 19 ops/sec (±0.29%) 0.76
Draw gradient#@napi-rs/skia 28 ops/sec (±0.2%) 21 ops/sec (±0.24%) 0.75

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

Please sign in to comment.