關(guān)于uniapp 開發(fā) 使用 canvas 繪圖 并保存在自己的手機(jī)上

<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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市荔棉,隨后出現(xiàn)的幾起案子闹炉,更是在濱河造成了極大的恐慌,老刑警劉巖润樱,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渣触,死亡現(xiàn)場離奇詭異,居然都是意外死亡壹若,警方通過查閱死者的電腦和手機(jī)嗅钻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來店展,“玉大人养篓,你說我怎么就攤上這事÷冈蹋” “怎么了柳弄?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長概说。 經(jīng)常有香客問我碧注,道長,這世上最難降的妖魔是什么糖赔? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任萍丐,我火速辦了婚禮,結(jié)果婚禮上放典,老公的妹妹穿的比我還像新娘逝变。我一直安慰自己船万,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布骨田。 她就那樣靜靜地躺著耿导,像睡著了一般。 火紅的嫁衣襯著肌膚如雪态贤。 梳的紋絲不亂的頭發(fā)上舱呻,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機(jī)與錄音悠汽,去河邊找鬼箱吕。 笑死,一個胖子當(dāng)著我的面吹牛柿冲,可吹牛的內(nèi)容都是我干的茬高。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼假抄,長吁一口氣:“原來是場噩夢啊……” “哼怎栽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起宿饱,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤熏瞄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后谬以,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體强饮,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年为黎,在試婚紗的時候發(fā)現(xiàn)自己被綠了邮丰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡铭乾,死狀恐怖剪廉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情片橡,我是刑警寧澤妈经,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站捧书,受9級特大地震影響吹泡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜经瓷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一爆哑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舆吮,春花似錦揭朝、人聲如沸队贱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柱嫌。三九已至,卻和暖如春屯换,著一層夾襖步出監(jiān)牢的瞬間编丘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工彤悔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嘉抓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓晕窑,卻偏偏與公主長得像抑片,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子杨赤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

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