科幻旋转机械螺母动画效果
代码语言:html
所属分类:动画
代码描述:科幻旋转机械螺母动画效果,外加文字跳动
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
* {
margin: 0;
padding: 0;
}
canvas#canvas {
display: block;
background: #000;
}
</style>
</head>
<body translate="no">
<canvas id="canvas">Canvas not supported.</canvas>
<script>
(function () {
'use strict';
window.addEventListener('load', function () {
var canvas = document.getElementById('canvas');
if (!canvas || !canvas.getContext) {
return false;
}
/********************
Random Number
********************/
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/********************
Var
********************/
// canvas
var ctx = canvas.getContext('2d');
var X = canvas.width = window.innerWidth;
var Y = canvas.height = window.innerHeight;
var mouseX = X / 2;
var mouseY = Y / 2;
var color = 'rgb(71, 255, 255)';
/********************
Animation
********************/
window.requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (cb) {
setTimeout(cb, 17);
};
/********************
Text
********************/
var text = '#9';
var textSize = '56px';
function drawText() {
ctx.save();
ctx.fillStyle = color;
ctx.font = textSize + " impact";
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, X / 2, Y / 2);
ctx.restore();
}
/********************
Line
********************/
var linesNum = 5;
var lines = [];
function Line(ctx, x, y) {
this.ctx = ctx;
this.init(x, y);
}
Line.prototype.init = function (x, y) {
this.x = x;
this.y = y;
this.c = color;
this.l = rand(10, 50);
this.lw = 1;
this.v = {
x: rand(-5, 5) * Math.random(),
y: rand(-5, 5) * Math.random() };
};
Line.prototype.draw = function () {
var ctx = this.ctx;
ctx.save();
ctx.lineWidth = this.lw;
ctx.strokeStyle = this.c;
ctx.beginPath();
ctx.moveTo(0, this.y);
ctx.lineTo(X, this.y);
ctx.stroke();
ctx.lineWidth = this.lw;
ctx.beginPath();
ctx.moveTo(this.x, 0);
ctx.lineTo(this.x, Y);
ctx.stroke();
ctx.restore();
};
Line.prototype.updatePosition = function () {
this.x += this.v.x;
this.y += this.v.y;
};
Line.prototype.wrapPosition = function () {
if (this.x < 0) this.x = X;
if (this.x > X) this.x = 0;
if (this.y < 0) this.y = Y;
if (this.y > Y) this.y = 0;
};
Line.prototype.updateParams = function () {
this.l -= 0.1;
if (this.l < 0) {
this.v.x = rand(-5, 5) * Math.random();
this.v.y = rand(-5, 5) * Math.random();
this.l = rand(10, 50);
}
};
Line.prototype.render = function () {
this.updatePosition();
this.wrap.........完整代码请登录后点击上方下载按钮下载查看
网友评论0