麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 課堂 > 小程序 > 正文

微信小程序使用canvas的畫圖操作示例

2020-03-21 16:03:14
字體:
來源:轉載
供稿:網友

本文實例講述了微信小程序使用canvas的畫圖操作。分享給大家供大家參考,具體如下:

基礎寫起來太沒動力了,也寫得亂七八糟的,還是直接解決一些小問題比較方便,代碼的方方面面的細節都會詳盡的解釋一下。

1、下面介紹一下canvas的畫圖,我這個簡單一點,畫一個帶圖文的背景圖,圖片可以從后臺獲取也可以選擇本地的。canvas畫圖首先要在wxml里面新建一個<canvas>標簽,一定要寫上canvas-id='canvas的id',這是必須條件,如下面代碼:

<canvas canvas-id='canvas' style='width:{{windowW}}px;height:{{windowH}}px'></canvas><button bindtap='chooseImage'>相冊</button>

2、上面canvas的寬高都是js控制的,使用?wx.getSystemInfo獲取屏幕的可見寬高。代碼如下:

wx.getSystemInfo({   success: function (res) {    that.setData({     windowW: res.windowWidth,     windowH: res.windowHeight    })   },})

相當的詳細,有沒有!??!

3、重點注意一下:在微信小程序的canvas畫圖中如果使用了網絡圖片,一定要先把這張圖片讀取下載下來(可使用wx.downloadFile),存為網絡格式的圖片?。?!

上面這個操作是避免網絡狀況不好的時候canvas畫圖完成了背景圖片確顯示不出來的情況,同時,這個圖片所在的域名必須在微信公眾平臺配置一下,代碼如下:

wx.downloadFile({   url: '圖片路徑',   success: function (res) {    that.setData({     canvasimgbg: res.tempFilePath    })   }})

4、 我上面wxml代碼里面寫了一個按鈕,使用wx.chooseImage調用了系統相冊,所以說,我們選擇一張圖片畫進canvas也是可以的,代碼如下:

wx.chooseImage({   success: function (res) {    that.setData({     chooseimg: res.tempFilePaths[0]    })   },})

5、下面就是cancas畫圖啦,畫和屏幕一樣寬高的,然后我們再寫一行字哈哈哈,代碼如下:

var that = this;var windowW = that.data.windowW;var windowH = that.data.windowH;var canvasimgbg = that.data.canvasimgbg;var canvasimg1 = that.data.chooseimg;canvas.drawImage(canvasimgbg, 0, 0, windowW, windowH);canvas.drawImage(canvasimg1, 0, 10, 200, 200);canvas.setFontSize(50)canvas.fillText('Hello', 200, 200)canvas.draw(true,setTimeout(function(){   that.daochu()},1000));

使用canvas.draw()畫圖,完畢之后直接wx.canvasToTempFilePath導出圖片

6、導出圖片,代碼如下:

var that = this;var windowW = that.data.windowW;var windowH = that.data.windowH;wx.canvasToTempFilePath({   x: 0,   y: 0,   width: windowW,   height: windowH,   destWidth: windowW,   destHeight: windowH,   canvasId: 'canvas',   success: function (res) {    console.log(res)    wx.saveImageToPhotosAlbum({     filePath: res.tempFilePath,     success(res) {     }    })    wx.previewImage({     urls: [res.tempFilePath],    })   }})

上面這些代碼已經完成啦?。?!

主要就是各位使用的時候看需要什么樣的啦!

下面還是附上完整的代碼把!

// pages/canvas/canvas.jsPage({ /**  * 頁面的初始數據  */ data: { }, onLoad: function (options) {  var that = this;  that.sys();  that.bginfo(); }, sys: function () {  var that = this;  wx.getSystemInfo({   success: function (res) {    that.setData({     windowW: res.windowWidth,     windowH: res.windowHeight    })   },  }) }, bginfo: function () {  var that = this;  wx.downloadFile({   url: '圖片鏈接',//注意公眾平臺是否配置相應的域名   success: function (res) {    that.setData({     canvasimgbg: res.tempFilePath    })   }  }) }, canvasdraw: function (canvas) {  var that = this;  var windowW = that.data.windowW;  var windowH = that.data.windowH;  var canvasimgbg = that.data.canvasimgbg;  var canvasimg1 = that.data.chooseimg;  canvas.drawImage(canvasimgbg, 0, 0, windowW, windowH);  canvas.drawImage(canvasimg1, 0, 10, 200, 200);  canvas.setFontSize(50)  canvas.fillText('Hello', 200, 200)  canvas.draw(true,setTimeout(function(){   that.daochu()  },1000));  // canvas.draw(); }, daochu: function () {  console.log('a');  var that = this;  var windowW = that.data.windowW;  var windowH = that.data.windowH;  wx.canvasToTempFilePath({   x: 0,   y: 0,   width: windowW,   height: windowH,   destWidth: windowW,   destHeight: windowH,   canvasId: 'canvas',   success: function (res) {    console.log(res)    wx.saveImageToPhotosAlbum({     filePath: res.tempFilePath,     success(res) {     }    })    wx.previewImage({     urls: [res.tempFilePath],    })   }  }) }, chooseImage: function () {  var that = this;  var canvas = wx.createCanvasContext('canvas');  wx.chooseImage({   success: function (res) {    that.setData({     chooseimg: res.tempFilePaths[0]    })    that.canvasdraw(canvas);   },  }) }})

希望本文所述對大家微信小程序開發有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久电影网站 | 亚洲电影在线观看高清免费 | 毛片在哪看 | 亚洲小视频在线观看,com | 免费放黄网站在线播放 | 久久免费精品视频 | 国产精品99久久久久久大便 | 中文字幕一区二区三区久久 | 成人一级视频 | 精品国产乱码久久久久久丨区2区 | 久久精品视频16 | 国产亚洲精品久久777777 | h色网站在线观看 | 一分钟免费观看完整版电影 | 黄色av免费 | 色戒在线版 | 日本高清com | 欧美精品一区二区三区在线播放 | 久久精品亚洲一区 | 国产九九在线视频 | 午夜视频免费播放 | 毛片一级网站 | 久久精品在线免费观看 | 麻豆视频国产在线观看 | av影院在线 | 成人免费在线观看视频 | 欧美一区中文字幕 | 一级成人免费 | 欧美 日韩 国产 成人 | 91精品国产99久久久久久 | 国产91久久精品一区二区 | 日本在线播放一区二区三区 | av成人免费 | 日本一级黄色大片 | 国产精品久久久久久久av | 日韩毛片免费观看 | 狠狠干91 | 成年人免费视频播放 | 欧美爱爱视频网站 | 中文字幕综合 | 国产午夜精品一区二区三区不卡 |