javascript - Adjust image inside a canvas -


resize image within canvas when generating it.

i'm trying generate image. image convert dataimage, , later encode image. , put inside canvas element. size of width 300px , of height of 311px. image not re-scaled. how can fix this?

https://jsfiddle.net/svvmrwvv/

<canvas id="result" width="300" height="310"></canvas> <br> <div class='frames'></div>  var res_c = document.getelementbyid("result"); var res_ctx = res_c.getcontext('2d'); var width = 300; var height = 311;  //codifing image  var image = new image; image.src = 'http://tosemdinheiro.com/wp-content/uploads/2012/10/carro.jpg'; res_ctx.drawimage(image, 0, 0); imagedata = res_ctx.getimagedata(0, 0, width, height); console.log(imagedata);  //decodifing image  var image_gif = new image(); image_gif.src  = res_c.todataurl("image/png"); image_gif.height=width; console.log(image_gif) image_gif.width=height; $(".frames").append(image_gif);   

enter image description here

you need wait image load before can modify it. when render must render @ size want. drawimage function has several versions 1 of ctx.drawimage(image,x,y,width,height) draw image @ x,y , size width,height.

const canvas = document.getelementbyid("result"); const ctx = canvas.getcontext('2d'); const width = 300; const height = 311; const image = new image; image.src = 'http://tosemdinheiro.com/wp-content/uploads/2012/10/carro.jpg'; image.onload = function(){   // wait image load   ctx.canvas.width = width;  // set size   ctx.canvas.height = height;   ctx.drawimage(image,0,0,width,height);  // draw image @ size    // dont know why doing following removed   //const imagedata = res_ctx.getimagedata(0, 0, width, height);    // create new image   const imagegif = new image;   imagegif.src  = canvas.todataurl(); // "image/png" default mime type   document.queryselector(".frames").appendchild(imagegif); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -