为什么此JavaScript画布绘图应用程序滞后?

时间:2019-04-27 19:46:27

标签: javascript html html5-canvas

根据this page的教程,我正在使用JavaScript开发HTML绘图应用程序。我已经到了“演示颜色”部分的末尾,就在“添加尺寸”部分之前。

我已经为自己制作了该应用程序,但是经过合理的绘图之后,该应用程序开始出现滞后现象。笔开始延迟到鼠标后面,但是最重要的是,当颜色改变时,笔的颜色实际上需要花费大量时间。

按下按钮时我正在呼叫console.log,告诉我按下了哪个按钮。按下按钮后,它们会立即显示,但是颜色仍然需要一段时间才能更改。

代码如下:

<html>
  <head></head>
<body>
  <canvas id="lessonCanvas" width="800" height="500" style="border:1px solid black;"></canvas>
  <button id="colorPurple">Purple</button>
  <button id="colorGreen">Green</button>
  <button id="colorYellow">Yellow</button>
  <button id="colorBrown">Brown</button>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
    //canvas drawing
    context = document.getElementById('lessonCanvas').getContext("2d");
    $('#lessonCanvas').mousedown(function(e){
      var mouseX = e.pageX - this.offsetLeft;
      var mouseY = e.pageY - this.offsetTop;

      paint = true;
      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
      redraw();
    });
    $('#lessonCanvas').mousemove(function(e){
      if(paint){
        addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
        redraw();
      }
    });
    $('#lessonCanvas').mouseup(function(e){
      paint = false;
    });
    $('#lessonCanvas').mouseleave(function(e){
      paint = false;
    });

    var clickX = new Array();
    var clickY = new Array();
    var clickDrag = new Array();
    var clickSize = new Array();
    var curSize = "normal";
    var clickTool = new Array();
    var curTool = "crayon";
    var paint;

    function addClick(x, y, dragging) {
      clickX.push(x);
      clickY.push(y);
      clickDrag.push(dragging);
      if(curTool == "eraser"){
        clickColor.push("white");
      }else{
        clickColor.push(curColor);
      }
      clickColor.push(curColor);
      clickSize.push(curSize);
    }

    function redraw() {
      context.lineJoin = "round";
      /* context.lineWidth = 5; */
      for(var i=0; i < clickX.length; i++) {        
        context.beginPath();
        if(clickDrag[i] && i) {
          context.moveTo(clickX[i-1], clickY[i-1]);
        } else {
          context.moveTo(clickX[i]-1, clickY[i]);
        }
        context.lineTo(clickX[i], clickY[i]);
        context.closePath();
        context.strokeStyle = clickColor[i];
        context.lineWidth = curSize;
        context.stroke();
      }
    }

    var colorPurple = "#cb3594";
    var colorGreen = "#659b41";
    var colorYellow = "#ffcf33";
    var colorBrown = "#986928";

    var curColor = colorPurple;
    var clickColor = new Array();

    document.getElementById("colorPurple").onclick = function() {
      curColor = colorPurple;
      console.log("Color changed to purple.");
    }
    document.getElementById("colorGreen").onclick = function() {
      curColor = colorGreen;
      console.log("Color changed to green.");
    }
    document.getElementById("colorYellow").onclick = function() {
      curColor = colorYellow;
      console.log("Color changed to yellow.");
    }
    document.getElementById("colorBrown").onclick = function() {
      curColor = colorBrown;
      console.log("Color changed to brown.");
    }
  </script>
</body>
</html>

this page处有一个演示,它使用完全相同的代码,但由于某种原因不会延迟。这是因为我的画布更大,还是还有其他我没有注意到的问题?任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

它不会滞后。你做错了。

在下面的addClick处查找,您两次添加相同的颜色,这就是为什么您会得到这种行为。

我做了些小修改,请阅读addClick上的评论

   
    var colorPurple = "#cb3594";
    var colorGreen = "#659b41";
    var colorYellow = "#ffcf33";
    var colorBrown = "#986928";

    var curColor = colorPurple;
    var clickColor = new Array();
   
   //canvas drawing
    context = document.getElementById('lessonCanvas').getContext("2d");
$('#lessonCanvas').mousedown(function(e){
      var mouseX = e.pageX - this.offsetLeft;
      var mouseY = e.pageY - this.offsetTop;

      paint = true;
      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
      redraw();
    });
    $('#lessonCanvas').mousemove(function(e){
      if(paint){
        addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
        redraw();
      }
    });
    $('#lessonCanvas').mouseup(function(e){
      paint = false;
    });
    $('#lessonCanvas').mouseleave(function(e){
      paint = false;
    });

    var clickX = new Array();
    var clickY = new Array();
    var clickDrag = new Array();
    var clickSize = new Array();
    var curSize = "normal";
    var clickTool = new Array();
    var curTool = "crayon";
    var paint;

    function addClick(x, y, dragging) {
      clickX.push(x);
      clickY.push(y);
      clickDrag.push(dragging);
      if(curTool == "eraser"){
        clickColor.push("white");
      }else{
         clickColor.push(curColor); 
      }
      // clickColor.push(curColor);//This should be removed
      clickSize.push(curSize);
    }

    function redraw() {
      context.lineJoin = "round";
      /* context.lineWidth = 5; */
      for(var i=0; i < clickX.length; i++) {        
        context.beginPath();
        if(clickDrag[i] && i) {
          context.moveTo(clickX[i-1], clickY[i-1]);
        } else {
          context.moveTo(clickX[i]-1, clickY[i]);
        }
        context.lineTo(clickX[i], clickY[i]);
        context.closePath();
        context.strokeStyle = clickColor[i];
        context.lineWidth = curSize;
        context.stroke();
      }
    }


    document.getElementById("colorPurple").onclick = function() {
      curColor = colorPurple;
      console.log("Color changed to purple.");
    }
    document.getElementById("colorGreen").onclick = function() {
      curColor = colorGreen;
      console.log("Color changed to green.");
    }
    document.getElementById("colorYellow").onclick = function() {
      curColor = colorYellow;
      console.log("Color changed to yellow.");
    }
    document.getElementById("colorBrown").onclick = function() {
      curColor = colorBrown;
      console.log("Color changed to brown.");
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="lessonCanvas" width="200" height="200" style="border:1px solid black;"></canvas>
  <button id="colorPurple">Purple</button>
  <button id="colorGreen">Green</button>
  <button id="colorYellow">Yellow</button>
  <button id="colorBrown">Brown</button>

答案 1 :(得分:0)

我也遇到了这种滞后。我遵循教程here。搜索解决方案后,我发现了这个tutorial。比较之后,它滞后的原因是由于这个循环。

function redraw() {
  context.lineJoin = "round";
  /* context.lineWidth = 5; */
  for(var i=0; i < clickX.length; i++) {        
    context.beginPath();
    if(clickDrag[i] && i) {
      context.moveTo(clickX[i-1], clickY[i-1]);
    } else {
      context.moveTo(clickX[i]-1, clickY[i]);
    }
    context.lineTo(clickX[i], clickY[i]);
    context.closePath();
    context.strokeStyle = clickColor[i];
    context.lineWidth = curSize;
    context.stroke();
  }
}

保存在数组中的值不会重置,并且每次发生鼠标移动事件时总是循环。要解决此问题,请删除addClick(),不要将其保存在数组中,而只保存最后的x,y和颜色。您还应保存鼠标按下事件的最后x和y。这是一个例子。

saveX;
saveY;
function redraw(x, y){
    ctx.beginPath();
    ctx.moveTo(saveX, saveY);
    ctx.lineTo(x, y);
    ctx.closePath();
    ctx.strokeStyle = selectedColor;
    ctx.lineJoin = 'round';
    ctx.lineWidth = curSize;
    ctx.stroke();
    saveX = x;
    saveY = y;
}