【问题标题】:createPattern not returning as expectedcreatePattern 未按预期返回
【发布时间】:2012-10-05 02:56:40
【问题描述】:

我在变量“pattern”上收到 TypeError,并且我的脚本在标记的断点之后没有执行。我正在使用 JCanvas 和 JQuery,并且所有依赖项都已到位。错误的原因是什么。有一个类似的实现使用here

function drawMe(thumbUrl){
  function drawThumb(){
    $("canvas").drawRect({
      //Code breaks here.
      fillStyle: pattern,
      x: 200, y: 100,
      width: 250, height: 100,
      fromCenter: false         
    });
  }

  var pattern = $("canvas").createPattern({
    source: thumbUrl,
    repeat: "repeat",
    load: drawThumb
  })

}

drawMe("http://placehold.it/100x100");

【问题讨论】:

    标签: javascript jquery scope variable-types jcanvas


    【解决方案1】:

    我认为你需要改变这个

    function drawThumb(){
        $("canvas").drawRect({
          //Code breaks here.
          fillStyle: pattern,
          x: 200, y: 100,
          width: 250, height: 100,
          fromCenter: false         
        });
      }
    

    到这里

    function drawThumb(pattern){
        $("canvas").drawRect({
          //Code breaks here.
          fillStyle: pattern,
          x: 200, y: 100,
          width: 250, height: 100,
          fromCenter: false         
        });
      }
    

    【讨论】:

    • 我不这么认为。 'pattern' 是稍后定义的 var。但是有些事情完全混淆了。 var-definition 引用了之前未定义但之前定义为函数参数的“thumbUrl”。我想说:重构你的代码,使用调试器。
    【解决方案2】:

    我在 Chrome 上遇到了这个问题。重新启动 chrome 后,它解决了我的问题......

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 2016-08-14
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-07-10
      • 2017-11-08
      • 1970-01-01
      相关资源
      最近更新 更多