简体   繁体   English

如何使用Javascript将流数据(已转换为颜色的数据)不断地推送到index.html中的连续div中?

[英]How can I push constantly streaming data (which I have converted to color) into successive divs in my index.html using Javascript?

I am trying to turn the computer screen into a grid and then push data (in the form of color) into each square of the grid. 我试图将计算机屏幕变成一个网格,然后将数据(以颜色的形式)推送到网格的每个正方形中。 When one square is full I want to fill the next one. 当一个正方形已满时,我想填充下一个。 I have the data streaming in using Socket.io. 我有使用Socket.io的数据流。 I'd appreciate some suggestions on how to adjust the Javascript below to enable this? 我将对以下有关如何调整Javascript以实现此功能的建议表示赞赏。

My html is a series of rows that look like this: 我的html是一系列看起来像这样的行:

<body>
<div class="row">
  <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

My CSS: 我的CSS:

body {
  text-align: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.row {
  line-height: 0;
  font-size: 0;
  overflow: hidden;
  height: 50px;
  width: 100%;
}

.row > div {
  display: inline-block;
  width: 50px;
  height: 50px;
}

My client-side javascript: 我的客户端javascript:

var socket = io()

socket.on('connect', function () {
  console.log('client is connected');
})

socket.on('color', function (data) {
  const { rgb } = data
  document.body.style.backgroundColor = 'rgb(' + [rgb[0], rgb[1], rgb[2]].join(',') + ')';

});

Here is the new code using socket.io: 这是使用socket.io的新代码:

var socket = io()
const div = document.querySelector(".row>div"); 

socket.on('connect', function () {
})

socket.on('color', function (data) {

  const { rgb } = data

  div.style.backgroundColor = 'rgb(' + [rgb[0], rgb[1], rgb[2]].join(',') + ')';
  if (div.nextElementSibling) {
   div = div.nextElementSibling;
  }
  else {
   div = div.parentElement.children[0];
 }
});

You can select your div with .querySelector(query) and then move to the next one with .nextElementSibling . 您可以使用.querySelector(query)选择div,然后使用.nextElementSibling移至下一个。 When you are at the last one you need to select the first child of the parent with parentElement.children[0] . 当您位于最后一个时,您需要使用parentElement.children[0]选择父级的第一个子级。

Here is an example with pure WebSockets, but you should be able to make it work with Socket.io: 这是纯WebSockets的示例,但是您应该能够使其与Socket.io一起使用:

 var div = document.querySelector(".row>div"); // select the first div var websocket = new WebSocket("wss://echo.websocket.org/"); websocket.onmessage = function(evt) { var rgb = JSON.parse(evt.data); // change the color of the div div.style.backgroundColor = 'rgb(' + [rgb[0], rgb[1], rgb[2]].join(',') + ')'; if (div.nextElementSibling) // if a div is not last, move to the next one div = div.nextElementSibling; else // if the div is last move to the first one div = div.parentElement.children[0]; }; // code just to make the demo work and trigger the onmessage event websocket.onopen = function(evt) { n = 0; setInterval(function() { websocket.send(JSON.stringify([255*((n>>2)&1), 255*((n>>1)&1), 255*(n&1)])); n++; }, 100); } 
 body { text-align: center; width: 100%; height: 100%; overflow: hidden; } .row { line-height: 0; font-size: 0; overflow: hidden; /*height: 50px;*/ width: 100%; } .row > div { display: inline-block; width: 50px; height: 50px; } 
 <div class="row"> <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 index.html 中 body 的背景颜色? - How can I change the background color of the body in index.html? 我的Javascript Ajax请求可在Phonegap index.html中使用,但不能在其他任何页面中使用,如何解决此问题? - My Javascript Ajax request works in Phonegap index.html but not in any other pages, how can I fix this? 如何使用 JavaScript 在我的 index.html 文件中嵌入 API 调用? - How do I embed an API call in my index.html file using JavaScript? 如何将index.html中的数据迁移到控制器? - How can I migrate data in index.html to a controller? 使用webpack时如何移动“index.html”? - How can I move “index.html” when using webpack? 如何从index.html加载我的Aurelia应用,该索引与应用的其余部分不在同一文件夹中? - How can I load my Aurelia app from an index.html which is not located in the same folder as the rest of the application? Express JS:我还能吗 <scripts> 在我的index.html中 - Express JS: Can I still have <scripts> in my index.html 如何将index.html中的内容放入我的view.html? - How can I put content from my index.html into my view.html? 当我将 javascript 文件添加到 index.html 时,数据未在我的数据库中重新编码 - Data isnt recode in my database when i add javascript file to the index.html 我可以在github页面站点中提供一个javascript文件而不是index.html吗? - Can I serve a javascript file instead of index.html in my github pages site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM