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

相关文章

Java继承映射的三种使用方法示例

《Java继承映射的三种使用方法示例》继承在Java中扮演着重要的角色,它允许我们创建一个类(子类),该类继承另一个类(父类)的所有属性和方法,:本文主要介绍Java继承映射的三种使用方法示例,需... 目录前言一、单表继承(Single Table Inheritance)1-1、原理1-2、使用方法1-

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间