字節(jié)小程序 canvas 使用及踩坑

先說說坑吧笼蛛,使用方法都在后面

踩了個不大不小的坑境肾,想實現(xiàn)的效果是保存canvas畫的圖片下的 canvas字體加粗框冀,依照社區(qū)的方法是使用ctx.font = 'normal bold 20px Arial, sans-serif';但尼瑪就離譜顶霞,微信上我在demo上使用無礙蓝翰,保存圖片效果實現(xiàn)徐伐,字節(jié)死活無法實現(xiàn)贯钩,結(jié)果。。角雷。把normal去了就行了祸穷。。勺三。字節(jié)的字號前面 只能寫 italic 雷滚、 small-caps 、bold這三種吗坚,好吧祈远,浪費了很久時間

接下來看看怎么實現(xiàn)字節(jié)小程序的canvas使用

首先是官方例子官方方法

ttml:

<canvas style="width: 300px; height: 200px;" canvas-id="firstCanvas"></canvas>

js:

const app = getApp()

Page({

? data: {

? },

? canvasIdErrorCallback: function (e) {

? ? console.error(e.detail.errMsg)

? },

? onLoad: function (e) {

? ? // 使用 wx.createContext 獲取繪圖上下文 context

? ? var context = wx.createCanvasContext('firstCanvas')

? ? context.setStrokeStyle("#00ff00")

? ? context.setLineWidth(5)

? ? context.rect(0, 0, 200, 200)

? ? context.stroke()

? ? context.setStrokeStyle("#ff0000")

? ? context.setLineWidth(2)

? ? context.moveTo(160, 100)

? ? context.arc(100, 100, 60, 0, 2 * Math.PI, true)

? ? context.moveTo(140, 100)

? ? context.arc(100, 100, 40, 0, Math.PI, false)

? ? context.moveTo(85, 80)

? ? context.arc(80, 80, 5, 0, 2 * Math.PI, true)

? ? context.moveTo(125, 80)

? ? context.arc(120, 80, 5, 0, 2 * Math.PI, true)

? ? context.stroke()

? ? context.draw()

? },

})

接下來在上面繪制文本


context.setFontSize(20)

context.setFillStyle('skyblue')

// context.font = 'normal bold 20px Arial, sans-serif' // 加粗效果不生效,字號前面只能寫 italic 、 small-caps 商源、bold

context.font = 'bold 20px Arial, sans-serif' // 字體加粗

context.fillText('測試文本', 10, 190)

注意:所有樣式效果都要在fillText之前書寫车份,如果需要字體加粗,需要調(diào)整繪制順序炊汹,先把細字體文案繪制完成躬充,再統(tǒng)一繪制加粗字體

例子如下:

context.fillText('測試文本1', 10, 190)

context.fillText('測試文本2', 10, 210)

context.fillText('測試文本3', 10, 230)

context.font = 'bold 20px Arial, sans-serif'?

context.fillText('測試文本4', 10, 250)

這樣1,2讨便,3都是細的充甚,只有4為粗字體

還有文字自動換行等效果,詳情可以參考canvas點擊生成圖片及文字自動換行霸褒,里面有一個GitHub的demo很好用

注意:字節(jié)小程序目前仍不支持將canvas 2d畫布中的內(nèi)容導(dǎo)出為圖片

抽空再把使用canvas保存整個頁面為圖片保存到本地的demo實現(xiàn)了伴找,上面的大佬其實已經(jīng)很寫的詳細了,就醬


更新一個我自己的例子

//先在onLoad中定義

onLoad: function (query) {

? ? var that = this

? ? //獲取用戶設(shè)備信息废菱,屏幕寬度

? ? tt.getSystemInfo({

? ? ? success: res => {

? ? ? ? console.log(res, 'getSystemInfo')

? ? ? ? that.setData({

? ? ? ? ? screenWidth: res.screenWidth,

? ? ? ? ? screenHeight: res.screenHeight,

? ? ? ? ? pixelRatio: res.pixelRatio,

? ? ? ? })

? ? ? ? console.log(that.data.pixelRatio, that.data.screenWidth, that.data.screenHeight)

? ? ? }

? ? })

? ? tt.getImageInfo({

? ? ? src: 'https://fcdn.qiushilaile.com/adx/app-101610706237575.png',

? ? ? success: function (res) {

? ? ? ? that.setData({

? ? ? ? ? shareImgSrc: res.path,

? ? ? ? ? shareWidth: res.width,

? ? ? ? ? shareHeight: res.height,

? ? ? ? });

? ? ? ? // console.log("111", that.data.shareImgSrc, that.data.shareWidth, that.data.shareHeight)

? ? ? }

? ? })

? ? tt.getImageInfo({

? ? ? src: 'https://fcdn.qiushilaile.com/adx/app-68841610539842230.png',

? ? ? success: function (res) {

? ? ? ? that.setData({

? ? ? ? ? shareImgSrc1: res.path,

? ? ? ? ? shareWidth1: res.width,

? ? ? ? ? shareHeight1: res.height,

? ? ? ? });

? ? ? ? // console.log("111", that.data.shareImgSrc1, that.data.shareWidth1, that.data.shareHeight1)

? ? ? }

? ? })

? ? tt.getImageInfo({

? ? ? src: 'https://fcdn.qiushilaile.com/adx/app-49601610539855858.png',

? ? ? success: function (res) {

? ? ? ? that.setData({

? ? ? ? ? shareImgSrc2: res.path,

? ? ? ? ? shareWidth2: res.width,

? ? ? ? ? shareHeight2: res.height,

? ? ? ? });

? ? ? }

? ? })

? ? this.setData({

? ? ? showScore: query.showScore

? ? })

? ? console.log(this.data.showScore)

? ? tuiwa.globalData.currentPage = this

? },

? //再獲取臨時路徑

? getTempFilePath: function () {

? ? let that = this

? ? console.log('111')

? ? tt.canvasToTempFilePath({

? ? ? canvasId: 'share',

? ? ? destWidth: that.data.screenWidth * that.data.pixelRatio,

? ? ? destHeight: that.data.screenHeight * that.data.pixelRatio,

? ? ? success: (res) => {

? ? ? ? console.log(res)

? ? ? ? that.setData({

? ? ? ? ? shareTempFilePath: res.tempFilePath

? ? ? ? })

? ? ? ? tt.saveImageToPhotosAlbum({

? ? ? ? ? filePath: that.data.shareTempFilePath,

? ? ? ? ? success: (res) => {

? ? ? ? ? ? console.log(res)

? ? ? ? ? ? tt.showToast({ title: "成功保存到本地相冊" });

? ? ? ? ? },

? ? ? ? ? fail: (err) => {

? ? ? ? ? ? console.log(err)

? ? ? ? ? }

? ? ? ? })

? ? ? },

? ? ? fail: (err) => {

? ? ? ? console.log(err)

? ? ? }

? ? })

? },

//點擊事件調(diào)用函數(shù)save生成canvas

save: function () {

? ? ? //繪制

? ? ? let that = this;

? ? ? let shareWidth = this.data.shareWidth

? ? ? let shareWidth1 = this.data.shareWidth1

? ? ? let shareWidth2 = this.data.shareWidth2

? ? ? let shareHeight = this.data.shareHeight

? ? ? let shareHeight1 = this.data.shareHeight1

? ? ? let shareHeight2 = this.data.shareHeight2

? ? ? let shareImgSrc = this.data.shareImgSrc

? ? ? let shareImgSrc1 = this.data.shareImgSrc1

? ? ? let shareImgSrc2 = this.data.shareImgSrc2

? ? ? let showScore = this.data.showScore

? ? ? let context = tt.createCanvasContext('share')

? ? ? // 圖片

? ? ? context.drawImage(shareImgSrc, 0, 0, shareWidth, shareHeight)

? ? ? context.drawImage(shareImgSrc1, 40, 200, shareWidth1 / 2.7, shareHeight1 / 2.7)

? ? ? context.drawImage(shareImgSrc2, 40, 320, shareWidth2 / 2.7, shareHeight2 / 2.7)

? ? ? // 數(shù)字

? ? ? context.setFontSize(60)

? ? ? context.setTextAlign('center')

? ? ? context.setFillStyle('#FFEC5D')

? ? ? context.setShadow(5, 5, 10, 'red')

? ? ? context.fillText(showScore, 240, 260, 115)

? ? ? // 文字

? ? ? context.setFontSize(20)

? ? ? context.font = 'bold 20px Arial, sans-serif'

? ? ? context.fillText('新年發(fā)大財', 220, 460, 115)

? ? ? context.fillText('好運不間斷', 220, 480, 115)

? ? ? context.fillText('升職再加薪', 220, 500, 115)

? ? ? context.fillText('2021牛氣沖天', 220, 520, 115)

? ? ? // 生成

? ? ? context.draw(true, this.getTempFilePath)

? },

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末技矮,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子殊轴,更是在濱河造成了極大的恐慌衰倦,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旁理,死亡現(xiàn)場離奇詭異樊零,居然都是意外死亡,警方通過查閱死者的電腦和手機孽文,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門驻襟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人芋哭,你說我怎么就攤上這事沉衣。” “怎么了减牺?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵豌习,是天一觀的道長存谎。 經(jīng)常有香客問我,道長斑鸦,這世上最難降的妖魔是什么愕贡? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮巷屿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘墩虹。我一直安慰自己嘱巾,他們只是感情好,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布诫钓。 她就那樣靜靜地躺著旬昭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪菌湃。 梳的紋絲不亂的頭發(fā)上问拘,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機與錄音惧所,去河邊找鬼骤坐。 笑死,一個胖子當著我的面吹牛下愈,可吹牛的內(nèi)容都是我干的纽绍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼势似,長吁一口氣:“原來是場噩夢啊……” “哼拌夏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起履因,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤障簿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后栅迄,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體站故,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年霞篡,在試婚紗的時候發(fā)現(xiàn)自己被綠了世蔗。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡朗兵,死狀恐怖污淋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情余掖,我是刑警寧澤寸爆,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布礁鲁,位于F島的核電站,受9級特大地震影響赁豆,放射性物質(zhì)發(fā)生泄漏仅醇。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一魔种、第九天 我趴在偏房一處隱蔽的房頂上張望析二。 院中可真熱鬧,春花似錦节预、人聲如沸叶摄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蛤吓。三九已至,卻和暖如春糠赦,著一層夾襖步出監(jiān)牢的瞬間会傲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工拙泽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留淌山,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓奔滑,卻偏偏與公主長得像艾岂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子朋其,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容