2013-04-08 113 views
8

我想使用百分比大小製作響應式畫布,並且用戶將調整畫布相對調整的窗口大小。 我可以通過使用下面的代碼來縮放畫布,但唯一的問題是當我縮放鼠標繪圖消失的窗口大小。HTML5響應式畫布:調整瀏覽器畫布的大小繪製消失

<style> 
body{margin:0px;padding:0px;background:#a9a9a9;} 
#main{ 
display:block; 
width:80%; 
padding:50px 10%; 
height:300px; 
} 
canvas{display:block;background:#fff;} 
</style> 
</head> 
<body> 
<div id="main" role="main"> 
<canvas id="paint" width="100" height="100"> 
< !-- Provide fallback --> 
</canvas> 
</div> 

<script> 

var c = document.getElementById('paint'); 
var ctx = c.getContext('2d'); 
var x = null; 
var y; 

c.onmousedown = function(e){ 
x = e.pageX - c.offsetLeft; 
y = e.pageY - c.offsetTop; 
ctx.beginPath(); 
ctx.moveTo(x,y); 

} 

c.onmouseup = function(e){ 
x=null; 

} 


c.onmousemove = function(e){ 
if(x==null) return; 
x = e.pageX - c.offsetLeft; 
y = e.pageY - c.offsetTop; 
ctx.lineTo(x,y); 
ctx.stroke(); 

} 

$(document).ready(function(){ 
//Get the canvas & 
var c = $('#paint'); 

var container = $(c).parent(); 

//Run function when browser resizes 
$(window).resize(respondCanvas); 

function respondCanvas(){ 
    c.attr('width', $(container).width()); //max width 
    c.attr('height', $(container).height()); //max height 

    //Call a function to redraw other content (texts, images etc) 
} 

//Initial call 
respondCanvas(); 

}); 


</script> 

謝謝。

+0

聽起來像是http:// stackoverflow的副本。com/questions/5517783/prevent-canvas-clear-when-resizing-window – 2013-04-08 12:01:08

+0

使用這個庫,並在每一幀畫出畫面:http://georgealways.github.io/gee/ – Saturnix 2013-04-25 23:31:26

+0

看看這個:http ://stackoverflow.com/a/23128583/1265753 – karaxuna 2014-04-17 08:52:05

回答

2
c.attr('width', $(container).width()); //max width 
c.attr('height', $(container).height()); //max height 

上面的代碼就相當於clearRect這是用來清除畫布。所以,如果你想保留什麼以前繪製不能調整寬度和高度。 ,你可以創建所需寬度一塊畫布繪製使用drawImage(c)ç以前的畫布的內容的解決方案是畫布對象。然後你必須刪除畫布

+0

謝謝所以有沒有任何選項先保存畫布繪圖,然後設置c.attr()..或任何其他方式設置動態響應寬度/高度到畫布。 – iawara 2013-04-08 12:07:06

10

調整畫布

當內容處理

如果您調整畫布大小,繪製的內容總是被擦除。這就是畫布的行爲。

您可以在調整大小後重新繪製內容,也可以將內容另存爲圖像數據並在調整大小後進行還原(請參閱canvas.toDataURL)。

這裏是代碼和一個小提琴:http://jsfiddle.net/m1erickson/V6SVz/

<!doctype html> 
<html> 
<head> 
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css --> 
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 

<style> 
    body{ background-color: ivory; padding:10px; } 
    canvas{border:1px solid red;} 
</style> 

<script> 
$(function(){ 

    var canvas=document.getElementById("canvas"); 
    var ctx=canvas.getContext("2d"); 

    // draw some content 
    ctx.lineWidth=3; 
    ctx.fillStyle="blue"; 
    ctx.strokeStyle="red"; 
    ctx.rect(50,50,100,50); 
    ctx.fill(); 
    ctx.stroke(); 
    ctx.font="14px Verdana"; 
    ctx.fillStyle="white"; 
    ctx.fillText("Scale Me",65,75); 

    function saveResizeAndRedisplay(scaleFactor){ 

     // save the canvas content as imageURL 
     var data=canvas.toDataURL(); 

     // resize the canvas 
     canvas.width*=scaleFactor; 
     canvas.height*=scaleFactor; 

     // scale and redraw the canvas content 
     var img=new Image(); 
     img.onload=function(){ 
      ctx.drawImage(img,0,0,img.width,img.height,0,0,canvas.width,canvas.height); 
     } 
     img.src=data; 

    } 

    $("#resizer").click(function(){ saveResizeAndRedisplay(1.5); }); 

}); // end $(function(){}); 
</script> 

</head> 

<body> 
    <button id="resizer">Click to resize the canvas</button><br/> 
    <canvas id="canvas" width=200 height=150></canvas> 
</body> 
</html> 
2

感謝。這真的很有幫助。下面是我想要做的代碼。

#main{ 
display:block; 
width:80%; 
height:300px; 
background:#e0e0e0; 
} 
canvas{display:block;background:#fff;border:1px solid red;} 

</style> 

<script> 

$(function(){ 

var container=document.getElementById("main"); 
var canvas=document.getElementById("canvas"); 
var ctx=canvas.getContext("2d"); 

// draw some content 
//alert(canvas.width); 

var w=$(container).width(); 
var h=$(container).height(); 



$(window).resize(saveResizeAndRedisplay); 
function saveResizeAndRedisplay(){ 

    // save the canvas content as imageURL 
    var data=canvas.toDataURL(); 

    // resize the canvas 
    canvas.width = $(container).width(); 
    canvas.height = $(container).height(); 
    //alert($(container).width()); 

    // scale and redraw the canvas content 
    var img=new Image(); 
    img.onload=function(){ 
     ctx.drawImage(img,0,0,img.width,img.height); 
    } 
    img.src=data; 

} 

saveResizeAndRedisplay(); 

}); 
</script> 

</head> 

<body> 

<div id="main" role="main"> 
<canvas id="canvas" width=200 height=150></canvas> 
</div> 

<script> 
var canvas = document.getElementById('canvas'); 
var ctx = canvas.getContext('2d'); 
var x = null; 
var y; 

canvas.onmousedown = function(e){ 
x = e.pageX - canvas.offsetLeft; 
y = e.pageY - canvas.offsetTop; 
ctx.beginPath(); 
ctx.moveTo(x,y); 
} 

canvas.onmouseup = function(e){ 
x=null; 
} 


canvas.onmousemove = function(e){ 
if(x==null) return; 
x = e.pageX - canvas.offsetLeft; 
y = e.pageY - canvas.offsetTop; 
ctx.lineTo(x,y); 
ctx.stroke(); 
} 
</script> 
2

我創建了一個jQuery插件,處理HTML5畫布大小調整。它旨在彌補移動用戶和桌面用戶之間的差距。它仍然是一個在製品,但它是可擴展的,並且內置了一些基本的繪圖功能。

http://trsanders.github.io/responsive-sketchpad/

3

我也有,你都面臨着萬畝應用程序相同的問題,閱讀關於它後,我來這個結論是,canvas元素的行爲是在重新調整大小後自行清除的,唯一的解決方法是爲窗口重新大小事件添加偵聽器,並在事件被觸發時重新繪製畫布。

$(window).resize(function(e) { 
// function to redraw on the canvas 
}); 



$(document).ready(function(e){ 
    var c = $('#example'); // getting the canvas element 
    var ct = c.get(0).getContext('2d'); 
    var container = $(c).parent(); 
    $(window).resize(respondCanvas); //handling the canvas resize 
    function respondCanvas(){ 
     // makign the canvas fill its container 
     c.attr('width', $(container).width()); //max width 
     c.attr('height', ($(container).height() -20)); //max height 
    } 
    respondCanvas(); 
    make_image('images/india.png',1,1); 
} 

希望它有幫助。

Source

+0

當您從網站複製代碼時,請提供源代碼。 – akshaynagpal 2015-07-03 05:32:40

0

我用這個方法:

var context = document.getElementById('paint').getContext('2d'); 
var canvas = context.canvas; 

function responsive(width){ 
    var p = width/canvas.width; 
    canvas.width *= p; 
    canvas.height *= p; 

    var scaleFactor = context.scaleFactor || 1; 
    context.scale(p * scaleFactor, p * scaleFactor); 
    context.scaleFactor = p * scaleFactor; 
} 

var mq = window.matchMedia("(min-width: 735px)"); 
mq.addListener(applyChanges); 
applyChanges(); 

function applyChanges(){ 
    if(!mq) 
     responsive(350); 
    else 
     responsive(735); 
} 
0

我有同樣的問題。我的解決方案是使用CSS3的屬性放大到畫布的父的容器:

<div id="parent_div"> 
    <div id="constructor_div"> 
     <canvas width="600" height="900"> 
    </div> 
</div> 

<script> 
    constructor_zoom(); 

    $(window).resize(function() { 
     constructor_zoom(); 
    }); 

    function constructor_zoom() 
    { 
     var parent_width = $('#parent_div').width(); 
     var item_width = $('#constructor_div').width(); 
     var coef = 0.1; 

     $('.constructor_image').css('zoom', parent_width/item_width - coef); 
    } 
</script> 

COEF - 係數,使來自父母的邊界畫布的縮進。你可以讓它爲零。

我希望對某人有幫助:-)