modernizr
Version:
Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser.
96 lines (79 loc) • 3.18 kB
JavaScript
/*!
{
"name": "Webp",
"async": true,
"property": "webp",
"tags": ["image"],
"builderAliases": ["img_webp"],
"authors": ["Krister Kari", "@amandeep", "Rich Bradshaw", "Ryan Seddon", "Paul Irish"],
"notes": [{
"name": "Webp Info",
"href": "https://developers.google.com/speed/webp/"
}, {
"name": "Chormium blog - Chrome 32 Beta: Animated WebP images and faster Chrome for Android touch input",
"href": "https://blog.chromium.org/2013/11/chrome-32-beta-animated-webp-images-and.html"
}, {
"name": "Webp Lossless Spec",
"href": "https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification"
}, {
"name": "Article about WebP support on Android browsers",
"href": "http://www.wope-framework.com/en/2013/06/24/webp-support-on-android-browsers/"
}, {
"name": "Chormium WebP announcement",
"href": "https://blog.chromium.org/2011/11/lossless-and-transparency-encoding-in.html?m=1"
}]
}
!*/
/* DOC
Tests for lossy, non-alpha webp support.
Tests for all forms of webp support (lossless, lossy, alpha, and animated)..
Modernizr.webp // Basic support (lossy)
Modernizr.webp.lossless // Lossless
Modernizr.webp.alpha // Alpha (both lossy and lossless)
Modernizr.webp.animation // Animated WebP
*/
define(['Modernizr', 'addTest'], function(Modernizr, addTest) {
Modernizr.addAsyncTest(function() {
var webpTests = [{
'uri': 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=',
'name': 'webp'
}, {
'uri': 'data:image/webp;base64,UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAABBxAR/Q9ERP8DAABWUDggGAAAADABAJ0BKgEAAQADADQlpAADcAD++/1QAA==',
'name': 'webp.alpha'
}, {
'uri': 'data:image/webp;base64,UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA',
'name': 'webp.animation'
}, {
'uri': 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=',
'name': 'webp.lossless'
}];
var webp = webpTests.shift();
function test(name, uri, cb) {
var image = new Image();
function addResult(event) {
// if the event is from 'onload', check the see if the image's width is
// 1 pixel (which indiciates support). otherwise, it fails
var result = event && event.type === 'load' ? image.width == 1 : false;
var baseTest = name === 'webp';
// if it is the base test, and the result is false, just set a literal false
// rather than use the Boolean contrsuctor
addTest(name, (baseTest && result) ? new Boolean(result) : result);
if (cb) {
cb(event);
}
}
image.onerror = addResult;
image.onload = addResult;
image.src = uri;
}
// test for webp support in general
test(webp.name, webp.uri, function(e) {
// if the webp test loaded, test everything else.
if (e && e.type === 'load') {
for (var i = 0; i < webpTests.length; i++) {
test(webpTests[i].name, webpTests[i].uri);
}
}
});
});
});