canvas画垂直渐变背景

html5不仅可以用css3绘制渐变背景,svg和canvas都可以做到,最近在我的一个项目中就使用到了canvas来绘制渐变背景。

<!doctype html>
<html>
<head>
	<meta charset="UTF-8">
	<link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<style>
		html,body{
			height: 100%;
			background: #545d93;
			overflow: hidden;
		}
		.btn-primary{
			width: 100%;
			background: #545d93;
			color: #fff;
		}
		.btn-primary:hover,.btn-primary:focus{
			color: #fff;
		}
		.btn-primary:hover,.btn-primary:active{
			background: #303F9F;
		}
		.logo{
			padding: 20px 0;
			color: #fff;
		}
		.box{
			background: #fff;
			border-radius: 2px;
			padding: 30px 20px;
		}
		.form-group{
			margin-bottom: 15px;
		}
		.form-group .form-control{
			height: 30px;
			padding: 4px 10px;
			border-radius: 3px;
			border-color: #ccc;
		}
		.form-group .form-control:focus{
			border-color: #545d93;
		}
		.form-group label{
			font-size: 12px;
		}
		.login-box{
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			top: 0;
			width: 300px;
			margin: 0 auto;
			padding-top: 120px;
		}
	</style>
	<title>LOGIN</title>
</head>
<body>
	<canvas id="canvas"></canvas>
	<section class="login-box">
		<h1 class="logo text-center">LOGO</h1>
		<div class="box">
			<div class="form-group">
				<label for="">用户名</label>
				<input type="text" class="form-control" placeholder="用户名">
			</div>
			<div class="form-group">
				<label for="">密码</label>
				<input type="password" class="form-control" placeholder="密码">
			</div>
			<button class="btn btn-primary">登录</button>
		</div>
	</section>

	<script>
	(function(){
		var width = window.innerWidth ,
			height = window.innerHeight;
		if(!canvas || !canvas.getContext) return false;

		canvas.setAttribute('width',width);
		canvas.setAttribute('height',height);
		var ctx = canvas.getContext('2d');
		var clg = ctx.createLinearGradient(0,0,0,height);
		clg.addColorStop(0,'#545d93'); // 第一个参数可以设定渐变位置 数字0~1之间
		clg.addColorStop(1,'#544D83');
		ctx.fillStyle = clg;
		ctx.fillRect(0,0,width,height);
	})();
	</script>
</body>
</html>

 

转载于:https://my.oschina.net/biejun/blog/756072

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值