通过 Meteor 更新 KineticJS Canvas 元素

Updating a KineticJS Canvas element via Meteor

本文关键字:Canvas 元素 KineticJS 更新 Meteor 通过      更新时间:2023-09-26

>我正在尝试通过Meteor更新KineticJS对象的位置。

看来问题出在:

  Players.update({name: "Rect"}, {xpos: this.attrs.x})

流星文档是这么说的:

  // Find the document with id "123", and completely replace it.
  Users.update({_id: "123"}, {name: "Alice", friends: ["Bob"]});

我试图检查数据是否通过以下方式更新:

  console.log(Players.findOne({name: "Rect"}).xpos);

这是 github:

https://github.com/randompast/randomtests/tree/master/meteorCanvasTest

首先,始终使用$set来更新您的属性,以免您踩到名称之类的东西。由于您在后续更新中踩到了该名称,因此没有名为"rect"的属性要更新。 Players.update({name: "Rect"}, {$set: {xpos: this.attrs.x}})

if (Meteor.is_client) {
  Players.find().observe({
    changed: function(new_doc, idx, old_doc) {
      if(MyTest.rect) {
        MyTest.rect.attrs.x = new_doc.xpos;
        MyTest.layer.draw();
      }
    }                      
  });  
  ....
    MyTest.rect.on("dragend", function() {
      Players.update({name: "Rect"}, {$set: {xpos: this.attrs.x}});
    });
  ....
}

只需插入该观察函数,并确保您的dragend使用$set表示法即可。