1

Modernizr对 canvas-todataurl-type 的测试有一条评论:

// This test is asynchronous. Watch out.

那么.. 了解何时可以安全使用此测试的最佳做法是什么。我看到它加载到图像中,但是该库看起来没有提供任何用于何时测试的回调。

4

1 回答 1

1

@jedierikb - 我想你知道答案,因为你帮助了我们!但是为了别人...

在 Modernizr 2.x 中,它需要一些手动工作。异步检测将undefined持续到它们完成,所以......:

<script src="path/to/modernizr.js"></script>
<script>
  (function withDataURL () {
    if (typeof Modernizr.todataurljpeg !== 'undefined') {
      // Do things with `Modernizr.todataurljpeg`
    }
    else {
      setTimeout(withDataURL, 100);
    }
  }());
</script>

或者,使用 watch library/shim/plugin,例如https://gist.github.com/eligrey/384583

在 Modernizr 3.0 中,我们进行了 2 项相关更改:

  • 画布todataurl检测都是同步的(感谢@jedierikb)
  • 其他同步检测将具有一流的支持,可能带有回调(我们仍在研究更详细的细节)
于 2013-03-15T16:23:14.907 回答