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

}

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

  1. html5 白云,html5绘制一朵逼真的白云动画特效

    html5绘制一朵逼真的白云动画特效,蓝蓝的天空中一朵白色模糊云朵飘动着. 查看演示 下载资源: 11 次 下载资源 下载积分: 10 积分 js代码 var Cloud = {}; Cloud.in ...

  2. html canvas图片上绘制文字,如何用HTML5 CANVAS绘制文字

    您可能感兴趣的话题: 绘制文字 核心提示:如何在HTML5 canvas上绘制绘制文字,并且可以设置文字的字体,大小和颜色. 我们可以在HTML5 canvas上绘制绘制文字,并且可以设置文字的字体, ...

  3. 在Canvas画布上绘制直线

    代码: <body><canvas style = "border:1px solid red"></canvas><br>< ...

  4. html5 放大镜效果,JavaScript+HTML5 canvas实现放大镜效果完整示例

    本文实例讲述了JavaScript+HTML5 canvas实现放大镜效果.分享给大家供大家参考,具体如下: 效果: www.jb51.net canvas放大镜 #copycanvas { bord ...

  5. html5三维空间效果,基于HTML5的空间环境数据三维成像研究与应用

    摘要: 自上世纪五,六十年代载人航天技术发展以来,研究人员对空间环境开始进行系统化及规模化的研究.他们根据对地球轨道参数及地磁参数等的研究,确定了多种粒子或剂量,并开始对初级粒子及剂量进行测量和分析. ...

  6. html5陀螺仪效果,基于HTML5陀螺仪实现移动动画效果

    这次给大家带来基于HTML5陀螺仪实现移动动画效果,基于HTML5陀螺仪实现移动动画效果的注意事项有哪些,下面就是实战案例,一起来看一下. 最近用ofo小黄车App的时候,发现以前下方扫一扫变成了一个 ...

  7. html5落叶效果,使用Html5实现树叶飘落的效果

    实现如图所示的东西效果(落叶下落): html代码: HTML5树叶飘落动画 这是基于webkit的落叶动画 css代码:body{ background-color: #4E4226; }#cont ...

  8. html5图钉效果,图钉风格在美国现代设计发展中的作用与影响

    图钉风格在美国现代设计发展中的作用与影响 20世纪50到60年代,一群美国的年轻设计家开始运用摄影和插图混合的手法进行平面设计,注重个人观念的形象表达,注重把设计与艺术形式结合起来,注重平面设计的感性 ...

  9. html5伸缩效果,【HTML5】Jquery打造竖向伸缩/展开菜单

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 效果图如下: jquery打造竖向伸缩/展开菜单 body { font-family: Arial; font-size: 16px; } dl { w ...

最新文章

  1. 指针的底层原理与使用
  2. wsdd文件是怎么生成的_Axis WSDD文件参考文档
  3. 又来说一下顺序~关于唯一索引和唯一约束的顺序
  4. numa对MySQL多实例性能影响
  5. 李航第六章的BFGS算法
  6. synchronized 异常_由浅入深,Java 并发编程中的 Synchronized
  7. java加载sql2016_SQl Server 2016 with R.
  8. CSS 实现加载动画之五-光盘旋转
  9. 借助 Cloud Toolkit 快速创建 Dubbo 工程
  10. 容器撑满占满整个高度,垂直居中等
  11. 离合器预减振超载造成变速箱怠速异响matlab与python仿真分析
  12. Linux加固(转)
  13. Coolite Toolkit学习笔记四:容器控件之FiledSet、Panel和Window
  14. 红帽学习笔记[RHCSA] 第六课[进程、服务相关]
  15. PowerPC L2-Cache Sram
  16. OpenSSL密码库算法笔记——第6.1章 密钥
  17. STM32 Boot模式设置方法
  18. 库存系统 代码 java_商品库存管理系统java源代码
  19. 宝塔面板网站解决跨域问题
  20. pagehelper返回的total总是等于pagesize问题解决

热门文章

  1. 麒麟信安:根植于openEuler,走操作系统自主创新之路
  2. Docker 概念很难理解?一文搞定 Docker 端口绑定
  3. java socket 线程池_程序员:java使用线程池和TCP实现简单多轮聊天系统
  4. ajax post 没有返回_Ajax异步技术之三:jQuery中的ajax学习
  5. hivesql修改字段类型_Hive SQL语法总结
  6. linux c url下载文件,OpenCV教程之使用cmake生成MakeFile时下载文件
  7. 500 OOPS: vsftpd: both local and anonymous access disabled
  8. 数据装载全/存量直接装载到目标表_09
  9. Spring Schema整合Quartz_01
  10. spring项目链接RabbitMQ集群