需求分析,根據當前網頁中的幾張圖片,在手機上長按,儲存圖片到相簿或者傳送給好友。
drawCanvas(){ var self = this; var imgsrcArray = [ require('@/page/agent/agentexpand/img/bg.jpg'), 'data:image/jpeg;base64,'+this.codeUrl ]; var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); canvas.width = 750; canvas.height = 1333; var imglen = imgsrcArray.length; var drawimg = (function f(n){ if(n < imglen){ var img = new Image(); img.crossOrigin = 'Anonymous'; //解決跨域問題 img.onload = function(){ //ctx.save(); if(n == 0){ ctx.drawImage(img,0,0,750,1333); }else{ ctx.drawImage(img,466,574,210,210); } f(n+1); } img.src = imgsrcArray[n]; }else{ self.downloadUrl = canvas.toDataURL("image/jpeg"); self.downloadUrl = self.downloadUrl.replace("data:image/jpeg;base64,", ""); } })(0); }
呼叫方式:
<img :src="'data:image/jpeg;base64,' + downloadUrl"/>
注:上面的呼叫是使用的vue語法。
可參考地址: