html5漂浮图片,HTML5 Canvas漂浮的幽灵球

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var canvas = document.getElementById('canvas');

ctx = canvas.getContext('2d'),

requestAnimationFrame = window.requestAnimationFrame ||

window.mozRequestAnimationFrame ||

window.webkitRequestAnimationFrame ||

window.msRequestAnimationFrame;

var WIDTH = document.documentElement.clientWidth,

HEIGHT = document.documentElement.clientHeight;

canvas.setAttribute("width", WIDTH);

canvas.setAttribute("height", HEIGHT);

var

body,

lines = [],

totalTentacles = 300;

function Line(x, y) {

this.x = WIDTH / 2;

this.y = HEIGHT / 2;

this.endAngle = Math.floor(Math.random() * 360);

this.endSpeed = (Math.floor(Math.random() * 10) + 1) / 50;

this.endDir = Math.floor(Math.random() * 2) == 0 ? -1 : 1;

this.endChangeFreq = Math.floor(Math.random() * 200) + 1;

this.c1Angle = Math.floor(Math.random() * 360);

this.c1Speed = (Math.floor(Math.random() * 10) + 1) / 20;

this.c1Dir = Math.floor(Math.random() * 2) == 0 ? -1 : 1;

this.c1ChangeFreq = Math.floor(Math.random() * 200) + 1;

this.c2Angle = Math.floor(Math.random() * 360);

this.c2Speed = (Math.floor(Math.random() * 10) + 1) / 20;

this.c2Dir = Math.floor(Math.random() * 2) == 0 ? -1 : 1;

this.c2ChangeFreq = Math.floor(Math.random() * 200) + 1;

this.c1;

this.end;

this.c2;

this.color = "rgba(43, 205, 255, .1)";

this.width = 1;

this.definePoints();

this.draw();

}

function easeInOutCubic(currentIteration, startValue, changeInValue, totalIterations) {

if ((currentIteration /= totalIterations / 2) < 1) {

return changeInValue / 2 * Math.pow(currentIteration, 3) + startValue;

}

return changeInValue / 2 * (Math.pow(currentIteration - 2, 3) + 2) + startValue;

}

Line.prototype.move = function() {

this.endChangeFreq--;

if (this.endChangeFreq == 0) {

this.endDir *= -1;

this.endChangeFreq = Math.floor(Math.random() * 200) + 1;

}

this.c1ChangeFreq--;

if (this.c1ChangeFreq == 0) {

this.c1Dir *= -1;

this.c1ChangeFreq = Math.floor(Math.random() * 200) + 1;

}

this.c2ChangeFreq--;

if (this.c2ChangeFreq == 0) {

this.c2Dir *= -1;

this.c2ChangeFreq = Math.floor(Math.random() * 200) + 1;

}

this.c1Angle = this.c1Angle + this.c1Dir * this.c1Speed;

this.c2Angle = this.c2Angle + this.c2Dir * this.c2Speed;

this.endAngle = this.endAngle + this.endDir * this.endSpeed;

this.definePoints();

this.draw();

}

Line.prototype.definePoints = function() {

this.c1 = aroundPoint(this.x, this.y, 100, this.c1Angle);

this.end = aroundPoint(this.x, this.y, 150, this.endAngle);

this.c2 = aroundPoint(this.end.x, this.end.y, 100, this.c2Angle);

}

Line.prototype.draw = function() {

ctx.beginPath();

ctx.moveTo(this.x, this.y);

ctx.bezierCurveTo(this.c1.x, this.c1.y, this.c2.x, this.c2.y, this.end.x, this.end.y);

ctx.strokeStyle = this.color;

ctx.lineWidth = this.width;

ctx.lineCap = 'round';

ctx.stroke();

ctx.closePath();

}

function aroundPoint(x, y, dist, ang) {

var point = [];

var angle = degToRad(ang);

point.x = x + Math.cos(angle) * dist;

point.y = y + Math.sin(angle) * dist;

return point;

}

function degToRad(deg) {

return deg * (Math.PI / 180);

}

function init() {

for (var i = 0; i <= totalTentacles - 1; i++) {

lines[lines.length] = new Line();

}

animate();

}

init();

function animate() {

ctx.clearRect(0, 0, WIDTH, HEIGHT);

ctx.shadowColor = "rgba(43, 205, 255, 1)";

ctx.shadowBlur = 10;

ctx.shadowOffsetX = 0;

ctx.shadowOffsetY = 0;

for (var i in lines) {

lines[i].move();

}

requestAnimationFrame(animate);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值