Questions tagged [drawimage]

The drawImage() method draws an image, canvas, or video onto the canvas. It can also draw parts of an image, and/or increase/reduce the image size.

The drawImage() method draws an image, canvas, or video onto the canvas.

The drawImage() method can also draw parts of an image, and/or increase/reduce the image size.

Example

function draw() {
  var ctx = document.getElementById('canvas').getContext('2d');
  var img = new Image();
  img.src = 'images/backdrop.png';
  img.onload = function(){
    ctx.drawImage(img,0,0);
    ctx.beginPath();
    ctx.moveTo(30,96);
    ctx.lineTo(70,66);
    ctx.lineTo(103,76);
    ctx.lineTo(170,15);
    ctx.stroke();
  }
}

References

  1. drawImage - MDN Link
  2. Canvas - drawImage Example
  3. Canvas - drawImage Tutorial
690 questions
70
votes
5 answers

How to draw photo with correct orientation in canvas after capture photo by using input[type='file'] in mobile web browser?

I am making a simple web app in mobile which allow visitor to capture photo by using html5 input[type=file] element. Then I will display it on the web for preview, and then visitor can choose to upload the photo to my server for other purpose(ie:…
Rock Yip
  • 713
  • 1
  • 6
  • 7
69
votes
3 answers

Canvas drawImage scaling

I'm trying to scale an image proportionately to the canvas. I'm able to scale it with fixed width and height as so: context.drawImage(imageObj, 0, 0, 100, 100) But I only want to resize the width and have the height resize proportionately.…
selanac82
  • 2,920
  • 4
  • 27
  • 37
60
votes
3 answers

How can I draw an image from the HTML5 File API on Canvas?

I would like to draw an image opened with the HTML5 File API on a canvas. In the handleFiles(e) method, I can access the File with e.target.files[0] but I can't draw that image directly using drawImage. How do I draw an image from the File API on…
Jonas
  • 121,568
  • 97
  • 310
  • 388
44
votes
3 answers

HTML5 canvas drawImage with at an angle

I am experimenting with animation in and can't work out how to draw an image at an angle. The desired effect is a few images drawn as usual, with one image rotating slowly. (This image is not at the centre of the screen, if that makes any…
Greg
  • 21,235
  • 17
  • 84
  • 107
38
votes
4 answers

Using Web Workers for drawing using native canvas functions

It's possible to send a CanvasPixelArray obtained via getImageData to a worker script, and let the worker script manipulate the pixels in its background thread, and eventually post the manipulated pixel array back. However, I'm using native canvas…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
38
votes
14 answers

How do I load url into Image into DrawImage in Compose UI Android Jetpack?

I can use val image = +imageResource(R.drawable.header) and use DrawImage(image) to load image from Drawable Resource, But how do I load a string url into the DrawImage(image)?. I've tried using Glide, but it needs to load into imageView.…
38
votes
2 answers

drawImage() not working

I am reading through "Making Isometric Social Real-Time Games with HTML5, CSS3 and Javascript." I am not far into it, and I have run into a canvas problem that has ahd me stumped for most of the day. drawImage() does not seem to be drawing. I have…
Jeremythuff
  • 1,518
  • 2
  • 13
  • 35
37
votes
5 answers

Drawing PNG to a canvas element -- not showing transparency

I'm trying to use drawImage to draw a semi-transparent PNG on a canvas element. However, it draws the image as completely opaque. When I look at the resource that's being loaded and load the actual PNG in the browser, it shows the transparency, but…
pixielex
  • 779
  • 2
  • 8
  • 13
36
votes
3 answers

JavaScript Failed to execute 'drawImage'

Ok so I'm creating a game with JavaScript Canvas Elements and such. I've been able to load in TONS of Images, but on a select few, JavaScript replies with Errors such as Uncaught TypeError: Failed to execute 'drawImage' on…
tanishalfelven
  • 1,186
  • 2
  • 10
  • 21
30
votes
5 answers

HTML5 Canvas: Get Event when drawing is finished

I'm drawing an image to a canvas element. I then have code that depends on this process to be finished. My code looks like this: var myContext = myCanvasElement.getContext('2d'), myImg = new Image(); myImg.onload = function() { …
elHornair
  • 412
  • 1
  • 4
  • 8
28
votes
1 answer

Strange HTML5 Canvas drawImage behaviour

I am writing some code that uses HTML5 canvas. Generally it works well, but now I found a very strange behaviour. The weird thing is that it is consistent on different browsers, so must be that I understood the thing wrong... Despite the docs seem…
gotch4
  • 13,093
  • 29
  • 107
  • 170
27
votes
5 answers

How to avoid HTML Canvas auto-stretching

I have the following piece of HTML: