canvas梦幻泡泡源码分享

1 篇文章 0 订阅

效果图如下


<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>
    <style>
    body{
    margin:0;
    }
    canvas{
    display:block;
   
    background-color:#222;
    }
    </style>
    <body>
    <canvas></canvas>
    </body>
</html>
<script>
 
//  var y = 100;
//  var canvas = document.querySelector('canvas');
//  var ctx = canvas.getContext('2d');
//  ctx.fillStyle = 'red';
// // 方法一 定时器  (非常消耗性能)
// //定时器 刷新频率为 1/60秒  代表60帧 刷新

// setInterval(function(){

// ctx.clearRect(0, 0, 1200, 800)


// // 清空 路径位置
// ctx.beginPath();
// //.arc(x坐标,y坐标,半径,其实坐标,旋转角度 true 顺时针 false 逆时针)
// ctx.arc(100,y,30,0,Math.PI*2,false);
// y++
// ctx.fill();
// }, 1000/60);

//利用 requestAnimationFrame()  根据浏览器刷新率刷新
// var canvas = document.querySelector('canvas');
// var ctx = canvas.getContext('2d');
// ctx.fillStyle = 'red';
// x = 10;
// function main(){
// ctx.clearRect(0, 0, 1200, 800)
// ctx.beginPath();
// ctx.arc(x,50,30,0,Math.PI*2,false);
// x++
// ctx.fill();


// requestAnimationFrame(main)
// }


// // 
// main()
Particle = {
Ball_config : function(x=10,y=10){// 构造 小球的函数
this.config = {};
this.config.x = x;//坐标x
this.config.y = y;//坐标y
this.config.r = 20;//半径大小
this.config.vx = random(-10,10);//坐标随机偏移x 的大小
this.config.vy = random(-10,10);//坐标随机偏移y 的大小
this.config.vr = random(-2,0);//随机缩小 半径大小
this.config.a  = 1;//初始值 透明度
this.config.va = 0.95;//以0.95倍的速度 透图案
this.colorList = ['#cc00ff','#ff0','#4caf50','#f60','#00f','#8bc34a','#e91e63'];//小球颜色的列表
this.config.color = this.colorList[Math.floor(random(0,7))];//随机挑选一个颜色 选中
return this.config;
},
update: function(item){// 更新 小球的状态
item.x += item.vx; //小球位置 递增
item.y += item.vy; //小球位置 递增
item.r += item.vr; //小球半径 递减
item.a *= item.va; //小球透明 递减
if(item.r < 0){
// 如果半径小于0 则等于0
item.r = 0;
}
},
draw:function(){//绘制小球
Ball.ctx.clearRect(0, 0, Ball.canvas.width, Ball.canvas.height);
//求对应的列表当中的每一球为 item
Ball.arrList.forEach((item) =>{
//清空历史路径
Ball.ctx.beginPath();
//颜色叠加在一起 变成白色
Ball.ctx.globalCompositeOperation = 'lightter'
//设置 透明度
Ball.ctx.globalAlpha = item.a;
//绘制小球
Ball.ctx.arc(item.x,item.y,item.r,0,Math.PI*2,false);
//添加样式颜色
Ball.ctx.fillStyle   = item.color;
Ball.ctx.fill();
Particle.update(item)
})


//如果小球的长度 超过200 删除最前面的第一个
if(Ball.arrList.length > 1) {
Ball.arrList.shift()
}
requestAnimationFrame(Particle.draw);//定时递归,每个浏览器刷新频率调用
}
}




Ball = {
arrList : [],
init:function(canvas){
this.canvas = canvas;
this.init_canvas_wh();
this.ctx = canvas.getContext('2d');//获取绘制环境
Ball.create(100,100);
this.mousemove_event();
Particle.draw();
},
init_canvas_wh : function(){
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
},
create : function (x = 100,y = 100){//添加小球到数组中
Ball.arrList.push(new Particle.Ball_config(x,y))
},
mousemove_event(){//移动事件绑定
this.canvas.addEventListener('mousemove',function(e) {
// console.log(e.clientX,e.clientY)
Ball.create(e.clientX,e.clientY);
})
}
}


/*
自定义随机函数
@param  int min 最小值
@param  int max 最大值
@return int     指定大小的一个随机数
*/
function random(min,max){
//Math.random() 返回0-1 中间的一个随机数
return (max-min)*Math.random()+min;
}




// 1.获取canvas dom元素
var canvas = document.querySelector('canvas');


Ball.init(canvas) // 初始化

// 监听  浏览器 宽度发生变化时 调用函数
// 动态调整 高度和宽度


window.onresize = function(){
Ball.init_canvas_wh();
}




</script>
<!-- 
课程笔记

canvas : 图片,画布上面的东西 ,都是有一个固定的尺寸
canvas.style.width 样式宽度
canvas.width       原始宽度
1. 动态修改页面的canves 高度的设定。
2.canvas 获取绘制环境 getContext('2d')
3.画圆操作


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值