gpt4 book ai didi

javascript - HTML5 响应式 Canvas : resizing the browser canvas draw disappear

转载 作者:太空狗 更新时间:2023-10-29 13:20:05 25 4
gpt4 key购买 nike

我想使用百分比大小制作一个响应式 Canvas ,一旦用户调整窗口大小, Canvas 就会相对调整。我可以使用以下代码缩放 Canvas ,但唯一的问题是当我缩放窗口大小时,鼠标绘图消失了。

<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>

谢谢。

最佳答案

调整 Canvas 大小时处理内容

如果调整 Canvas 大小,绘制的内容总是会被删除。这就是 Canvas 的行为方式。

您可以在调整大小后重新绘制内容,也可以将内容保存为图像数据并在调整大小后恢复(请参阅 canvas.toDataURL)。

这是代码和 fiddle :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>

关于javascript - HTML5 响应式 Canvas : resizing the browser canvas draw disappear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com