Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing canvas property on Context2D #597

Merged
merged 1 commit into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions __test__/canvas-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 13 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ impl CanvasElement {
width: u32,
height: u32,
) -> Result<ClassInstance<CanvasRenderingContext2D>> {
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")?)
Expand All @@ -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 })
}

Expand Down