简体   繁体   English

如何使用原始画布的上下文和数据更新克隆的HTML画布元素?

[英]How do I update a cloned HTML canvas element with the context and data of the original canvas?

I have written a script that allows users to draw simple lines on top of an html 5 canvas element. 我编写了一个脚本,允许用户在html 5 canvas元素上绘制简单的线条。 The ultimate goal is to have this drawing being tiled and repeated across the rest of the browser. 最终目标是在浏览器的其余部分平铺和重复绘制此绘图。 I have gotten a cloned canvas onto the page but am struggling on how to draw the same lines simultaneously on top of multiple canvases. 我已经在页面上获得了一个克隆的画布,但我正在努力如何在多个画布上同时绘制相同的线条。

I will paste my code below 我将在下面粘贴我的代码

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;
}

The way I would go about it is to update the cloned canvas after drawing on the main canvas. 我想要的方法是在主画布上绘制后更新克隆的画布。

Based on your current code I would first want to return the cloned canvas instead of the old canvas. 根据您当前的代码,我首先要返回克隆的画布而不是旧的画布。 This way you have a reference to it. 这样你就可以参考它了。

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

Next, looking at the line: 接下来,看看这一行:

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

I see that you're using your own "posx" and "posy" variables. 我看到你正在使用自己的“posx”和“posy”变量。 I would get rid of those since you will always want to copy the whole canvas. 我会摆脱那些,因为你总是想要复制整个画布。 (In this case). (在这种情况下)。 Change the line like this 像这样改变这条线

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

Next, I wrote a little helper function to link two canvases. 接下来,我写了一个小帮手函数来链接两个画布。

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

This function returns a callback that when called draws the the orginal canvas onto a cloned canvas. 此函数返回一个回调函数,该函数在调用时将原始画布绘制到克隆的画布上。

You initialize it like this: 你初始化它像这样:

var updateClone = drawFromSource(canvas, clone);

And last but not least you have to add the newly created callback into the draw function. 最后但并非最不重要的是,您必须将新创建的回调添加到绘图功能中。 Right after you draw onto your main canvas. 在你画到主画布上之后。

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

};

Voila! 瞧! Here is a link to the working code, I shifted some of your code around. 这是工作代码的链接,我转移了一些代码。

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

Just for fun. 纯娱乐。 Tiled version just change "numberofTiles" 平铺版只需更改“numberofTiles”

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

Try this code 试试这个代码

In this I have created a function for registering mouse event according to canvas. 在这里我创建了一个根据画布注册鼠标事件的功能。 and call this function for all canvases. 并为所有画布调用此功能。

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 这是工作小提琴

Actually you were close, I have made just some changes and achieved what you desired, here is the result . 实际上你很接近,我做了一些改变,实现了你想要的,这就是结果

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

And, 和,

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

Last change is, 最后的改变是,

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 output; Jsfiddle输出; 在此输入图像描述

Hope helps, 希望有帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 canvas 中 Html 元素的颜色? - How do I change a color of a Html element in a canvas? 我如何抓取数据<canvas>元素与 python 或 javascript? - How do I scrape data in <canvas> element with python or javascript? 如何使用React的Context功能将HTML5 Canvas上下文传递给this.props.children? - How do I use React's Context feature to pass an HTML5 Canvas context to this.props.children? 如何使用JavaScript更新HTML 5画布? - How can I update an HTML 5 canvas with JavaScript? 如何复制 HTML5 CANVAS 标签上的线段旋转并将其放置在原始线段旁边? - How do I copy line segment on HTML5 CANVAS tag rotate it and place it next to the original segment? HTML2Canvas:无法在克隆的 iframe 中找到元素 - HTML2Canvas: Unable to find element in cloned iframe HTML5 Canvas:如何将2个画布合并为1个(其中1个可拖动) - HTML5 Canvas : How do I merge 2 canvas into 1 (of which 1 is draggable) 如何更改画布元素的位置? - How do I change the location of a canvas element? 如何初始化html2canvas? - how do I initialize html2canvas? 通过动态创建html文件元素来上载克隆的画布照片,默认值为画布图像 - Uploading a cloned canvas photo by dynamically creating a html file element with the default value being the canvas image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM