js实现新年倒计时点击放烟花快乐代码
代码语言:html
所属分类:其他
代码描述:js实现新年倒计时点击放烟花快乐代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> @import url("https://fonts.googleapis.com/css?family=Caveat"); </style> <style> html, body { margin: 0; padding: 0; overflow: hidden; background: #000; background-image: url(//repo.bfw.wiki/bfwrepo/image/604a26e01dd36.png) ; background-repeat: no-repeat; background-size: cover; } canvas { mix-blend-mode: lighten; cursor: pointer; } #hero { display: inline; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); mix-blend-mode: color-dodge; } #year { font-size: 30vw; color: #d0d0d0; font-weight: bold; } #timeleft { color: #fbfbfb; font-size: 2.5vw; text-align: center; } </style> </head> <body> <div id="hero"> <div id="year">2022</div> <div id="timeleft"></div> </div> <script> var vector = { _x: 1, _y: 0, create: function(x, y) { var obj = Object.create(this); obj.setX(x); obj.setY(y); return obj; }, duplicate: function() { return vector.create(this._x, this._y); }, random: function(maxX, maxY, minX, minY){ var x = Math.random() * (maxX-minX) + minX; var y = Math.random() * (maxY-minY) + minY; return vector.create(x,y); }, setX: function(value) { this._x = value; }, getX: function() { return this._x; }, setY: function(value) { this._y = value; }, getY: function() { return this._y; }, bounds: function(size, mX, mY){ var mapsize = size; if(this._x > mX + mapsize){ this._x = -size; } if(this._x < -mapsize){ this._x = mX + size; } if(this._y > mY + mapsize){ this._y = -size; } if(this._y < -mapsize){ this._y = mY + size; } }, outOfBounds: function(sx, sy, ex, ey){ if(this._x < sx){ return true; } if(this._x > ex){ return true; } if(this._y < sy){ return true; } if(this._y > ey){ return true; } }, setAngle: function(angle) { var length = this.getLength(); this._x = Math.cos(angle) * length; this._y = Math.sin(angle) * length; }, getAngle: function() { return Math.atan2(this._y, this._x); }, angleTo: function(p2) { return Math.atan2(p2.getY() - this._y, p2.getX() - this._x); }, distanceTo: function(p2) { var dx = p2.getX() - this._x, dy = p2.getY() - this._y; return Math.sqrt(dx * dx + dy * dy); }, setLength: function(length) { var angle = this.getAngle(); this._x = Math.cos(angle) * length; this._y = Math.sin(angle) * length; }, gravitateTo: function(p2, mass, mymass) { var grav = Vector.create(0, 0), dist = mypos.distanceTo(p2); //grav.setLength(mass / (dist * dist)); grav.setLength((G*mass*mymass) / (dist * dist)); grav.setAngle(this.angleTo(p2)); this.addTo(grav); }, getLength: function() { return Math.sqrt(this._x * this._x + this._y * this._y); }, maxSpeed: function(s){ if(this.getLength() > s){ this.setLength(s); } }, add: function(v2) { return vector.create(this._x + v2.getX(), this._y + v2.getY()); }, subtract: function(v2) { return vector.create(this._x - v2.getX(), this._y - v2.getY()); }, multiply: function(val) { return vector.create(this._x * val, this._y * val); }, divide: function(val) { return vector.create(this._x / val, this._y / val); }, addTo: function(v2) { this._x += v2.getX(); this._y += v2.getY(); }, subtractFrom: function(v2) { this._x -= v2.getX(); this._y -= v2.getY(); }, multiplyBy: function(val) { this._x *= val; this._y *= val; }, divideBy: function(val) { this._x /= val; this._y /= val; }, normalize: function(scale){ var norm = Math.sqrt(this._x * this._x + this._y * this._y); if (norm != 0) { return vector.create( scale * this._x / norm, scale * this._y / norm ); } }, dot: function(v2){ return this._x * v2._x + this._y * v2._y; } }; const canvas = document.createElement('canvas'), context = canvas.getContext('2d'), width = canvas.width = window.innerWidth, height = canvas.height = window.innerHeight, HalfPI = Math.PI / 2, gravity = vector.create(0, 0.35), year = document.getElementById('year'), timeleft = document.getElementById('timeleft'), newyear = new Date('01/01/2022'); let objects = [], startFireworks = false, newYearAlready = false; function renderTimeLeft() { let date = new Date(); let delta = Math.abs(newyear - date) / 1000; let hours = Math.floor(delta / 3600) % 24; delta -= hours * 3600; let minutes = Math.floor(delta / 60) % 60; delta -= minutes * 60; let seconds = Math.floor(delta % 60) + 1; let string = ''; let DaysLeft = Math.floor((newyear - date) / (1000 * 60 * 60 * 24)), HoursLeft = `${hours} ${hours > 1 ? 'hours' : 'hour'}`, MinutesLeft = `${minutes} ${minutes > 1 ? 'minutes' : 'minute'}`, SecondsLeft = `${seconds} ${seconds > 1 ? 'seconds' : 'second'}`; if (hours > 0) string = `${HoursLeft} & ${MinutesLeft}`;else if (minutes > 0) string = `${MinutesLeft} & ${SecondsLeft}`;else string = `${SecondsLeft}`; if (DaysLeft > 0) str.........完整代码请登录后点击上方下载按钮下载查看
网友评论0