码迷,mamicode.com
首页 > Web开发 > 详细

【HTML5】Canvas之globalCompositeOperation属性详解

时间:2014-12-11 12:30:31      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   os   java   on   

globalCompositeOperation即Canvas中的合成操作。

1、source-over

这是默认值,他表示绘制的图形将画在现有画布之上

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="source-over"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣

bubuko.com,布布扣

2、destination-over

这个操作的值与前一个值相反,所以现在目标绘制在源之上

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="destination-over"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣

3、source-atop

这个操作会将源绘制在目标之上,但是在重叠区域上两者都是不透明的。绘制在其他位置的目标是不透明的,但源是透明的。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="source-atop"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣

4、destination-atop

这个操作与source-atop相反,目标绘制在源之上

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="destination-atop"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣

bubuko.com,布布扣


5、source-in

在源于目标重叠的区域只绘制源,而不重叠的部分编程透明的。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="source-in"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣


6、destination-in

这个操作与source-in相反,在源于目标重叠的区域保留目标。而不重叠的部分都变成透明的。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="destination-in"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣

7、source-out

在与目标不重叠的区域上绘制源,其他部分都变成透明的。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="source-out"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣

bubuko.com,布布扣


8、destination-out

在与源不重叠的区域上保留目标。其他部分都变成透明的。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="destination-out"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣

bubuko.com,布布扣

9、lighter

这个值与顺序无关,如果源与目标重叠,就将两者的颜色值想家。得到的颜色值的最大取值为255,结果就为白色。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="lighter"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣

bubuko.com,布布扣


10、copy

这个值与顺序无关,只绘制源,覆盖掉目标。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="copy"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣

11、xor

这个值与顺序无关,只绘制出不重叠的源与目标区域。所有重叠的部分都变成透明的

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			var canvas=document.getElementById("myCanvas");
			var context=canvas.getContext("2d");
			context.fillStyle="rgb(63,169,245)";
			context.fillRect(50,50,100,100);
			context.globalCompositeOperation="xor"
			context.fillStyle="rgb(255,123,172)";
			context.fillRect(100,100,100,100);
		});
		</script>
	</head>
	<body>
		<canvas id="myCanvas" width="1000px" height="1000px"></canvas>
	</body>
</html>
bubuko.com,布布扣

bubuko.com,布布扣








【HTML5】Canvas之globalCompositeOperation属性详解

标签:des   style   blog   http   io   ar   os   java   on   

原文地址:http://blog.csdn.net/laijieyao/article/details/41862473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!