Skip to content

Commit

Permalink
fix: Creating canvas with width or height of zero or less (#739)
Browse files Browse the repository at this point in the history
* fix: Creating canvas with width or height of zero or less

* test: createCanvas default sizes for non-positive values

* fix

---------

Co-authored-by: LongYinan <[email protected]>
  • Loading branch information
kaloyanBozhkov and Brooooooklyn authored Oct 14, 2024
1 parent 52dedc9 commit 3a26c7d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 17 additions & 0 deletions __test__/canvas-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,22 @@ test('ctx.canvas should be equal to canvas', (t) => {
test('[SVG] ctx.canvas should be equal to canvas', (t) => {
const canvas = createCanvas(100, 100, SvgExportFlag.NoPrettyXML)
const ctx = canvas.getContext('2d')
// @ts-expect-error
t.is(ctx.canvas, canvas)
})

test('Canvas size should equal 350x150 when provided non-positive values', (t) => {
let canvas = createCanvas(0, 0)
t.is(canvas.width, 350)
t.is(canvas.height, 150)
canvas = createCanvas(-1, 10)
t.is(canvas.width, 350)
t.is(canvas.height, 10)
canvas = createCanvas(10, -10)
t.is(canvas.height, 150)
t.is(canvas.width, 10)

const svgCanvas = createCanvas(10, -10, SvgExportFlag.ConvertTextToPaths)
t.is(svgCanvas.height, 150)
t.is(svgCanvas.width, 10)
})
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ impl<'scope> CanvasElement<'scope> {
}

#[napi(constructor)]
pub fn new(env: &Env, mut this: This, width: u32, height: u32) -> Result<Self> {
pub fn new(env: &Env, mut this: This, width: i32, height: i32) -> Result<Self> {
// Default fallback of canvas on browser and skia-canvas is 350x150
let width = (if width <= 0 { 350 } else { width }) as u32;
let height = (if height <= 0 { 150 } else { height }) as u32;
let ctx = Self::create_context(env, width, height)?;
let ctx = ctx.assign_to_this_with_attributes("ctx", PropertyAttributes::Default, &mut this)?;
let mut ctx_obj = ctx.as_object(env);
Expand All @@ -116,7 +119,8 @@ impl<'scope> CanvasElement<'scope> {
}

#[napi(setter)]
pub fn set_width(&mut self, env: Env, width: u32) -> Result<()> {
pub fn set_width(&mut self, env: Env, width: i32) -> Result<()> {
let width = (if width <= 0 { 350 } else { width }) as u32;
self.width = width;
let height = self.height;
let old_ctx = mem::replace(
Expand All @@ -133,7 +137,8 @@ impl<'scope> CanvasElement<'scope> {
}

#[napi(setter)]
pub fn set_height(&mut self, env: Env, height: u32) -> Result<()> {
pub fn set_height(&mut self, env: Env, height: i32) -> Result<()> {
let height = (if height <= 0 { 150 } else { height }) as u32;
self.height = height;
let width = self.width;
let old_ctx = mem::replace(
Expand Down Expand Up @@ -444,10 +449,13 @@ impl<'scope> SVGCanvas<'scope> {
pub fn new(
env: &Env,
mut this: This,
width: u32,
height: u32,
width: i32,
height: i32,
flag: SvgExportFlag,
) -> Result<SVGCanvas<'scope>> {
// Default fallback of canvas on browser and skia-canvas is 350x150
let width = (if width <= 0 { 350 } else { width }) as u32;
let height = (if height <= 0 { 150 } else { height }) as u32;
let ctx = CanvasRenderingContext2D::into_instance(
CanvasRenderingContext2D {
context: Context::new_svg(width, height, flag.into(), ColorSpace::default())?,
Expand Down

0 comments on commit 3a26c7d

Please sign in to comment.