scala-js/scala-js-dom

Return Type of HTMLCanvasElement getContext is not clear.

Opened this issue · 1 comments

Problem

  • Return type of getContext is not clear
val canvas = document.querySelector("#monadcanvas").asInstanceOf[HTMLCanvasElement]
val ctx = canvas.getContext("2d", js.Dictionary("alpha" -> true))

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

Current

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
      

Thanks, I agree that returning js.Dynamic is strange. I think this is an API we could improve if we do #296.

For now, perhaps we can return a union type 😕