原生js实现万圣节快乐动画效果代码
代码语言:html
所属分类:动画
代码描述:原生js实现万圣节快乐动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
overflow: hidden;
}
body {
position: relative;
background: #1a2a6c;
background: -webkit-linear-gradient(to top, #fdbb2d, #b21f1f, #1a2a6c);
background: linear-gradient(to top, #fdbb2d, #b21f1f, #1a2a6c);
}
</style>
</head>
<body translate="no" >
<script >
/*
* File Name / halloween.js
* Created Date / Oct 13, 2020
* Aurhor / Toshiya Marukubo
* Twitter / https://twitter.com/toshiyamarukubo
*/
/*
Common Tool.
*/
class Tool {
// random number.
static randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
// random color rgb.
static randomColorRGB() {
return (
"rgb(" +
this.randomNumber(0, 255) +
", " +
this.randomNumber(0, 255) +
", " +
this.randomNumber(0, 255) +
")");
}
// random color hsl.
static randomColorHSL(saturation, lightness) {
return (
"hsl(" +
this.randomNumber(0, 360) +
", " +
saturation +
"%, " +
lightness +
"%)");
}
// gradient color.
static gradientColor(ctx, cr, cg, cb, ca, x, y, r) {
const col = cr + "," + cg + "," + cb;
const g = ctx.createRadialGradient(x, y, 0, x, y, r);
g.addColorStop(0, "rgba(" + col + ", " + ca * 1 + ")");
g.addColorStop(0.5, "rgba(" + col + ", " + ca * 0.5 + ")");
g.addColorStop(1, "rgba(" + col + ", " + ca * 0 + ")");
return g;
}}
/*
When want to use angle.
*/
class Angle {
constructor(angle) {
this.a = angle;
this.rad = this.a * Math.PI / 180;
}
incDec(num) {
this.a += num;
this.rad = this.a * Math.PI / 180;
return this.rad;
}}
/*
When want to use controller.
*/
class Controller {
constructor(id) {
this.id = document.getElementById(id);
}
getVal() {
return this.id.value;
}}
/*
When want to use time.
*/
class Time {
constructor(time) {
this.startTime = time;
this.lastTime;
this.elapsedTime;
}
getElapsedTime() {
this.lastTime = Date.now();
this.elapsedTime = (this.startTime - this.lastTime) * -1;
return this.elapsedTime;
}}
let canvas;
class Canvas {
constructor(bool) {
// create canvas.
this.canvas = document.createElement("canvas");
// if on screen.
if (bool === true) {
this.canvas.style.display = 'block';
this.canvas.style.top = 0;
this.canvas.style.left = 0;
document.getElementsByTagName("body")[0].appendChild(this.canvas);
}
this.ctx = this.canvas.getContext("2d");
this.width = this.canvas.width = window.innerWidth;
this.height = this.canvas.height = window.innerHeight;
this.standard = this.width > this.height ? this.width : this.height * 1.5;
// mouse infomation.
this.mouseX = this.width / 2;
this.mouseY = this.height / 10;
this.mouseZ = null;
// ground
this.ground;
this.groundHeight = 100;
// grave
this.graves = [];
this.graveSize = this.standard / 50;
this.graveDist = 10;
this.graveTime = null;
this.graveBehavior = 1;
// pumpkin
this.pumpkin;
this.pumpkinSize = this.standard / 9;
this.pumpkinTime = null;
this.pumpkinBehavior = 1;
// text
this.text;
this.sentence = this.width > this.height ? 'HAPPY HALLOWEEN' : 'HALLOWEEN';
this.textSize = this.standard / 4;
this.fontSize = this.standard / 25;
this.textTime = null;
this.textBehavior = null;
// house
this.houses = [];
this.houseSize = this.standard / 10;
this.houseNum = 3;
this.houseTime = null;
this.houseBehavior = null;
// spider
this.spiders = [];
this.spiderSize = this.standard / 80;
this.spiderNum = 3;
this.spiderTime = null;
this.spiderBehavior = null;
// shooting star
this.pointX = this.width + 100;
this.pointY = Tool.randomNumber(0, this.height / 10);
this.starNum = 300;
this.stars = [];
this.starBehavior = null;
.........完整代码请登录后点击上方下载按钮下载查看
















网友评论0