Html2Canvas的调整大小

7

我正在使用html2canvas尝试对网页进行截图,并将其渲染成缩略图(实际大小为400x300,不完全是缩略图)。

根据截图控制台代码,除了缩略图部分之外,一切都很顺利。

如何将图像大小设置为400x300?在firebug中,我找到了属性:<canvas style="width: 973px; height: 2184px;" width="973" height="2184"></canvas>。但是,在我的代码(如下所示)或html2canvas.js中,我无法找到硬编码400x300参数的位置。

screenshot.html:

<html>
<head> 
<title>Screenshot</title> 
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]--> 
</head> 
<body> 
<div class=container> </div> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

<script type="text/javascript" src="js/html2canvas.js"></script> 
<script type="text/javascript">
var date=new Date();
var message,timeoutTimer,timer;
var proxyUrl="http://html2canvas.appspot.com";
function addRow(a,c,d){var b=$("<tr />").appendTo($(a));b.append($("<td />").css("font-weight","bold").text(c)).append($("<td />").text(d))}function throwMessage(b,a){window.clearTimeout(timeoutTimer);timeoutTimer=window.setTimeout(function(){message.fadeOut(function(){message.remove()})},a||2000);$(message).remove();message=$("<div />").html(b).css({margin:0,padding:10,background:"#000",opacity:0.7,position:"fixed",top:10,right:10,fontFamily:"Tahoma",color:"#fff",fontSize:12,borderRadius:12,width:"auto",height:"auto",textAlign:"center",textDecoration:"none"}).hide().fadeIn().appendTo("body")}$(function(){$("#recommended a").click(function(c){c.preventDefault();$("#url").val(this.href);$("button").click()});var a,b;$('input[type="button"]').click(function(){$(a.contentWindow).unbind("load");$(a).contents().find("body").html2canvas({canvasHeight:b.body.scrollHeight,canvasWidth:b.body.scrollWidth,logging:true})});$("#getscreenshot").click(function(d){d.preventDefault();$(this).prop("disabled",true);var c=$("#url").val();$("#content").append($("<img />").attr("src","loading.gif").css("margin-top",40));var f=document.createElement("a");f.href=c;$.ajax({data:{xhr2:false,url:f.href},url:proxyUrl,dataType:"jsonp",success:function(e){a=document.createElement("iframe");$(a).css({visibility:"hidden"}).width($(window).width()/2).height($(window).height()/2);$("#content").append(a);b=a.contentWindow.document;b.open();$(a.contentWindow).load(function(){var g=$(a).contents().find("body"),h={onrendered:function(j){$("#content").empty().append(j);$("#getscreenshot").prop("disabled",false);$("base").attr("href","")},allowTaint:true,taintTest:false,flashcanvas:"src/flashcanvas.min.js"},i=html2canvas(g,h)});$("base").attr("href",f.protocol+"//"+f.hostname+"/"+f.pathname);e=e.replace("<head>","<head><base href='"+f.protocol+"//"+f.hostname+"/"+f.pathname+"'  />");if($("#disablejs").prop("checked")){e=e.replace(/\<script/gi,"<!--<script");e=e.replace(/\<\/script\>/gi,"<\/script>-->")}b.write(e);b.close()}})})});
</script> 

<form class="well form-search"> 
<label for=url>Website URL:</label> 
<input type=url id=url value="http://www.yahoo.com" class="input-medium search-query"/>
<button class=btn id=getscreenshot>Get screenshot!</button> 
</form> 

 <div id=content></div> 

</body> 
</html>
6个回答

23

根据Mikko的提示,以下内容对我起作用了

window.html2canvas([$('body')[0]], {
              onrendered: function(canvas) {
                var extra_canvas = document.createElement("canvas");
                extra_canvas.setAttribute('width',70);
                extra_canvas.setAttribute('height',70);
                var ctx = extra_canvas.getContext('2d');
                ctx.drawImage(canvas,0,0,canvas.width, canvas.height,0,0,70,70);
                var dataURL = extra_canvas.toDataURL();
                var img = $(document.createElement('img'));
                img.attr('src', dataURL);
                // insert the thumbnail at the top of the page
                $('body').prepend(img);
              },
            });

1
你好,你的代码让我离成功很近了,只是ctx方面还有些问题。我有一张宽高未知的图片。我想让它适应800x500像素的大小(保持其纵横比)。但当图片更宽时,我得到了一个不希望出现的裁剪:(有什么好的建议可以设置一个合适的适应方式吗?谢谢。 - Héctor León

3

2

This works great for me...

    html2canvas(document.getElementById("divTextOrig"), { allowTaint: true, 
    scrollX:0, scrollY: -window.scrollY}).then(function(canvas)
    {
        document.getElementById("divText1").appendChild(canvas);
        $('canvas').css("width",txtWidth);
        $('canvas').css("height",txtHeight);                    
    });

1

我已经为您的问题使用了类似的方法,部分基于其他答案 :) 由于调整图像大小更容易如果它们是实际的图像,这是我的代码:

var img = new Image();
img.src = canvas.toDataURL("image/png");
img.width = 500;
document.getElementsByClassName('modal-body')[0].appendChild(img);

canvas是html2canvas函数的输出;然后我创建一个img,将其源设置为img数据(canvas的base64编码)。其余部分是标准的老式JavaScript:设置img宽度,将其附加到div元素...


0

你可以使用getContext('2d')方法从画布上下文中获取引用,然后将返回的上下文分配给一个变量(例如ctx),就可以使用上下文scale方法来缩放画布上下文到所需大小。此外,您可以使用样式属性定义画布大小,并通过将实际图像大小除以所需缩略图大小来设置样式widthheight属性的大小。在最后一步中,将结果数字放入ctx.scale(width, height);方法中。


0

这个方法对我来说是意料之外的(后来我发现了它为什么这么做)。只需要在位置属性后面添加一些属性,像这样doc.addImage(img,'JPEG',20,20,200,250),最后的两个数字可以让你调整图片大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接