HTML Canvas清除并重新出现

时间:2018-02-13 00:16:37

标签: html html5 canvas html5-canvas

我有一个html画布,我画了一个矩形,当我清除画布时屏幕清除但是当你移动鼠标时,矩形重新出现。我垂直拉伸直肠。下面是一个代码笔示例,您必须双击画布才能显示rect。 here

HTML

<div id="app">
  <div class="cv">
    <canvas  v-on:mousedown="mouseDown" v-on:mousemove="mouseMove" v-on:mouseup="mouseUp" @dblclick="dclick" id="rect" class="rect"  width="150" height="700"></canvas>
    <button v-on:click="clear">
      Clear
    </button>
  </div>
</div>

JS

methods: {
       checkCloseEnough: function (p1, p2) {
            return Math.abs(p1 - p2) < this.closeEnough;
        },
        getVal: function (x) {
            this.canvas2 = document.getElementById('rect');
            this.ctx2 = this.canvas2.getContext('2d');
            this.st = this.canvas2.height;
            this.ic = (this.st / x);           
        },
        draw: function () {
                this.ctx2.fillStyle = "#222222";
                this.ctx2.fillRect(this.ctx2.rect.startX, this.ctx2.rect.startY, this.ctx2.rect.w, this.ctx2.rect.h);
                this.drawHandles();

        },
        drawHandles: function () {
            this.drawCircle(this.ctx2.rect.startX + this.ctx2.rect.w/2, this.ctx2.rect.startY, this.closeEnough); //top left corner
            //drawCircle(rect.startX + rect.w, rect.startY, closeEnough);
            //drawCircle(rect.startX + rect.w, rect.startY + rect.h, closeEnough);
            this.drawCircle(this.ctx2.rect.startX + this.ctx2.rect.w/2, this.ctx2.rect.startY + this.ctx2.rect.h, this.closeEnough);
        },
        drawCircle: function (x, y, radius) {
            this.ctx2.fillStyle = "#FF0000";
            this.ctx2.beginPath();
            this.ctx2.arc(x, y, radius, 0, 2 * Math.PI);
            this.ctx2.closePath();
            this.ctx2.fill();

        },
        checkCloseEnough: function (p1, p2) {
            return Math.abs(p1 - p2) < this.closeEnough;
        },
        mouseDown: function (event) {

            this.mouseX = event.pageX - this.canvas2.offsetLeft;
            this.mouseY = event.pageY - this.canvas2.offsetTop;
              // if there isn't a rect yet
            if (this.ctx2.rect.w === undefined) {
                this.ctx2.rect.startX = this.mouseY;
                this.ctx2.rect.startY = this.mouseX;
                this.dragBR = true;
            }
            if (this.checkCloseEnough(this.mouseX, this.ctx2.rect.startX + this.ctx2.rect.w/2) && this.checkCloseEnough(this.mouseY, this.ctx2.rect.startY)) {
                this.dragTL = true;

            }
            else if (this.checkCloseEnough(this.mouseX, this.ctx2.rect.startX + this.ctx2.rect.w/2) && this.checkCloseEnough(this.mouseY, this.ctx2.rect.startY + this.ctx2.rect.h)) {
                this.dragBR = true;

            }
            else {
                // handle not resizing
            }



            this.ctx2.clearRect(0, 0, this.canvas2.width, this.canvas2.height);
            this.draw();

        },
        mouseMove: function (event) {
            this.mouseX = event.pageX - this.canvas2.offsetLeft;
            this.mouseY = event.pageY - this.canvas2.offsetTop;
            if (this.dragTL) {
                //rect.w += rect.startX - mouseX;
                this.ctx2.rect.h += this.ctx2.rect.startY - this.mouseY;
                //rect.startX = mouseX;
                this.ctx2.rect.startY = this.mouseY;
            }
            else if (this.dragBR) {
                //rect.w = Math.abs(rect.startX - mouseX);
                this.ctx2.rect.h = Math.abs(this.ctx2.rect.startY - this.mouseY);
            }
            this.ctx2.clearRect(0, 0, this.canvas2.width, this.canvas2.height);
            this.draw();
        },
        mouseUp: function () {
            this.dragTL = false;
            this.dragTR = false;
            this.dragBL = false; 
            this.dragBR = false;
        },

        dclick: function (e) {
            console.log("Fires");

            e.preventDefault();
            this.ctx2.rect = {
                startX: 25,
                startY: 100,
                w: (this.canvas2.width - 50),
                h: 300,
            }
            this.draw();

            this. ln = this.lines;
            this.getVal(10);
        },

        clear: function () {
            this.cv2 = 'rect';
            this.canvas2 = document.getElementById(this.cv2);
            this.ctx2 = this.canvas2.getContext('2d');
           console.log(this.ctx2.clearRect(0, 0, this.canvas2.width, this.canvas2.height));
            console.log("Clear should run");
        },
    }

  })

1 个答案:

答案 0 :(得分:0)

事实证明我的逻辑错误是由Kamal Singh指出的,我需要在我的方法中添加明确的标志,以便在屏幕被清除时它们不会运行。

<html>
    <head>
        <title>Test</title>
        <script src="vue.min.js"></script>
    </head>
    <body>
        <div id="app">

            <div class="cv">
                <canvas style='border:1px solid;'  v-on:mousedown="mouseDown" v-on:mousemove="mouseMove" v-on:mouseup="mouseUp" @dblclick="dclick" id="rect" class="rect"  width="150" height="700"></canvas>
                <button v-on:click="clear">
            Clear
            </button>
            </div>
        </div>
        <script>
         new Vue({
          el: '#app',
          data: function() {
                return {

                    rect : {},
                    drag : false,
                  closeEnough : 10,
                    st : 0,
                    ic : 0,
                    mouseX : 0,
                    mouseY : 0,
                    dragTL : false,
                    dragBL : false,
                    dragTR : false,
                    dragBR : false,
                    cv2: '',
                    ln: 0,
                cleared: true
                }
            },
            mounted: function () {
                this.getVal(10);
                this.draw();
            },                
            methods: {
               checkCloseEnough: function (p1, p2) {
                    return Math.abs(p1 - p2) < this.closeEnough;
                },
                getVal: function (x) {
                    this.canvas2 = document.getElementById('rect');
                    this.ctx2 = this.canvas2.getContext('2d');
                    this.st = this.canvas2.height;
                    this.ic = (this.st / x);           
                },
                draw: function () {
                        this.ctx2.fillStyle = "#222222";
                        this.ctx2.fillRect(this.ctx2.rect.startX, this.ctx2.rect.startY, this.ctx2.rect.w, this.ctx2.rect.h);
                        this.drawHandles();

                },
                drawHandles: function () {
                    this.drawCircle(this.ctx2.rect.startX + this.ctx2.rect.w/2, this.ctx2.rect.startY, this.closeEnough); //top left corner
                    //drawCircle(rect.startX + rect.w, rect.startY, closeEnough);
                    //drawCircle(rect.startX + rect.w, rect.startY + rect.h, closeEnough);
                    this.drawCircle(this.ctx2.rect.startX + this.ctx2.rect.w/2, this.ctx2.rect.startY + this.ctx2.rect.h, this.closeEnough);
                },
                drawCircle: function (x, y, radius) {
                    this.ctx2.fillStyle = "#FF0000";
                    this.ctx2.beginPath();
                    this.ctx2.arc(x, y, radius, 0, 2 * Math.PI);
                    this.ctx2.closePath();
                    this.ctx2.fill();

                },
                checkCloseEnough: function (p1, p2) {
                    return Math.abs(p1 - p2) < this.closeEnough;
                },
                mouseDown: function (event) {
                    if(this.cleared) return;
                    this.mouseX = event.pageX - this.canvas2.offsetLeft;
                    this.mouseY = event.pageY - this.canvas2.offsetTop;
                      // if there isn't a rect yet
                    if (this.ctx2.rect.w === undefined) {
                        this.ctx2.rect.startX = this.mouseY;
                        this.ctx2.rect.startY = this.mouseX;
                        this.dragBR = true;
                    }
                    if (this.checkCloseEnough(this.mouseX, this.ctx2.rect.startX + this.ctx2.rect.w/2) && this.checkCloseEnough(this.mouseY, this.ctx2.rect.startY)) {
                        this.dragTL = true;

                    }
                    else if (this.checkCloseEnough(this.mouseX, this.ctx2.rect.startX + this.ctx2.rect.w/2) && this.checkCloseEnough(this.mouseY, this.ctx2.rect.startY + this.ctx2.rect.h)) {
                        this.dragBR = true;

                    }
                    else {
                        // handle not resizing
                    }