Modernizr对 canvas-todataurl-type 的测试有一条评论:
// This test is asynchronous. Watch out.
那么.. 了解何时可以安全使用此测试的最佳做法是什么。我看到它加载到图像中,但是该库看起来没有提供任何用于何时测试的回调。
@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)