1.气泡事件-随机位置/颜色梦幻全屏
1.less
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
overflow: hidden;
body {
height: 100%;
#wrap {
overflow: hidden;
height: 600px;
width: 600px;
position: absolute;
background: skyblue;
border: 10px solid steelblue;
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
box-shadow: 2px 5px 5px #000000;
h1 {
position: absolute;
text-align: center;
color: tomato;
top: 10%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
}
h2 {
position: absolute;
float: right;
margin-right: 10%;
color: white;
top: 20%;
right: -15%;
transform: translate3d(-50%, -50%, 0);
}
#wrap-canvas {
position: absolute;
top: 61%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
background: tomato;
}
}
}
}
3.js
$(function() {
var canvasNode = document.querySelector("#wrap-canvas");
canvasNode.width = 550;
canvasNode.height = 450;
if(canvasNode.getContext) {
var canvasPen = canvasNode.getContext("2d");
var positionArr = [];
setInterval(function() {
var x = Math.random() * canvasNode.width;
var y = Math.random() * canvasNode.height;
var radius = 10;
var r=Math.round(Math.random() *255);
var g=Math.round(Math.random() *255);
var b=Math.round(Math.random() *255);
var a=1;
positionArr.push({
x: x,
y: y,
radius: radius,
r:r,
g:g,
b:b,
a:a
});
}, 50);
setInterval(function() {
canvasPen.clearRect(0, 0, canvasNode.width, canvasNode.height);
console.log(positionArr);
for(var j=0;j<positionArr.length;j++){
if(positionArr[j].a<=0){
positionArr.splice(j,1);
}
positionArr[j].radius++;
positionArr[j].a-=0.01;
}
for(var i = 0; i < positionArr.length; i++) {
canvasPen.save();
canvasPen.fillStyle = "rgba("+positionArr[i].r+","+positionArr[i].g+","+positionArr[i].b+","+positionArr[i].a+")";
canvasPen.beginPath();
canvasPen.arc(positionArr[i].x, positionArr[i].y, positionArr[i].radius, 0, 2 * Math.PI);
canvasPen.fill();
canvasPen.restore();
}
},1000/60);
}
})
4.html
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
<title>canvas动画气泡-bubbletitle>
<link rel="stylesheet" href="css/4-canvas-bubble.css" />
head>
<body>
<div id="wrap">
<h1>canvas动画气泡-bubbleh1>
<h2>PS:一刀coderh2>
<canvas id="wrap-canvas">canvas>
div>
body>
<script type="text/javascript" src="../../6-bootstrap/js/jquery.min.js">script>
<script type="text/javascript" src="js/4-canvas-bubble.js">script>
html>
5.效果图
![canvas
2.彩色气泡事件-固定位置向上浮动
1.js更新
$(function() {
var canvasNode = document.querySelector("#wrap-canvas");
canvasNode.width = 550;
canvasNode.height = 450;
if(canvasNode.getContext) {
var canvasPen = canvasNode.getContext("2d");
var positionArr = [];
setInterval(function() {
var radius = Math.random() * 6 + 2;
var x = Math.random() * canvasNode.width;
var y = canvasNode.height - radius;
var r = Math.round(Math.random() * 255);
var g = Math.round(Math.random() * 255);
var b = Math.round(Math.random() * 255);
var a = 1;
var deg = 0;
var initialX = x;
var initialY = y;
var effect=Math.random()*20+10;
positionArr.push({
x: x,
y: y,
radius: radius,
r: r,
g: g,
b: b,
a: a,
deg: deg,
initialX: initialX,
initialY: initialY,
effect: effect
});
}, 50);
setInterval(function() {
canvasPen.clearRect(0, 0, canvasNode.width, canvasNode.height);
console.log(positionArr);
for(var j = 0; j < positionArr.length; j++) {
positionArr[j].deg+=7;
positionArr[j].x = positionArr[j].initialX + Math.sin(positionArr[j].deg * Math.PI / 180) * positionArr[j].effect * 2;
positionArr[j].y = positionArr[j].initialY - (positionArr[j].deg * Math.PI / 180) * positionArr[j].effect ;
if(positionArr[j].y<=50){
positionArr.splice(j,1);
}
}
for(var i = 0; i < positionArr.length; i++) {
canvasPen.save();
canvasPen.fillStyle = "rgba(" + positionArr[i].r + "," + positionArr[i].g + "," + positionArr[i].b + "," + positionArr[i].a + ")";
canvasPen.beginPath();
canvasPen.arc(positionArr[i].x, positionArr[i].y, positionArr[i].radius, 0, 2 * Math.PI);
canvasPen.fill();
canvasPen.restore();
}
}, 1000 / 60);
}
})
2.效果图