From b0a351a8a8c1693ca2edcf009e77ec56df616fb5 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 1 Jan 2023 22:00:07 +0800 Subject: [PATCH] fix: missing canvas property on Context2D (#597) --- __test__/canvas-class.spec.ts | 6 ++++++ index.d.ts | 3 ++- src/lib.rs | 19 +++++++++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/__test__/canvas-class.spec.ts b/__test__/canvas-class.spec.ts index 3cceb6a9..0743f4f2 100644 --- a/__test__/canvas-class.spec.ts +++ b/__test__/canvas-class.spec.ts @@ -5,3 +5,9 @@ import { createCanvas, Canvas } from '../index' test('Canvas constructor should be equal to createCanvas', (t) => { t.true(new Canvas(100, 100) instanceof createCanvas(100, 100).constructor) }) + +test('ctx.canvas should be equal to canvas', (t) => { + const canvas = createCanvas(100, 100) + const ctx = canvas.getContext('2d') + t.is(ctx.canvas, canvas) +}) diff --git a/index.d.ts b/index.d.ts index 536b86db..248668a7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -248,8 +248,9 @@ export interface StrokeOptions { export interface SKRSContext2D extends Omit< CanvasRenderingContext2D, - 'drawImage' | 'createPattern' | 'getTransform' | 'drawFocusIfNeeded' | 'scrollPathIntoView' + 'drawImage' | 'createPattern' | 'getTransform' | 'drawFocusIfNeeded' | 'scrollPathIntoView' | 'canvas' > { + canvas: Canvas /** * @param startAngle The angle at which to begin the gradient, in radians. Angle measurements start vertically above the centre and move around clockwise. * @param x The x-axis coordinate of the centre of the gradient. diff --git a/src/lib.rs b/src/lib.rs index 686e381f..baab19d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,12 +84,10 @@ impl CanvasElement { width: u32, height: u32, ) -> Result> { - let ctx = CanvasRenderingContext2D::into_instance( - CanvasRenderingContext2D { - context: Context::new(width, height, ColorSpace::default())?, - }, - env, - )?; + let ctx = CanvasRenderingContext2D { + context: Context::new(width, height, ColorSpace::default())?, + } + .into_instance(env)?; ctx.as_object(env).define_properties(&[ Property::new(FILL_STYLE_HIDDEN_NAME)? .with_value(&env.create_string("#000")?) @@ -108,6 +106,15 @@ impl CanvasElement { this.define_properties(&[Property::new("ctx")? .with_value(&ctx) .with_property_attributes(PropertyAttributes::Default)])?; + ctx + .as_object(env) + .define_properties(&[Property::new("canvas")? + .with_value(&this) + .with_property_attributes( + PropertyAttributes::Default + | PropertyAttributes::Writable + | PropertyAttributes::Enumerable, + )])?; Ok(Self { width, height, ctx }) }