美文网首页
03-uniapp签字板

03-uniapp签字板

作者: 零涂 | 来源:发表于2023-07-20 10:50 被阅读0次

    一.直接使用canvas实现

    原文地址:https://www.jianshu.com/p/800fecd63094

    <template>
        <view >
            <view class="title">请在下面输入签名:</view>
            <canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
            <view class="footer">
                <view class="left" @click="finish">完成</view>
                <view class="right" @click="clear">清除</view>
            </view>
        </view>
    </template>
     
    <script>
        var x = 20;
        var y =20;
        export default{
            data(){
                return {
                    ctx:'',         //绘图图像
                    points:[]       //路径点集合
                }
            },
            onLoad() {
                this.ctx = uni.createCanvasContext("mycanvas",this);   //创建绘图对象
                 
                //设置画笔样式
                this.ctx.lineWidth = 4;
                this.ctx.lineCap = "round"
                this.ctx.lineJoin = "round"
            },
            methods:{
                //触摸开始,获取到起点
                touchstart:function(e){
                    let startX = e.changedTouches[0].x;
                    let startY = e.changedTouches[0].y;
                    let startPoint = {X:startX,Y:startY};
                     
                    /* **************************************************
                        #由于uni对canvas的实现有所不同,这里需要把起点存起来
                     * **************************************************/
                    this.points.push(startPoint);
                     
                    //每次触摸开始,开启新的路径
                    this.ctx.beginPath();
                },
                 
                //触摸移动,获取到路径点
                touchmove:function(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();                   //绘制路径
                    }
                     
                },
                 
                // 触摸结束,将未绘制的点清空防止对后续路径产生干扰
                touchend:function(){                  
                    this.points=[];
                },
                 
                /* ***********************************************
                #   绘制笔迹
                #   1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
                #   2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
                #   3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
                ************************************************ */
                draw: function() {
                    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:function(){
                    let that = this;
                    uni.getSystemInfo({
                        success: function(res) {
                            let canvasw = res.windowWidth;
                            let canvash = res.windowHeight;
                            that.ctx.clearRect(0, 0, canvasw, canvash);
                            that.ctx.draw(true);
                        },
                    })
                },
                 
                //完成绘画并保存到本地
                finish:function(){
                    uni.canvasToTempFilePath({
                      canvasId: 'mycanvas',
                      success: function(res) {
                        //如果是h5页面,res.tempFilePath为base64
                        let path = res.tempFilePath;
                        uni.saveImageToPhotosAlbum({
                            filePath:path
                        })
                      }
                    })
                }
            },
        }
    </script>
     
    <style>
        .title{
            height:50upx;
            line-height: 50upx;
            font-size: 16px;
        }
        .mycanvas{
            width: 100%;
            height: calc(100vh - 200upx);
            background-color: #ECECEC;
        }
        .footer{
            font-size: 16px;
            height: 150upx;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }
        .left,.right{
            line-height: 100upx;
            height: 100upx;
            width: 250upx;
            text-align: center;
            font-weight: bold;
            color: white;
            border-radius: 5upx;
        }
        .left{
            background: #007AFF;
        }
        .right{
            background:orange;
        }
    </style>
    

    二.zwp-draw-pad插件使用

    插件地址:https://ext.dcloud.net.cn/plugin?id=4329

    <template>
        <view class="pageCss">
            <zwp-draw-pad ref="zwpDrawPad" :width="500" :height="500" style="background-color: #fff;"/>
            <u-button type="primary" @click="clearBtn">清除</u-button>
            <u-button type="primary" @click="backoutBtn">撤销</u-button>
            <u-button type="primary" @click="saveBtn">保存</u-button>
        </view>
    </template>
    
    <script>
        // 插件地址:https://ext.dcloud.net.cn/plugin?id=4329
        export default {
            data() {
                return {
                    
                };
            },
            methods:{
                clearBtn(){
                    this.$refs.zwpDrawPad.init();
                },
                backoutBtn(){
                    this.$refs.zwpDrawPad.back();
                },
                saveBtn(){
                    this.$refs.zwpDrawPad.save().then(res=>{
                        console.log('保存图片的地址', res.tempFilePath)
                    })
                },
            },
        }
    </script>
    
    <style lang="scss">
    page{
        background: #F7F8F9FF;
    }
    </style>
    

    三. l-signature插件使用

    插件地址:https://test-ext.dcloud.net.cn/plugin?id=4354
    演示地址:http://liangei.gitee.io/limeui/#/signature

    <template>
        <view>
            <view class="topBtnBox">
                <u-button type="primary" shape="circle" @click="onClick('clear')" class="btnCss btnCssClear">清除</u-button>
                <u-button type="primary" shape="circle" @click="onClick('undo')" class="btnCss btnCssBackout">撤消</u-button>
            </view>
            <view class="signatureCss" style="margin: 0 30rpx;background-color: #fff;border-radius: 20rpx;">
                <l-signature 
                    disableScroll 
                    ref="signatureRef" 
                    :penColor="penColor" 
                    :penSize="penSize" 
                    :maxLineWidth="20" 
                    :openSmooth="openSmooth" 
                ></l-signature>
            </view>
            <view>
                <u-button type="primary" shape="circle" @click="onClick('save')"  class="btnCss btnCssSave">保存</u-button>
                <!-- <u-button type="primary" shape="circle" @click="onClick('openSmooth')">压感关</u-button> -->
            </view>
        </view>
    
    </template>
    
    <script>
        // 插件地址:https://test-ext.dcloud.net.cn/plugin?id=4354
    export default {
        data() {
            return {
                penColor: '#454545',
                penSize: 5,
                url: '',
                openSmooth: true
            }
        },
        methods: {
            onClick(type) {
                 if(type == 'openSmooth') {
                     this.openSmooth = !this.openSmooth
                     return
                 }
                if (type == 'save') {
                    this.$refs.signatureRef.canvasToTempFilePath({
                        success: (res) => {
                            this.url = res.tempFilePath;
                            console.log('url',this.url);
                        }
                    })
                    return
                }
                if (this.$refs.signatureRef)
                    this.$refs.signatureRef[type]()
            }
        }
    }
    
    </script>
    
    <style lang="scss">
    page{
        background: #F7F8F9FF;
    }
    .btnCss{
        font-size: 32rpx;
        width: 150rpx;
    }
    .topBtnBox{
        display: flex;
        margin: 32rpx;
    }
    .btnCssSave{
        width: 200rpx;
        margin: 80rpx auto 0;
    }
    .signatureCss{
        height: 750rpx;
    }
    </style>
    
    lADPKGXzW6VbyfLNCWDNBDg_1080_2400.jpg_720x720q90g.jpg

    相关文章

      网友评论

          本文标题:03-uniapp签字板

          本文链接:https://www.haomeiwen.com/subject/bptzudtx.html