scala-js / scala-js-dom

Statically typed DOM API for Scala.js
Other
315 stars 162 forks source link

Return Type of HTMLCanvasElement getContext is not clear. #845

Open G-yhlee opened 5 months ago

G-yhlee commented 5 months ago

Problem

ctx.beginPath(); // beginPath is not automatically recommended even though it is an internal function of ctx


#### Current 
```scala

abstract class HTMLCanvasElement extends HTMLElement {

  // When returning to js.Dynamic, the API type required for drawing is not inferred
  def getContext(contextId: String, args: js.Any*): js.Dynamic = js.native
}

Suggest is..

[ contextId ] -> [ return type ]
"2d" -> CanvasRenderingContext2D
"webgl" or "experimental-webgl" -> WebGLRenderingContext
"webgl2" -> WebGL2RenderingContext
"webgpu" -> GPUCanvasContext
"bitmaprenderer" -> ImageBitmapRenderingContext

// This is only match when contextType value is "2d" , We need general matching function ...
def getContext(contextType: String,
      contextAttributes: js.UndefOr[js.Dictionary[js.Any]] = js.native): CanvasRenderingContext2D = js.native
armanbilge commented 5 months ago

Thanks, I agree that returning js.Dynamic is strange. I think this is an API we could improve if we do https://github.com/scala-js/scala-js-dom/issues/296.

For now, perhaps we can return a union type 😕