【问题标题】:How do I update a cloned HTML canvas element with the context and data of the original canvas?如何使用原始画布的上下文和数据更新克隆的 HTML 画布元素?
【发布时间】:2017-07-31 05:37:05
【问题描述】:

我编写了一个脚本,允许用户在 html 5 画布元素上绘制简单的线条。最终目标是让这个绘图平铺并在浏览器的其余部分重复。我已经在页面上获得了一个克隆的画布,但正在努力研究如何在多个画布上同时绘制相同的线条。

我会在下面粘贴我的代码

var size = 40;
var md = false;
var canvas = document.getElementById('canvas');
canvas.addEventListener('mousedown', down);
canvas.addEventListener('mouseup', toggledraw);

canvas.width  = 600;
canvas.height = 600;

canvas.addEventListener('mousemove', move);

function move(evt){

  var mousePos = getMousePos(canvas, evt);
  var posx = mousePos.x;
  var posy = mousePos.y;
  draw(canvas, posx, posy);

  window.posx;


  return mousePos;



};


function down(){
  md = true;
}

function toggledraw(){
  md = false;
}

function getMousePos(canvas, evt){
  var rect = canvas.getBoundingClientRect();
  return{
    x:evt.clientX - rect.left,
    y:evt.clientY - rect.top
  };
};

function draw(canvas, posx, posy){
  var context = canvas.getContext('2d');


  if(md){
    context.fillRect(posx, posy, size, size)
  }

};

cloneCanvas(canvas, window.posx, window.posy, size);

function cloneCanvas(canvas, posx, posy, size,) {
  console.log(posx);


  var newCanvas = document.createElement('canvas');
  var context = newCanvas.getContext('2d');
  newCanvas.className = "newNew";


  newCanvas.width = canvas.width;
  newCanvas.height = canvas.height;
  document.body.appendChild(newCanvas);



  //context.fillRect(posx, posy, size, size)
  context.drawImage(canvas, posx, posy);



  return canvas;
}

【问题讨论】:

    标签: javascript jquery html canvas


    【解决方案1】:

    我会采取的方法是在主画布上绘制后更新克隆的画布。

    根据您当前的代码,我首先要返回克隆的画布而不是旧画布。这样你就有了对它的引用。

     function cloneCanvas(canvas, posx, posy, size,) {
       ...
     //  --> return canvas;
     return newCanvas // Becomes this.
    }
    

    接下来,看线:

    cloneCanvas(canvas, window.posx, window.posy, size);
    

    我看到您正在使用自己的“posx”和“posy”变量。我会摆脱那些,因为你总是想复制整个画布。 (在这种情况下)。像这样换行

    var clone = cloneCanvas(canvas, 0, 0, size);
    

    接下来,我编写了一个小辅助函数来链接两个画布。

    function drawFromSource(source, destination) {
      return function() {
        var context = destination.getContext('2d');
        context.clearRect(0, 0, destination.width, destination.height);
        context.drawImage(source, 0, 0);
      }
    }
    

    此函数返回一个回调,调用该回调时会将原始画布绘制到克隆的画布上。

    你像这样初始化它:

    var updateClone = drawFromSource(canvas, clone);
    

    最后但同样重要的是,您必须将新创建的回调添加到绘图函数中。在您绘制到主画布之后。

    function draw(canvas, posx, posy) {
      var context = canvas.getContext('2d');
      if (md) {
        context.fillRect(posx, posy, size, size)
        updateClone();
      }
    
    };
    

    瞧! 这是工作代码的链接,我改变了你的一些代码。

    https://jsfiddle.net/30efdvz3/5/

    只是为了好玩。平铺版只需更改“numberofTiles”

    https://jsfiddle.net/30efdvz3/3/

    【讨论】:

    • 是的 drawImage 是要走的路,不过只有一个注释,在重新绘制更新的画布之前,您需要clear your clones
    • @Kaiido 我添加了两行以在重绘之前清除克隆。
    【解决方案2】:

    试试这个代码

    在此我创建了一个根据画布注册鼠标事件的函数。并为所有画布调用此函数。

    var size = 40;
    var md = false;
    var canvas1 = document.getElementById('canvas');
    registerEvents(canvas1)
    function move(evt){
        console.log(evt.target.id);
        var canvas = document.getElementById(evt.target.id);
        var mousePos = getMousePos(canvas, evt);
        var posx = mousePos.x;
        var posy = mousePos.y;
        draw(canvas, posx, posy);
        window.posx;
        return mousePos;
    };
    
    function registerEvents(canvas){
        canvas.addEventListener('mousedown', down);
        canvas.addEventListener('mouseup', toggledraw);
        canvas.width  = 600;
        canvas.height = 600;
        canvas.addEventListener('mousemove', move);
    }
    function down(){
        md = true;
    }
    
    function toggledraw(){
        md = false;
    }
    
    function getMousePos(canvas, evt){
        var rect = canvas.getBoundingClientRect();
        return{
            x:evt.clientX - rect.left,
            y:evt.clientY - rect.top
        };
    };
    
    function draw(canvas, posx, posy){
        var context = canvas.getContext('2d');
        if(md){
            context.fillRect(posx, posy, size, size)
        }
    };
    cloneCanvas(canvas, window.posx, window.posy, size);
    function cloneCanvas(canvas, posx, posy, size,) {
        console.log(posx);
        var newCanvas = document.createElement('canvas');
        var context = newCanvas.getContext('2d');
        var id = "newCanvas";
        newCanvas.className = "newNew";
        newCanvas.id = id;
        newCanvas.width = canvas.width;
        newCanvas.height = canvas.height;
        newCanvas.style.border = "1px solid black";
        document.body.appendChild(newCanvas);
        context.drawImage(canvas, posx, posy);
        registerEvents(document.getElementById(id))
        return canvas;
    }
    

    Here is working fiddle

    【讨论】:

      【解决方案3】:

      其实你已经很接近了,我只是做了一些改变,达到了你想要的,这里是result

      function cloneCanvas(canvas, posx, posy, size,) {
        var newCanvas = document.createElement('canvas');
        var context = newCanvas.getContext('2d');
        ...
        return context; // this should return the context!
      }
      

      还有,

      var clonedCanvas = cloneCanvas(canvas, window.posx, window.posy, size);
      // after you call the function get the 2d context to variable.
      

      最后的变化是,

      function draw(canvas, posx, posy){
        var context = canvas.getContext('2d');
        if(md){
        clonedCanvas.fillRect(posx, posy, size, size); // fillRect to variable that you defined at above,
          context.fillRect(posx, posy, size, size)
        }
      };
      

      Jsfiddle 输出;

      希望有所帮助,

      【讨论】:

        猜你喜欢
        • 2020-04-23
        • 2012-01-12
        • 2018-03-05
        • 1970-01-01
        • 1970-01-01
        • 2015-07-15
        • 1970-01-01
        • 1970-01-01
        • 2012-01-26
        相关资源
        最近更新 更多