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模拟火焰燃烧效果示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/cuclife/article/details/134184011
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/839611

相关文章

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示