需求如下:根據(jù)后臺返回的海報背景圖和一個鏈接二維碼(vue-qr)舵变,合成一張圖配椭,然后可以進行長按保存本地操作
思路如下
- 使用 canvs.drawImage 繪制圖片到相應位置
- canvas.toDataUrl 導出圖片數(shù)據(jù)流到 img 上, 顯示到頁面
遇到的問題
- 圖片地址是網(wǎng)絡地址且不同域名垢夹,js中新建Image對象并直接賦值src, 會產(chǎn)生跨域
- canvas繪制的圖片會變得模糊
跨域問題的解決
- 將圖片網(wǎng)絡地址轉(zhuǎn)換為base64流,不推薦
- 將圖片下載放置本地列肢,使用本地固定的圖片载庭,部分需求下不能使用, 不理想
- 將圖片網(wǎng)絡地址的域名與運行環(huán)境域名保持一致即可, 能解決部分,但也不太理想
- 需要后端配合設置Access-Control-Allow-Origin: *
另外豁遭,img 標簽和 js 中的image 都建議增加 crossorigin = "anonymous" 屬性
**注意: **有部分網(wǎng)友評論img.setAttribute('crossOrigin', 'anonymous') 必須是寫 在你賦值 img.src 之前友多,所以樓主寫法有誤 可以看看這個鏈接 去看看, https://stackoverflow.com/questions/20424279/canvas-todataurl-securityerror/27260385#27260385
模糊問題的解決
未優(yōu)化的就決方案
效果圖如下:
<!DOCTYPE html><html><style> .content{ display: flex; } .poster_img { width: 300px; }</style><body> <div class="content"> <div> <p>要繪制的圖像:</p> <img id="tulip" class="poster_img" src="./poster.jpg" alt="The Tulip" /> </div> <div> <p>畫布:</p> <canvas id="myCanvas" width="300" style="border:1px solid #d3d3d3;background:#ffffff;"> Your browser does not support the HTML5 canvas tag. </canvas> </div> <div> <p>DataURL后的圖片樣式:</p> <img class="poster_img" id="canvas_img" src="" alt="" srcset=""> </div> </div></body><script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var canvasImg = document.getElementById("canvas_img") const img = new Image() img.src = document.getElementById("tulip").src; img.onload = function () { const imgScale = img.width / img.height; c.style.width = c.width + 'px' c.style.height = c.width / imgScale + 'px' c.width = c.width; c.height = c.width / imgScale; ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, c.width, c.height); canvasImg.src = c.toDataURL('image/jpeg', 1) }</script></html>
模糊原因分析:
??在瀏覽器的 window 對象中有一個 devicePixelRatio 的屬性,該屬性表示了屏幕的設備像素比堤框,即用幾個(通常是2個)像素點寬度來渲染1個像素域滥。
??舉例來說,假設 devicePixelRatio 的值為 2 蜈抓,一張 100×100 像素大小的圖片启绰,在 Retina 屏幕下,會用 2 個像素點的寬度去渲染圖片的 1 個像素點沟使,因此該圖片在 Retina 屏幕上實際會占據(jù) 200×200 像素的空間委可,相當于圖片被放大了一倍,因此圖片會變得模糊。
??類似的着倾,在 canvas context 中也存在一個 backingStorePixelRatio 的屬性拾酝,該屬性的值決定了瀏覽器在渲染canvas之前會用幾個像素來來存儲畫布信息。 backingStorePixelRatio 屬性在各瀏覽器廠商的獲取方式不一樣卡者,所以需要加上瀏覽器前綴來實現(xiàn)兼容蒿囤。
解決方案:
1.首先一樣,獲取 Canvas 對象:
2.獲取像素比崇决,將 Canvas 寬高進行放大材诽,放大比例為:devicePixelRatio / webkitBackingStorePixelRatio , 我們寫了一個兼容的方法。
3.按實際渲染倍率來縮放canvas恒傻。
注意基礎知識點:
要設置canvas的畫布大小脸侥,使用的是 canvas.width 和 canvas.height;
要設置畫布的實際渲染大小盈厘,使用的 style 屬性或CSS設置的 width 和height睁枕,只是簡單的對畫布進行縮放。
4.繪制
優(yōu)化后的解決方案
效果圖如下
<!DOCTYPE html><html><style> .content{ display: flex; } .poster_img { width: 300px; }</style><body> <div class="content"> <div> <p>要繪制的圖像:</p> <img id="tulip" class="poster_img" src="./psoter.jpg" alt="The Tulip" /> </div> <div> <p>畫布:</p> <canvas id="myCanvas" width="300" style="border:1px solid #d3d3d3;background:#ffffff;"> Your browser does not support the HTML5 canvas tag. </canvas> </div> <div> <p>DataURL后的圖片樣式:</p> <img class="poster_img" id="canvas_img" src="" alt="" srcset=""> </div> </div></body><script> var getPixelRatio = function (context) { var backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; return (window.devicePixelRatio || 1) / backingStore; }; var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var ratio = getPixelRatio(ctx); ctx.scale(ratio, ratio) var canvasImg = document.getElementById("canvas_img") const img = new Image() img.src = document.getElementById("tulip").src; img.onload = function () { const imgScale = img.width / img.height; c.style.width = c.width + 'px' c.style.height = c.width / imgScale + 'px' c.width = c.width * ratio; c.height = c.width / imgScale; ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, c.width, c.height); canvasImg.src = c.toDataURL('image/jpeg', 1) }</script></html>
參考鏈接:
一個關(guān)于image訪問圖片跨域的問題,http://www.reibang.com/p/8fa0fb53c183