uniapp實現(xiàn)橫屏簽字版
本文實例為大家分享了uniapp實現(xiàn)橫屏簽字版的具體代碼,供大家參考,具體內(nèi)容如下
兼容H5、APP、微信小程序
可作為組件直接引入
通過this.$emit(‘tempFilePath’, val.tempFilePath)給予回調(diào)

<template>
?? ?<view class="main-content" v-if="isShow" @touchmove.stop.prevent="">
?? ??? ?<!-- 簽字canvas -->
?? ??? ?<canvas?
?? ??? ??? ?class="mycanvas"?
?? ??? ??? ?id="mycanvas"?
?? ??? ??? ?canvas-id="mycanvas"?
?? ??? ??? ?@touchstart="touchstart"?
?? ??? ??? ?@touchmove="touchmove"?
?? ??? ??? ?@touchend="touchend"
?? ??? ?></canvas>
?? ??? ?<!-- 旋轉(zhuǎn)canvas -->
?? ??? ?<canvas
?? ??? ??? ?class="mycanvas"
?? ??? ??? ?:style="{ 'z-index': -1, width: `${screenWidth}px`, height: `${(screenWidth * screenWidth) / screenHeight}px` }"
?? ??? ??? ?id="rotatCanvas"
?? ??? ??? ?canvas-id="rotatCanvas"
?? ??? ?></canvas>
?? ??? ?<cover-view class="button-line">
?? ??? ??? ?<cover-view class="save-button" @tap="finish">保存</cover-view>
?? ??? ??? ?<cover-view class="clear-button" @tap="clear">清除</cover-view>
?? ??? ??? ?<cover-view class="cancel-button" @tap="hide">關(guān)閉</cover-view>
?? ??? ?</cover-view>
?? ?</view>
</template>
<script>
export default {
?? ?data() {
?? ??? ?return {
?? ??? ??? ?ctx: '', //繪圖圖像
?? ??? ??? ?points: [], //路徑點集合
?? ??? ??? ?isShow: false,
?? ??? ??? ?screenWidth: '',
?? ??? ??? ?screenHeight: ''
?? ??? ?};
?? ?},
?? ?mounted() {
?? ??? ?this.createCanvas();
?? ??? ?uni.getSystemInfo({
?? ??? ??? ?success: e => {
?? ??? ??? ??? ?this.screenWidth = e.screenWidth;
?? ??? ??? ??? ?this.screenHeight = e.screenHeight;
?? ??? ??? ?}
?? ??? ?});
?? ?},
?? ?methods: {
?? ??? ?show() {
?? ??? ??? ?this.clear();
?? ??? ??? ?this.isShow = true;
?? ??? ?},
?? ??? ?hide() {
?? ??? ??? ?this.isShow = false;
?? ??? ?},
?? ??? ?//創(chuàng)建并顯示畫布
?? ??? ?createCanvas() {
?? ??? ??? ?this.showCanvas = true;
?? ??? ??? ?this.ctx = uni.createCanvasContext('mycanvas', this); //創(chuàng)建繪圖對象
?? ??? ??? ?//設(shè)置畫筆樣式
?? ??? ??? ?this.ctx.lineWidth = 2;
?? ??? ??? ?this.ctx.lineCap = 'round';
?? ??? ??? ?this.ctx.lineJoin = 'round';
?? ??? ?},
?? ??? ?//觸摸開始,獲取到起點
?? ??? ?touchstart(e) {
?? ??? ??? ?let startX = e.changedTouches[0].x;
?? ??? ??? ?let startY = e.changedTouches[0].y;
?? ??? ??? ?let startPoint = {
?? ??? ??? ??? ?X: startX,
?? ??? ??? ??? ?Y: startY
?? ??? ??? ?};
?? ??? ??? ?this.points.push(startPoint);
?? ??? ??? ?//每次觸摸開始,開啟新的路徑
?? ??? ??? ?this.ctx.beginPath();
?? ??? ?},
?? ??? ?//觸摸移動,獲取到路徑點
?? ??? ?touchmove(e) {
?? ??? ??? ?let moveX = e.changedTouches[0].x;
?? ??? ??? ?let moveY = e.changedTouches[0].y;
?? ??? ??? ?let movePoint = {
?? ??? ??? ??? ?X: moveX,
?? ??? ??? ??? ?Y: moveY
?? ??? ??? ?};
?? ??? ??? ?this.points.push(movePoint); //存點
?? ??? ??? ?let len = this.points.length;
?? ??? ??? ?if (len >= 2) {
?? ??? ??? ??? ?this.draw(); //繪制路徑
?? ??? ??? ?}
?? ??? ?},
?? ??? ?// 觸摸結(jié)束,將未繪制的點清空防止對后續(xù)路徑產(chǎn)生干擾
?? ??? ?touchend() {
?? ??? ??? ?this.points = [];
?? ??? ?},
?? ??? ?/* ***********************************************
?? ??? ??? ?# ? 繪制筆跡
?? ??? ??? ?#?? ?1.為保證筆跡實時顯示,必須在移動的同時繪制筆跡
?? ??? ??? ?#?? ?2.為保證筆跡連續(xù),每次從路徑集合中區(qū)兩個點作為起點(moveTo)和終點(lineTo)
?? ??? ??? ?#?? ?3.將上一次的終點作為下一次繪制的起點(即清除第一個點)
?? ??? ??? ?************************************************ */
?? ??? ?draw() {
?? ??? ??? ?let point1 = this.points[0];
?? ??? ??? ?let point2 = this.points[1];
?? ??? ??? ?this.points.shift();
?? ??? ??? ?this.ctx.moveTo(point1.X, point1.Y);
?? ??? ??? ?this.ctx.lineTo(point2.X, point2.Y);
?? ??? ??? ?this.ctx.stroke();
?? ??? ??? ?this.ctx.draw(true);
?? ??? ?},
?? ??? ?//清空畫布
?? ??? ?clear() {
?? ??? ??? ?this.ctx.clearRect(0, 0, this.screenWidth, this.screenHeight);
?? ??? ??? ?this.ctx.draw(true);
?? ??? ?},
?? ??? ?//完成繪畫并保存到本地
?? ??? ?finish() {
?? ??? ??? ?uni.canvasToTempFilePath(
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?canvasId: 'mycanvas',
?? ??? ??? ??? ??? ?success: res => {
?? ??? ??? ??? ??? ??? ?this.rotat(res.tempFilePath);
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?complete: com => {}
?? ??? ??? ??? ?},
?? ??? ??? ??? ?this
?? ??? ??? ?);
?? ??? ?},
?? ??? ?// 將圖片選裝
?? ??? ?rotat(e) {
?? ??? ??? ?let rotatCtx = uni.createCanvasContext('rotatCanvas', this); //創(chuàng)建繪圖對象
?? ??? ??? ?// 重新定位中心點
?? ??? ??? ?rotatCtx.translate(0, (this.screenWidth * this.screenWidth) / this.screenHeight);
?? ??? ??? ?// 將畫布逆時針旋轉(zhuǎn)90度
?? ??? ??? ?rotatCtx.rotate((270 * Math.PI) / 180);
?? ??? ??? ?// 將簽字圖片繪制進(jìn)入Canvas
?? ??? ??? ?rotatCtx.drawImage(e, 0, 0, (this.screenWidth * this.screenWidth) / this.screenHeight, this.screenWidth);
?? ??? ??? ?// 保存后旋轉(zhuǎn)后的結(jié)果
?? ??? ??? ?rotatCtx.draw(true);
?? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ?// 生成圖片并回調(diào)
?? ??? ??? ??? ?uni.canvasToTempFilePath(
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?canvasId: 'rotatCanvas',
?? ??? ??? ??? ??? ??? ?success: val => {
?? ??? ??? ??? ??? ??? ??? ?this.$emit('tempFilePath', val.tempFilePath);
?? ??? ??? ??? ??? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ??? ??? ??? ??? ?this.hide();
?? ??? ??? ??? ??? ??? ??? ?}, 500);
?? ??? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ??? ?complete: com => {
?? ??? ??? ??? ??? ??? ??? ?// console.log(com);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?this
?? ??? ??? ??? ?);
?? ??? ??? ?}, 500);
?? ??? ?}
?? ?}
};
</script>
<style lang="scss" scoped>
.main-content {
?? ?width: 100vw;
?? ?height: 100vh;
?? ?background-color: red;
?? ?position: fixed;
?? ?top: 0rpx;
?? ?left: 0rpx;
?? ?z-index: 9999;
}
.mycanvas {
?? ?width: 100vw;
?? ?height: 100vh;
?? ?background-color: #efefef;
?? ?position: fixed;
?? ?left: 0rpx;
?? ?top: 0rpx;
?? ?z-index: 2;
}
.button-line {
?? ?transform: rotate(90deg);
?? ?position: fixed;
?? ?bottom: 170rpx;
?? ?left: -100rpx;
?? ?width: 340rpx;
?? ?height: 50rpx;
?? ?display: flex;
?? ?align-items: center;
?? ?justify-content: space-between;
?? ?z-index: 999;
}
.button-style {
?? ?color: #ffffff;
?? ?width: 100rpx;
?? ?height: 60rpx;
?? ?text-align: center;
?? ?line-height: 60rpx;
?? ?border-radius: 10rpx;
}
.save-button {
?? ?@extend .button-style;
?? ?background-color: #02b340;
}
.clear-button {
?? ?@extend .button-style;
?? ?background-color: #ffa500;
}
.cancel-button {
?? ?@extend .button-style;
?? ?background-color: #e10b2b;
}
</style>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript數(shù)組reduce()方法的語法與實例解析
js函數(shù)中有三個在特定場合很好用的函數(shù):reduce(),map(),filter(),這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組reduce()方法的相關(guān)資料,需要的朋友可以參考下2021-07-07
javascript:void(0)是什么意思及href=#與href=javascriptvoid(0)的區(qū)別
Javascript中void是一個操作符,該操作符指定要計算一個表達(dá)式但是不返回值,本文給大家介紹javascript:void(0)是什么意思及href=#與href=javascriptvoid(0)的區(qū)別,需要的朋友參考下2015-11-11
Javascript連接數(shù)據(jù)庫查詢并插入數(shù)據(jù)
這篇文章主要介紹了Javascript連接數(shù)據(jù)庫查詢并插入數(shù)據(jù),下面文章圍繞主題展開詳細(xì)內(nèi)容,具有一的參考價值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助2022-03-03
document.execCommand()的用法小結(jié)
本篇文章主要是對document.execCommand()的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
js中eval()函數(shù)和trim()去掉字符串左右空格應(yīng)用
對于js中eval()函數(shù)的理解和寫一個函數(shù)trim()去掉字符串左右空格;對于js中eval()函數(shù)的理解是本人心得不一定正確,感興趣的朋友參考下,或許對你學(xué)習(xí)eval()函數(shù)有所幫助2013-02-02

