You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* Detect-zoom * ----------- * Cross Browser Zoom and Pixel Ratio Detector * Version 1.0.4 | Apr 1 2013 * dual-licensed under the WTFPL and MIT license * Maintained by https://github/tombigel * Original developer https://github.com/yonran */exportclassDetectZoom{constructor(privatewindow: Window){}/** * Use devicePixelRatio if supported by the browser */getdevicePixelRatio(): number{returnthis.window.devicePixelRatio||1;}/** * Fallback function to set default values */privatefallback(){return{zoom: 1,devicePxPerCssPx: 1,};}/** * IE 8 and 9: no trick needed! */privateie8(){constzoom=Math.round(((this.window.screenasany).deviceXDPI/(this.window.screenasany).logicalXDPI)*100)/100;return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}/** * For IE10 we need to change our technique again... * thanks https://github.com/stefanvanburen */privateie10(){constzoom=Math.round((this.window.document.documentElement.offsetHeight/this.window.innerHeight)*100)/100;return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}/** * For chrome */privatechrome(){constzoom=Math.round((this.window.outerWidth/this.window.innerWidth)*100)/100;return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}/** * For safari (same as chrome) */privatesafari(){constzoom=Math.round((this.window.document.documentElement.clientWidth/this.window.innerWidth)*100)/100;return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}/** * Mobile WebKit * the trick: window.innerWIdth is in CSS pixels, while * screen.width and screen.height are in system pixels. * And there are no scrollbars to mess up the measurement. */privatewebkitMobile(){constdeviceWidth=Math.abs(this.window.screen.orientation.angle)===90 ? screen.height : screen.width;constzoom=deviceWidth/window.innerWidth;return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}/** * Desktop Webkit * the trick: an element's clientHeight is in CSS pixels, while you can * set its line-height in system pixels using font-size and * -webkit-text-size-adjust:none. * device-pixel-ratio: http://www.webkit.org/blog/55/high-dpi-web-sites/ * * Previous trick (used before http://trac.webkit.org/changeset/100847): * documentElement.scrollWidth is in CSS pixels, while * document.width was in system pixels. Note that this is the * layout width of the document, which is slightly different from viewport * because document width does not include scrollbars and might be wider * due to big elements. */privatewebkit(){constimportant=str=>str.replace(/;/g,' !important;');constdiv=this.window.document.createElement('div');div.innerHTML='1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>0';div.setAttribute('style',important('font: 100px/1em sans-serif; -webkit-text-size-adjust: none; text-size-adjust: none; height: auto; width: 1em; padding: 0; overflow: visible;'));// The container exists so that the div will be laid out in its own flow// while not impacting the layout, viewport size, or display of the// webpage as a whole.// Add !important and relevant CSS rule resets// so that other rules cannot affect the results.constcontainer=this.window.document.createElement('div');container.setAttribute('style',important('width:0; height:0; overflow:hidden; visibility:hidden; position: absolute;'));container.appendChild(div);document.body.appendChild(container);letzoom=1000/div.clientHeight;zoom=Math.round(zoom*100)/100;document.body.removeChild(container);return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}/** * no real trick; device-pixel-ratio is the ratio of device dpi / css dpi. * (Note that this is a different interpretation than Webkit's device * pixel ratio, which is the ratio device dpi / system dpi). * * Also, for Mozilla, there is no difference between the zoom factor and the device ratio. */privatefirefox4(){constzoom=Math.round(this.mediaQueryBinarySearch('min--moz-device-pixel-ratio','',0,10,20,0.0001)*100)/100;return{zoom: zoom,devicePxPerCssPx: zoom,};}/** * Firefox 18.x * Mozilla added support for devicePixelRatio to Firefox 18, * but it is affected by the zoom level, so, like in older * Firefox we can't tell if we are in zoom mode or in a device * with a different pixel ratio */privatefirefox18(){return{zoom: this.firefox4().zoom,devicePxPerCssPx: this.devicePixelRatio,};}/** * works starting Opera 11.11 * the trick: outerWidth is the viewport width including scrollbars in * system px, while innerWidth is the viewport width including scrollbars * in CSS px */privateopera11(){constzoom=Math.round((this.window.top.outerWidth/this.window.top.innerWidth)*100)/100;return{zoom: zoom,devicePxPerCssPx: zoom*this.devicePixelRatio,};}binarySearch(property: string,unit: string,a: number,b: number,maxIter: number,epsilon: number,matchMedia: any){constmid=(a+b)/2;if(maxIter<=0||b-a<epsilon){returnmid;}constquery='('+property+':'+mid+unit+')';if(matchMedia(query).matches){returnthis.binarySearch(property,unit,mid,b,maxIter-1,epsilon,matchMedia);}else{returnthis.binarySearch(property,unit,a,mid,maxIter-1,epsilon,matchMedia);}}/** * Use a binary search through media queries to find zoom level in Firefox */privatemediaQueryBinarySearch(property: string,unit: string,a: number,b: number,maxIter: number,epsilon: number){letmatchMedia;lethead,style,div;if(window.matchMedia){matchMedia=window.matchMedia;}else{head=document.getElementsByTagName('head')[0];style=document.createElement('style');head.appendChild(style);div=document.createElement('div');div.className='mediaQueryBinarySearch';div.style.display='none';document.body.appendChild(div);matchMedia=function(query){style.sheet.insertRule('@media '+query+'{.mediaQueryBinarySearch '+'{text-decoration: underline} }',0);constmatched=getComputedStyle(div,null).textDecoration==='underline';style.sheet.deleteRule(0);return{matches: matched};};}constratio=this.binarySearch(property,unit,a,b,maxIter,epsilon,matchMedia);if(div){head.removeChild(style);document.body.removeChild(div);}returnratio;}privatedetect(){//IE8+if(!isNaN((this.window.screenasany).logicalXDPI)&&!isNaN((this.window.screenasany).systemXDPI)){returnthis.ie8();}// IE10+ / Touchelseif(this.window.navigator.msMaxTouchPoints){returnthis.ie10();}//chromeelseif(!!(this.windowasany).chrome&&!(!!(this.windowasany).opera||this.window.navigator.userAgent.indexOf(' Opera')>=0)){returnthis.chrome();}//safarielseif(Object.prototype.toString.call((this.windowasany).HTMLElement).indexOf('Constructor')>0){returnthis.safari();}//Mobile Webkitelseif('orientation'inthis.window&&'webkitRequestAnimationFrame'inthis.window){returnthis.webkitMobile();}//WebKitelseif('webkitRequestAnimationFrame'inthis.window){returnthis.webkit();}//Operaelseif((this.windowasany).navigator.userAgent.indexOf('Opera')>=0){returnthis.opera11();}//Last one is Firefox//FF 18.xelseif((this.windowasany).devicePixelRatio){returnthis.firefox18();}//FF 4.0 - 17.xelseif(this.firefox4().zoom>0.001){returnthis.firefox4();}returnthis.fallback();}zoom(){returnthis.detect().zoom;}/** * Ratios.devicePxPerCssPx shorthand */device(){returnthis.detect().devicePxPerCssPx;}}
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: