vue 를 사용 하여 전자 서명 구성 요소 의 예제 코드 를 실현 합 니 다.

생활 속 에서 우리 가 전자 서명 을 가장 많이 사용 하 는 곳 은 아마도 은행 일 것 이다.매번 너 에 게 이름 을 남기 게 할 것 이다.오늘 우 리 는 vue 로 전자 서명 패 널 을 실현 할 것 이다.
그림 을 그 리 려 면 첫 번 째 단 계 는 canvas 라벨 을 사용 하 는 것 입 니 다.이전 글 에서 canvas 를 사용 하여 전단 에 그래 픽 인증 코드 를 만 드 는 구성 요 소 를 실 현 했 습 니 다.토 크 되 는 것 이 안전 하지 않 습 니 다.그러면 이 전자 서명 구성 요 소 는 토 크 되 지 않 을 것 입 니 다.
canvas
탭 은 HTML 5 의 새 탭 입 니 다.
탭 은 그래 픽 용기 일 뿐 발 로 그림 을 그 려 야 합 니 다.
canvas 태그 자 체 는 그림 그리 기 능력 이 없습니다.모든 그림 그리 기 작업 은 자바 스 크 립 트 내부 에서 이 루어 져 야 합 니 다.
canvas 를 사용 하여 그림 을 그 리 는 데 필요 한 몇 가지 절차 가 있 습 니 다.
canvas 요 소 를 가 져 옵 니 다
  • canvas 요 소 를 통 해 context 대상 을 만 듭 니 다
  • context 대상 을 통 해 도형 을 그립 니 다
  • 현재 전자 서명 수요 에서 서명 은 하나의 선 으로 구성 되 기 때문에 우 리 는 다음 과 같은 몇 가지 방법 을 사용 할 것 이다.
  • beginPath():경 로 를 시작 하거나 현재 경 로 를 초기 화 합 니 다
  • moveto():캔버스 의 지정 점 으로 경 로 를 이동 하고 라인 을 만 들 지 않 습 니 다
  • lineto():새로운 점 을 추가 한 다음 에 캔버스 에 이 점 에서 마지막 으로 지정 한 선 을 만 듭 니 다
  • stroke():정 의 된 경 로 를 그립 니 다
  • closePath():현재 점 에서 시작 점 으로 돌아 가 는 경 로 를 만 듭 니 다
  • 이벤트
    canvas 에 그림 을 그 리 려 면 몇 가지 특정한 사건 을 연결 해 야 합 니 다.이 사건 들 은 pc 쪽 과 핸드폰 쪽 이 다 릅 니 다.
    pc 엔 드 이벤트
  • mousedown
  • mousemove
  • mouseup
  • 핸드폰 엔 드 사건
  • touchstart
  • touchmove
  • touchend
  • 핵심 코드
    canvas 태그 초기 화 및 귀속 이벤트
    
    <canvas
        @touchstart="touchStart"
        @touchmove="touchMove"
        @touchend="touchEnd"
        ref="canvasF"
        @mousedown="mouseDown"
        @mousemove="mouseMove"
        @mouseup="mouseUp"
       ></canvas>
    
    화필 을 얻다
    mounted 수명 주기 초기 화
    
    mounted() {
      let canvas = this.$refs.canvasF;
      canvas.height = this.$refs.canvasHW.offsetHeight - 100;
      canvas.width = this.$refs.canvasHW.offsetWidth - 10;
      this.canvasTxt = canvas.getContext("2d");
      this.canvasTxt.strokeStyle = this.color;
      this.canvasTxt.lineWidth = this.linewidth;
     }
    이벤트 처리
    mouseDown
    
    //      
      mouseDown(ev) {
       ev = ev || event;
       ev.preventDefault();
    
       let obj = {
        x: ev.offsetX,
        y: ev.offsetY
       };
       this.startX = obj.x;
       this.startY = obj.y;
       this.canvasTxt.beginPath();//    
       this.points.push(obj);//   
       this.isDown = true;
      },
    
    
    touchStart
    
    //      
      touchStart(ev) {
       ev = ev || event;
       ev.preventDefault();
       if (ev.touches.length == 1) {
        this.isDraw = true; //    
        let obj = {
         x: ev.targetTouches[0].clientX,
         y:
          ev.targetTouches[0].clientY -
          (document.body.offsetHeight * 0.5 +
           this.$refs.canvasHW.offsetHeight * 0.1)
        }; //y     :document.body.offsetHeight*0.5          signatureBox    ,this.$refs.canvasHW.offsetHeight*0.1        
        this.startX = obj.x;
        this.startY = obj.y;
        this.canvasTxt.beginPath();//    
        this.points.push(obj);//   
       }
      },
    
    mouseMove
    
    //      
      mouseMove(ev) {
       ev = ev || event;
       ev.preventDefault();
       if (this.isDown) {
        let obj = {
         x: ev.offsetX,
         y: ev.offsetY
        };
        this.moveY = obj.y;
        this.moveX = obj.x;
        this.canvasTxt.moveTo(this.startX, this.startY);//    
        this.canvasTxt.lineTo(obj.x, obj.y);//    
        this.canvasTxt.stroke();//  
        this.startY = obj.y;
        this.startX = obj.x;
        this.points.push(obj);//   
       }
      },
    
    touchMove
    
    //      
      touchMove(ev) {
       ev = ev || event;
       ev.preventDefault();
       if (ev.touches.length == 1) {
        let obj = {
         x: ev.targetTouches[0].clientX,
         y:
          ev.targetTouches[0].clientY -
          (document.body.offsetHeight * 0.5 +
           this.$refs.canvasHW.offsetHeight * 0.1)
        };
        this.moveY = obj.y;
        this.moveX = obj.x;
        this.canvasTxt.moveTo(this.startX, this.startY);//    
        this.canvasTxt.lineTo(obj.x, obj.y);//    
        this.canvasTxt.stroke();//  
        this.startY = obj.y;
        this.startX = obj.x;
        this.points.push(obj);//   
       }
      },
    
    mouseUp
    
    //      
      mouseUp(ev) {
       ev = ev || event;
       ev.preventDefault();
       if (1) {
        let obj = {
         x: ev.offsetX,
         y: ev.offsetY
        };
        this.canvasTxt.closePath();//  
        this.points.push(obj);//   
        this.points.push({ x: -1, y: -1 });
        this.isDown = false;
       }
      },
    
    touchEnd
    
    //      
      touchEnd(ev) {
       ev = ev || event;
       ev.preventDefault();
       if (ev.touches.length == 1) {
        let obj = {
         x: ev.targetTouches[0].clientX,
         y:
          ev.targetTouches[0].clientY -
          (document.body.offsetHeight * 0.5 +
           this.$refs.canvasHW.offsetHeight * 0.1)
        };
        this.canvasTxt.closePath();//  
        this.points.push(obj);//   
        this.points.push({ x: -1, y: -1 });//   
       }
      },
    
    다시 쓴다
    자신 이 오 자 를 쓴 것 을 발견 하고 화판 을 지우 고 다시 썼 다.
    
    //  
      overwrite() {
       this.canvasTxt.clearRect(
        0,
        0,
        this.$refs.canvasF.width,
        this.$refs.canvasF.height
       );
       this.points = [];
       this.isDraw = false; //    
      },
    
    사용 한 data
    
    data() {
      return {
       points: [],
       canvasTxt: null,
       startX: 0,
       startY: 0,
       moveY: 0,
       moveX: 0,
       endY: 0,
       endX: 0,
       w: null,
       h: null,
       isDown: false,
       color: "#000",
       linewidth: 3,
       isDraw: false //    
      };
     },
    

    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기