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

Cannot draw image via canvas context.drawImage #3216

Closed
NekR opened this issue May 19, 2015 · 8 comments
Closed

Cannot draw image via canvas context.drawImage #3216

NekR opened this issue May 19, 2015 · 8 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

Comments

@NekR
Copy link

NekR commented May 19, 2015

2345 Argument of type 'HTMLImageElement' is not assignable to parameter of type 'HTMLVideoElement'.
  Property 'msHorizontalMirror' is missing in type 'HTMLImageElement'.

drawImage call:

context.drawImage(
  image,

  sliceLeft, sliceTop,
  sliceWidth, sliceHeight,

  drawLeft, drawTop,
  drawWidth, drawHeight
);
@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 19, 2015
@RyanCavanaugh
Copy link
Member

This code compiles. Can you post more details?

// What I'm guessing your variables are?
var sliceLeft = 0, sliceTop = 0, sliceWidth = 0, sliceHeight = 0;
var drawLeft = 0, drawTop = 0, drawWidth = 0, drawHeight = 0;
var context: CanvasRenderingContext2D;
var image: HTMLImageElement;

context.drawImage(
  image,

  sliceLeft, sliceTop,
  sliceWidth, sliceHeight,

  drawLeft, drawTop,
  drawWidth, drawHeight
);

@NekR
Copy link
Author

NekR commented May 19, 2015

@RyanCavanaugh

Yep, you are right, there is something else behind this error. It seems what this error occurse when not all arguments (expect image of course) are numbers. For example do this:

let sliceWidth = '';
let sliceHeight = '';

And you will get same error as I got. I know it's wrong to pass string there and that was a bug in my code because I forgot to cast it to number after .toFixed(), but error message is not informative for this situation.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript and removed Question An issue which isn't directly actionable in code labels May 19, 2015
@RyanCavanaugh
Copy link
Member

This happens because of how we do overload resolution:

  1. For each overload, try to assign the argument types to the parameter types
  2. As soon as you find one that works, stop
  3. If none of them work, repeat step 1 on the first overload and report the error that stopped you from succeeding

The first overload to drawImage is the one that takes a HTMLVideoElement, which is not assignable from image, so that's the error report.

We just don't have any way of knowing which overload you were trying to call:

declare function f(x: string, y: boolean);
declare function f(x: number, y: string);

// Is the problem that x is a number, or that y is a boolean?
f(32, true);
// Is the problem that x is a string, or that y is a string?
f('foo', 'foo');

In this particular case it's more obviously bad because the parameters only differ on that first argument; the ideal type of drawImage would type its first argument as HTMLImageElement|HTMLVideoElement|HTMLCanvasElement.

We should run some kind of tool over lib.d.ts to detect and fix up these cases so the error messages are more useful.

@mhegazy mhegazy added this to the TypeScript 1.6 milestone May 19, 2015
zhengbli added a commit to zhengbli/TypeScript that referenced this issue Jul 1, 2015
@RyanCavanaugh RyanCavanaugh added the Fixed A PR has been merged for this issue label Jul 2, 2015
@ekalaya
Copy link

ekalaya commented Jan 24, 2018

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'
at handlePathRoot (leaflet-image.js:133)
at pop (leaflet-image.js:201)
at leaflet-image.js:215
at tileQueueFinish (leaflet-image.js:116)
at notify (leaflet-image.js:223)
at leaflet-image.js:216
at Image.im.onload (leaflet-image.js:105)

@hetsit
Copy link

hetsit commented Jun 25, 2018

Hello, I'm getting this error, so please help me,
Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'

@AmShubhangi
Copy link

ror): Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'

@anilsamelia
Copy link

Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'

please help me

@seetpalsingh
Copy link

Hi,

i couldn't understand that fixed this issue in this post.

can anyone help me on this?

i am using this setup in vuejs 3

const startup = () => {
  // const videos: any = document.getElementById('video')
  // const canvas: HTMLElement | null = document.getElementById('canvas')
  const photo: HTMLElement = document.createElement('img')
  // const startbutton: any = document.getElementById('startbutton')

  navigator.mediaDevices
    .getUserMedia({
      video: true,
      audio: false
    })
    .then(function (stream) {
      videos.value.srcObject = stream
      videos.value.play()
    })
    .catch(function (err) {
      console.log('An error occurred: ' + err)
    })

  // clearphoto()
}

const canBePlayed = () => {
  if (!streaming) {
    height = 4 / (6 / width)

    if (isNaN(height)) {
      height = width / (4 / 3)
    }

    streaming = true
  }
}

const takepicture = () => {
  // const canvas: any = document.getElementById('canvas')
  const canvas: HTMLCanvasElement = document.createElement('canvas')
  // const output: HTMLElement | null = document.getElementById('output')
  const context: any = canvas.getContext('2d')

  console.log(typeof context)

  const img = new Image()
  // img.onload = function () {
  context.drawImage(videos, 0, 0, 200, 200)
  // }
  img.src = canvas.toDataURL('image/png')
  img.width = 240
  img.height = 240

  // const takeAPic2: any = document.getElementById('takeAPic')

  takeAPic.value.innerHTML = ''
  takeAPic.value.appendChild(img)
  // takeAPic.value = img
}

@microsoft microsoft locked as resolved and limited conversation to collaborators Apr 27, 2021
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
Projects
None yet
Development

No branches or pull requests

9 participants