一:创建画布

<canvas width="600" height="600" id="canvas" style="border:1px solid red;"></canvas>

二:实现功能

var canvas = document.getElementById('canvas');
var cxt = canvas.getContext('2d');

var timer;
var iStop = false;
var fires = [];

function draw(){
var _fires = [];
for(var i=0; i<fires.length; i++){
cxt.save();
cxt.translate(fires[i].x, fires[i].y);
for(var j=0; j<12; j++){
cxt.save();
cxt.beginPath();
cxt.strokeStyle = 'rgba('+fires[i].color+', '+(fires[i].alpha--)/1000+')';
cxt.rotate(30*j*Math.PI/180+fires[i].offset);
cxt.translate(fires[i].radius++/10, 0);
cxt.rotate(45*Math.PI/180);
cxt.moveTo(0, 0);
cxt.lineTo(10, 0);
cxt.stroke();
cxt.closePath();
cxt.restore();
}
cxt.restore();

if(fires[i].alpha > 0){
_fires.push(fires[i])
}
}
fires = _fires;
}

function drawTip() {
cxt.save();
cxt.font="20px Verdana";
cxt.fillStyle = 'skyblue';
cxt.fillText('请单击页面!', 230, 290);
cxt.restore();
}

function erase(){
cxt.clearRect(0, 0, canvas.width, canvas.height);
}

canvas.onmousedown = function(e){
var ev = e || window.event;

var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;

var r = Math.round(Math.random()*255);
var g = Math.round(Math.random()*255);
var b = Math.round(Math.random()*255);

var offset = Math.round(Math.random()*45);

fires.push({'radius':0,'alpha':1000,'x':x,'y':y,'color':r+','+g+','+b, 'offset':offset});
};

window.requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;

window.cancelRequestAnimationFrame =
window.cancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame;

function animate() {
erase();
draw();
drawTip();
if(iStop){
cancelRequestAnimationFrame(timer);
}else{
timer = requestAnimationFrame(animate);
}
}

animate();

canvas 实现烟花效果的更多相关文章

  1. WEB烟花效果——Canvas实现

    摘要        本文主要介绍一种WEB形式的烟花(fireworks)效果(图1所示),该效果基于Canvas实现,巧妙地运用了canvas绘图的特性,并加入了物理力作用的模拟,使整体效果非常绚丽 ...

  2. 还在用canvas画格子吗?文字烟花效果更不错噢

    大家好,我是小丞同学,一名前端爱好者 欢迎访问博主的个人网站:一口奶盖 "在人间贩卖声音 等凑够满天星辰 放烟花给你看" 上次的烟花有些许平淡,这次来放大招了,让你的名字在天空绽放 ...

  3. Android开发——为EditText添加烟花效果的实现

    )什么时候发射烟花:监听EditText的文字改变,获取文字数量的变化以确定风的方向,还有获取光标的位置确定爆炸的位置.光标的位置没有具体的方法确定坐标,要通过反射自己计算. 2.  主要实现类 库里 ...

  4. Canvas 放烟花合集 -- 用粉丝头像做成烟花绽放🧨

    "我对着烟花许愿,希望你永远在我身边" "凑不够满天星辰那就去看看烟花吧,人间烟火气,最抚凡人心" 小tips:喜欢的可以关注博主私信代码噢~ 也可以看看前面两 ...

  5. 使用Canvas实现动画效果 | DKlogs -- 设计 | 生活

    使用Canvas实现动画效果 | DKlogs -- 设计 | 生活 使用Canvas实现动画效果

  6. 『HTML5梦幻之旅』-缤纷多姿的烟花效果

    天花无数月中开,五采祥云绕绛台.堕地忽惊星彩散,飞空旋作雨声来.怒撞玉斗翻晴雪,勇踏金轮起疾雷.更漏已深人渐散,闹竿挑得彩灯回. ——明·瞿佑·<烟火戏> 记得每年过春节的那段时间,除了欣 ...

  7. canvas实现倒计时效果示例(vue组件内编写)

    前言: 此事例是在vue组件中,使用canvas实现倒计时动画的效果.其实,实现效果的逻辑跟vue没有关系,只要读懂canvas如何实现效果的这部分逻辑就可以了 canvas动画的原理:利用定时器,给 ...

  8. 原生js实现canvas气泡冒泡效果

    说明: 本文章主要分为ES5和ES6两个版本 ES5版本是早期版本,后面用ES6重写优化的,建议使用ES6版本. 1, 原生js实现canvas气泡冒泡效果的插件,api丰富,使用简单2, 只需引入J ...

  9. canvas/CSS仪表盘效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. chattr 和 lsattr 命令介绍---案例:修改passwd文件

    chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,如果Linux内核版本低于2.2,那么许多 功能不能实现.同样-D检查压缩文件中的错误的功能,需要2.5.19以上内核才能支持. ...

  2. Markdown总结整理

    今天朋友(SolskGare)向我普及了一个很方便的文本编辑语言Markdown,才意识到之前自己用鼠标一点一点的排版有多low,而且往往还是费力不讨好.今天我就整理一下Markdown的用法,真的是 ...

  3. Zabbix 3.0编译安装

    环境准备Centos 6.X 数据库准备默认centos yum源中mysql包的版本号为5.1,为了能使zabbix 3.0能达到最好的性能效果,安装最新版的mysql数据库. yum list i ...

  4. 207. Course Schedule(Graph; BFS)

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  5. Codeforces Beta Round #34 (Div. 2)

    Codeforces Beta Round #34 (Div. 2) http://codeforces.com/contest/34 A #include<bits/stdc++.h> ...

  6. Jquery的Ajax中contentType和dataType的区别

    $.ajax({ type: httpMethod, cache:false, async:false, contentType: "application/json; charset=ut ...

  7. es数组去重的简写

    console.log([...new Set([2, 2, 12, 1, 2, 1, 6, 12, 13, 6])])

  8. nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)

    报错信息 [root@bogon sbin]# ./nginx nginx: [emerg] mkdir() : No such file or directory) 解决方法 [root@bogon ...

  9. solr简介、学习详细过程!(超详细~)

    solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...

  10. Task 和 ThreadPool

    在C#中 TASK 和 ThreadPool 都可以完成多任务并行的工作.但是 TASK实际上是微软定义好的,基于 ThreadPool 的一个类.这里面微软做了很多优化工作. Task Parall ...