您好,登錄后才能下訂單哦!
這篇文章主要介紹了JS+HTML圖片顯示Canvas壓縮功能的方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
一.選擇圖片并顯示
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>Title</title> </head> <body> <input id="imginput" type="file" accept="image/*" onchange="showImg(this)"> <div id="img1"> <canvas class="convas01"></canvas> </div> </body> <script> window.URL=window.URL||window.webkitURL; var divimg1 = document.getElementById("img1"); function showImg(obj){ var files = obj.files; var img = new Image(); img.width = 100; if(window.URL){ img.src = window.URL.createObjectURL(files[0]); }else{ //opera不支持createObjectURL/revokeObjectURL方法。需要用FileReader對象來處理 var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onload = function(e){ img.src = this.result; img.width = 200; img.id="img01"; divimg1.appendChild(img); } } img.id="img01"; divimg1.appendChild(img); } </script> </html>
二.使用canvas代碼
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>Title</title> </head> <body> <input id="imginput" type="file" accept="image/*" onchange="showImg(this)"> <div id="img1"> <canvas id="convas01"></canvas> </div> </body> <script> window.URL=window.URL||window.webkitURL; var divimg1 = document.getElementById("img1"); var convas = document.getElementById("convas01"); var ht = convas01.getContext("2d"); convas.width = 200; convas.height = 200; function showImg(obj){ var files = obj.files; var img = new Image(); img.width = 200; if(window.URL){ img.src = window.URL.createObjectURL(files[0]); img.onload = function(e){ window.URL.revokeObjectURL(this.src);//方便引用無效回收 ht.clearRect(0, 0, convas.width, convas.width);//clearRect() 方法清空給定矩形內的指定像素。 ht.drawImage(img,0,0,convas.width,convas.height); convas.toDataURL("image/png"); } }else{ //opera不支持createObjectURL/revokeObjectURL方法。需要用FileReader對象來處理 var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onload = function(){ img.src = this.result; img.width = 200; img.id="img01"; divimg1.appendChild(img); } } } </script> </html>
三.壓縮處理
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>Title</title> </head> <body> <input id="imginput" type="file" accept="image/*" onchange="showImg(this)"> <div id="img1"> canvas圖片 <canvas id="convas01"></canvas> </div> <div id="img2"> 壓縮圖片 </div> </body> <script> window.URL=window.URL||window.webkitURL; var divimg1 = document.getElementById("img1"); var divimg2 = document.getElementById("img2"); var convas = document.getElementById("convas01"); var ht = convas01.getContext("2d"); convas.width = 200; convas.height = 200; function showImg(obj){ var files = obj.files; var img = new Image(); img.width = 200; if(window.URL){ img.src = window.URL.createObjectURL(files[0]); img.onload = function(e){ window.URL.revokeObjectURL(this.src);//方便引用無效回收 ht.drawImage(img,0,0,convas01.width,convas01.height); var imgdata = convas.toDataURL("image/png"); //將canvas轉化成Blob對象 var imgobj = new Image(); var blob =dataURLtoBlob(imgdata); var url = window.URL.createObjectURL(blob); imgobj.onload = function(e){ window.URL.revokeObjectURL(this.src); } imgobj.src= url; divimg2.appendChild(imgobj); } }else{ //opera不支持createObjectURL/revokeObjectURL方法。需要用FileReader對象來處理 var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onload = function(){ img.src = this.result; img.width = 200; img.id="img01"; divimg1.appendChild(img); } } } function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type:mime}); } </script> </html>
四.分辨率調節嘗試
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0;padding:0; } body{ width:100%;height:100%;background:black; } canvas{ width:800px;height:480px;outline:1px solid pink;position:absolute; left:0;top:0;right:0;bottom:0;margin:auto; } </style> </head> <body> <button>240x120</button> <button>320x240</button> <button>480x320</button> <button>640x420</button> <button>800x480</button> <canvas id="canvasId" width="480" height="320"> Your browser does not support the canvas element. </canvas> <script> var canvas = document.getElementById('canvasId'); var context = canvas.getContext('2d'); var btns = document.getElementsByTagName('button'); var resolution = [ [240, 120], [320, 240], [480, 320], [640, 420], [800, 480] ]; //canvas分辨率 var resolutionW = canvas.getAttribute('width'); var resolutionH = canvas.getAttribute('height'); //canvas大小 var sizeW = canvas.offsetWidth; var sizeH = canvas.offsetHeight; console.log(resolutionW); console.log(resolutionH); console.log(sizeW); console.log(sizeH); var img = new Image(); img.src = "https://cache.yisu.com/upload/information/20200622/114/71251.jpg"; //這張圖片像素為800x480 img.onload = function() { draw(3); for (var i = 0; i < btns.length; i++) { (function(i) { btns[i].onclick = function() { draw(i); } })(i); } } function draw(i) { canvas.setAttribute('width', resolution[i][0]); canvas.setAttribute('height', resolution[i][1]); context.clearRect(0, 0, sizeW, sizeH); context.drawImage(img, 0, 0, sizeW, sizeH, 0, 0, resolution[i][0], resolution[i][1]); } </script> </body> </html>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“JS+HTML圖片顯示Canvas壓縮功能的方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。