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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的