canvas基础学习(二)-填充样式(线性渐变和径向渐变)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>线性渐变</title>
	</head>
	<body>
		<canvas id="canvas" style="border: 1px solid #aaa;display: block;margin: 50px auto;">
			你当前的浏览器不支持canvas,请更换浏览器再试。
		</canvas>
		
		<script>
			
			window.onload = function(){
				var canvas = document.getElementById("canvas");
				
				canvas.width = 800;
				canvas.height = 600;
				
				var context = canvas.getContext("2d");
				
				//var linearGrad = context.createLinearGradient(0, 0, 800, 0);//竖直渐变
				//var linearGrad = context.createLinearGradient(0, 0, 0, 600);//水平渐变
				var linearGrad = context.createLinearGradient(0, 0, 400, 300);//对角线渐变到(400,300)这个坐标
				linearGrad.addColorStop(0.0, 'white');
				linearGrad.addColorStop(0.25, 'yellow');
				linearGrad.addColorStop(0.5, 'green');
				linearGrad.addColorStop(0.75, 'blue');
				linearGrad.addColorStop(1.0, 'black');
				context.fillStyle = linearGrad;
				context.fillRect(0, 0, 800,600);
				
			}
		</script>
	</body>
</html>




<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>径向渐变</title>
	</head>
	<body>
		<canvas id="canvas" style="border: 1px solid #aaa;display: block;margin: 50px auto;">
			你当前的浏览器不支持canvas,请更换浏览器再试。
		</canvas>
		
		<script>
			
			window.onload = function(){
				var canvas = document.getElementById("canvas");
				
				canvas.width = 800;
				canvas.height = 600;
				
				var context = canvas.getContext("2d");
				
				//var linearGrad = context.createLinearGradient(0, 0, 800, 0);//竖直渐变
				//var linearGrad = context.createLinearGradient(0, 0, 0, 600);//水平渐变
				var radialGrad = context.createRadialGradient(400, 300, 0, 400, 300, 400);//对角线渐变到(400,300)这个坐标
				radialGrad.addColorStop(0.0, 'white');
				radialGrad.addColorStop(0.25, 'yellow');
				radialGrad.addColorStop(0.5, 'green');
				radialGrad.addColorStop(0.75, 'blue');
				radialGrad.addColorStop(1.0, 'black');
				context.fillStyle = radialGrad;
				context.fillRect(0, 0, 800,600);
				
			}
		</script>
	</body>
</html>




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/heye13/article/details/51535915
个人分类: canvas学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

canvas基础学习(二)-填充样式(线性渐变和径向渐变)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭