Skip to content

Return Type of HTMLCanvasElement getContext is not clear. #845

@G-yhlee

Description

@G-yhlee
Contributor

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
      

Activity

armanbilge

armanbilge commented on May 2, 2024

@armanbilge
Member

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 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @armanbilge@G-yhlee

        Issue actions

          Return Type of HTMLCanvasElement getContext is not clear. · Issue #845 · scala-js/scala-js-dom