前端如何用canvas實(shí)現(xiàn)圖片的等比例縮放
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Canvas Image Resize</title> </head> <body> <input type="file" id="imageInput" accept="image/*"> <div> <canvas id="canvasOriginal" style="border:1px solid #000;"></canvas> <canvas id="canvasResized" style="border:1px solid #000;"></canvas> </div> <script src="script.js"></script> </body> </html>
JavaScript代碼
document.getElementById('imageInput').addEventListener('change', function (event) { const file = event.target.files[0]; if (file) { const reader = new FileReader(); reader.onload = function (e) { const img = new Image(); img.onload = function () { const originalCanvas = document.getElementById('canvasOriginal'); const resizedCanvas = document.getElementById('canvasResized'); const ctxOriginal = originalCanvas.getContext('2d'); const ctxResized = resizedCanvas.getContext('2d'); const originalWidth = img.width; const originalHeight = img.height; let width, height, scale; // 設(shè)置原始圖片canvas尺寸 originalCanvas.width = originalWidth; originalCanvas.height = originalHeight; // 繪制原始圖片 ctxOriginal.drawImage(img, 0, 0, originalWidth, originalHeight); // 計算縮放比例 if (Math.max(originalWidth, originalHeight) > 1080) { scale = 1080 / Math.max(originalWidth, originalHeight); } else { scale = 1; } // 計算縮放后的尺寸 width = originalWidth * scale; height = originalHeight * scale; // 設(shè)置縮放后的圖片canvas尺寸 resizedCanvas.width = width; resizedCanvas.height = height; // 繪制縮放后的圖片 ctxResized.drawImage(img, 0, 0, width, height); }; img.src = e.target.result; }; reader.readAsDataURL(file); } });
代碼解釋
1.加載圖片: 使用 FileReader
讀取用戶選擇的圖片文件。
const reader = new FileReader(); reader.onload = function (e) { const img = new Image(); img.onload = function () { // 處理代碼邏輯 }; img.src = e.target.result; }; reader.readAsDataURL(file);
2.獲取圖片原始尺寸: 獲取圖片的原始寬度和高度。
const originalWidth = img.width; const originalHeight = img.height;
3.計算縮放比例: 根據(jù)目標(biāo)容器的尺寸和圖片的原始尺寸計算縮放比例。
let width, height, scale; if (Math.max(originalWidth, originalHeight) > 1080) { scale = 1080 / Math.max(originalWidth, originalHeight); } else { scale = 1; } width = originalWidth * scale; height = originalHeight * scale;
4.計算新的尺寸: 根據(jù)縮放比例計算新的寬度和高度。
const resizedWidth = originalWidth * scale; const resizedHeight = originalHeight * scale;
5. 繪制圖片: 使用drawImage
方法繪制圖片到canvas
上。
// 使用canvas繪制原始圖片。 originalCanvas.width = originalWidth; originalCanvas.height = originalHeight; ctxOriginal.drawImage(img, 0, 0, originalWidth, originalHeight); // 使用canvas繪制縮放后的圖片。 resizedCanvas.width = width; resizedCanvas.height = height; ctxResized.drawImage(img, 0, 0, width, height);
總結(jié)
到此這篇關(guān)于前端如何用canvas實(shí)現(xiàn)圖片的等比例縮放的文章就介紹到這了,更多相關(guān)前端canvas圖片等比例縮放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ES6標(biāo)準(zhǔn) Arrow Function(箭頭函數(shù)=>)
ES6標(biāo)準(zhǔn)新增了一種新的函數(shù):Arrow Function(箭頭函數(shù)),為什么叫Arrow Function?因?yàn)樗亩x用的就是一個箭頭2020-05-05前端報錯Failed?to?resolve?component:?smile-outlined?If?thi
這篇文章主要為大家介紹了前端報錯?Failed?to?resolve?component:?smile-outlined?If?this?is?a?native?custom?的問題分析解決,有需要的朋友可以借鑒參考下2023-06-06JavaScript中的property和attribute介紹
JavaScript中的property和attribute介紹,需要的朋友可以參考下。2011-12-12JS 進(jìn)度條效果實(shí)現(xiàn)代碼整理
進(jìn)度條效果實(shí)現(xiàn)代碼,有助于緩解頁面顯示慢的頁面,給用戶一個等待時間的效果2011-05-05