您好,登錄后才能下訂單哦!
小編給大家分享一下JavaScript中canvas如何實現流星特效,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
function easeInQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = x*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } //用平方根構建的緩慢減速運動 核心函數:x*x + 2*x function easeOutQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = -x*x + 2*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } function easeInoutQuad(curtime,begin,end,duration){ if(curtime<duration/2){ //前半段時間 return easeInQuad(curtime,begin,(begin+end)/2,duration/2);//改變量和時間都除以2 }else{ let curtime1 = curtime-duration/2; //注意時間要減去前半段時間 let begin1 = (begin+end)/2;//初始量要加上前半段已經完成的 return easeOutQuad(curtime1,begin1,end,duration/2);//改變量和時間都除以2 } }
function intervalOpcity (cur=0,start=0.3,end=0.8,dur=3) { if(fadeOpcity(cur,start,end,dur)===end){ cur = 0 let temp = start start = end end = temp } let opcity = fadeOpcity(cur,start,end,dur) let color = `rgba(0,0,0,${opcity})` clearBgcolor(wrap) newParticle (wrap,[x,y],r,color) cur=parseFloat(cur+0.1) setTimeout(()=>{intervalOpcity (cur,start,end,dur)},33) }
function intervalMove (speed,g=0) { if(g===720){ g = 360 } g = g+speed let nextPosition = movePosition (a,g,startPosition) x = nextPosition[0] y = nextPosition[1] clearBgcolor(wrap1) for(let i =1;i<=360;i++){ let g1 = g-i/2 if(g1<0&&g<=360){ break } let g1Position = movePosition (a,g1,startPosition) newParticle (wrap1,[g1Position[0],g1Position[1]],r/(1+i/5),`rgba(255,200,200,0.5)`) } setTimeout(()=>{intervalMove (speed,g)},33) }
function easeInQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = x*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } //用平方根構建的緩慢減速運動 核心函數:x*x + 2*x function easeOutQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = -x*x + 2*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } function easeInoutQuad(curtime,begin,end,duration){ if(curtime<duration/2){ //前半段時間 return easeInQuad(curtime,begin,(begin+end)/2,duration/2);//改變量和時間都除以2 }else{ let curtime1 = curtime-duration/2; //注意時間要減去前半段時間 let begin1 = (begin+end)/2;//初始量要加上前半段已經完成的 return easeOutQuad(curtime1,begin1,end,duration/2);//改變量和時間都除以2 } } function newCanvas (width,height) { let bodyEl = document.body let canvasEl = document.createElement("canvas") canvasEl.width = width canvasEl.height = height canvasEl.style.position = "absolute" bodyEl.append(canvasEl) let wrap = canvasEl.getContext("2d") return wrap } function setBgcolor (wrap,color) { wrap.fillStyle=color; wrap.fillRect(0,0,wrap.canvas.width,wrap.canvas.height); } function clearBgcolor(wrap){ wrap.clearRect(0,0,wrap.canvas.width,wrap.canvas.height) } function newParticle (wrap,position,r,color) { let x = position[0] let y = position[1] wrap.fillStyle=color; wrap.beginPath(); wrap.arc(x,y,r,0,2*Math.PI); wrap.shadowBlur=20; wrap.shadowColor=`rgba(255,255,255,0.8)`; wrap.fill(); } function fadeOpcity(cur,start,end,dur){ let opcity = parseFloat(easeInoutQuad(cur,start,end,dur).toFixed(2)) return opcity } let wrap0 = newCanvas (1000,800) let wrap2 = newCanvas (1000,800) let wrap = newCanvas (1000,800) let wrap1 = newCanvas (1000,800) setBgcolor (wrap0,"black") setBgcolor (wrap,"rgba(0,0,0,0)") setBgcolor (wrap1,"rgba(0,0,0,0)") setBgcolor (wrap2,"rgba(0,0,0,0)") let startPosition = [500,200] let r = 10 let a = -200 let x = a*(1-Math.cos(0))*Math.sin(0)+startPosition[0] let y = a*(1-Math.cos(0))*Math.cos(0)+startPosition[1] function movePosition (a,g,startPosition) { let t = Math.PI*2/360 let x = a*(1-Math.cos(g*t))*Math.sin(g*t)+startPosition[0] let y = a*(1-Math.cos(g*t))*Math.cos(g*t)+startPosition[1] return [x,y] } function intervalMove (speed,g=0) { if(g===720){ g = 360 } g = g+speed let nextPosition = movePosition (a,g,startPosition) x = nextPosition[0] y = nextPosition[1] clearBgcolor(wrap1) for(let i =1;i<=360;i++){ let g1 = g-i/2 if(g1<0&&g<=360){ break } let g1Position = movePosition (a,g1,startPosition) newParticle (wrap1,[g1Position[0],g1Position[1]],r/(1+i/5),`rgba(255,200,200,0.5)`) } setTimeout(()=>{intervalMove (speed,g)},33) } function intervalOpcity (cur=0,start=0.3,end=0.8,dur=3) { if(fadeOpcity(cur,start,end,dur)===end){ cur = 0 let temp = start start = end end = temp } let opcity = fadeOpcity(cur,start,end,dur) let color = `rgba(0,0,0,${opcity})` clearBgcolor(wrap) newParticle (wrap,[x,y],r,color) cur=parseFloat(cur+0.1) setTimeout(()=>{intervalOpcity (cur,start,end,dur)},33) } intervalOpcity () intervalMove (1)
1.可以使網頁具有交互性,例如響應用戶點擊,給用戶提供更好的體驗。 2.可以處理表單,檢驗用戶的輸入,并提供及時反饋節省用戶時間。 3.可以根據用戶的操作,動態的創建頁面。 4使用JavaScript可以通過設置cookie存儲在瀏覽器上的一些臨時信息。
看完了這篇文章,相信你對“JavaScript中canvas如何實現流星特效”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。