简体   繁体   English

将画布输出图像调整为特定大小(宽度/高度)

[英]Resize the canvas output image to a specific size (width/height)

I have a big canvas ( 5000x5000 ) and I want to take a picture of it and create a thumbnail on client side.我有一个大画布( 5000x5000 ),我想给它拍照并在客户端创建一个缩略图。 I can capture the image using canvas.toDataURL but how do i re-size it?我可以使用canvas.toDataURL捕获图像,但如何调整它的大小? Do i have to create an new $("<canvas></canvas>") element and then put that image inside and run the canvas2.toDataURL();我是否必须创建一个新的$("<canvas></canvas>")元素,然后将该图像放入其中并运行canvas2.toDataURL(); Can anyone help me with this?谁能帮我这个? I can't get my head around it how to do it.我不知道该怎么做。

var canvas = document.getElementById("main");
var ctx = canvas.getContext("2d");
var tumbnail64 = null;
var image = new Image();
image.src = canvas.toDataURL();
image.onload = function() {

    $c2 = $("<canvas></canvas>");
    $c2[0].width=100;
    $c2[0].height=100;
    $c2[0].getContext("2d");
    $c2[0].drawImage(image, 0, 0,100,100);
    tumbnail64 = $c2[0].toDataURL();
};

Something like this should work, given you don't have security restrictions on the original canvas element:鉴于您对原始画布元素没有安全限制,这样的事情应该可行:

var resizedCanvas = document.createElement("canvas");
var resizedContext = resizedCanvas.getContext("2d");

resizedCanvas.height = "100";
resizedCanvas.width = "200";

var canvas = document.getElementById("original-canvas");

resizedContext.drawImage(canvas, 0, 0, 200, 100);
var myResizedData = resizedCanvas.toDataURL();

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

相关问题 图像旋转 - 根据图像高度和宽度调整画布大小 - Image rotate - resize canvas based on image height and width 使用jQuery宽度和高度调整画布大小以适合图像尺寸 - resize a canvas with jquery width & height to fit an image dimensions 如何在图像编辑器中根据输入的高度和宽度设置画布大小? - how to set canvas size on inputted height and width in my image editor? 将画布拉伸至全宽和全高以动态调整尺寸 - Stretch canvas to full width and height for dynamic resize 画布有时无法调整为宽度和高度 - Canvas sometimes does not resize to width and height 自动调整画布大小以适应浏览器的宽度和高度 - Auto-resize Canvas to Browser Width & Height Javascript上传前调整图像大小-从画布读取-图像在那里,宽度和高度0 - Javascript resize image before upload - read from canvas - image there, width and height 0 使用 JavaScript(也许是 CSS)在特定宽度/高度的 HTML5 Canvas 内显示完整图像大小(100% 宽度/高度) - Display the Full Image Size (100% Width/Height) inside a HTML5 Canvas of a certain Width/Height using JavaScript, maybe CSS 根据javascript中的最小宽度/高度和最大宽度/高度调整图像大小 - resize image respecting a min width / height and max width / height in javascript 根据视口宽度动态调整图像高度 - Resize an image height dynamically according to viewport width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM