williamngan / pts

A library for visualization and creative-coding
https://ptsjs.org
Apache License 2.0
5.16k stars 182 forks source link

CanvasForm(ctx) methods error. #186

Closed cdaein closed 2 years ago

cdaein commented 2 years ago

Hi,

I've been recreating the official demo sketches with an existing canvas (my repo) with CanvasForm(ctx) without using CanvasSpace and ran into some issues with methods.

Upon looking at the Canvas.ts source code, I noticed the 3 methods below make use of this.ctx, not this._ctx that are used by all the other methods.

this.ctx comes from Canvas.ts line:499:

get ctx():PtsCanvasRenderingContext2D { return this._space.ctx; }

but CanavsForm(ctx) created with an existing context does not have this._space initialized. (Canvas.ts line 478)

I think the quick fix might be just updating this.ctx to this._ctx or add a check inside get ctx() although I am not familiar enough with the library yet to know what other issues might be down the line. When I subclass CanvasForm and overwrite gradient() it worked for me.

Thank you!

williamngan commented 2 years ago

Thank you so much, @cdaein - good catch! I'll make a patch for this.

williamngan commented 2 years ago

Patched in v0.10.12. Give it a try :)

cdaein commented 2 years ago

Thank you for the quick fixes! It's working fine now. 🙏