【问题标题】:d3.js with coffeescript - use the timer make strange behaviord3.js 与咖啡脚本 - 使用计时器做出奇怪的行为
【发布时间】:2014-11-14 22:41:25
【问题描述】:

我从 d3.js 开始,我不明白一个奇怪的调用函数。我正在使用咖啡脚本,这是我的代码:

它不起作用:

handleTick = () ->
    t += 0.01   
    d3.select('.ball').attr({cx: (width / 2) + 100 * Math.cos(w * t), cy: (height / 2) + 100 * Math.sin(w * t)})    
d3.timer handleTick

它工作:

handleTick = () ->
    t += 0.01   
    console.log d3.select('.ball').attr({cx: (width / 2) + 100 * Math.cos(w * t), cy: (height / 2) + 100 * Math.sin(w * t)})    
d3.timer handleTick

谢谢!

【问题讨论】:

  • 阅读:github.com/mbostock/d3/wiki/Transitions#d3_timer - 这两个函数的结果有何不同?请记住,与 JavaScript 不同,CoffeeScript 会自动返回最后一个表达式的值,即使没有 return
  • 我试图添加一个明确的真实回报,但仍然不是北斗

标签: animation d3.js timer attributes coffeescript


【解决方案1】:

试试

handleTick = () ->
  t += 0.01   
  d3.select('.ball').attr({cx: (width / 2) + 100 * Math.cos(w * t), cy: (height / 2) + 100 * Math.sin(w * t)})
  return 0

正如 cmets 中提到的,coffeescript 总是返回最后一个表达式,因为 console.log 语句返回 void,所以 d3.select 语句可能返回 api 不期望的东西是有道理的。

【讨论】:

  • Void 是保留字,coffee 编译器返回错误
  • 啊,@user2864740 确实如此。改变我的答案来解决这个问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-10-28
  • 2014-01-13
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 2016-01-05
相关资源
最近更新 更多