js實(shí)現(xiàn)旋轉(zhuǎn)的星空效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)旋轉(zhuǎn)的星空效果的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="keywords" content="starry sky"> <meta name="description" content="the starry sky"> <title>旋轉(zhuǎn)的星空(the starry sky)</title> <style> body { margin: 0; padding: 0; overflow: hidden; } #canvas { position: absolute; left: 0; } #starCanvasDiv { background-color: white; } </style> </head> <body> <canvas id="canvas">Your browser can not support canvas</canvas> <script> var doublePI = Math.PI * 2; var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cx,cy; var starCanvas; var alphaChangeProbability = new AlphaChangeProbability(); //色相 var hue = 217; //星空背景顏色 var bgColor = 'hsl(' + hue + ', 60%, 5%)'; //畫布的外切圓半徑 var canvasRadius; //每旋轉(zhuǎn)一圈要需要的幀數(shù) var radianStepCount; //星星的總個(gè)數(shù) var starCount; //群星 var stars; function onResize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; cx = canvas.width / 2; cy = canvas.height / 2; canvasRadius = Math.sqrt(Math.pow(canvas.width, 2) + Math.pow(canvas.height, 2)) / 2; radianStepCount = canvasRadius * 100; starCount = parseInt((canvas.width + canvas.height) * 0.5); stars = []; for(var i=0; i<starCount; i++) { stars.push(new Star()); } //初始時(shí)要先繪制一層背景,否則第一波星星走過(guò)的路徑會(huì)有畫布底料涂抹不均勻的感覺 ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 1; ctx.fillStyle = bgColor; ctx.fillRect(0, 0, canvas.width, canvas.height); } function init() { createStarCanvas(); window.addEventListener("resize", onResize); onResize(); loop(); } //在[min, max)范圍內(nèi)隨機(jī)一個(gè)整數(shù) function randomInt(min, max) { if(arguments.length === 1) { max = min; min = 0; } else if(min > max) { var tmp = max; mix = min; min = tmp; } return Math.floor(Math.random() * (max - min) + min); } //透明度改變的概率 function AlphaChangeProbability() { //透明度改變的步長(zhǎng) var alphaStep = 0.05; //透明度增加 var plus = 1; //透明度減少 var minus = 1; //透明度不變 var invariant = 8; //總概率 var totalChance = plus + minus + invariant; //獲取隨機(jī)的透明度改變值 function getRandomChangeValue() { var value = Math.random() * totalChance; if(value < plus) { return alphaStep; } value -= plus; if(value < minus) { return -alphaStep; } return 0; } //隨機(jī)改變alpha的值 this.randomChangeAlpha = function(alpha) { alpha += getRandomChangeValue(); if(alpha > 1) { alpha = 1; } else if(alpha < 0) { alpha = 0; } return alpha; } } //創(chuàng)建星星圖片 function createStarCanvas() { starCanvas = document.createElement("canvas"); var ctx = starCanvas.getContext("2d"); starCanvas.width = 100; starCanvas.height = 100; var cx = starCanvas.width / 2; var cy = starCanvas.height / 2; var radius = Math.max(cx, cy); var gradient = ctx.createRadialGradient(cx, cy, 0, cx, cy, radius); gradient.addColorStop(0.025, '#CCC'); gradient.addColorStop(0.1, 'hsl(' + hue + ', 65%, 35%)'); gradient.addColorStop(0.25, bgColor); gradient.addColorStop(1, "transparent"); ctx.fillStyle = gradient; ctx.beginPath(); ctx.arc(cx, cy, radius, 0, doublePI); ctx.fill(); } //星體對(duì)象 var Star = function() { //星體運(yùn)行的軌道半徑 this.orbitRadius = Math.random() * canvasRadius; //星體的半徑 this.radius = randomInt(60, this.orbitRadius) / 8; //星體透明度 this.alpha = Math.random(); //相對(duì)軌道中心(即畫布中心)的角度 this.theta = Math.random() * doublePI; //角速度 this.speed = Math.random() * this.orbitRadius / radianStepCount; //獲取當(dāng)前坐標(biāo) this.getPos = function() { return { x: cx + this.orbitRadius * Math.cos(this.theta), y: cy + this.orbitRadius * Math.sin(this.theta) } } } Star.prototype.update = function() { this.alpha = alphaChangeProbability.randomChangeAlpha(this.alpha); this.theta += this.speed; this.pos = this.getPos(); } Star.prototype.draw = function() { ctx.globalAlpha = this.alpha; ctx.drawImage(starCanvas, this.pos.x, this.pos.y, this.radius, this.radius); } function loop() { ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 0.5; ctx.fillStyle = bgColor; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.globalCompositeOperation = "lighter"; for(var i = 0; i < stars.length; i++) { stars[i].update(); stars[i].draw(); } //繪制星體圖樣 ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 1; ctx.fillStyle = "white"; ctx.fillRect(0, 0, starCanvas.width, starCanvas.height); ctx.drawImage(starCanvas, 0, 0, starCanvas.width, starCanvas.height); requestAnimationFrame(loop); } init(); </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實(shí)現(xiàn)隨機(jī)抽選效果、隨機(jī)抽選紅色球效果
本文主要分享了js實(shí)現(xiàn)隨機(jī)抽選效果、隨機(jī)抽選紅色球效果的示例代碼。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01用Div仿showModalDialog模式菜單的效果的代碼
用Div仿showModalDialog模式菜單的效果的代碼...2007-03-03JS代碼屏蔽F12,右鍵,粘貼,復(fù)制,剪切,選中,操作實(shí)例
在本篇文章里小編給大家分享的是關(guān)于利用JS代碼屏蔽F12,右鍵,粘貼,復(fù)制,剪切,選中,操作,需要的朋友們學(xué)習(xí)下。2019-09-09JavaScript使用canvas實(shí)現(xiàn)錨點(diǎn)摳圖功能
在日常的圖片處理中,我們經(jīng)常會(huì)遇到需要摳圖的情況,無(wú)論是為了美化照片、制作海報(bào),還是進(jìn)行圖片合成,摳圖對(duì)于我們來(lái)說(shuō)也是一種很常用的功能了,今天就讓我們一起來(lái)看下怎么使用canvas來(lái)實(shí)現(xiàn)一個(gè)錨點(diǎn)摳圖功能2024-03-03如何解決js函數(shù)防抖、節(jié)流出現(xiàn)的問(wèn)題
這篇文章主要介紹了如何解決js函數(shù)防抖、節(jié)流出現(xiàn)的問(wèn)題。SyntheticEvent對(duì)象是通過(guò)合并得到的。 這意味著在事件回調(diào)被調(diào)用后,SyntheticEvent 對(duì)象將被重用并且所有屬性都將被取消。 因此,您無(wú)法以異步方式訪問(wèn)該事件。,需要的朋友可以參考下2019-06-06詳解webpack之圖片引入-增強(qiáng)的file-loader:url-loader
這篇文章主要介紹了詳解webpack之圖片引入-增強(qiáng)的file-loader:url-loader,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10