canvas动态曲线

<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<canvas width="800" height="450"></canvas>
<script>
var context = document.getElementsByTagName('canvas')[0].getContext('2d');
var lastX = context.canvas.width*Math.random();
var lastY = context.canvas.height*Math.random();
var hue = 0;

//绘制线条
function line(){
    context.save();
    context.translate(context.canvas.width/2 , context.canvas.height/2);
    //缩放当前绘图
    context.scale(0.9,0.9);
    context.translate(-context.canvas.width/2 , -context.canvas.height/2);
    context.beginPath();
    context.lineWidth = 5+Math.random()*10;
    context.moveTo(lastX,lastY);
    lastX = context.canvas.width*Math.random();
    lastY = context.canvas.height*Math.random();
    context.bezierCurveTo(
        context.canvas.width*Math.random(),context.canvas.height*Math.random(),
        context.canvas.width*Math.random(),context.canvas.height*Math.random(),
        lastX,lastY
    );
    //随机色调
    hue = hue + 10*Math.random();
    context.strokeStyle = 'hsl('+hue+',50%,50%)';
    //给笔触加上阴影和高斯模糊,实现光晕的效果
    context.shadowColor = 'white';
    context.shadowBlur = 10;
    context.stroke();
    context.restore();    
}

setInterval(line,50);

//绘制背景
function background(){
    context.fillStyle = 'rgba(0,0,0,0.1)';
    context.fillRect(0,0,context.canvas.width,context.canvas.height);
    //通过每隔一段时间以半透明的黑色重绘整个画布,可以实现已绘制的线条渐隐的效果
}

setInterval(background,40);
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值