前言
我们之前做过了一些好看的CSS+js动画网页,是闪电格子代码,还有粒子动态效果,我是第一次
旋转晃动,代码仅供大家参考谢谢大家!大佬可以先看一下,闪电格子代码现场效果图片在下方
它是高级动态效果。上代码!代码如何操作在这里不说了(白色背景版)
方法:新建一个文本文档,输入这些代码然后将它另存为把文件扩展名txt改成html,就可以了,十分炫酷!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>canvas背景动画二</title>
<style>
body {
background: #000;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas,
ctx,
width,
height,
size,
lines,
tick;
function line() {
this.path = [];
this.speed = rand(10, 20);
this.count = randInt(10, 30);
this.x = width / 2, +1;
this.y = height / 2 + 1;
this.target = {
x: width / 2,
y: height / 2
};
this.dist = 0;
this.angle = 0;
this.hue = tick / 6;
this.life = 1;
this.updateAngle();
this.updateDist();
}
line.prototype.step = function (i) {
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
this.updateDist();
if (this.dist < this.speed) {
this.x = this.target.x;
this.y = this.target.y;
this.changeTarget();
}
this.path.push({
x: this.x,
y: this.y
});
if (this.path.length > this.count) {
this.path.shift();
}
this.life -= 0.001;
if (this.life <= 0) {
this.path = null;
lines.splice(i, 1);
}
};
line.prototype.updateDist = function () {
var dx = this.target.x - this.x,
dy = this.target.y - this.y;
this.dist = Math.sqrt(dx * dx + dy * dy);
}
line.prototype.updateAngle = function () {
var dx = this.target.x - this.x,
dy = this.target.y - this.y;
this.angle = Math.atan2(dy, dx);
}
line.prototype.changeTarget = function () {
var randStart = randInt(0, 3);
switch (randStart) {
case 0: // up
this.target.y = this.y - size;
break;
case 1: // right
this.target.x = this.x + size;
break;
case 2: // down
this.target.y = this.y + size;
break;
case 3: // left
this.target.x = this.x - size;
}
this.updateAngle();
};
line.prototype.draw = function (i) {
ctx.beginPath();
var rando = rand(0, 10);
for (var j = 0, length = this.path.length; j < length; j++) {
ctx[(j === 0) ? 'moveTo' : 'lineTo'](this.path[j].x + rand(-rando, rando), this.path[j].y + rand(-rando,
rando));
}
ctx.strokeStyle = 'hsla(' + rand(this.hue, this.hue + 30) + ', 80%, 55%, ' + (this.life / 3) + ')';
ctx.lineWidth = rand(0.1, 2);
ctx.stroke();
};
function rand(min, max) {
return Math.random() * (max - min) + min;
}
function randInt(min, max) {
return Math.floor(min + Math.random() * (max - min + 1));
};
function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
size = 30;
lines = [];
reset();
loop();
}
function reset() {
width = Math.ceil(window.innerWidth / 2) * 2;
height = Math.ceil(window.innerHeight / 2) * 2;
tick = 0;
lines.length = 0;
canvas.width = width;
canvas.height = height;
}
function create() {
if (tick % 10 === 0) {
lines.push(new line());
}
}
function step() {
var i = lines.length;
while (i--) {
lines[i].step(i);
}
}
function clear() {
ctx.globalCompositeOperation = 'destination-out';
ctx.fillStyle = 'hsla(0, 0%, 0%, 0.1';
ctx.fillRect(0, 0, width, height);
ctx.globalCompositeOperation = 'lighter';
}
function draw() {
ctx.save();
ctx.translate(width / 2, height / 2);
ctx.rotate(tick * 0.001);
var scale = 0.8 + Math.cos(tick * 0.02) * 0.2;
ctx.scale(scale, scale);
ctx.translate(-width / 2, -height / 2);
var i = lines.length;
while (i--) {
lines[i].draw(i);
}
ctx.restore();
}
function loop() {
requestAnimationFrame(loop);
create();
step();
clear();
draw();
tick++;
}
function onresize() {
reset();
}
window.addEventListener('resize', onresize);
init();
</script>
</body>
</html>
2.粒子变换效果(高级)
这里用到了动态粒子效果模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态粒子效果</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<style>
html,body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.container{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-color: #000000;
}
</style>
</head>
<body>
<div id="jsi-particle-container" class="container"></div>
<script>
var RENDERER = {
PARTICLE_COUNT : 1000,
PARTICLE_RADIUS : 1,
MAX_ROTATION_ANGLE : Math.PI / 60,
TRANSLATION_COUNT : 500,
init : function(strategy){
this.setParameters(strategy);
this.createParticles();
this.setupFigure();
this.reconstructMethod();
this.bindEvent();
this.drawFigure();
},
setParameters : function(strategy){
this.$window = $(window);
this.$container = $('#jsi-particle-container');
this.width = this.$container.width();
this.height = this.$container.height();
this.$canvas = $('<canvas />').attr({width : this.width, height : this.height}).appendTo(this.$container);
this.context = this.$canvas.get(0).getContext('2d');
this.center = {x : this.width / 2, y : this.height / 2};
this.rotationX = this.MAX_ROTATION_ANGLE;
this.rotationY = this.MAX_ROTATION_ANGLE;
this.strategyIndex = 0;
this.translationCount = 0;
this.theta = 0;
this.strategies = strategy.getStrategies();
this.particles = [];
},
createParticles : function(){
for(var i = 0; i < this.PARTICLE_COUNT; i ++){
this.particles.push(new PARTICLE(this.center));
}
},
reconstructMethod : function(){
this.setupFigure = this.setupFigure.bind(this);
this.drawFigure = this.drawFigure.bind(this);
this.changeAngle = this.changeAngle.bind(this);
},
bindEvent : function(){
this.$container.on('click', this.setupFigure);
this.$container.on('mousemove', this.changeAngle);
},
changeAngle : function(event){
var offset = this.$container.offset(),
x = event.clientX - offset.left + this.$window.scrollLeft(),
y = event.clientY - offset.top + this.$window.scrollTop();
this.rotationX = (this.center.y - y) / this.center.y * this.MAX_ROTATION_ANGLE;
this.rotationY = (this.center.x - x) / this.center.x * this.MAX_ROTATION_ANGLE;
},
setupFigure : function(){
for(var i = 0, length = this.particles.length; i < length; i++){
this.particles[i].setAxis(this.strategies[this.strategyIndex]());
}
if(++this.strategyIndex == this.strategies.length){
this.strategyIndex = 0;
}
this.translationCount = 0;
},
drawFigure : function(){
requestAnimationFrame(this.drawFigure);
this.context.fillStyle = 'rgba(0, 0, 0, 0.2)';
this.context.fillRect(0, 0, this.width, this.height);
for(var i = 0, length = this.particles.length; i < length; i++){
var axis = this.particles[i].getAxis2D(this.theta);
this.context.beginPath();
this.context.fillStyle = axis.color;
this.context.arc(axis.x, axis.y, this.PARTICLE_RADIUS, 0, Math.PI * 2, false);
this.context.fill();
}
this.theta++;
this.theta %= 360;
for(var i = 0, length = this.particles.length; i < length; i++){
this.particles[i].rotateX(this.rotationX);
this.particles[i].rotateY(this.rotationY);
}
this.translationCount++;
this.translationCount %= this.TRANSLATION_COUNT;
if(this.translationCount == 0){
this.setupFigure();
}
}
};
var STRATEGY = {
SCATTER_RADIUS :150,
CONE_ASPECT_RATIO : 1.5,
RING_COUNT : 5,
getStrategies : function(){
var strategies = [];
for(var i in this){
if(this[i] == arguments.callee || typeof this[i] != 'function'){
continue;
}
strategies.push(this[i].bind(this));
}
return strategies;
},
createSphere : function(){
var cosTheta = Math.random() * 2 - 1,
sinTheta = Math.sqrt(1 - cosTheta * cosTheta),
phi = Math.random() * 2 * Math.PI;
return {
x : this.SCATTER_RADIUS * sinTheta * Math.cos(phi),
y : this.SCATTER_RADIUS * sinTheta * Math.sin(phi),
z : this.SCATTER_RADIUS * cosTheta,
hue : Math.round(phi / Math.PI * 30)
};
},
createTorus : function(){
var theta = Math.random() * Math.PI * 2,
x = this.SCATTER_RADIUS + this.SCATTER_RADIUS / 6 * Math.cos(theta),
y = this.SCATTER_RADIUS / 6 * Math.sin(theta),
phi = Math.random() * Math.PI * 2;
return {
x : x * Math.cos(phi),
y : y,
z : x * Math.sin(phi),
hue : Math.round(phi / Math.PI * 30)
};
},
createCone : function(){
var status = Math.random() > 1 / 3,
x,
y,
phi = Math.random() * Math.PI * 2,
rate = Math.tan(30 / 180 * Math.PI) / this.CONE_ASPECT_RATIO;
if(status){
y = this.SCATTER_RADIUS * (1 - Math.random() * 2);
x = (this.SCATTER_RADIUS - y) * rate;
}else{
y = -this.SCATTER_RADIUS;
x = this.SCATTER_RADIUS * 2 * rate * Math.random();
}
return {
x : x * Math.cos(phi),
y : y,
z : x * Math.sin(phi),
hue : Math.round(phi / Math.PI * 30)
};
},
createVase : function(){
var theta = Math.random() * Math.PI,
x = Math.abs(this.SCATTER_RADIUS * Math.cos(theta) / 2) + this.SCATTER_RADIUS / 8,
y = this.SCATTER_RADIUS * Math.cos(theta) * 1.2,
phi = Math.random() * Math.PI * 2;
return {
x : x * Math.cos(phi),
y : y,
z : x * Math.sin(phi),
hue : Math.round(phi / Math.PI * 30)
};
}
};
var PARTICLE = function(center){
this.center = center;
this.init();
};
PARTICLE.prototype = {
SPRING : 0.01,
FRICTION : 0.9,
FOCUS_POSITION : 300,
COLOR : 'hsl(%hue, 100%, 70%)',
init : function(){
this.x = 0;
this.y = 0;
this.z = 0;
this.vx = 0;
this.vy = 0;
this.vz = 0;
this.color;
},
setAxis : function(axis){
this.translating = true;
this.nextX = axis.x;
this.nextY = axis.y;
this.nextZ = axis.z;
this.hue = axis.hue;
},
rotateX : function(angle){
var sin = Math.sin(angle),
cos = Math.cos(angle),
nextY = this.nextY * cos - this.nextZ * sin,
nextZ = this.nextZ * cos + this.nextY * sin,
y = this.y * cos - this.z * sin,
z = this.z * cos + this.y * sin;
this.nextY = nextY;
this.nextZ = nextZ;
this.y = y;
this.z = z;
},
rotateY : function(angle){
var sin = Math.sin(angle),
cos = Math.cos(angle),
nextX = this.nextX * cos - this.nextZ * sin,
nextZ = this.nextZ * cos + this.nextX * sin,
x = this.x * cos - this.z * sin,
z = this.z * cos + this.x * sin;
this.nextX = nextX;
this.nextZ = nextZ;
this.x = x;
this.z = z;
},
rotateZ : function(angle){
var sin = Math.sin(angle),
cos = Math.cos(angle),
nextX = this.nextX * cos - this.nextY * sin,
nextY = this.nextY * cos + this.nextX * sin,
x = this.x * cos - this.y * sin,
y = this.y * cos + this.x * sin;
this.nextX = nextX;
this.nextY = nextY;
this.x = x;
this.y = y;
},
getAxis3D : function(){
this.vx += (this.nextX - this.x) * this.SPRING;
this.vy += (this.nextY - this.y) * this.SPRING;
this.vz += (this.nextZ - this.z) * this.SPRING;
this.vx *= this.FRICTION;
this.vy *= this.FRICTION;
this.vz *= this.FRICTION;
this.x += this.vx;
this.y += this.vy;
this.z += this.vz;
return {x : this.x, y : this.y, z : this.z};
},
getAxis2D : function(theta){
var axis = this.getAxis3D(),
scale = this.FOCUS_POSITION / (this.FOCUS_POSITION + axis.z);
return {x : this.center.x + axis.x * scale, y : this.center.y - axis.y * scale, color : this.COLOR.replace('%hue', this.hue + theta)};
}
};
$(function(){
RENDERER.init(STRATEGY);
});
</script>
</body>
</html>