canvas绘制爱心

无图不欢,先上图

<!DOCTYPE html>
<html>
<head>
	<meta name="keywords" content="风舞红枫,前端技术,canvas"/>
	<meta name="description" content="风舞红枫,前端技术,canvas,vue,react,node,个人博客"/>
	<meta charset="utf-8">
	<link rel="icon" href="../image/icon2.ico">
	<title>love you</title>
	<style type="text/css">
		html,body{
			height:100%;
			margin:0;
			padding:0;
			background-color:#111;
		}
		canvas{
			display:block;
		}
		.top{
			position:absolute;
			top:0;
			left:0;
		}
	</style>
</head>
<body>
<canvas></canvas>
<canvas class="top"></canvas>
</body>
<script type="text/javascript">
var width = document.body.clientWidth;
var height = document.body.clientHeight ;

var canvas = document.getElementsByTagName('canvas')[0];
var canvas1 = document.getElementsByTagName('canvas')[1];
canvas.width = width;
canvas.height = height;
canvas1.width = width;
canvas1.height = height;

var context = canvas.getContext('2d');
var context1 = canvas1.getContext('2d');

var Style = context.createLinearGradient(0,0,width,0);
Style.addColorStop(0,"Skyblue");
Style.addColorStop(1,"pink");
var Style1 = context1.createLinearGradient(0,0,width,0);
Style1.addColorStop(0,"Skyblue");
Style1.addColorStop(1,"pink");

var leftI = 2;
var rightI = 2;
var leftInterval = setInterval(function(){
	var obj = loveCurve(2*Math.PI*(leftI/60),10);
	heart(context,rand(6,8)/10,width/4,height/4,obj.x*1.4,obj.y*1.4,Style);
	leftI += 1;
	if(leftI == 29){
		leftI += 4;
	}
	if(leftI == 59){
		clearInterval(leftInterval);
		var rightInterval = setInterval(function(){
			var obj = loveCurve(2*Math.PI*(rightI/60),10);
			heart(context,rand(4,8)/10,width/4+200,height/4,obj.x*1.4,obj.y*1.4,Style);
			rightI += 1;
			if(rightI == 29){
				rightI += 4;
			}
			if(rightI == 59){
				clearInterval(rightInterval);
				arrow();
				
			}
		},20)
	}
},20)

var hotArr = [];
var text = "LOVE";
var hotInterval = setInterval(function(){
	if(rand(-19,19) == 1 && hotArr.length < 2){
		hotArr.push([2,rand(30,width-30),height,text[rand(0,text.length-1)]]);
	}
	context1.clearRect(0,0,width,height);
	context1.font="30px Verdana";
	for(var i = 0 ; i < hotArr.length ; i++){
		heart(context1,hotArr[i][0],hotArr[i][1],hotArr[i][2],0,0,Style1);
		context1.fillStyle = Style1;
		context1.fillRect(hotArr[i][1]+85,hotArr[i][2]+90,30,30);
		context1.fillStyle = "violet";
		context1.fillText(hotArr[i][3],hotArr[i][1]+90,hotArr[i][2]+115);
		hotArr[i][2] -= 3;
		if(hotArr[i][2] <= -20){
			hotArr.splice(i,1);
			i--;
		}
	}
},30)


function arrow(){
	context.beginPath();
	context.lineWidth = 4;
	context.strokeStyle = Style;
	context.lineCap = 'round';
	context.lineJoin = 'round';
	var arrowI = 0;
	var arrowX = width/4*3.4 - width/4*0.6;
	var arrowY = height/4*3 - height/4*1;
	var arrowInterval = setInterval(function(){
		context.lineTo(width/4*3.4-arrowX*arrowI/10,height/4*1+arrowY*arrowI/10);
		context.lineTo(width/4*3.4-arrowX*(arrowI+1)/10,height/4*1+arrowY*(arrowI+1)/10);
		context.stroke();
		arrowI += 1;
		if(arrowI == 10){
			clearInterval(arrowInterval);
			context.lineTo(width/4*0.6,height/4*3);
			context.lineTo(width/4*0.6+20,height/4*3-32);
			context.stroke();
			context.lineTo(width/4*0.6,height/4*3);
			context.lineTo(width/4*0.6+30,height/4*3+12);
			context.stroke();
		}
	},20)
	context.closePath();
}

function heart(context,r,scaleX,scaleY,x,y,color){
	context.beginPath();
	context.strokeStyle = color;
	context.fillStyle = color;
	for(var i = 0 ; i <= 360 ; i++){
		var obj = loveCurve(2*Math.PI*(i/360),r);
		context.lineTo(obj.x+scaleX+x,(obj.y+scaleY+y).toFixed(2));
		context.stroke();
	}
	context.fill();
	context.closePath();
}

//爱心曲线	t的取值范围是0到2*Math.PI。
function loveCurve(t,r){
	var obj = {
		x : 100 + r * (16 * Math.pow(Math.sin(t), 3)),
		y : 50 - r * (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t))
	}
	return obj;
}

function rand(n,m){
	var c = m - n + 1;
	return Math.floor(Math.random() * c + n);
}
</script>
</html>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风舞红枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值