Html2Canvas调整大小

时间:2012-07-09 15:03:44

标签: javascript html canvas screenshot html2canvas

我正在使用html2canvas来获取网页的屏幕截图并将其渲染为缩略图(好吧,400x300,不完全是缩略图)。

基于Screenshot console code,除缩略图部分外,一切都很有效。

如何将图像尺寸设置为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 个答案:

答案 0 :(得分:16)

根据Mikkos暗示以下内容对我有用

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 :(得分:3)

您可以使用缩略图尺寸创建其他新<canvas>,并使用drawImage()在新的<canvas>上缩小它。

drawImage()可以将<canvas>视为image来源,您可以设置目标宽度和高度。

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Using_images

答案 2 :(得分:1)

这对我来说很棒...

    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);                    
    });

答案 3 :(得分:0)

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

答案 4 :(得分:0)

I have used a similar approach for your problem, partly based on other answers :) Since resizing images is easier if they are actual images, here is my code:

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

canvasis the output of the html2canvas function; then I create an img setting its source with the img data (the base64 encoding of the canvas). the rest is standard good-old-javascript: set the img width, append to the div element ...

答案 5 :(得分:0)

发现这种方式对我来说是无法解释的(后来我发现为什么会这样做)。只需在位置属性之后添加一些属性,例如doc.addImage(img, 'JPEG', 20, 20, 200, 250),最后两个数字就可以调整img的大小。