python粒子风暴代码_从WebSocket获取数据的粒子风暴动画
JavaScript
语言:
JaveScriptBabelCoffeeScript
确定
(function() {
'use strict';
var socket = new WebSocket('ws://duel.uncontext.com:80'),
d;
// cross browser requestAnimationFrame loop to handle the animation looping
var rAF = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
var winWidth, winHeight, maxRadius,
minRadius = 10,
gridCols, gridRows,
particles,
tracer = {},
origin = {},
countChangeRate = 5,
gridSize = 7,
particleSize = 2,
canvas, ctx,
avgFPS = 60,
timer = (new Date()).getTime(),
frameBuffer = [60, 60, 60, 60, 60, 60, 60, 60, 60, 60],
FPS = 0,
tick = 1,
fillStyles = [],
friction = 0.97,
initialized = false,
initializing = true;
// ------------------------ //
// #### Particle Class #### //
// ------------------------ //
function Particle() {
var _p = this;
_p.newTracer = true;
_p.x = tracer.x;
_p.y = tracer.y;
_p.fillOffset = 0;
_p.tracer = {
x: tracer.x,
y: tracer.y
};
// start this particle with velocity
_p.velx = Math.random() * 10 - 5;
_p.vely = Math.random() * 10 - 5;
}
// Setup a common draw function that will be called
// by all members of the Particle class
Particle.prototype.draw = function() {
var _p = this,
box = {
t: Math.floor(_p.y / gridSize) * gridSize,
l: Math.floor(_p.x / gridSize) * gridSize,
r: Math.ceil(_p.x / gridSize) * gridSize,
b: Math.ceil(_p.y / gridSize) * gridSize
};
if (Math.abs(_p.velx) + Math.abs(_p.vely) < 1) {
ctx.fillRect(box.l + particleSize, box.t + particleSize, gridSize - particleSize * 2, gridSize - particleSize * 2);
} else if (Math.abs(_p.velx) > Math.abs(_p.vely)) {
if (_p.y - box.b > box.t - _p.y) {
ctx.fillRect(_p.x, box.t, particleSize, particleSize);
} else {
ctx.fillRect(_p.x, box.b, particleSize, particleSize);
}
} else {
if (_p.x - box.l > box.r - _p.x) {
ctx.fillRect(box.r, _p.y, particleSize, particleSize);
} else {
ctx.fillRect(box.l, _p.y, particleSize, particleSize);
}
}
if (_p.newTracer) {
ctx.fillRect(Math.floor(_p.tracer.x / gridSize) * gridSize, Math.floor(_p.tracer.y / gridSize) * gridSize, gridSize, gridSize);
_p.newTracer = false;
}
};
Particle.prototype.update = function(tick) {
var _p = this,
_rnd = Math.random(),
_vel,
_t = _p.tracer,
diffX, diffY,
maxBurst = 10,
burst;
diffX = _t.x - _p.x;
diffY = _t.y - _p.y;
if ((Math.abs(_p.velx) < 1 && Math.abs(_p.vely) < 1) && (Math.abs(diffX) < 1 && Math.abs(diffY) < 1)) {
burst = maxBurst * d.d + 2;
_p.velx = Math.random() * burst - (burst / 2);
_p.tracer.x = tracer.x + (d.d * maxRadius + minRadius) * Math.sin(_rnd * (Math.PI * 2));
_p.vely = Math.random() * burst - (burst / 2);
_p.tracer.y = tracer.y + (d.d * maxRadius + minRadius) * Math.cos(_rnd * (Math.PI * 2));
if (!d.c) {
_p.x = origin.x;
_p.y = origin.y;
}
_p.fillOffset = 100 * d.e;
_p.newTracer = true;
// _p.tracer.y = d.a[1] * winHeight;
}
_p.velx += diffX / 1000;
_p.vely += diffY / 1000;
// friction
_p.velx *= friction;
_p.vely *= friction;
// We don't want to let the particles leave the
// area, so just change their position when they
// touch the walls of the window
if (_p.x >= winWidth && _p.velx > 0 || _p.x < 0 && _p.velx < 0) {
_p.velx *= -1;
}
if (_p.y >= winHeight && _p.vely > 0 || _p.y < 0 && _p.vely < 0) {
_p.vely *= -1;
}
// tick adjust
_p.x += _p.velx * tick;
_p.y += _p.vely * tick;
_vel = Math.abs(_p.velx) + Math.abs(_p.vely);
_p.fillStyle = Math.min(Math.round((_vel / 15) * 99), 99) + (_p.fillOffset);
_p.fillStyle = (_p.fillStyle + 100) % 100;
};
// ------------------------------ //
// #### Draw the canvas data #### //
// ------------------------------ //
function drawScene() {
var i, len, f, _p;
ctx.fillStyle = 'rgba(20, 20, 20, 80.15)';
ctx.fillRect(0, 0, winWidth, winHeight);
_p = particles[i - 1];
for (len = fillStyles.length, f = 0; f < len; f++) {
// draw each particle
i = particles.length;
ctx.fillStyle = fillStyles[f];
for (; i--;) {
_p = particles[i];
if (_p.fillStyle !== f) {
continue;
}
_p.draw();
}
}
// check if we are connected
if (socket.readyState != 1) {
ctx.lineWidth = 1;
ctx.fillStyle = "#FFFFFF";
ctx.font = "24px sans-serif";
ctx.fillText("Can't connect to the uncontext server :-(", 100, 100);
ctx.fillText("...please try again later", 150, 140);
} else {
// update the scene
update();
}
// schedule the next update
rAF(drawScene);
}
// ------------------------------------------------ //
// #### Update the scene, animate all elements #### //
// ------------------------------------------------ //
function update() {
// 1000ms is 1s, divided by the number of ms it took
// us since last update gives us how many times we
// could have performed this frame within 1 second (FPS)
var _timer = (new Date()).getTime();
var _lastFPS = FPS;
var i;
FPS = 1000 / (_timer - timer);
frameBuffer.shift();
frameBuffer.push(FPS);
// average the new FPS value with the last one to ease the change rate
avgFPS = frameBuffer.reduce(function(prev, cur) {
return prev + cur;
}, 0) / frameBuffer.length;
timer = _timer;
// tick is how far from the ideal speed we are
// if we are rendering slower, this number will be higher.
// we'll use this to adjust the amount of movement we perform
// on each particle per frame.
tick = 60 / FPS;
i = countChangeRate;
if (avgFPS > 58) {
for (; i--;) {
particles.push(new Particle());
}
} else if (avgFPS < 55) {
for (; i--;) {
particles.pop();
}
}
// look at each particle and evaluate how it should move
// next based on it's current valocity and attraction to
// any other particles that are close enough.
for (i = particles.length; i--;) {
particles[i].update(tick);
}
}
// -------------------------------------------- //
// #### Initialize and setup the animation #### //
// -------------------------------------------- //
function init() {
var i;
initializing = true;
// Get a reference to our canvas
canvas = document.getElementById("canvas") || document.createElement("canvas");
canvas.id = 'canvas';
document.body.appendChild(canvas);
sizeCanvas();
ctx = canvas.getContext("2d");
// setup init values
particles = [];
for (i = 100; i--;) {
fillStyles[i] = 'hsla(' +
(360 * (i / 99)) + ', ' +
'100%, 60%, 0.4)';
}
// create all of our particles and push them into our main array to hold.
i = countChangeRate;
for (; i--;) {
particles[i] = new Particle();
}
if (!initialized) {
drawScene();
bindEvents();
initialized = true;
}
initializing = false;
}
function sizeCanvas() {
winHeight = window.innerHeight;
winWidth = window.innerWidth;
maxRadius = Math.min(winWidth, winHeight) / 2;
canvas.width = winWidth;
canvas.height = winHeight;
canvas.style.width = winWidth + 'px';
canvas.style.height = winHeight + 'px';
}
// -------------------------------------------------- //
// #### Set up the event bindings for user input #### //
// -------------------------------------------------- //
function bindEvents() {
// resize canvas when window resizes
window.addEventListener('resize', init, false);
}
// Initialize the animation
socket.onmessage = function(data) {
var initCode = d == null;
d = JSON.parse(data.data);
tracer.x = d.a[0] * (winWidth - maxRadius) + (maxRadius / 2);
tracer.y = d.a[1] * (winHeight - maxRadius) + (maxRadius / 2);
origin.x = ((d.b[0] + d.e) / 2) * winWidth;
origin.y = ((d.b[1] + d.e) / 2) * winHeight;
};
init();
}());
python粒子风暴代码_从WebSocket获取数据的粒子风暴动画相关推荐
- python面板数据分析代码_对于大面板数据,回归就绪格式的Excel到Python?
试图从Excel中获取一些大面板数据到python中,所以我可以做一些GMM /横截面面板数据回归分析(想想sci-kit软件包).我把我的数据从excel移到了Python,但是回归分析的格式不正确 ...
- python爬虫登录微博_为爬虫获取登录cookies: 使用Charles和requests模拟微博登录
上一节,我们讲了如何配置Charles代理,这一节我们通过模拟微博登录这个例子来看看如何使用Charles分析网站加载流程,顺便把微博模拟登录的Python代码也给实现了. 1. 用Charles记录 ...
- python嵌入c代码_怎样把Python代码嵌入到C程序
匿名用户 1级 2017-11-03 回答 这篇文章主要介绍了将Python代码嵌入C++程序进行编写的实例,尽管通常还是Python代码中调用C++程序的情况较多...需要的朋友可以参考下 把pyt ...
- 前端websocket获取数据后需要存本地吗_是什么让我放弃了restful api?了解清楚后我全面拥抱GraphQL...
GraphQL初步认识 背景 REST作为一种现代网络应用非常流行的软件架构风格,自从Roy Fielding博士在2000年他的博士论文中提出来到现在已经有了20年的历史.它的简单易用性,可扩展性, ...
- 前端websocket获取数据后需要存本地吗_是什么让我放弃了Restful API?了解清楚后我全面拥抱GraphQL!...
背景 REST作为一种现代网络应用非常流行的软件架构风格,自从Roy Fielding博士在2000年他的博士论文中提出来到现在已经有了20年的历史.它的简单易用性,可扩展性,伸缩性受到广大Web开发 ...
- python二手交易平台代码_使用Python探索二手车市场(含代码)
原标题:使用Python探索二手车市场(含代码) 前言 上一期中,我们已经手把手的分享了如何使用Python实现某二手车平台数据的抓取,并成功的完成11,281条二手车数据的搜集.基于此,我们需要对获 ...
- python新年有趣代码_搞几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码!...
下载好向圈APP可以快速联系圈友 您需要 登录 才可以下载或查看,没有帐号?立即注册 x 为提高大家对"Python"编程语言的学习兴趣,今天给大家分享几款有趣的Python程序代 ...
- python灰色模型代码_几行代码搞定ML模型,低代码机器学习Python库正式开源
机器之心报道 机器之心编辑部 PyCaret 库支持在「低代码」环境中训练和部署有监督以及无监督的机器学习模型,提升机器学习实验的效率. 想提高机器学习实验的效率,把更多精力放在解决业务问题而不是写代 ...
- python画五角星代码_Python GUI 编程tkinter--画五角星和简单的动画制作
1.利用Python的tkinter画一个五角星: 2.做一个简单的动画 分析五角心的五个顶点的坐标: 确立五角星的中心点和半径,再确立五角星的五个角的坐标信息,其中五角星的各个角读书为36度,具体的 ...
最新文章
- LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
- 把原来的所有Blog全部转移过来了:-P
- 关于fragment backstate的运用
- 如何从ios酷我音乐盒中导出已下载的音乐文件(使用Java编程实现)
- 不存在_施文忠 | ”存在“与“不存在”——巴蜀文明概论
- 当年,兔子学姐靠这个面试小抄拿了个22k
- Essential Grouping高性能的数据分组引擎介绍及下载
- 12015.linux通过代码或命令形式操作内存/dev/mem
- 拿 1% 月收入买比特币,比养老金更靠谱! —— CSDN 蒋涛答王峰十问
- 小马儿随笔(七)——综合布线的安装时代
- 《穿越计算机的迷雾》读书笔记九
- 【MATLAB】图像分割
- 洗衣机也时尚?UDE展上你不能错过的家电好物!
- c语言项目研发实训,C语言实训项目表V1.0.doc
- HQChart使用教程60-新版k线训练使用教程
- 上海科学家研制出新型“耐火宣纸”
- Java 程序员必备的辅助开发神器(2022 年版),建议收仓
- 因子分析(factor analyis)
- tensorflow学习系列
- 【java】Java连接mysql数据库及mysql驱动jar包下载和使用
热门文章
- python代码html显示数据_Python爬虫基础之认识html和学习数据提取(上)
- TimedCache 带时间缓存工具类,附加监听回调 | Java工具类
- Composer安装与PHPWord的下载与使用
- 云队友丨可乐百年之争的启示
- iOS 和 swift 中常见的 Int、Int8、Int16、Int32和 Int64介绍
- 莫道桑榆晚,为霞尚满天
- vlookup使用步骤_vlookup怎么用详细步骤(vlookup函数的使用方法是什么)
- 说好不哭,现在就带你了解直播类音视频测试
- 阿里云田涛涛解读未来自动化运维新思路:CloudOps
- canvas绘制中国象棋棋盘