Canvas模拟火焰燃烧效果示例

2024-03-23 21:50

本文主要是介绍Canvas模拟火焰燃烧效果示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

canvas实例应用100+ 专栏提供canvas的基础知识,高级动画,相关应用扩展等信息。
canvas作为html的一部分,是图像图标地图可视化的一个重要的基础,学好了canvas,在其他的一些应用上将会起到非常重要的帮助。

文章目录

    • 示例效果
    • 源代码
    • canvas基本属性
    • canvas基础方法

canvas能做好很多事情,能显示各种动画。这里是模拟火焰燃烧效果的示例。

示例效果

在这里插入图片描述

源代码

							
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas模拟火焰演示</title><style>
@import url(https://fonts.googleapis.com/css?family=Amatic+SC);
html, body {margin:0;padding:0;height: 100%;
}
</style></head>
<body>
<canvas id="zjcopy"></canvas><script>
var Fire  = function(){this.canvas 		= document.getElementById('zjcopy');this.ctx 			= this.canvas.getContext('2d');this.canvas.height 	= window.innerHeight;this.canvas.width 	= window.innerWidth;this.aFires 		= [];this.aSpark 		= [];this.aSpark2 		= [];this.mouse = {x : this.canvas.width * .5,y : this.canvas.height * .75,}this.init();}
Fire.prototype.init = function()
{this.canvas.addEventListener('mousemove', this.updateMouse.bind( this ), false);}
Fire.prototype.run = function(){this.update();this.draw();if( this.bRuning )requestAnimationFrame( this.run.bind( this ) );}
Fire.prototype.start = function(){this.bRuning = true;this.run();}
Fire.prototype.stop = function(){this.bRuning = false;}
Fire.prototype.update = function(){this.aFires.push( new Flame( this.mouse ) );this.aSpark.push( new Spark( this.mouse ) );this.aSpark2.push( new Spark( this.mouse ) );for (var i = this.aFires.length - 1; i >= 0; i--) {if( this.aFires[i].alive )this.aFires[i].update();elsethis.aFires.splice( i, 1 );}for (var i = this.aSpark.length - 1; i >= 0; i--) {if( this.aSpark[i].alive )this.aSpark[i].update();elsethis.aSpark.splice( i, 1 );}for (var i = this.aSpark2.length - 1; i >= 0; i--) {if( this.aSpark2[i].alive )this.aSpark2[i].update();elsethis.aSpark2.splice( i, 1 );}}Fire.prototype.draw = function(){this.ctx.globalCompositeOperation = "source-over";this.ctx.fillStyle = "rgba( 15, 5, 2, 1 )";this.ctx.fillRect( 0, 0, window.innerWidth, window.innerHeight );this.grd = this.ctx.createRadialGradient( this.mouse.x, this.mouse.y - 200,200,this.mouse.x, this.mouse.y - 100,0 );this.grd.addColorStop(0,"rgb( 15, 5, 2 )");this.grd.addColorStop(1,"rgb( 30, 10, 2 )");this.ctx.beginPath();this.ctx.arc( this.mouse.x, this.mouse.y - 100, 200, 0, 2*Math.PI );this.ctx.fillStyle= this.grd;this.ctx.fill();this.ctx.font = "15em Amatic SC";this.ctx.textAlign = "center";this.ctx.strokeStyle = "rgb(50, 20, 0)";this.ctx.fillStyle = "rgb(120, 10, 0)";this.ctx.lineWidth = 2;this.ctx.strokeText("Fire",this.canvas.width/2, this.canvas.height * .72 );this.ctx.fillText("Fire",this.canvas.width/2, this.canvas.height * .72 );	this.ctx.globalCompositeOperation = "overlay";//or lighter or soft-lightfor (var i = this.aFires.length - 1; i >= 0; i--) {this.aFires[i].draw( this.ctx );}this.ctx.globalCompositeOperation = "soft-light";//"soft-light";//"color-dodge";for (var i = this.aSpark.length - 1; i >= 0; i--) {if( ( i % 2 ) === 0 )this.aSpark[i].draw( this.ctx );}this.ctx.globalCompositeOperation = "color-dodge";//"soft-light";//"color-dodge";for (var i = this.aSpark2.length - 1; i >= 0; i--) {this.aSpark2[i].draw( this.ctx );}}Fire.prototype.updateMouse = function( e ){this.mouse.x = e.clientX;this.mouse.y = e.clientY;//this.aFires.push( new Flame( this.mouse ) );}var Flame = function( mouse ){this.cx = mouse.x;this.cy = mouse.y;this.x = rand( this.cx - 25, this.cx + 25);this.y = rand( this.cy - 5, this.cy + 5);this.vy = rand( 1, 3 );this.vx = rand( -1, 1 );this.r = rand( 20, 30 );this.life = rand( 3, 6 );this.alive = true;this.c = {h : Math.floor( rand( 2, 40) ),s : 100,l : rand( 80, 100 ),a : 0,ta : rand( 0.8, 0.9 )}}
Flame.prototype.update = function()
{this.y -= this.vy;this.vy += 0.05;this.x += this.vx;if( this.x < this.cx )this.vx += 0.1;elsethis.vx -= 0.1;if(  this.r > 0 )this.r -= 0.1;if(  this.r <= 0 )this.r = 0;this.life -= 0.15;if( this.life <= 0 ){this.c.a -= 0.05;if( this.c.a <= 0 )this.alive = false;}else if( this.life > 0 && this.c.a < this.c.ta ){this.c.a += .08;}}
Flame.prototype.draw = function( ctx ){ctx.beginPath();ctx.arc( this.x, this.y, this.r * 3, 0, 2*Math.PI );ctx.fillStyle = "hsla( " + this.c.h + ", " + this.c.s + "%, " + this.c.l + "%, " + (this.c.a/20) + ")";ctx.fill();ctx.beginPath();ctx.arc( this.x, this.y, this.r, 0, 2*Math.PI );ctx.fillStyle = "hsla( " + this.c.h + ", " + this.c.s + "%, " + this.c.l + "%, " + this.c.a + ")";ctx.fill();}var Spark = function( mouse ){this.cx = mouse.x;this.cy = mouse.y;this.x = rand( this.cx -40, this.cx + 40);this.y = rand( this.cy, this.cy + 5);this.lx = this.x;this.ly = this.y;this.vy = rand( 1, 3 );this.vx = rand( -4, 4 );this.r = rand( 0, 1 );this.life = rand( 4, 5 );this.alive = true;this.c = {h : Math.floor( rand( 2, 40) ),s : 100,l : rand( 40, 100 ),a : rand( 0.8, 0.9 )}}
Spark.prototype.update = function()
{this.lx = this.x;this.ly = this.y;this.y -= this.vy;this.x += this.vx;if( this.x < this.cx )this.vx += 0.2;elsethis.vx -= 0.2;this.vy += 0.08;this.life -= 0.1;if( this.life <= 0 ){this.c.a -= 0.05;if( this.c.a <= 0 )this.alive = false;}}
Spark.prototype.draw = function( ctx ){ctx.beginPath();ctx.moveTo( this.lx , this.ly);ctx.lineTo( this.x, this.y);ctx.strokeStyle = "hsla( " + this.c.h + ", " + this.c.s + "%, " + this.c.l + "%, " + (this.c.a / 2) + ")";ctx.lineWidth = this.r * 2;ctx.lineCap = 'round';ctx.stroke();ctx.closePath();ctx.beginPath();ctx.moveTo( this.lx , this.ly);ctx.lineTo( this.x, this.y);ctx.strokeStyle = "hsla( " + this.c.h + ", " + this.c.s + "%, " + this.c.l + "%, " + this.c.a + ")";ctx.lineWidth = this.r;ctx.stroke();ctx.closePath();}rand = function( min, max ){ return Math.random() * ( max - min) + min; };
onresize = function () { oCanvas.canvas.width = window.innerWidth; oCanvas.canvas.height = window.innerHeight; };var oCanvas;
init = function()
{oCanvas = new Fire();oCanvas.start();}window.onload = init;
</script></body>
</html>

canvas基本属性

属性属性属性
canvasfillStylefilter
fontglobalAlphaglobalCompositeOperation
heightlineCaplineDashOffset
lineJoinlineWidthmiterLimit
shadowBlurshadowColorshadowOffsetX
shadowOffsetYstrokeStyletextAlign
textBaselinewidth

canvas基础方法

方法方法方法
arc()arcTo()addColorStop()
beginPath()bezierCurveTo()clearRect()
clip()close()closePath()
createImageData()createLinearGradient()createPattern()
createRadialGradient()drawFocusIfNeeded()drawImage()
ellipse()fill()fillRect()
fillText()getImageData()getLineDash()
isPointInPath()isPointInStroke()lineTo()
measureText()moveTo()putImageData()
quadraticCurveTo()rect()restore()
rotate()save()scale()
setLineDash()setTransform()stroke()
strokeRect()strokeText()transform()
translate()

这篇关于Canvas模拟火焰燃烧效果示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/839611

相关文章

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

Java中的Schema校验技术与实践示例详解

《Java中的Schema校验技术与实践示例详解》本主题详细介绍了在Java环境下进行XMLSchema和JSONSchema校验的方法,包括使用JAXP、JAXB以及专门的JSON校验库等技术,本文... 目录1. XML和jsON的Schema校验概念1.1 XML和JSON校验的必要性1.2 Sche