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

HTMLCanvasElement.getContext() missing "webgl" type in lib.d.ts #5773

Closed
GoToLoop opened this issue Nov 24, 2015 · 11 comments
Closed

HTMLCanvasElement.getContext() missing "webgl" type in lib.d.ts #5773

GoToLoop opened this issue Nov 24, 2015 · 11 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@GoToLoop
Copy link

getContext(contextId: "2d"): CanvasRenderingContext2D;
getContext(contextId: "experimental-webgl"): WebGLRenderingContext;
getContext(contextId: string, ...args: any[]): CanvasRenderingContext2D | WebGLRenderingContext;

Overloaded getContext(contextId: "experimental-webgl"): WebGLRenderingContext; is missing "webgl" according to these reference sites below:
https://developer.Mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
http://www.JavaScripture.com/HTMLCanvasElement

Also all the overloaded methods got an optional contextAttributes?: {} parameter.
And I don't think ...args: any[] represents the 2nd parameter correctly.
This is my change proposal for them btW: :-D

getContext(contextType: "2d", contextAttributes?: {}): CanvasRenderingContext2D;
getContext(contextType: "webgl" | "experimental-webgl", contextAttributes?: {}): WebGLRenderingContext;
getContext(contextType: string, contextAttributes?: {}): CanvasRenderingContext2D | WebGLRenderingContext;
@mhegazy mhegazy added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this Good First Issue Well scoped, documented and has the green light labels Dec 1, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2015

PRs are welcomed. here is some details on adding these type definitions: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes

@rohitverma007
Copy link

Hi, I am fairly new to TypeScript and OSS, and this looks like a great way for me to start!
I would like to work on this, if nobody has already done so.
Although, I do have some more recommendations.
The contextAttributes can have various optional attributes according to the context ("2d" vs "webgl") as stated in the docs. There is also a new "webgl2" context that is not yet ready for production but, I think it could be included as well. With that in mind, here is what I was thinking of updating it to:

getContext(contextType: "2d", contextAttributes?: {alpha?: boolean, willReadFrequently?: boolean, storage?: string}): CanvasRenderingContext2D;
getContext(contextType: "webgl" | "experimental-webgl" | "webgl2" | "experimental-webgl2" , contextAttributes?: {alpha?: boolean, depth?: boolean, stencil?: boolean, antialias?: boolean, premultipliedAlpha?: boolean, preserveDrawingBuffer?: boolean, failIfMajorPerformanceCaveat?: boolean}): WebGLRenderingContext | WebGL2RenderingContext;
getContext(contextType: string, contextAttributes?: {}): CanvasRenderingContext2D | WebGLRenderingContext | WebGL2RenderingContext;

Let me know how you guys feel about that.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 3, 2015

I would split into two signatures, one for "webgl" | "experimental-webgl" and another for "webgl2" | "experimental-webgl2" since they return two different types. this way the user does not have to check/cast the result.

@rohitverma007
Copy link

right, makes sense, I'll get started on this then :)

@GoToLoop
Copy link
Author

GoToLoop commented Dec 3, 2015

My initial proposal was trying to be simple while still describing the overload signatures correctly.
But since it's been decided to go for some full blown complete specification, here's my re-proposal now:

getContext(contextType: "2d", contextAttributes?: {
  alpha?: boolean; willReadFrequently?: boolean; storage?: string;
  [attrib: string]: boolean | string; }): CanvasRenderingContext2D;

getContext(contextType: "webgl" | "experimental-webgl", contextAttributes?: {
  alpha?: boolean; depth?: boolean; stencil?: boolean; antialias?: boolean;
  premultipliedAlpha?: boolean; preserveDrawingBuffer?: boolean; failIfMajorPerformanceCaveat?: boolean;
  [attrib: string]: boolean; }): WebGLRenderingContext;

getContext(contextType: "webgl2" | "experimental-webgl2", contextAttributes?: {
  alpha?: boolean; depth?: boolean; stencil?: boolean; antialias?: boolean;
  premultipliedAlpha?: boolean; preserveDrawingBuffer?: boolean; failIfMajorPerformanceCaveat?: boolean;
  [attrib: string]: boolean; }): WebGL2RenderingContext;

getContext(contextType: string, contextAttributes?: { [prop: string]: boolean | string; }):
  CanvasRenderingContext2D | WebGLRenderingContext | WebGL2RenderingContext;

And maybe in order to shorten the WebGL overloads, how about creating some interface for parameter contextAttributes?:

interface WebGLAttributesObj {
  alpha?: boolean;
  depth?: boolean;
  stencil?: boolean;
  antialias?: boolean;
  premultipliedAlpha?: boolean;
  preserveDrawingBuffer?: boolean;
  failIfMajorPerformanceCaveat?: boolean;

  [attrib: string]: boolean;
}

getContext(contextType: "webgl"  | "experimental-webgl",  contextAttributes?: WebGLAttributesObj): WebGLRenderingContext;
getContext(contextType: "webgl2" | "experimental-webgl2", contextAttributes?: WebGLAttributesObj): WebGL2RenderingContext;

P.S.: In order to have WebGL2RenderingContext type, we're gonna need some stub for it as well:

interface WebGL2RenderingContext extends WebGLRenderingContext {}

@rohitverma007
Copy link

The re-proposal looks good.
But, I am not too sure about creating an interface for the contextAttributes as wouldn't it be possible that the webgl2 contextattributes could have more options than webgl at some point and hence that interface could no longer be used?

@GoToLoop
Copy link
Author

GoToLoop commented Dec 4, 2015

If or when that happens, it's as simple as making another interface extending that.
Anyways, WebGL2RenderingContext is more like a draft right now and it's Firefox exclusive so far.

@mhegazy mhegazy added this to the Community milestone Dec 8, 2015
@rohitverma007
Copy link

@GoToLoop sounds good, I guess I will ignore the webgl2 stuff for now then, but still use an interface for the webgl context,
@mhegazy kind of stuck at something, any help would be appreciated :).
I noticed there is already a WebGLContextAttributes ( threw me off a bit since I named it the exact same before realizing it already existed :)) , to add a new property to the existing WebGLContextAttributes, I am adding the following to addedTypes.json:

    {
        "kind": "property",
        "interface": "WebGLContextAttributes",
        "name": "failIfMajorPerformanceCaveat?",
        "type": "boolean"
    },

But the dom.generated.d.ts is not updating the existing interface... does it need to go into overridingTypes.json instead? I have tried that as well.. doesn't work either.
The overriding of the getContext methods works fine, I simply modified the already existing override in overridingTypes.json.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 4, 2016

But the dom.generated.d.ts is not updating the existing interface... does it need to go into overridingTypes.json instead? I have tried that as well.. doesn't work either.
The overriding of the getContext methods works fine, I simply modified the already existing override in overridingTypes.json.

Sorry for the delay. @zhengbli can you help @rohitverma007 out.

@zhengbli
Copy link
Contributor

zhengbli commented Jan 5, 2016

@rohitverma007 You did the right thing to add the content in addedTypes.json file. Though the script has a bug that doesn't pick up the change for dictionaries in the spec, I'm fixing it and will update you soon.

@zhengbli
Copy link
Contributor

@rohitverma007 the script has been updated. Can you try with the new content in addedTypes.json now? Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
4 participants