html5 电流效果,在HTML5 Canvas 2D上绘制云雾中的电流动画特效

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

ctrl = {

numParticles: 35,

maxRadius: 80,

hue: 220,

hueRange: 15,

fade: 0.36,

halo: true,

zappy: true,

zapComplexity: 1

}

var gui = new dat.GUI();

gui.add(ctrl, 'numParticles', 1, 150).step(1);

gui.add(ctrl, 'maxRadius', 30, 150).step(1);

gui.add(ctrl, 'hue', 0, 359).step(1);

gui.add(ctrl, 'hueRange', 0, 180).step(1);

gui.add(ctrl, 'fade', 0, 0.4).step(0.001);

gui.add(ctrl, 'zapComplexity', 0, 4).step(1);

gui.add(ctrl, 'halo');

// gui.add(ctrl, 'zappy');

// create a canvas element

var canvas = document.createElement("canvas")

// attach element to DOM

document.body.appendChild(canvas)

// background color [r, g, b]

var bg = [10, 10, 30]

var wh = window.innerHeight

// get the canvas context (this is the part we draw to)

var ctx = canvas.getContext("2d")

function setup() {

// setup the canvas size to match the window

canvas.width = window.innerWidth

canvas.height = window.innerHeight

wh = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight

// set the 0,0 point to the middle of the canvas, this is not necessary but it can be handy

ctx.translate(canvas.width / 2, canvas.height / 2)

fill(bg, 1)

}

// fill entire canvas with a preset color

function fill(rgb, amt) {

ctx.beginPath(); // start path

ctx.rect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height) // set rectangle to be the same size as the window

ctx.fillStyle = `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${amt})` // use the rgb array/color for fill, and amt for opacity

ctx.fill() // do the drawing

}

function drawCircle(x, y, r, color) {

ctx.beginPath()

ctx.arc(x, y, r, 0, 2 * Math.PI)

ctx.fillStyle = color || 'white'

ctx.fill()

ctx.closePath()

}

function Particle() {

// initialize loopers with random trange and offset

this.loop1 = new Looper(1200 + 200 * Math.random(), 9000 * Math.random())

this.loop2 = new Looper(880 + 950 * Math.random(), 9000 * Math.random())

this.loop3 = new Looper(550 + 920 * Math.random(), 9000 * Math.random())

this.history = []

this.history_max = 1

this.c = ``

this.hsv = {}

this.offset = Math.random() // some color offset for the color

this.signals = 0 // count connection - update every frame

this.sstrength = 0 // running average using signals

this.destroy = function() {

this.loop1 = null

this.loop2 = null

this.loop3 = null

this.history = null

this.history_max = null

this.c = null

this.hsv = null

this.offset = null

delete this

}

this.draw = function() {

this.sstrength = this.signals * 0.02 + this.sstrength * 0.91;

this.loop1.update() // update looper

this.loop2.update() // update looper

this.loop3.update() // update looper

// set x,y, radius, and color params

var x = this.loop1.sin * (canvas.width / 4) + this.loop2.sin * (canvas.width / 3) * this.loop3.cos * this.loop2.sin

var y = this.loop1.cos * (canvas.height / 4) + this.loop2.cos * (canvas.height / 3) * this.loop3.cos * this.loop2.sin

// var r = 0.2 + 3 * this.loop1.sinNorm * this.loop2.sinNorm // set the radius

this.hsv = {

// this is where we set the color...

h: ctrl.hue + ctrl.hueRange * (this.loop3.cosNorm + this.offset) * this.loop2.sinNorm,

// the saturation depends on the loop

s: 80 + 7 * this.loop1.sinNorm,

// ..and so does the value - we want to keep that close to 50%

v: 70 + 5 * this.loop3.sin

}

this.c = `hsla(${this.hsv.h}, ${this.hsv.s}%, ${this.hsv.v}%, ${1})`

if (ctrl.halo) {

var grd = ctx.createRadialGradient(Math.round(x), Math.round(y), 0, Math.round(x), Math.round(y), ctrl.maxRadius);

grd.addColorStop(0.0, `hsla(${this.hsv.h}, ${this.hsv.s}%, ${this.hsv.v}%, ${0.1 * this.sstrength})`);

// grd.addColorStop(0.2, `hsla(${this.hsv.h}, ${this.hsv.s}%, ${this.hsv.v}%, ${0.03 * this.sstrength})`);

grd.addColorStop(0.9, `hsla(${this.hsv.h}, ${this.hsv.s}%, ${this.hsv.v}%, ${0})`);

drawCircle(x, y, ctrl.maxRadius, grd); // draw the circle

}

this.history = [

[x, y]

]

this.signals = 0;

}

this.addSignal = function() {

this.signals++;

}

}

// initialize a set of particle

var particles = []

function draw() {

// fill context with background color

fill(bg, ctrl.fade)

//add particles

while (particles.length < ctrl.numParticles) {

particles.push(new Particle())

}

// drop old particles

while (particles.length >= ctrl.numParticles) {

particles.pop().destroy()

}

var x0

var y0

var x1

var y1

var d

var r = ctrl.maxRadius * ctrl.maxRadius

var s

// update all the particles

for (var i = 0; i < particles.length; i++) {

particles[i].draw() // do it once

for (var j = 0; j < i; j++) {

if (particles[j] && particles[j].history && particles[j].history.length > 0) {

x0 = particles[i].history[0][0]

y0 = particles[i].history[0][1]

x1 = particles[j].history[0][0]

y1 = particles[j].history[0][1]

d = (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)

if (d < r && d > 50) {

particles[i].addSignal();

particles[j].addSignal();

s = 1 - Math.sin(d / r * d / r * Math.PI / 2)

if (!ctrl.zappy) {

// straight line

ctx.beginPath();

ctx.moveTo(x0, y0);

ctx.lineTo(x1, y1);

ctx.lineWidth = s * 1.6;

ctx.strokeStyle = particles[i].c

ctx.stroke()

ctx.closePath()

} else {

// zappy line

var iterations = [{

x: x0,

y: y0

}, {

x: x1,

y: y1

}]

var newiterations, ii, ij

for (ii = 0; ii < ctrl.zapComplexity; ii++) {

newiterations = [iterations[0]]

for (ij = 1; ij < iterations.length; ij++) {

newiterations.push(getRandMidpoint(iterations[ij - 1], iterations[ij], ctrl.maxRadius / 3 / (ii * ii + 1) * (1 - s * 0.7)))

newiterations.push(iterations[ij])

}

iterations = newiterations.concat([])

}

ctx.beginPath();

ctx.moveTo(iterations[0].x, iterations[0].y);

ctx.lineWidth = s * 1.6;

ctx.strokeStyle = particles[i].c

for (ii = 1; ii < iterations.length; ii++) {

ctx.lineTo(iterations[ii].x, iterations[ii].y);

}

ctx.stroke()

ctx.closePath()

}

}

}

}

}

// this is a draw loop, this will execute frequently and is comparable to EnterFrame on other platform

window.requestAnimationFrame(function() {

draw()

})

}

// start enterFrame loop

window.requestAnimationFrame(draw);

// force running setup

setup()

// re-setup canvas when the size of the window changes

window.addEventListener("resize", setup)

// create a class to hold value and have built in incrementing functionality

function Looper(steps, start) {

this.val = start || 0 // set value to start value if defined, or 1

this.steps = steps || 100 // set steps to passed value or default to 100

this.norm = this.val / this.range // initialize normalized value (between 0 and 1)

this.sin = Math.sin(this.norm * Math.PI * 2) // get sine value from norm normalized to [0, 2PI]

this.sinNorm = (this.sin + 1) / 2 // normalize sin to [0,1]

this.cos = Math.cos(this.norm * Math.PI * 2) // get cosine value from norm normalized to [0, 2PI]

this.cosNorm = (this.cos + 1) / 2 // normalize cos to [0,1]

this.update = function() {

this.val = (this.val + 1) % this.steps // update value

this.norm = this.val / this.steps // update normalize value (between 0 and 1)

this.sin = Math.sin(this.norm * Math.PI * 2) // get sine value from norm normalized to [0, 2PI]

this.sinNorm = (this.sin + 1) / 2 // normalize sine to [0,1]

this.cos = Math.cos(this.norm * Math.PI * 2) // get cosine value from norm normalized to [0, 2PI]

this.cosNorm = (this.cos + 1) / 2 // normalize cos to [0,1]

}

}

function getRandMidpoint(pa, pb, range) {

var a = Math.atan2(pb.y - pa.y, pb.x - pa.x) + Math.PI / 2

var half = {

y: (pb.y - pa.y) / 2 + pa.y,

x: (pb.x - pa.x) / 2 + pa.x

}

var offset = Math.random() * range - range / 2

var ho = {

x: Math.cos(a) * offset + half.x,

y: Math.sin(a) * offset + half.y

}

return ho

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/527846.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2021深圳各中学高考成绩查询,深圳四大高中在2021年高考深一模的前500名分布数据...

看到深圳四大高中在2021年高考深一模的前500名分布数据:深圳中学名列前茅&#xff0c;前500名中占166席&#xff0c;且文科、理科第一名均来自深中&#xff1b;物理方向前10名中深中6人&#xff0c;实验3人&#xff1b;历史方向前10名中深中3人。深圳实验学校高中部有84人进入前…

微型计算机各部件之间通过总线传递各种信息,2015年9月计算机一级考试基础及MSOffice应用选择真题...

一、选择题(每小题1分&#xff0c;共20分)1、控制器的主要功能是 .A&#xff0e;指挥计算机各部件自动、协调地工作B&#xff0e;对数据进行算术运算C&#xff0e;进行逻辑判断D&#xff0e;控制数据的输入和输出2、下列度量单位中,用来度量CPU时钟主频的是 .A&#xff0e;MB&a…

土木工程计算机仿真学科未来前景,同济大学土木工程学院土木工程计算机仿真2010级学历教育硕士--培养方案...

一、简介土木工程计算机仿真学科是土木工程属下的二级学科&#xff0c;在国民经济建设中有着重要的地位&#xff0c;在促进城市建设、社会发展过程中做出了重大贡献。土木工程计算机仿真是同济大学于2005年自主设立的二级学科&#xff0c;虽然设立时间不长&#xff0c;但取得的…

html推箱子怎么清除走过的,第九讲:HTML5该canvas推箱子原型实现

动body{margin:0px;padding:0px;}#main{margin: 100 auto 0 auto;border: 10px solid #030303;300px;height:460px;border-radius:10px;}var canvas document.getElementById(mc);var cxt canvas.getContext(2d);//定义背景的颜色function init_background(){cxt.fillStyle …

大学计算机要学多久,大学刚开学要不要带电脑?很多人都很后悔,学长学姐把经验告诉你...

大学刚开学要不要带电脑&#xff1f;很多人都很后悔&#xff0c;学长学姐把经验告诉你文/小川说车况大学刚开学&#xff0c;学生们要不要带电脑呢&#xff0c;想必这个问题一直对还没进入大学的准大学生来说很困扰&#xff0c;一方面既担心在学校里面用电脑的时候不方便&#x…

西南医科大学计算机应用基础历年真题,网络选修课-计算机应用基础-期末考.docx...

1 单选 (1 分)Outlook 数据文件的扩展名是().A..patB..datC..ptsD..dll正确答案&#xff1a; A2Windows 系统中 ,若 2 个纯文本文件分别包含100 和 200 个英文字符 ,下列关于它们占用磁盘空间大小的说法中,正确的是 ().A.前者大于后者B.不确定 C.后者大于前者D.两者相等正确答案…

html自定义js程序,JS中微信小程序自定义底部弹出框

实现微信小程序底部弹出框效果&#xff0c;代码分为html&#xff0c;css和js两部分&#xff0c;具体代码详情大家参考下本文。htmlCSS.commodity_screen {width: 100%;height: 100%;position: fixed;top: 0;left: 0;background: #000;opacity: 0.2;overflow: hidden;z-index: 1…

怎么知道自己是否适合计算机专业,事实:我怎么知道我的旧计算机是否适合win7或win10?...

实际上&#xff0c;win10并不大&#xff01;只是对内存和硬盘的要求更高&#xff01;根据官方的最低操作要求&#xff0c;硬件要求如下&#xff1a;1、[处理器]&#xff1a;Win10系统的安装要求计算机的处理器以不低于1GHz的速度运行2、[运行内存]&#xff1a;Win10 32位操作系…

html5 canvas ios限制,HTML5画布的drawImage比错误的iOS(HTML5 Canvas drawImage ra

我想调整从iOS相机上使用HTML5画布客户端拍摄的图像&#xff0c;但我一直在这个奇怪的错误&#xff0c;这错误的图像有一个错误的比率&#xff0c;如果运行超过〜1.5MB大它的工作原理在桌面上&#xff0c;但不是在最新的iOS版本&#xff0c;媒体上传API。&#xff1a;你可以在这…

金工如何运用计算机思维,金工实训心得体会

金工实训心得体会我们有一些启发后&#xff0c;就很有必要写一篇心得体会&#xff0c;如此可以一直更新迭代自己的想法。那么心得体会到底应该怎么写呢&#xff1f;以下是小编为大家整理的金工实训心得体会&#xff0c;希望能够帮助到大家。金工实训心得体会1时间过得真快&…

湖南女子学院 计算机,2019湖南女子学院专业排名

湖南女子学院的前身是成立于1985年的湖南女子职业大学&#xff0c;2010年3月18日经教育部批准升格为全日制普通本科院校。截止到目前为止&#xff0c;湖南女子学院重点专业共有1个专业&#xff0c;其中国家品牌专业0个&#xff0c;省部重点专业1个。下面是学习啦小编给大家带来…

2020计算机考研只考数据结构的学校,【择校必看】十三所计算机专业课只考数据结构的985院校!...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼敲黑板&#xff1a;本文涉及到的学校计算机专业考研只考数据结构&#xff0c;其中部分院校同时也会考算法、C语言等相关内容。但是&#xff0c;相对其他几门&#xff0c;无疑在专业课的复习上大大降低了难度。如果各位同学目前的专…

福州3 2五年制计算机专业的学校,福州有哪些五年制大专

福州的五年制大专有福州环保职业中专学校、福建工业学校、福建理工学校、福建经济学校、福建建材工业学校、福建商贸学校、福州电子职业中专学校、福建化工学校、福建工贸学校、福州文教职业中专学校、福建经贸学校、福州商贸职业中专学校、福州工业学校、福州建筑工程职业中专…

无法删除计算机文件是什么意思,为何计算机的文件删除不了,说是被占用要解除占用...

要解决此问题&#xff0c;必须关闭“简单文件共享”&#xff0c;然后获取文件夹的所有权&#xff1a;1. 关闭“简单文件共享”&#xff1a;a. 单击“开始”&#xff0c;然后单击“我的电脑”。b. 在“工具”菜单上&#xff0c;单击“文件夹选项”&#xff0c;然后单击“查看”选…

石家庄计算机专接本学校有哪些,河北省内的专接本学校都有哪些?

满意答案mary3978702013.02.28采纳率&#xff1a;41% 等级&#xff1a;13已帮助&#xff1a;9063人防灾技术高等专科学校河北工程技术高等专科学校保定师范专科学校承德民族师范高等专科学校保定金融高等专科学校承德石油高等专科学校沧州师范专科学校沧州医学高等专科学校邢…

计算机原理转移指令题,转移指令计算机原理.pdf

转移指令计算机原理.pdf 3.3.5 程序控制指令程序控制指令 程序控制指令又称为控制转移指令&#xff0c;包括转移指令、循环控制指令、过程调用指令和 中断指令 4 类。转移指令又分为无条件转移指令和条件转移指令。 1无条件转移指令无条件转移指令 JMP 计算机程序的执行完全按照…

计算机一级资料书推荐,推荐版 全国计算机一级考试试题【史上最全面的资料,不看你就亏!】.doc...

一、选择题(1-30)每题1分&#xff0c;(31-55)每题2分&#xff0c;共80分)二、1&#xff0e;第二代电子计算机使用的电子器件是 。三、A)电子管 B)晶体管 C)集成电路 D)超大规模集成电路四、2&#xff0e;计算机病毒是指 。A)带细菌的磁盘 B)已损坏的磁盘C)具有破坏性的特制程序…

浙江高校计算机等级考试二级办公,浙江省高校计算机等级考试二级(高级办公)Word操作提示.doc...

浙江省高校计算机等级考试二级(高级办公)Word操作提示说明&#xff1a;经过多位教师的反复验证&#xff0c;以下操作步骤能做出与效果图一样的文档。具体操作步骤如下&#xff1a;1(1)~(2)操作&#xff1a;选择“格式”→“样式和格式”命令(或在格式工具栏上单击“格式窗格”按…

静态常量放在什么包里面_在沙雕游戏里面用表情包打架,是一种什么体验

在日常的聊天中&#xff0c;不少人都会随手发一些表情包表达自己的心情&#xff0c;其中就有不少是动物的表情包。早些时候&#xff0c;推特上曾经流行过一些沙雕的动物表情包&#xff0c;大多数都是通过视觉上的误差&#xff0c;许多网友发挥自己丰富的想象力&#xff0c;让这…

win8计算机丢失xinput1+3.dll,xinput1 3.dll丢失怎么办 win8下xinput1 3.dll丢失解决方法

xinput1 3.dll是Microsoft DirectX for Windows的控制模块,它适合于WinXP,Vista,Win7,Win8系统。当运行程序或者游戏时&#xff0c;系统弹出错误提示“ 找不到 xinput1 3.dll ”,或者“没有找到 xinput1 3.dll ”时&#xff0c;说明您系统中缺失这个dll文件或者该dll文件没有被…