<template>
<view>
<view class="invitation" @longpress="logoTime" v-if="long">
長摁保存 cv 改改參數(shù)即可使用
</view>
<canvas canvas-id="myCanvas" id="myCanvas" :style="{width:canvasWidth, height: canvasHeight, position: fixed}"></canvas>
</view>
</template>
<script>
export default {
data() {
return {
// 邀請碼
Invitation: null,
// 名字
name: 'name',
// 二維碼地址
codeUrl: '../../../static/come .pn',
// 畫布寬高
canvasWidth: 100 + '%',
canvasHeight: 100 + '%',
fixed: 'fixed',
// 畫布二維碼對象
qrCodeImage: {},
// 設(shè)備信息
deviceInformation: {},
long: true,
url: '../../../static/come .png',
}
},
onLoad() {
},
methods: {
// 長按
async logoTime() {
this.xtc = uni.canvasPutImageData('myCanvas', this)
// 獲取設(shè)備信息
this.deviceInformation = await this.getSystemInfo()
this.canvasWidth = (this.deviceInformation.windowWidth)
this.canvasHeight = (this.deviceInformation.windowHeight)
// 獲取圖片信息
this.qrCodeImage = await this.getImageInfo(this.codeUrl)
this.qrCodeImages = await this.getImageInfo(this.url)
if (this.deviceInformation.errMsg == "getSystemInfo:ok" && this.qrCodeImage.errMsg ==
"getImageInfo:ok" && this.qrCodeImages.errMsg== "getImageInfo:ok") {
uni.showToast({
icon: 'loading',
mask: true,
duration: 10000,
title: '海報(bào)繪制中',
});
setTimeout(() => {
let ctx = uni.createCanvasContext('myCanvas');
// 填充背景色
ctx.setFillStyle('#f3f3f3')
ctx.fillRect(0, 0, this.deviceInformation.windowWidth,
// 這里有個細(xì)節(jié) 也因?yàn)橛玫搅藘蓮垐D圖片 大圖片要先進(jìn)行渲染不然 會覆蓋掉小圖片
this.deviceInformation.windowHeight) // fillRect(x,y,寬度滩报,高度)
// 繪制背景圖 ctx.drawImage (圖片路徑丈探, 圖片距離屏幕左邊寬度 ,圖片距離屏幕頂部高度蒋困, 圖片寬度矛双,圖片高度)
ctx.drawImage(this.qrCodeImages.path, 10,50, this.qrCodeImage.width*1.3, this.qrCodeImage .height*2)
// 繪制二維碼
ctx.drawImage(this.qrCodeImage.path, 250, 400, this.qrCodeImage.width/4, this.qrCodeImage .height/4)
// 邀請碼
ctx.setFontSize(16) // 字號
ctx.setFillStyle('#333') // 顏色
// ctx.setFontSize(12)
ctx.fillText('邀請碼', 20, this.deviceInformation.windowHeight -350); // (文字渊抽,x,y)
// 邀請碼议忽,碼
ctx.setFontSize(30) // 字號
ctx.setFillStyle('#FC0334') // 顏色
ctx.fillText(this.Invitation, 20, this.deviceInformation.windowHeight - 310); // (文字懒闷,x,y)
// 邀請人
ctx.setFontSize(12) // 字號
ctx.setFillStyle('#333') // 顏色
ctx.fillText('用戶' + this.name + '邀請您加入', 20,
this.deviceInformation.windowHeight - 286.5); // (文字栈幸,x愤估,y)
ctx.setFontSize(11) // 字號
ctx.setFillStyle('#999') // 顏色
ctx.fillText("掃碼入", 245, this.deviceInformation.windowHeight - 288); // (文字,x侦镇,y)
ctx.draw(true, (rest) => { // draw方法 把以上內(nèi)容畫到 canvas 中灵疮。
if (rest.errMsg == 'drawCanvas:ok') {
this.long = false
}
console.log(rest, '走到了這里')
uni.showToast({
icon: 'success',
mask: true,
title: '繪制完成',
});
// 這里因?yàn)槲覀冺?xiàng)目是需要 全屏保存的 所以 x y 的參數(shù)都是0
uni.canvasToTempFilePath({ // 保存canvas為圖片 uni 官方api文檔
x: 0,
y: 0,
fileType: 'jpg',
width: this.deviceInformation.windowWidth,
height: this.deviceInformation.windowHeight,
destWidth: this.deviceInformation.windowWidth, // 如果保存到手機(jī)比較模糊 改這里 一般*2 即可解決
destHeight: this.deviceInformation.windowHeight,
// 如果保存到手機(jī)比較模糊 改這里 一般*2 即可解決
canvasId: 'myCanvas',
quality: 1,
success: (res) => {
// 在H5平臺下织阅,tempFilePath 為 base64
console.log(res, '回調(diào)成功')
this.conserve(res.tempFilePath)
console.log(res.tempFilePath, '//要保存的文件路徑')
},
})
});
}, 1500)
} else {
console.log('err')
// } else {
// console.log('err')
}
},
// 獲取圖片信息
getImageInfo(image) {
return new Promise((rq, rj) => {
uni.getImageInfo({
src: image,
success: function(res) {
console.log(res, '圖片')
req(res)
},
});
})
},
// 獲取設(shè)備信息
getSystemInfo() {
return new Promise((rq, rj) => {
uni.getSystemInfo({
success: function(res) {
req(res)
}
});
})
},
// 保存到手機(jī)
conserve(ip) {
uni.saveImageToPhotosAlbum({
filePath: ip, //返回的臨時路徑
success: function(
res2) {
console.log( res2, '臨時' )
uni.showToast({
title: "保存成功",
icon: "none"
});
},
fail: function() {
uni.showToast({
title: "保存失敗壳繁,請稍后重試",
icon: "none"
});
}
});
}
}
}
</script>
<style lang="scss">
</style>
關(guān)于uniapp 開發(fā) 使用 canvas 繪圖 并保存在自己的手機(jī)上
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來店展,“玉大人养篓,你說我怎么就攤上這事÷冈蹋” “怎么了柳弄?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長概说。 經(jīng)常有香客問我碧注,道長,這世上最難降的妖魔是什么糖赔? 我笑而不...
- 正文 為了忘掉前任萍丐,我火速辦了婚禮,結(jié)果婚禮上放典,老公的妹妹穿的比我還像新娘逝变。我一直安慰自己船万,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布骨田。 她就那樣靜靜地躺著耿导,像睡著了一般。 火紅的嫁衣襯著肌膚如雪态贤。 梳的紋絲不亂的頭發(fā)上舱呻,一...
- 文/蒼蘭香墨 我猛地睜開眼假抄,長吁一口氣:“原來是場噩夢啊……” “哼怎栽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起宿饱,我...
- 序言:老撾萬榮一對情侶失蹤熏瞄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后谬以,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體强饮,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年为黎,在試婚紗的時候發(fā)現(xiàn)自己被綠了邮丰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站捧书,受9級特大地震影響吹泡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜经瓷,卻給世界環(huán)境...
- 文/蒙蒙 一爆哑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舆吮,春花似錦揭朝、人聲如沸队贱。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽柱嫌。三九已至,卻和暖如春屯换,著一層夾襖步出監(jiān)牢的瞬間编丘,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓晕窑,卻偏偏與公主長得像抑片,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子杨赤,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 寫在前面: 大家好望拖,這是光堂兄的第一篇文章渺尘,其實(shí)很早之前就有將自己學(xué)習(xí)python的點(diǎn)滴記錄下來的想法,只是沒有找...
- 5月以來架诞,哪怕對市場風(fēng)向再不敏感的人,也感覺到陣陣涼意干茉。二級市場連續(xù)下挫谴忧,一級市場融資環(huán)境惡化,不論企業(yè)融資數(shù)量還...
- 推薦指數(shù): 6.0 書籍主旨關(guān)鍵詞:特權(quán)角虫、焦點(diǎn)沾谓、注意力、語言聯(lián)想戳鹅、情景聯(lián)想 觀點(diǎn): 1.統(tǒng)計(jì)學(xué)現(xiàn)在叫數(shù)據(jù)分析均驶,社會...