特效描述:利用HTML5实现庆祝2018年烟花动画特效。利用HTML5实现庆祝2018年烟花动画特效

代码结构

1. HTML代码

2018

var ctx = document.querySelector('canvas').getContext('2d')

ctx.canvas.width = window.innerWidth

ctx.canvas.height = window.innerHeight

var 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 = 'rgba(0, 0, 0, 0.1)';

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 || '#FFF'

ctx.fillStyle = color

ctx.fillRect(x-radius/2, y-radius/2, radius, radius)

}

function randomColor(){

return ['#6ae5ab','#88e3b2','#36b89b','#7bd7ec','#66cbe1'][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

}

如何用html制作一个动态烟花,利用HTML5实现庆祝2018年烟花动画特效相关推荐

  1. html5网站粒子时钟,利用HTML5实现SVG多边形粒子时钟动画特效

    特效描述:利用HTML5实现 SVG 多边形 粒子时钟 动画特效.利用HTML5实现SVG多边形粒子时钟动画特效 代码结构 1. 引入JS 2. HTML代码  svg { position: ab ...

  2. html5 可旋转的三维树形结构,利用HTML5实现勾股树植物生长动画特效

    特效描述:利用HTML5实现 勾股树 植物生长 动画特效.利用HTML5实现勾股树植物生长动画特效 代码结构 1. HTML代码 const canvas = document.querySelect ...

  3. html 延长直线,利用HTML5实现绘制3D线条延伸动画特效

    特效描述:利用HTML5实现 绘制3D 线条延伸 动画特效.利用HTML5实现雷达地区扫描动画特效 代码结构 1. HTML代码 ;(function() { 'use strict'; var c ...

  4. 如何用html制作一个动态烟花,视频加烟花特效 视频如何制作烟花效果|视频上添加动态的焰火效果...

    有句mmp不知当讲不当讲,今天竟然是孔子的诞辰,这样一算孔子很有可能是处女座呢!!当看到这条消息时我的内心是拒绝的,在我看来孔子的中庸思想表示的"平庸.折中.调和"明明是我们天秤座 ...

  5. html怎么做一个动态广告图,用CSS3实现广告的展示动画特效

    用CSS3实现广告的展示动画特效,不需要用JS哦,大家可以学习下,很多地方都可以用到,还在等什么呢,代码奉上啦! 展示图: DEMO代码:html> 用CSS3实现广告的展示动画特效 - Web ...

  6. 怎么用python制作简单的程序-神级程序员教你如何用python制作一个牛逼的外挂!...

    玩过电脑游戏的同学对于外挂肯定不陌生,但是你在用外挂的时候有没有想过如何做一个外挂呢?(当然用外挂不是那么道义哈,呵呵),那我们就来看一下如何用python来制作一个外挂.... 我打开了4399小游 ...

  7. 手把手教你如何用Python制作一个电子相册?末附python教程

    这里简单介绍一下python制作电子相册的过程,主要用到tkinter和pillow这2个库,tkinter用于窗口显示照片,pillow用来处理照片,照片切换分为2种方式,一种是自动切换(每隔5秒) ...

  8. python可视化迷宫求解_如何用 Python 制作一个迷宫游戏

    相信大家都玩过迷宫的游戏,对于简单的迷宫,我们可以一眼就看出通路,但是对于复杂的迷宫,可能要仔细寻找好久,甚至耗费数天,然后可能还要分别从入口和出口两头寻找才能找的到通路,甚至也可能找不到通路. 虽然 ...

  9. 电子脑PHP动画制作,PS简单制作一个动态的字体动画

    这篇教程是向PHP中文网的朋友分享PS简单制作一个动态的字体动画方法,教程制作出来的字体动画非常漂亮,难度不是很大,来看看吧 动态的文字比静态的文字更加吸引人的目光.如果在平时的海报中,在平时的促销活 ...

最新文章

  1. SQL Server故障排除圣经
  2. SAP SD 微观研究之如何得到Customer List?
  3. CSS:IE与Firefox的CSS兼容大全
  4. 中国台湾地区几乎所有院校的硕博论文库(部分可下全文)分享该日志 举报...
  5. SAP Spartacus的全局配置
  6. codeforces1453 D. Checkpoints
  7. 2021高考理综单科成绩查询,2021全国各省市高考总分及各科分数 分值是多少
  8. 大家可以放心了!Redmi K20将配备双频GPS
  9. building a new horizon
  10. cocos之观察者模式应用实例
  11. RestTemplate的基本使用
  12. 纯干货内容:关于ivx和mendix的对比 还在犹豫选择那个低代码平台的小伙伴看过来
  13. vs2005 无法启动调试 绑定句柄无效的解决
  14. MySQL --- 函数大全3
  15. Luogu P4484 [BJWC2018]最长上升子序列
  16. 某百度程序员:每天十点上班,午休两小时,每天闲逛,晚上八点就下班!
  17. 京东前台PC首页系统技术详解
  18. 3060Ti显卡,tensorflow2.4-GPU安装
  19. 关于12864液晶屏汉字显示问题
  20. List的remove()方法避坑

热门文章

  1. springboot引入国际化
  2. 1-1 MySQL数据库的基本操作 【增删改查】
  3. 金融工具(也称信用工具)
  4. 深入理解java虚拟机脑图文档
  5. 【DOS】通过for命令截取字符串
  6. Sourc Insight 添加 磁盘目录
  7. 三棱锥之刻(求三棱锥中心球与表面覆盖面积之和)
  8. 大众集团「换舵手」,软件战略从「自研优先」转向开放协作
  9. 思科、IBM、甲骨文、Uber相继裁员,寒冬将至 ?
  10. CTU 2017 B - Pond Cascade 二分