我想使用百分比大小製作響應式畫布,並且用戶將調整畫布相對調整的窗口大小。 我可以通過使用下面的代碼來縮放畫布,但唯一的問題是當我縮放鼠標繪圖消失的窗口大小。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>
謝謝。
聽起來像是http:// stackoverflow的副本。com/questions/5517783/prevent-canvas-clear-when-resizing-window – 2013-04-08 12:01:08
使用這個庫,並在每一幀畫出畫面:http://georgealways.github.io/gee/ – Saturnix 2013-04-25 23:31:26
看看這個:http ://stackoverflow.com/a/23128583/1265753 – karaxuna 2014-04-17 08:52:05