简体   繁体   English

从画布设置图像大小

[英]Set size of image from canvas

So I'm using Protractor in my project and in my test I want to convert the canvas element to an image with a specific size. 所以我在我的项目中使用Protractor,在我的测试中我想将canvas元素转换为具有特定大小的图像。 I have the conversion down but can't seem to change the size of the image. 我有转换,但似乎无法改变图像的大小。 Here's my code: 这是我的代码:

it('should save an image', function(done) {
  //because of Protractor
  browser.driver.executeScript(function () {
    var can = document.querySelector('#workspace-canvas');
    var data = can.toDataURL("image/png");

    var image = new Image();
    image.width = 500;
    image.height = 500;
    image.src = data;
    console.log(image.height, image.width); //1122 1888
    var link = document.createElement('a');
    link.href = image.src;
    link.download = 'study-chart.png';
    link.click();

    return data;
  }).then(function (result) {
    browser.sleep(2000);
    done();
  });
});

I'm not able to change the size of the canvas. 我无法改变画布的大小。 In my fiddle you can see the image size is changed in the DOM, but when I download it, the image is only 200x200 pixels because of the canvas size. 在我的小提琴中你可以看到DOM中的图像大小已经改变,但是当我下载它时,由于画布大小,图像只有200x200像素。 What am I missing? 我错过了什么?

The problem is that adjusting the width and height of the image element doesn't actually change its original size. 问题是调整图像元素的宽度和高度实际上不会改变其原始大小。 It will appear larger when displayed in the browser, but the original size of the image is still the size of the canvas. 在浏览器中显示时它会显得更大,但图像的原始大小仍然是画布的大小。

I've modified your fiddle to show you how you can create a new image with a desired size by dynamically creating a new canvas with the specified width and height and drawing your original image on it. 我已经修改过您的小提琴,通过动态创建具有指定宽度和高度的新画布并在其上绘制原始图像,向您展示如何创建具有所需大小的新图像。

 var can = document.querySelector('#canvas1'); var ctx = can.getContext('2d'); ctx.fillRect(50, 50, 50, 50); var img = new Image(); img.src = can.toDataURL(); var link = document.createElement('a'); var img2 = resizeImage(img, 500, 500); link.href = img2.src; link.download = 'study-chart.png'; link.click(); function resizeImage(img, w, h) { var result = new Image(); var canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h; canvas.getContext('2d').drawImage(img, 0, 0, w, h); result.src = canvas.toDataURL(); return result; } 
 canvas { border: 1px solid gray; } 
 <canvas id="canvas1" width="200" height="200"></canvas> 

Have you tried using the canvas context and scaling that and then saving out as an image? 您是否尝试过使用画布上下文并对其进行缩放,然后将其另存为图像?

context.scale context.scale

@Kurumi, your codes can work somehow but better to add onload method @Kurumi,您的代码可以以某种方式工作,但更好地添加onload方法

        var img = new Image();
        img.src = c.toDataURL('image/jpeg');
        var img2 = '';
        img.onload = function () {
            img2 = resizeImage(img, oWidth, oHeight);
            var link = document.createElement('a');
            img2.onload = function () {
                link.href = img2.src;
                link.download = 'study-chart.jpeg';
                link.click();
            }
        }

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

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