html信号动画,HTML5带音效的雷达检测信号动画
JavaScript
语言:
JaveScriptBabelCoffeeScript
确定
var c, cx,
width,
height,
axis,
speed = 4,
notes,
smokeParticles,
numSmokeParticles = 15,
centre = {
x: 0,
y: 0
},
animationFrameId,
tau = Math.PI * 2,
axisLength = 200,
axisSpeed = 0.01,
resetSwitch = document.getElementById('reset'),
audioCx = new(window.AudioContext || window.webkitAudioContext)();
resetSwitch.onclick = function(e) {
e.preventDefault();
window.cancelAnimationFrame(animationFrameId);
for (let n = 0; n < notes.length; n++) {
let note = notes[n];
if (note.osc) note.osc.stop();
}
init();
loop();
};
window.addEventListener('resize', function() {
width = c.width = window.innerWidth;
height = c.height = window.innerHeight;
}, false);
window.addEventListener('click', function(e) {
checkNote(e.pageX, e.pageY);
});
class SmokeParticle {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = rand(1, 4);
this.dx = rand(-0.5, 0.5);
this.dy = rand(-0.5, 0.5);
this.age = 0;
this.lifeSpan = rand(30, 80);
this.colours = newColours();
}
draw() {
cx.beginPath();
cx.arc(this.x, this.y, this.radius, 0, tau);
cx.fillStyle = 'rgba(' + this.colours.r + ',' + this.colours.g + ',' + this.colours.b + ', 1)';
cx.fill();
}
step() {
this.y += this.dy;
this.x += this.dx;
this.age++;
}
}
class Axis {
constructor(x, y) {
this.x = x;
this.y = y;
this.colours = newColours();
this.endX = x + axisLength;
this.endY = y;
this.angle = 0;
}
step() {
this.angle += axisSpeed;
if (this.angle >= tau) this.angle = 0;
this.endX = axisLength * Math.cos(this.angle) + this.x;
this.endY = axisLength * Math.sin(this.angle) + this.y;
}
draw() {
cx.beginPath();
cx.moveTo(this.x, this.y);
cx.lineTo(this.endX, this.endY);
cx.lineWidth = 1;
cx.strokeStyle = 'rgba(' + this.colours.r + ',' + this.colours.g + ',' + this.colours.b + ', 1)';
cx.stroke();
}
}
class Note {
constructor(x, y) {
this.x = x;
this.y = y;
this.colours = newColours();
this.permColours = this.colours;
this.radius = 5;
this.hitLife = 40;
this.dying = 0;
this.beenHit = false;
this.distance = lineDistance({
x: this.x,
y: this.y
}, {
x: centre.x,
y: centre.y
});
}
draw() {
cx.beginPath();
cx.arc(this.x, this.y, this.radius, 0, tau);
cx.fillStyle = 'rgba(' + this.colours.r + ',' + this.colours.g + ',' + this.colours.b + ', 1)';
cx.fill();
}
step() {
if (this.dying > 0) {
this.dying -= 1;
} else if (this.beenHit) {
this.beenHit = false;
this.osc.stop();
}
if (!this.beenHit) {
this.colours = this.permColours;
}
}
hit() {
if (!this.beenHit) {
this.beenHit = true;
this.dying = this.hitLife;
this.smoke();
this.colours = {
r: 255,
g: 255,
b: 255
};
this.osc = audioCx.createOscillator();
this.osc.type = 'square';
this.osc.frequency.value = this.distance + 100;
this.osc.connect(audioCx.destination);
this.osc.start();
}
}
smoke() {
for (let x = 0; x < numSmokeParticles; x++) {
smokeParticles.push(new SmokeParticle(this.x, this.y));
}
}
}
pointToLineDistance = (line, point) => {
let normalLength = Math.sqrt(Math.pow(line.endX - line.x, 2) + Math.pow(line.endY - line.y, 2));
let ptd = Math.abs((point.x - line.x) * (line.endY - line.y) - (point.y - line.y) * (line.endX - line.x)) / normalLength;
return ptd;
}
lineDistance = (point1, point2) => {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
checkNote = (x, y) => {
let removed = false;
for (let n = 0; n < notes.length; n++) {
let note = notes[n];
if (x < note.x + note.radius && x > note.x - note.radius && y < note.y + note.radius && y > note.y - note.radius) {
if (note.osc) note.osc.stop();
notes.splice(n, 1);
removed = true;
}
}
if (!removed) {
notes.push(new Note(x, y));
}
}
addSmokeParticle = () => {
let xDiff = rand(0, 1);
let yDiff = 0;
smoke.push(new SmokeParticle(player.x + xDiff, player.y + player.radius + yDiff));
}
newColours = () => {
let r = Math.floor(rand(64, 128));
let g = Math.floor(rand(0, 64));
let b = Math.floor(rand(128, 255));
return {
r, g, b
};
}
checkLineHit = (note) => {
if (pointToLineDistance(axis, note) < 1 && lineDistance({
x: axis.x,
y: axis.y
}, note) < axisLength && lineDistance({
x: axis.endX,
y: axis.endY
}, note) < axisLength) {
note.hit();
}
}
rand = (min, max) => {
return Math.random() * (max - min) + min;
}
loop = () => {
animationFrameId = window.requestAnimationFrame(loop);
cx.clearRect(0, 0, width, height);
axis.step();
axis.draw();
for (let n = 0; n < notes.length; n++) {
let note = notes[n];
note.step();
note.draw();
checkLineHit(note);
}
for (let s = 0; s < smokeParticles.length; s++) {
let smokeParticle = smokeParticles[s];
smokeParticle.step();
smokeParticle.draw();
if (smokeParticle.age >= smokeParticle.lifeSpan) {
smokeParticles.shift();
}
}
}
init = () => {
c = document.querySelector('canvas');
width = c.width = window.innerWidth;
height = c.height = window.innerHeight;
cx = c.getContext('2d');
cx.globalCompositeOperation = 'lighter';
centre.x = width / 2;
centre.y = height / 2;
notes = [];
smokeParticles = [];
axis = new Axis(centre.x, centre.y);
}
init();
loop();
html信号动画,HTML5带音效的雷达检测信号动画相关推荐
- html5点线动画,HTML5带音效的两点连线动画
JavaScript 语言: JaveScriptBabelCoffeeScript 确定 (function() { var App = function() { this.el_svg = doc ...
- php3D动画,html5的canvas实现几何模型3D运动动画效果
html5的canvas实现几何模型3D运动动画效果 JS片段: var {atan2,sqrt,sin,cos,PI,acos} = Math; function project3D(x,y,z,v ...
- iOS动画:带时分秒指针的时钟动画(上)
经过几次实验,发现如果分享的文章能构成系列,效果会非常好.同时自己也能收获很大,能够整块整块的复习,也能够帮助自己更深入的块状学习知识.对自己梳理线条,整理知识体系作用非常大. 所以这次还是打算用这种 ...
- html鼠标滑过带音效,HTML5带音效的交互式日食动画
JavaScript 语言: JaveScriptBabelCoffeeScript 确定 var moon = document.getElementById("moon"), ...
- css金币动画_HTML5 超级马里奥游戏里面的金币动画(带音效)
CSS 语言: CSSSCSS 确定 @import url(https://fonts.googleapis.com/css?family=Sacramento); /* OFF-TOPIC */ ...
- android 漩涡动画,html5 canvas酷炫的粒子漩涡动画特效
特效描述:html5 canvas 粒子漩涡 动画特效.html5粒子漩涡动画,酷炫的粒子动画特效 代码结构 1. HTML代码 function project3D(x,y,z,vars){ var ...
- html5+赛车动画,HTML5 SVG可交互拖拉的赛车动画
JavaScript 语言: JaveScriptBabelCoffeeScript 确定 var animData = { wrapper: document.querySelector('.bm_ ...
- html5标签云,TagCanvas - HTML5 Canvas技术开发的标签云动画
TagCanvas 是一个基于HTML5Canvas技术开发的标签云动画.还提供一个以jQuery插件形式实现的版本.它支持文本和图片两种格式,能够以 Sphere, hcylinder 或 vcy ...
- Html5实现生日快乐网站,自带音效与蛋糕动画!
Html5实现生日快乐网站,自带音效与蛋糕动画! 目录 前言 二.课前准备 1.工具选择 2.语言使用 三.源码展示 总结 前言 前端即网站前台部分,运行在PC端,移动端等浏览器上展现给用户浏览的网页 ...
最新文章
- 用命令行工具创建 NuGet 程序包
- ESLint 配置说明
- Netiler annotation 用法
- 数据解析,重中之重!
- c# datagridview 相关操作。
- 炸窝(Java)拼接
- 支付宝放出数字化经营新神器,可提升商家60%消费频次
- python-函数的闭包
- Make 输出重定向到文件
- 万有引力的意思_从牛顿的苹果到牛顿的大炮:万有引力定律
- Lync 2013持久聊天迁移至Skype for Business
- [2018.10.15 T3] 数列
- linux磁盘挂载特别慢,arch开机速度竟然是挂载磁盘拖慢了。。
- 优化 Laravel 网站打开速度9条
- Windows网络连接指示器,NCSI
- 转帖:免费完美激活Windows7旗舰版
- 【只推荐一位】木东居士,带着大家一起成长的数据科学大神!
- 在JavaScript实现基于原型的继承
- unzip解压缩linux文件时出现error [Testing.zip]: start of central directory not found;zipfile corrupt——7z
- 使用U3D给物体添加脚本时提示Can‘t add script component
热门文章
- 微信安卓版8.0.18内测更新 增加个人信息收集清单
- 2000以内!一加Nord 2渲染图曝光:搭载联发科天玑1200
- 阿里影业授出1672.5万份购股权
- 拜登政府撤销对Tik Tok和Wechat禁令 启动对外国应用审查
- 小米平板5采用120Hz高刷LCD屏:纯平中框+侧面指纹
- 华为留了一手!将继续发布P50、Mate50:搭载麒麟9000...
- 博纳影业IPO即将过会 阿里、腾讯均为股东
- 宠物也能买保险了!鼻纹识别就能建立专属电子档案
- 找工作的人太多导致平台崩了?BOSS直聘回应:系统故障 已修复
- 估值150亿,账上还有近10亿现金,却减员500人,这家公司CEO的说法你认同吗?...