知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
小程序canvan畫布,現(xiàn)兩張圖片合成一張,并保存到本地
發(fā)表時間:2020-9-29
發(fā)布人:葵宇科技
瀏覽次數(shù):201
微信小程序合成照片 應用canvas把他們繪制到一張圖上保存到本地相冊然后點擊分享
自定義一個組件
components/canvas.js里
// components/canvas/canvas.js
const app = getApp()
Component({
/**
* 組件的屬性列表
*/
properties: {
scene: {
type: String,
value: ''
}
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
pixelRatio: 2,
programCode: '',
count: 0,
spinning: false,
loadingFlag: false, // 是否重新加載圖片
image:''
},
// 在組件完全初始化完畢、進入頁面節(jié)點樹后
attached () {
wx.nextTick(() => {
this.getFile() // 獲取小程序碼和頭像的臨時文件
})
},
/**
* 組件的方法列表
*/
methods: {
getFile () {
// 請求接口 獲取頭像和小程序碼的臨時文件
if (!this.data.loadingFlag) {
this.initData()
}
},
writeCanvas () {
let that = this
const ctx = wx.createCanvasContext('myCanvas', that)
let canvasW = that.data.canvasW
let canvasH = that.data.canvasH
let bgImgPath = that.data.image //背景圖片(在這里換圖片)
let programCode = that.data.erweima // 小程序碼(在這里換圖片)
// 畫大背景 單位是 px 不是 rpx
ctx.drawImage(bgImgPath, 0, 0, canvasW, canvasH)
// 保存上下文
ctx.save()
// 恢復畫布
ctx.restore()
// 畫小程序碼
ctx.drawImage(programCode, that.computedPercent(85), that.computedPercent(165), that.computedPercent(117), that.computedPercent(117))
ctx.draw(true, () => {
that.setData({
spinning: false
})
})
},
// 初始化數(shù)據(jù)
initData () {
let that = this
// 獲取設備寬度,計算canvas寬高
wx.getSystemInfo({
success: function(res) {
let canvasW = Math.round(res.screenWidth *0.978)
let canvasH = canvasW * 1.929
that.setData({
pixelRatio: res.pixelRatio, // 圖片像素比
canvasW,
canvasH
})
that.writeCanvas() // 暫時在此執(zhí)行
}
})
},
// 保存圖片
save () {
let that = this
wx.canvasToTempFilePath({
x: 0, // 起點橫坐標
y: 0, // 起點縱坐標
width: that.data.canvasW, // canvas 當前的寬
height: that.data.canvasH, // canvas 當前的高
destWidth: that.data.canvasW * that.data.pixelRatio, // canvas 當前的寬 * 設備像素比
destHeight: that.data.canvasH * that.data.pixelRatio, // canvas 當前的高 * 設備像素比
canvasId: 'myCanvas',
success: function (res) {
console.log(res)
//調(diào)取小程序當中獲取圖片
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '圖片保存成功!',
icon: 'none'
})
},
fail: function (res) {
console.log(res)
if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny" || res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
that.doAuth()
}
}
})
},
fail: function (res) {
console.log(res)
}
}, this)
},
// 獲取授權
doAuth () {
wx.showModal({
title: '獲取授權',
content: '您是否同意重新授權保存圖片',
cancelText: '不同意',
confirmText: '同意',
confirmColor: '#21c0ae',
success: function(res) {
if (res.confirm) { // 點擊確認
wx.openSetting({
success(settingdata) {
if (settingdata.authSetting["scope.writePhotosAlbum"]) {
console.log("獲取權限成功,再次點擊圖片保存到相冊")
} else {
console.log("獲取權限失敗")
}
},
fail: function (res) {
console.log(res)
}
})
}
}
})
},
/**
* 計算比例
* @param {String} value 像素(二倍圖量出來的要除2)
*/
computedPercent (value) {
let currentWidth = this.data.canvasW
let oldWidth = 288
return Math.floor(value * currentWidth / oldWidth)
}
},
created: function () {
let that = this
let image = wx.getStorageSync('images')
var erweima =wx.getStorageSync('erweima')
that.setData({
image:image,
erweima:erweima
})
},
})
components/canvas.wxml里
<!--components/canvas/canvas.wxml-->
<view class="generate-pic-box">
<view class="mask" wx:if="{{spinning}}">
<view class="mask-cont">
<view class="loading"></view>
</view>
</view>
<block hidden="{{!spinning}}">
<canvas class="canvas" style="{{'width: ' + (canvasW) + 'px; height: ' + (canvasH) + 'px;'}}" canvas-id="myCanvas"
hidden="{{canvasHidden}}"></canvas>
<!-- <view class="save-text">保存圖片,分享至朋友圈</view> -->
</block>
<!-- <view class="save-btn-box"> -->
<cover-view class="btuns">
<button open-type="share" class="button">分享</button>
</cover-view>
<cover-view class="save-btn" bindtap="save">保存圖片</cover-view>
<!-- </view> -->
</view>
components/canvas.wxss里
.generate-pic-box {
width: 100%;
position: relative;
}
.generate-pic-box .mask {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1001;
background-color: rgba(255, 255, 255, 0.5);
}
.generate-pic-box .mask .mask-cont {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.generate-pic-box .mask .mask-cont .loading {
width: 36rpx;
height: 36rpx;
margin-right: 20rpx;
}
.generate-pic-box .mask .mask-cont .loading-text {
color: #fff;
}
.generate-pic-box .canvas {
margin: 0 auto;
}
.generate-pic-box .generate-pic-btm {
padding-top: 24rpx;
padding-bottom: 30rpx;
background-color: #fff;
}
.generate-pic-box .save-text {
width: 100%;
margin-bottom: 21rpx;
font-size: 26rpx;
line-height: 1.5;
color: #999;
text-align: center;
}
.save-btn-box {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.save-btn {
width: 84%;
height: 64rpx;
line-height: 64rpx;
font-size: 28rpx;
border-radius: 8rpx;
text-align: center;
color: #fff;
margin-left: 60rpx;
position: absolute;
z-index: 1000;
bottom: 80rpx;
background: #076BFF ;
}
.btuns {
width: 84%;
color: #fff;
font-size: 29rpx;
position: absolute;
bottom: 171rpx;
border-radius: 8rpx;
background: #076BFF ;
margin-left: 60rpx;
}
.button {
/* width: 84%; */
color: #fff;
font-size: 29rpx;
border-radius: 8rpx;
background: #076BFF ;
}
把組件引入json里
"usingComponents": {
"canvas": "../../components/canvas/canvas"
},
直接在wxml里調(diào)用就行了
<view class="component-display-box">
<generate-pic></generate-pic>
</view>
合成照片必須是本地圖片 如果用網(wǎng)絡圖片 要先下載到本地wx.downloadFile
官網(wǎng)上也都有詳細說明的。重要的一點 下載完圖片真機調(diào)試什么的照片都可以顯示 如果上傳為測試版的話 圖片沒有顯示 那就說明下載的路徑的問題了可以在url 里加replace("http:","https:")
,一定要加在圖片地址后面
如果有更好的 歡迎一起討論
相關案例查看更多
相關閱讀
- 昆明小程序開發(fā)
- 云南做網(wǎng)站
- 云南建設廳網(wǎng)站首頁
- 電商網(wǎng)站建設
- web服務
- 汽車報廢管理系統(tǒng)
- 前端
- 昆明小程序代建
- 大理網(wǎng)站建設公司
- 云南網(wǎng)站建設百度
- 云南網(wǎng)站建設招商
- 快排推廣
- 網(wǎng)站建設高手
- 云南手機網(wǎng)站建設
- 小程序用戶登錄
- 云南網(wǎng)站開發(fā)
- 安家微信小程序
- 定制小程序開發(fā)
- 云南小程序開發(fā)哪家好
- 開通微信小程序被騙
- 報廢車拆解系統(tǒng)
- 汽車拆解系統(tǒng)
- 云南網(wǎng)站建設優(yōu)化
- 網(wǎng)頁制作
- 網(wǎng)站建設方案 doc
- 微信小程序開發(fā)入門課程
- 網(wǎng)絡公司報價
- 云南網(wǎng)站建設百度官方
- 云南網(wǎng)站建設公司
- 昆明小程序開發(fā)聯(lián)系方式