這篇文章主要介紹了微信小程序的canvas如何使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇微信小程序的canvas如何使用文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
濟(jì)源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
canvas簡(jiǎn)單理解成一張可以自定義大小(有上限)的畫布,在畫布上可以寫字,畫圖形,放置圖片。一般可以用來(lái)動(dòng)態(tài)生成截圖,方便用戶保存和轉(zhuǎn)發(fā)。
頁(yè)面canvas被隱藏了
<!--畫出試卷內(nèi)容,通過(guò)css(position:fixed; left:100%;)隱藏canvas--><!--A4是2480*3508象素 210*297毫米--><canvas wx:if="{{page==4}}" type="2d" id="canvas" style="width:750rpx; height:1050rpx; position:fixed; left:100%;"></canvas>
通過(guò) onCanvas 事件開始繪制畫布內(nèi)容
每繪制完成一頁(yè),通過(guò) wx.canvasToTempFilePath 保存圖片到本地
全部繪制完成,通過(guò) wx.previewImage 進(jìn)行圖片預(yù)覽
// 初始化canvas onCanvas: function (e) { // 繪制完成直接顯示 if(this.data.drawFinish) { this.previewPapers() return } wx.showLoading({ title: '試卷轉(zhuǎn)圖片中', }) const query = wx.createSelectorQuery() query.select('#canvas') .fields({ node: true, size: true }) .exec((res) => { const canvas = res[0].node const ctx = canvas.getContext('2d') const dpr = wx.getSystemInfoSync().pixelRatio canvas.width = res[0].width * dpr canvas.height = res[0].height * dpr console.log('canvas.width, canvas.height', canvas.width, canvas.height) ctx.scale(dpr, dpr) // 繪制記憶卷 this.drawMemoryPaperImage(canvas, ctx) }) }, // 繪制記憶卷 drawMemoryPaperImage(canvas, ctx) { // 獲取記憶卷數(shù)據(jù) let memoryList = this.data.memoryList // 繪制一頁(yè)記憶卷(繪制第一頁(yè)) let oneMemoryPaper = memoryList[0] let currentIndex = 0 // 0-表示繪制保存第一頁(yè)數(shù)據(jù) let totalPages = memoryList.length this.drawOneMemoryPaperImage(canvas, ctx, oneMemoryPaper, currentIndex, totalPages) }, // 繪制一頁(yè)記憶卷 drawOneMemoryPaperImage(canvas, ctx, oneMemoryPaper, currentIndex, totalPages) { let maxWidth = 750 let pg = oneMemoryPaper // 繪制前清空canvas ctx.clearRect(0, 0, canvas.width, canvas.height) // 繪制標(biāo)題 ctx.font = '18px sans-serif' ctx.textAlign = 'center'; ctx.fillText('快速數(shù)字記憶卷', 375, 70, maxWidth ) for(let j=0; j<pg.length; j++) { let row = pg[j] for(let k=0; k<row.length; k++) { let text = pg[j][k] let x = k * 17 + 40 let y = j * 35 + 150 // 繪制數(shù)字 ctx.font = '14px sans-serif' ctx.textAlign = 'left'; ctx.fillText(text, x, y, maxWidth ) // 繪制行號(hào) if(k==39) { let rowNo = 'Row ' + (j<9?'0'+(j+1):(j+1)) ctx.fillText(rowNo, x+25, y, maxWidth ) } } } // 繪制頁(yè)碼 ctx.font = '18px sans-serif' ctx.textAlign = 'center'; ctx.fillText('第 '+(currentIndex+1)+' 頁(yè)', 375, 1050, maxWidth ) // 保存試卷到小程序臨時(shí)目錄 let paper = 'paper.memoryPaper['+currentIndex+']' this.saveToTempFilePath(canvas, ctx, paper, currentIndex, totalPages) }, // 保存繪制的試卷到小程序臨時(shí)目錄 saveToTempFilePath(canvas, ctx, paper, currentIndex, totalPages) { var that = this wx.canvasToTempFilePath({ canvas: canvas, success(res) { that.setData({ [paper]: res.tempFilePath, }) // 繪制下一頁(yè)數(shù)據(jù) let nextIndex = currentIndex + 1 if(nextIndex < totalPages) { that.drawNextOneMemoryPaperImage(canvas, ctx, nextIndex, totalPages) }else { // 記憶卷全部繪制完成 that.setData({ drawFinish:true }) wx.hideLoading({ success: (res) => { console.log('記憶卷全部繪制完成') }, }) that.previewPapers() } }, fail(err) { console.log(err) } }) }, // 繪制下一頁(yè)數(shù)據(jù) drawNextOneMemoryPaperImage(canvas, ctx, nextIndex, totalPages) { // 獲取記憶卷數(shù)據(jù) let memoryList = this.data.memoryList let currentIndex = nextIndex let oneMemoryPaper = memoryList[nextIndex] this.drawOneMemoryPaperImage(canvas, ctx, oneMemoryPaper, currentIndex, totalPages) }, // 預(yù)覽試卷 previewPapers() { let paper = this.data.paper wx.previewImage({ current: paper.memoryPaper[0], urls: paper.memoryPaper }) },
關(guān)于“微信小程序的canvas如何使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“微信小程序的canvas如何使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:微信小程序的canvas如何使用
網(wǎng)頁(yè)鏈接:http://bm7419.com/article28/pcshcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站改版、定制網(wǎng)站、Google、手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)