Skip to content

Commit

Permalink
fix: missing canvas property on Context2D (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Jan 1, 2023
1 parent 13c36c0 commit b0a351a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
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

0 comments on commit b0a351a

Please sign in to comment.