個(gè)人制作,該文章主要講解最近基于 uni-app 框架編寫(xiě)的集圖文拖拽等多方位編輯、油墨電子簽名、開(kāi)放式海報(bào)于一體的小程序的制作思路和實(shí)現(xiàn)代碼。
1、完整源碼鏈接
2、實(shí)現(xiàn)思路
3、核心代碼
3-1、圖文多方位編輯
3-2、油墨電子簽名
3-3、開(kāi)放式海報(bào)
3-4、小結(jié)
4.效果展示和體驗(yàn)
完整代碼:https://github.com/TensionMax/mini-ps
其中演示的文字編輯、圖片編輯、油墨電子簽名、開(kāi)放式海報(bào)可單獨(dú)食用,含文檔說(shuō)明。
該工具主要由五個(gè)不同組件模塊:文字編輯、圖片編輯,油墨電子簽名、控制、開(kāi)放式海報(bào)
1、文字編輯模塊設(shè)置好的文字參數(shù)對(duì)象插入到文字隊(duì)列中。
2、圖片編輯模塊設(shè)置好的圖片參數(shù)對(duì)象插入到圖片隊(duì)列中。
3、油墨電子簽名模塊完成繪制后轉(zhuǎn)為利用 canvasToTempFilePath 轉(zhuǎn)成臨時(shí)圖片,獲取參數(shù)后插入圖片隊(duì)列中,也可以直接導(dǎo)出。
4、利用控制模塊調(diào)整/文字隊(duì)列和圖片隊(duì)列的參數(shù)。
5、開(kāi)放式海報(bào)模塊,利用控制臺(tái)的參數(shù)將PS畫(huà)板上的效果繪制到canvas上來(lái)實(shí)現(xiàn)的效果,接著再利用 canvasToTempFilePath 轉(zhuǎn)成圖片導(dǎo)出。
文字/圖片編輯模塊主要是實(shí)現(xiàn)移動(dòng)/縮放功能,其他附帶的屬于甜品,
由于兩個(gè)模塊功能類似,該篇僅講解圖片編輯模塊。
<img
style="position: absolute"
:style="{
left: item.x+'px',
top: item.y+'px',
width: item.w+'px',
height: item.h+'px',
}"
@touchstart='touchStart($event,item,index)'
@longpress='longPress($event,item,index)'
@touchmove.stop='touchMove($event,item,index)'
@touchcancel="touchEnd($event,item,index)"
@touchend='touchEnd($event,item,index)'
v-for="(item,index) of imagelist"
:key="index"
:src="item.src"
/>
在 imageList 的數(shù)組標(biāo)簽中,每個(gè)綁定的事件中用 $event 來(lái)調(diào)用事件本身的參數(shù),其中 $event 的 touches 或 changedTouches 包含我們需要的位置參數(shù),示例如下:
touches:[{
clientX: 14 //與顯示區(qū)域(不含頂部欄)左上角的水平距離
clientY: 16 //與顯示區(qū)域(不含頂部欄)左上角的垂直距離
pageX: 14 //與整個(gè)頁(yè)面(不含頂部欄)左上角的水平距離
pageY: 16 //與整個(gè)頁(yè)面(不含頂部欄)左上角的垂直距離
},
{
clientX: 14
clientY: 16
pageX: 14
pageY: 16
}]
touches 長(zhǎng)度為2代表雙指觸碰,通過(guò)判定雙指觸摸點(diǎn)的變化方向可實(shí)現(xiàn)雙指縮放效果。因?yàn)槊總€(gè)標(biāo)簽都設(shè)置為 style="position: absolute" 所以只需要根據(jù)位置參數(shù)來(lái)更新 x、y、w、h 即可
一次移動(dòng)多次操作DOM影響性能
—— 虛擬DOM了解一下
為何不用事件委派
—— 不必要,Vue已經(jīng)幫我們做了優(yōu)化,在非常影響性能時(shí)再考慮
由于 touchmove 事件在小程序真機(jī)的觸發(fā)頻率和精確度很迷,不太好根據(jù)速度來(lái)判定繪制的線寬,我只好用其他方式去實(shí)現(xiàn),雖然效果不完美。
其實(shí)現(xiàn)思路是通過(guò)多次的循環(huán)繪制以達(dá)到油墨效果,每次循環(huán)繪制的長(zhǎng)度和寬度都不相同。
<canvas canvas-id="canvas" @touchstart.stop="touchStart" @touchmove.stop="touchMove" @touchend.stop="touchEnd" > </canvas>
export default {
data() {
return {
lineWidth0: 5, //初始線寬 建議1~5
ctx: null,
x0: 0, //初始橫坐標(biāo)或上一段touchmove事件中觸摸點(diǎn)的橫坐標(biāo)
y0: 0, //初始縱坐標(biāo)或上一段touchmove事件中觸摸點(diǎn)的縱坐標(biāo)
t0: 0, //初始時(shí)間或上一段touchmove事件發(fā)生時(shí)間
v0: 0, //初始速率或touchmove事件間發(fā)生速率
lineWidth: 0, //動(dòng)態(tài)線寬
keenness: 5, //油墨程度 建議0~5
k: 0.3, //油墨因子,即每次繪制線條時(shí)線寬的變化程度
}
},
onReady() {
this.ctx = uni.createCanvasContext('canvas', this);
this.ctx.setLineCap('round')
},
methods: {
//設(shè)置初始值
touchStart(e) {
this.lineWidth = this.lineWidth0
this.t0 = new Date().getTime()
this.v0 = 0
this.x0 = e.touches[0].clientX
this.y0 = e.touches[0].clientY
},
touchMove(e) {
let dx = e.touches[0].clientX - this.x0,
dy = e.touches[0].clientY - this.y0,
ds = Math.pow(dx * dx + dy * dy, 0.5),
dt = (new Date().getTime()) - this.t0,
v1 = ds / dt; //同 this.v0 初始速率或touchmove事件間發(fā)生速率
if (this.keenness === 0) { //油墨為0時(shí)
this.ctx.moveTo(this.x0, this.y0)
this.ctx.lineTo(this.x0 + dx, this.y0 + dy)
this.ctx.setLineWidth(this.lineWidth)
this.ctx.stroke()
this.ctx.draw(true)
} else {
//由于touchMove的觸發(fā)頻率問(wèn)題,這里采用for循環(huán)繪制,原理如圖所示
//這里的k因?yàn)?
let a = this.keenness
if (this.keenness > 5) {
a = 5
}
for (let i = 0; i < a; i++) {
this.ctx.moveTo(this.x0 + dx * i / a, this.y0 + dy * i / a)
this.ctx.lineTo(this.x0 + dx * (i + 1) / a, this.y0 + dy * (i + 1) / a)
//此時(shí)touchmove事件間發(fā)生與上一個(gè)事件的發(fā)生的速率比較
if (v1 < this.v0) {
this.lineWidth -= this.k
if (this.lineWidth < this.lineWidth * 0.25) this.lineWidth = this.lineWidth * 0.25
} else {
this.lineWidth += this.k
if (this.lineWidth > this.lineWidth * 1.5) this.lineWidth = this.lineWidth * 1.5
}
this.ctx.setLineWidth(this.lineWidth)
this.ctx.stroke()
this.ctx.draw(true)
}
}
this.x0 = e.touches[0].clientX
this.y0 = e.touches[0].clientY
this.t0 = new Date().getTime()
this.v0 = v1
},
touchEnd(e) {
this.x0 = 0
this.y0 = 0
this.t0 = 0
this.v0 = 0
}
}
}
使用的大部分是canvas的基礎(chǔ)api,注意繪制單位都為px。
如果說(shuō)微信小程序是銀色金灘,那么截至2020年1月6日或者未來(lái),小程序的canvas就是金灘上充斥著未知數(shù)個(gè)的玻璃塊的那一片 ——
魯迅說(shuō)起小程序canvas,那bug不是一般的多,部分不常見(jiàn)bug我會(huì)在代碼注釋里說(shuō)明。
<canvas canvas-id="generate" :style="{ width: canvasW + 'rpx', height: canvasH + 'rpx'}"></canvas>
spread 語(yǔ)法
async 函數(shù)
如果圖片是網(wǎng)絡(luò)路徑,記得獲取臨時(shí)路徑。
//別忘了在函數(shù)前加 async
let src = 'https://s2.ax1x.com/2020/01/05/lrCDx0.jpg'
src = (await uni.getImageInfo({src}))[1].path;
JAVASCRIPT輸出字段部分
//為方便設(shè)置,以下除角度外,單位均以rpx為主
data() {
return {
canvasW:720,
canvasH:1000,
img:[{
src: 'https://s2.ax1x.com/2020/01/05/lrCDx0.jpg',
x: 0,
y: 0,
w: 100,
h: 100,
r: 50,//圓角度
degrees: 30,//旋轉(zhuǎn)度
mirror: true//是否鏡像
}],
text:[{
content: 'TensionMax',
x: 50,
y: 50,
w: 100,
lineHeight: 35,//行間距
color: '#000000',
size: 28,
weight: 'normal',//字體粗細(xì)
lineThrough: true,//是否貫穿
}],
ctx: null,
k: null //單位轉(zhuǎn)換因子
};
}
JAVASCRIPTrpx 或 upx與 px 的單位統(tǒng)一轉(zhuǎn)換方法
px2rpx() {
//當(dāng)轉(zhuǎn)換的參數(shù)只有一個(gè)時(shí)直接返回?cái)?shù)值如
//當(dāng)不為一個(gè)時(shí)返回?cái)?shù)組,然后用spread語(yǔ)法將其展開(kāi)為幾個(gè)參數(shù)
//Math.floor()是為了防止在安卓機(jī)上造成的數(shù)據(jù)紊亂,開(kāi)發(fā)者工具無(wú)此bug
if (arguments.length === 1) return Math.floor(arguments[0] / this.k)
let params = []
for (let i of arguments) {
params.push(Math.floor(i / this.k))
}
return params
},
rpx2px() {
if (arguments.length === 1) return Math.floor(arguments[0] * this.k)
let params = []
for (let i of arguments) {
params.push(Math.floor(i * this.k))
}
return params
},
JAVASCRIPT繪制圖片的函數(shù)
async drawImg() {
this.ctx.setFillStyle('#FFFFFF')
this.ctx.fillRect(0, 0, ...this.rpx2px(this.canvasW, this.canvasH)) //繪制背景
for (let i of this.img) { //for循環(huán)繪制圖片
i.src = (await uni.getImageInfo({src: i.src}))[1].path;//獲取圖片臨時(shí)路徑
this.ctx.save() //保存當(dāng)前繪制內(nèi)容
if (i.mirror) { //如果設(shè)置鏡像
//因?yàn)閏anvas的translate屬性是基于原點(diǎn)(初始原點(diǎn)為右上角)變化
//所以需要先將原點(diǎn)移動(dòng)至圖片中心,變化后再還原
//旋轉(zhuǎn)變化同理
this.ctx.translate(...this.rpx2px(i.x + i.w / 2, i.y + i.h / 2))
this.ctx.scale(-1, 1)
this.ctx.translate(...this.rpx2px(-i.x - i.w / 2, -i.y - i.h / 2))
}
if (i.degrees) { //如果設(shè)置旋轉(zhuǎn)
this.ctx.translate(...this.rpx2px(i.x + i.w / 2, i.y + i.h / 2))
this.ctx.rotate(i.degrees * Math.PI / 180)
this.ctx.translate(...this.rpx2px(-i.x - i.w / 2, -i.y - i.h / 2))
}
this.radiusRect(...this.rpx2px(i.x, i.y, i.w, i.h, i.r)) //圓角或矩形路徑繪制
this.ctx.clip() //裁剪
this.ctx.drawImage(i.src, ...this.rpx2px(i.x, i.y, i.w, i.h))
this.ctx.restore() //恢復(fù)非裁剪區(qū)域
}
this.ctx.draw(true)
}
radiusRect(x, y, w, h, r) {
if (r > w / 2 || r > h / 2) {
r = Math.min(w, h) / 2
}
this.ctx.beginPath();
this.ctx.moveTo(x, y); // 將操作點(diǎn)移至左上角
this.ctx.arcTo(x + w, y, x + w, y + r, r); // 畫(huà)右上角的弧
this.ctx.lineTo(x + w, y) //可省略,但由于安卓真機(jī)的小程序bug,留之,下同。
this.ctx.arcTo(x + w, y + h, x + w - r, y + h, r); // 畫(huà)右下角的弧
this.ctx.lineTo(x + w, y + h) //可省略
this.ctx.arcTo(x, y + h, x, y + h - r, r); // 畫(huà)左下角的弧
this.ctx.lineTo(x, y + h) //可省略
this.ctx.arcTo(x, y, x + r, y, r); // 畫(huà)左上角的弧
this.ctx.lineTo(x, y) //可省略
},
繪制自定義文字
文字繪制稍微麻煩些,主要是canvas不會(huì)自動(dòng)幫我們換行排版,網(wǎng)上類似的實(shí)現(xiàn)方法太多,該篇就不講,直接放在Demo里面。
既然我們知道了這幾個(gè)組件自定義調(diào)整參數(shù)的方式,那么最后只需要一個(gè)父組件作為控制臺(tái)來(lái)調(diào)整他們的參數(shù)即可,可以通過(guò) props 、 sync 修飾符 等來(lái)實(shí)現(xiàn)父子通信,當(dāng)然如果想做更復(fù)雜的可以考慮用 Vuex 傳參。接下來(lái)就可以根據(jù)這思路來(lái)實(shí)現(xiàn)繁瑣的業(yè)務(wù)邏輯了。