Html5 Canvas开发之清除一个特定区域内的Canvas、宽高技巧、使Canvas填充整个浏览器窗口

1.清除一个特定区域内的Canvas

context.fillRect(40, 40, 100, 100);
context.beginPath();
context.arc(230, 90, 50, 0, Math.PI*2, false);
context.closePath();
context.fill();
context.clearRect(230, 90, 50, 50);


2.宽高技巧

context.fillStyle = "rgb(255, 0, 0)";
context.fillRect(40, 40, 100, 100);
context.beginPath();
context.arc(230, 90, 50, 0, Math.PI*2, false);
context.closePath();
context.fill();
canvas.attr("width", canvas.width());
canvas.attr("height", canvas.height());
context.fillRect(40, 40, 100, 100);

注意:

The downside with the width/height trick isthat absolutely everything in the canvas is reset, includingstyles and colors.This is why you should only use this trick if you!re prepared to completelyreset the canvas, not just wipe the display clean.


3.

使Canvas填充整个浏览器窗口

<!DOCTYPE html>

<html>
	<head>
		<title>Learning the basics of canvas</title>
		<meta charset="utf-8">
		
		<link href="canvas.css" rel="stylesheet" type="text/css">
		
		<script type="text/javascript" src="jquery.js"></script>
		
		<script type="text/javascript">
			$(document).ready(function() {	
				var canvas = $("#myCanvas");
				var context = canvas.get(0).getContext("2d");
				
				$(window).resize(resizeCanvas);
				
				function resizeCanvas() {
					canvas.attr("width", $(window).get(0).innerWidth);
					canvas.attr("height", $(window).get(0).innerHeight);
					context.fillRect(0, 0, canvas.width(), canvas.height());
				};
				
				resizeCanvas();
			});
		</script>
	</head>
	
	<body>
		<canvas id="myCanvas" width="500" height="500">
			<!-- Insert fallback content here -->
		</canvas>
	</body>
</html>

* { margin: 0; padding: 0; }
html, body { height: 100%; width: 100%; }
canvas { display: block; }

注意点:1.

If only jQuery had a resize method that was fired at the moment a browser window was resized。

                2.

changing the width and height will reset the canvas, so everything has to be redrawn). 




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值