modernizr todataurljpeg是异步的,怎么用?

时间:2013-02-04 22:05:22

标签: modernizr

canvas-todataurl-type的

Modernizr's test有一个注释:

// This test is asynchronous. Watch out.

那么..了解何时使用此测试是安全的最佳做法是什么。我看到它在图像中加载,但是库看起来不像它提供了什么时候进行测试的回调。

1 个答案:

答案 0 :(得分: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>

或者,使用监视库/填充/插件,例如https://gist.github.com/eligrey/384583

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

  • Canvas todataurl检测到的都是同步的(感谢@jedierikb)
  • 其他同步检测会有一流的支持,可能还有回调(我们还在制定更精细的细节)