目錄
2018
首頁 web前端 js教程 canvas如何做出黑色背景的青色煙火

canvas如何做出黑色背景的青色煙火

Mar 13, 2018 pm 04:07 PM
canvas 背景 黑色

這次為大家帶來canvas怎樣做出黑色背景的青色煙火,canvas做出黑色背景的青色煙火的注意事項有哪些,下面就是實戰案例,一起來看一下。

canvas如何做出黑色背景的青色煙火

html

<canvas></canvas><h1 id="span-span">201<span>8</span></h1>
登入後複製

css

html,body {    padding: 0px;    margin: 0px;    background: #222;    font-family: &#39;Karla&#39;, sans-serif;    color: #FFF;    height: 100%;    overflow: hidden;
}h1 {    z-index: 1000;    position: fixed;    top: 50%;    left: 50%;    transform: translateX(-50%) translateY(-100%);    font-size: 58px;    overflow: hidden;
}span {    position: relative;    display: inline-block;    animation: drop 0.75s ease 0s;
}canvas {    width: 100%;    height: 100%;
}
@keyframes drop {
    0% {        transform: translateY(-100px);        opacity: 0;
    }
    90% {        opacity: 1;        transform: translateY(10px);
    }
    100% {        transform: translateY(0px);
    }
}
登入後複製

js

var ctx = document.querySelector(&#39;canvas&#39;).getContext(&#39;2d&#39;)ctx.canvas.width = window.innerWidthctx.canvas.height = window.innerHeightvar sparks = []var fireworks = []var i = 20;while (i--) {    fireworks.push(        new Firework(Math.random() * window.innerWidth, window.innerHeight * Math.random())    )}render()function render() {    setTimeout(render, 1000 / 60)    ctx.fillStyle = &#39;rgba(0, 0, 0, 0.1)&#39;;    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)    for (var firework of fireworks) {        if (firework.dead) continue        firework.move()        firework.draw()    }    for (var spark of sparks) {        if (spark.dead) continue        spark.move()        spark.draw()    }    if (Math.random() < 0.05) {        fireworks.push(new Firework())    }}function Spark(x, y, color) {    this.x = x    this.y = y    this.dir = Math.random() * (Math.PI * 2)    this.dead = false    this.color = color    this.speed = Math.random() * 3 + 3;    this.walker = new Walker({        radius: 20,        speed: 0.25    })    this.gravity = 0.25    this.dur = this.speed / 0.1    this.move = function () {        this.dur--            if (this.dur < 0) this.dead = true        if (this.speed < 0) return        if (this.speed > 0) this.speed -= 0.1        var walk = this.walker.step()        this.x += Math.cos(this.dir + walk) * this.speed        this.y += Math.sin(this.dir + walk) * this.speed        this.y += this.gravity        this.gravity += 0.05    }    this.draw = function () {        drawCircle(this.x, this.y, 3, this.color)    }}function Firework(x, y) {    this.xmove = new Walker({        radius: 10,        speed: 0.5    })    this.x = x || Math.random() * ctx.canvas.width    this.y = y || ctx.canvas.height    this.height = Math.random() * ctx.canvas.height / 2    this.dead = false    this.color = randomColor()    this.move = function () {        this.x += this.xmove.step()        if (this.y > this.height) this.y -= 1;        else this.burst()    }    this.draw = function () {        drawCircle(this.x, this.y, 1, this.color)    }    this.burst = function () {        this.dead = true        var i = 100;        while (i--) sparks.push(new Spark(this.x, this.y, this.color))    }}function drawCircle(x, y, radius, color) {    color = color || &#39;#FFF&#39;    ctx.fillStyle = color    ctx.fillRect(x - radius / 2, y - radius / 2, radius, radius)}function randomColor() {    return [&#39;#6ae5ab&#39;, &#39;#88e3b2&#39;, &#39;#36b89b&#39;, &#39;#7bd7ec&#39;, &#39;#66cbe1&#39;][Math.floor(Math.random() * 5)];}function Walker(options) {    this.step = function () {        this.direction = Math.sign(this.target) * this.speed        this.value += this.direction        this.target ?            this.target -= this.direction :            (this.value) ?            (this.wander) ?            this.target = this.newTarget() :            this.target = -this.value :            this.target = this.newTarget()        return this.direction    }    this.newTarget = function () {        return Math.round(Math.random() * (this.radius * 2) - this.radius)    }    this.start = 0    this.value = 0    this.radius = options.radius    this.target = this.newTarget()    this.direction = Math.sign(this.target)    this.wander = options.wander    this.speed = options.speed || 1}
登入後複製

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

JS中的async/await

canvas如何做出黑色背景的紅色煙火

以上是canvas如何做出黑色背景的青色煙火的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Win11底部狀態列改為了黑色的樣式 Win11底部狀態列改為了黑色的樣式 Dec 31, 2023 pm 08:11 PM

win11預設的底部任務欄是淺藍色的,比較好看,但是有朋友發現突然win11底部狀態欄變成了黑色,這可能是更改了主題或背景顏色,改回來就可以了。 win11底部狀態列變成了黑色:1、首先點開底部開始選單,打開「設定」2、接著進入左邊欄的「個人化」設定。 3、然後開啟其中的「顏色」設定。 4.進入後,關掉其中的「透明效果」(如果打開後,我們的桌面是黑色的那狀態欄就是黑色的)5、如果壁紙不是黑色的,那麼把選擇模式改為「淺色」即可。 6.喜歡別的顏色的話,還可以在主題色裡自由選擇。

Win11 新版畫圖:一鍵移除背景實現摳圖功能 Win11 新版畫圖:一鍵移除背景實現摳圖功能 Sep 15, 2023 pm 10:53 PM

微軟邀請Canary和Dev頻道的WindowsInsider專案成員,測試和體驗新版畫圖(Paint)應用,最新版本號為11.2306.30.0。這次版本更新最值得關注的新功能是一鍵摳圖功能,使用者只需要點擊一下,就能自動消除背景,凸顯畫面主體,方便使用者後續操作。整個步驟非常簡單,使用者在新版畫圖應用程式中匯入圖片,然後點擊工具列上「移除背景」(removebackground)按鈕,就可以刪除圖片中的背景,使用者也可以使用矩形來選擇要消除背景的區域。

ppt背景怎麼全部替換 ppt背景怎麼全部替換 Mar 25, 2024 pm 04:25 PM

PPT背景替換是一種重要的操作,可快速統一簡報的視覺風格。透過修改投影片母版或使用「格式背景」功能,可以快速替換整個簡報的背景。此外,某些PPT版本還提供批次替換功能,可以輕鬆替換所有投影片的背景。在替換背景時,應注意選擇與簡報主題相符的背景,並確保背景清晰度和解析度符合要求。

學習canvas框架 詳解常用的canvas框架 學習canvas框架 詳解常用的canvas框架 Jan 17, 2024 am 11:03 AM

探索Canvas框架:了解常用的Canvas框架有哪些,需要具體程式碼範例引言:Canvas是HTML5中提供的一個繪圖API,透過它我們可以實現豐富的圖形和動畫效果。為了提高繪圖的效率和便利性,許多開發者開發了不同的Canvas框架。本文將介紹一些常用的Canvas框架,並提供具體程式碼範例,以幫助讀者更深入地了解這些框架的使用方法。一、EaselJS框架Ea

在 iPhone 上播放環境背景聲音以保持專注 在 iPhone 上播放環境背景聲音以保持專注 Nov 29, 2023 pm 11:27 PM

在iPhone和iPad上,Apple包含的多項輔助功能之一是背景聲音。這些聲音旨在幫助您保持專注、保持冷靜,並幫助您在忙於某事時盡量減少分心。提供的背景聲音包括平衡、明亮和黑暗的噪音,以及海洋、雨水和溪流等自然聲音。所有聲音都可以設定為在背景播放,以掩蓋不需要的環境或外部噪音,並且聲音混合到其他音訊和系統聲音或隱藏在其他音訊和系統聲音下。在iPhone和iPad上啟用背景聲音以下步驟介紹如何在運行iOS15/iPadOS15及更高版本的iPhone和iPad上啟用背景聲音。在iPhone或i

美圖秀如何換照片背景底色 美圖秀如何換照片背景底色 Apr 08, 2024 pm 03:56 PM

1.開啟美圖秀軟體,選擇【圖片美化】,從相簿匯入照片。 2.點選底部工具列的【摳圖】,選擇【背景替換】功能。 3.在【背景】選項中,從純色方格中挑選所需底色,或上傳自訂圖片。 4.確認選擇後,點選【儲存】即可完成底色更換。

Go語言的誕生背景與初衷 Go語言的誕生背景與初衷 Apr 04, 2024 am 08:48 AM

Go語言誕生於Google,旨在解決C++的複雜性和並發支援不足的問題。它的初衷是創造一種簡潔易學、高效並發、記憶體安全、跨平台的語言,以提高程式設計師的生產力,建立可靠可擴展的系統,並促進程式碼的移植和共享。

html2canvas有哪些版本 html2canvas有哪些版本 Aug 22, 2023 pm 05:58 PM

html2canvas的版本有html2canvas v0.x、html2canvas v1.x等。詳細介紹:1、html2canvas v0.x,這是html2canvas的早期版本,目前最新的穩定版本是v0.5.0-alpha1。它是一個成熟的版本,已經被廣泛使用,並且在許多專案中得到了驗證;2、html2canvas v1.x,這是html2canvas的新版本。

See all articles