简体   繁体   English

HTML5 canvas保存实际尺寸的图像

[英]HTML5 canvas save actual size image

I have a html5 canvas where I am showing image as 350x450 pixles. 我有一个html5画布,其中将图像显示为350x450像素。

But actual size of image is 600x900 px. 但图片的实际尺寸为600x900像素。

How can I save it in original size. 如何保存为原始大小。

var canvas = document.getElementById('my_canvas');
  var ctx = canvas.getContext('2d');
  var imageObj = new Image();
  ctx.canvas.width = 350;
  ctx.canvas.height = 450;
  imageObj.onload = function() {
    ctx.drawImage(imageObj, 0, 0,100,100);
  };
imageObj.src = 'https://davidwalsh.name/demo/ringo-ftw.jpg';
var base64 = canvas.toDataURL();

As you see when Im getting base64 Image size is reduced. 如您所见,当Im获得base64时,图像大小减小。 I am getting image 350x450. 我正在获取图片350x450。 But I want to save it in 600x900. 但我想将其保存为600x900。

Is there any way ? 有什么办法吗?

Basically what I am doing in below code is that> 基本上我在下面的代码中所做的是

I am creating a temp canvas element with display: none style & also load an image into that and save from this while show original canvas. 我正在创建一个带显示的临时画布元素:无样式,还将图像加载到其中并在显示原始画布时从中保存。

 var canvas = document.getElementById('my_canvas');
 var tempCanvas = document.getElementById('temp_canvas'); // use style  display : none  for this temp element 
 var ctx = canvas.getContext('2d');
 var tempCtx = tempCanvas.getContext('2d');
 var imageObj = new Image();
 ctx.canvas.width = 350;
 ctx.canvas.height = 450;
 imageObj.onload = function() {
     ctx.drawImage(imageObj, 0, 0,100,100);
     tempCtx.drawImage(imageObj, 0, 0,100,100);
 };
imageObj.src = 'https://davidwalsh.name/demo/ringo-ftw.jpg';
var base64 = tempCanvas.toDataURL();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM