2

我将 Raphael 与 Meteor 一起使用,但遇到了问题。我正在paper使用创建一个var paper = Raphael("paper", 800, 600); 如果我把这段代码放在一个渲染函数中,比如 Template.board.rendered,我可以很好地使用它。但是,这个模板中的纸质变量显然不能被其他函数访问,即使我从模板中调用它们也是如此。我认为我可以通过在 中创建论文来解决这个Meteor.startup问题,但我仍然遇到同样的问题。

例如,以下代码不起作用:

Pieces = new Meteor.Collection('pieces');

Meteor.startup(function () {
  var paper = Raphael("paper", 800, 600);
});

Template.board.rendered = function () {
  // If I create the paper in here along with the contents of createGoban
  // it works, but then I can't use it anywhere else...
  // var paper = Raphael("paper", 800, 600);

  createGoban();
};

var createGoban = function () {
  // Create goban
  // Based off of svg in public domain: http://commons.wikimedia.org/wiki/File:Blank_Go_board.svg
  // Converted using http://readysetraphael.com/
  var rect_a = paper.rect(0, 0, 96, 96);
  rect_a.attr({fill: '#DCB35C','stroke-width': '0','stroke-opacity': '1'}).data('id', 'rect_a');
  var path_b = paper.path("M2.9,93h90.2m-.2-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m-.1-5h90.2M3,3V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3");
  path_b.attr({stroke: '#000',"stroke-width": '0.2',fill: 'none','stroke-opacity': '1'}).data('id', 'path_b');
  var path_c = paper.path("M18,78l0,0m30,0l0,0m30,0l0,0m0-30l0,0m-30,0l0,0m-30,0l0,0m0-30l0,0m30,0l0,0m30,0l0,0");
  path_c.attr({stroke: '#000',"stroke-width": '4',"stroke-linecap": 'round','stroke-opacity': '1','fill': '#000000'}).data('id', 'path_c');
  var goban = [rect_a, path_b, path_c];

  // Scale goban up since rsr outputs reduced size
  for(var i = 0; i < goban.length; i++) {
    goban[i].transform('S5,5,0,0');
  };
};
4

1 回答 1

3

删除varinside后,您的代码可以正常工作startup(),这使得paper全局:

Meteor.startup(function () {
  paper = Raphael("paper", 800, 600);
});
于 2013-03-13T07:42:57.217 回答