1// retina.js, a high-resolution image swapper (http://retinajs.com), v0.0.2 2 3(function() { 4 5 var root = (typeof exports == 'undefined' ? window : exports); 6 7 8 9 root.Retina = Retina; 10 11 function Retina() {} 12 13 Retina.init = function(context) { 14 if (context == null) context = root; 15 16 var existing_onload = context.onload || new Function; 17 18 context.onload = function() { 19 var images = document.getElementsByTagName("img"), retinaImages = [], i, image; 20 for (i = 0; i < images.length; i++) { 21 image = images[i]; 22 retinaImages.push(new RetinaImage(image)); 23 } 24 existing_onload(); 25 } 26 }; 27 28 Retina.isRetina = function(){ 29 var mediaQuery = "(-webkit-min-device-pixel-ratio: 1.5),\ 30 (min--moz-device-pixel-ratio: 1.5),\ 31 (-o-min-device-pixel-ratio: 3/2),\ 32 (min-resolution: 1.5dppx)"; 33 34 if (root.devicePixelRatio > 1) 35 return true; 36 37 if (root.matchMedia && root.matchMedia(mediaQuery).matches) 38 return true; 39 40 return false; 41 }; 42 43 44 root.RetinaImagePath = RetinaImagePath; 45 46 function RetinaImagePath(path) { 47 this.path = path; 48 this.at_2x_path = path.replace(/\.\w+$/, function(match) { return "@2x" + match; }); 49 } 50 51 RetinaImagePath.confirmed_paths = []; 52 53 RetinaImagePath.prototype.at_2x_path_loads = function(callback) { 54 var variant = new Image(); 55 variant.onload = function() { return callback(true); } 56 variant.onerror = function() { return callback(false); } 57 variant.src = this.at_2x_path; 58 } 59 60 RetinaImagePath.prototype.check_2x_variant = function(callback) { 61 var that = this; 62 if (RetinaImagePath.confirmed_paths.indexOf(this.at_2x_path) != -1) { 63 return callback(true); 64 } else { 65 this.at_2x_path_loads(function(wasLoaded) { 66 if (wasLoaded) RetinaImagePath.confirmed_paths.push(that.at_2x_path); 67 return callback(wasLoaded); 68 }); 69 } 70 } 71 72 73 74 function RetinaImage(el) { 75 this.el = el; 76 this.path = new RetinaImagePath(this.el.getAttribute('src')); 77 var that = this; 78 this.path.check_2x_variant(function(hasVariant) { 79 if (hasVariant) that.swap(); 80 }); 81 } 82 83 root.RetinaImage = RetinaImage; 84 85 RetinaImage.prototype.swap = function(path) { 86 if (typeof path == 'undefined') path = this.path.at_2x_path; 87 88 var that = this; 89 function load() { 90 if (! that.el.complete) { 91 setTimeout(load, 5); 92 } else { 93 that.el.setAttribute('width', that.el.offsetWidth); 94 that.el.setAttribute('height', that.el.offsetHeight); 95 that.el.setAttribute('src', path); 96 } 97 } 98 load(); 99 } 100 101 102 103 104 if (Retina.isRetina()) { 105 Retina.init(root); 106 } 107 108})();;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.