-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageCapture.min.js
1 lines (1 loc) · 1.69 KB
/
imageCapture.min.js
1
export default function(e){return new Promise((function(t,i){function h(){n&&n.parentNode&&n.parentNode.removeChild(n),n=null}var n=document.createElement("input");if(n.type="file",n.setAttribute("accept","image/*"),"boolean"==typeof e){var o=e;n.setAttribute("capture",o?"user":"environment")}else"object"==typeof e&&"boolean"==typeof e.useFrontCamera?n.setAttribute("capture",e.useFrontCamera?"user":"environment"):n.setAttribute("capture","environment");n.style.opacity=0,n.style.position="absolute",n.style.bottom="-30px",n.style.left="0px",document.body.appendChild(n),n.onchange=function(){var n=new FileReader;n.onload=function(n){var o=n.target.result;if(h(),"object"==typeof e&&e.size&&(e.size.width||e.size.height)){var s=document.createElement("canvas"),a=s.getContext("2d",{alpha:!1,desynchronized:!0}),r=new Image;r.onerror=function(){i("Failed to load image")},r.onload=function(){var i,h,n=0,d=0,l=1,c=1;e.size.width&&e.size.height?"stretch"==e.scaling?(l=e.size.width/r.width,c=e.size.height/r.height):l=c="fill"==e.scaling?r.width/r.height>e.size.width/e.size.height?e.size.height/r.height:e.size.width/r.width:r.width/r.height>e.size.width/e.size.height?e.size.width/r.width:e.size.height/r.height:e.size.width?l=c=e.size.width/r.width:e.size.height&&(l=c=e.size.height/r.height),l>1&&(c*=1/l,l=1),c>1&&(l*=1/c,c=1),i=r.width*l,h=r.height*c,"fill"==e.scaling&&e.size.width&&e.size.height?(n=e.size.width/2-i/2,d=e.size.height/2-h/2,s.width=i+2*n,s.height=h+2*d):(s.width=i,s.height=h),a.drawImage(r,n,d,i,h),o=s.toDataURL("image/jpeg"),t(o)},r.src=o}else setTimeout((function(){t(o)}))},n.onerror=function(e){h(),setTimeout((function(){i("Failed to read image")}))},n.readAsDataURL(this.files[0])},n.click()}))}