使用新数据更新每个 AJAX 请求的 HTML 画布标签

2023-06-20前端开发问题
6

本文介绍了使用新数据更新每个 AJAX 请求的 HTML 画布标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果找到新用户或现有用户有新连接,我想在每个 AJAX 请求上更新我的画布.

我有用户和他们之间的联系:

 var data=[{身份证":38,连接":[39,40],名称":ABC"},{身份证":39,连接":[40],名称":pqr"},{身份证":40,连接":[],名称":lmn"}]

在上面的示例中,具有 Id:38 的用户连接到 user 39 和 40 并且 user 39 连接到用户 40 和用户 40 已经连接到 user 39 和 user 38 所以 user 40 连接数组是空白的.

我有一个网络服务,我将每 1-2 秒触发一次,以在此页面上显示新加入的用户,以及我存储在我的表中的现有用户之间的新连接,此网络服务将获取所有用户及其连接.

所以一开始我的页面会加载,但之后我的页面不会刷新,并且应该显示新用户,并且应该通过 AJAX 调用连接到我的 Web 服务的新连接.

但是有了 ajax 请求,一切都变得一团糟.这是我创建的

正如您在我的 JSBin 输出中看到的那样,我只有 4 个用户和 3 个连接,那么它应该只有 4 个矩形,我没有添加更多用户,但仍然得到这个混乱的输出.

源码参考:

解决方案

盒子没有移动,因为移动它们的代码从未被调用过.

如果您将动画框的部分从 updateUsers() 移动到您的主动画循环,它们将动画化.

从这部分移动部分:

function updateUsers() {获取数据();//这部分  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  ---------$.each(用户,功能(索引,用户){if (user.x <= 0) user.dir.x = 1;if (user.x + user.width > canvas.width) user.dir.x = -1;if (user.y <= 0) user.dir.y = 1;if (user.y + user.height > canvas.height) user.dir.y = -1;用户.x += 用户.dir.x;用户.y += 用户.dir.y;});//到这里  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -----------//window.setTimeout(updateUsers, 1000/60);window.setTimeout(updateUsers, 9000);}

到这里:

function drawUsers() {ctx.clearRect(0, 0, canvas.width, canvas.height);$.each(用户,功能(索引,用户){ctx.beginPath();ctx.rect(user.x, user.y, user.width, user.height);ctx.strokeStyle = '红色';ctx.stroke();if (user.connections != null) {user.connections.forEach(function(id) {const other = users.find(user => user.id === id);ctx.beginPath();ctx.moveTo(user.x + (user.width/2), user.y + (user.height/2));ctx.lineTo(other.x + (other.width/2), other.y + (other.height/2));ctx.strokeStyle = '黑色';ctx.stroke();});}});//这里动画$.each(用户,功能(索引,用户){if (user.x <= 0) user.dir.x = 1;if (user.x + user.width > canvas.width) user.dir.x = -1;if (user.y <= 0) user.dir.y = 1;if (user.y + user.height > canvas.height) user.dir.y = -1;用户.x += 用户.dir.x;用户.y += 用户.dir.y;});window.requestAnimationFrame(drawUsers);}

由于我们无法访问您正在使用的 Web 服务,并且假设数据已正确返回,我不得不取消对预定义数据的注释以进行工作演示:

更新的 jsbin

I want to update my canvas on every AJAX request if new user is found or there is new connection of existing users.

I have users and connections between them:

 var data=[
           {
              "Id": 38,
               "Connections":[39,40],
               "Name":"ABc"
          },
           {
              "Id": 39,
               "Connections":[40],
               "Name":"pqr"
           },
           {
               "Id": 40,
               "Connections":[],
               "Name":"lmn"
           }]

In the above example user with Id:38 is connected to user 39 and 40 and user 39 is connected to user 40 and user 40 is already connected to user 39 and user 38 so user 40 Connection array is blank.

I have one web service which I will fire every 1-2 seconds to display newly joined users on this page and new connection between existing users which I have stored in my table and this web service will fetch all users along with their connections.

So at first my page will load but after then my page will not refresh and new users should be displayed and new connections should be connected with AJAX call to my web service.

But with ajax request everything gets messed up. This is a JSBin I have created.

Output I am getting:

I have just 4 users and 3 connections as you can see in my JSBin output then it should be just 4 rectangles and I am not adding more users but still getting this messy output.

Source code reference: Reference Fiddle

I am trying to get the output as shown in above fiddle but not getting.

Expected output: 4 rectangles with animation

Updated Js bin Demo: Updated JSBin

With above updated JSBin I am getting this output but they are not moving (animation is not working):

解决方案

The boxes aren't moving because the code for moving them is never called.

If you move the part animating the boxes from updateUsers() to your main animation loop they will animate.

Move the section from this part:

function updateUsers() {
  fetchData();

  // this part ------------------------------------------------------
  $.each(users, function(index, user) {
    if (user.x <= 0) user.dir.x = 1;
    if (user.x + user.width > canvas.width) user.dir.x = -1;
    if (user.y <= 0) user.dir.y = 1;
    if (user.y + user.height > canvas.height) user.dir.y = -1;

    user.x += user.dir.x;
    user.y += user.dir.y;
  });
  // to here --------------------------------------------------------

  //window.setTimeout(updateUsers, 1000 / 60);
  window.setTimeout(updateUsers, 9000);
}

to here:

function drawUsers() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  $.each(users, function(index, user) {
    ctx.beginPath();
    ctx.rect(user.x, user.y, user.width, user.height);
    ctx.strokeStyle = 'red';
    ctx.stroke();

    if (user.connections != null) {
      user.connections.forEach(function(id) {
        const other = users.find(user => user.id === id);

        ctx.beginPath();
        ctx.moveTo(user.x + (user.width / 2), user.y + (user.height / 2));
        ctx.lineTo(other.x + (other.width / 2), other.y + (other.height / 2));
        ctx.strokeStyle = 'black';
        ctx.stroke();
      });
    }
  });

  // animate here
  $.each(users, function(index, user) {
    if (user.x <= 0) user.dir.x = 1;
    if (user.x + user.width > canvas.width) user.dir.x = -1;
    if (user.y <= 0) user.dir.y = 1;
    if (user.y + user.height > canvas.height) user.dir.y = -1;

    user.x += user.dir.x;
    user.y += user.dir.y;
  });
  window.requestAnimationFrame(drawUsers);
}

Since we don't have access to the web service you're using, and assuming the data is correctly returned, I had to uncomment the predefined data to make a working demo:

Updated jsbin

这篇关于使用新数据更新每个 AJAX 请求的 HTML 画布标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125