diff --git a/__test__/draw.spec.ts b/__test__/draw.spec.ts index 0eab7ab8..5b1749bc 100644 --- a/__test__/draw.spec.ts +++ b/__test__/draw.spec.ts @@ -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' diff --git a/__test__/snapshots/lineTo-with-invalid-point.png b/__test__/snapshots/lineTo-with-invalid-point.png new file mode 100644 index 00000000..7de175b6 Binary files /dev/null and b/__test__/snapshots/lineTo-with-invalid-point.png differ diff --git a/src/ctx.rs b/src/ctx.rs index 08e8b56f..a4bcbb2c 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -1326,10 +1326,16 @@ fn line_to(ctx: CallContext) -> Result { let this = ctx.this_unchecked::(); let context_2d = ctx.env.unwrap::(&this)?; - let x = ctx.get::(0)?.get_double()? as f32; - let y = ctx.get::(1)?.get_double()? as f32; - - context_2d.path.line_to(x, y); + if let Ok((x, y)) = ctx.get::(0)?.get_double().and_then(|x| { + ctx + .get::(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() }