Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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;
}
like image 662
Brendan Russo Avatar asked Jul 31 '17 05:07

Brendan Russo


People also ask

How do you clone a canvas?

We can clone a canvas instance by using the clone() method. Usually, this is useful when we want to send our canvas instance remotely to somewhere else, it is usually a good idea to send the canvas instance clone in JSON form instead of sending the image of the canvas.

How do I add HTML elements to canvas?

According to the HTML specification you can't access the elements of the Canvas. You can get its context, and draw in it manipulate it, but that is all. BUT, you can put both the Canvas and the html element in the same div with a a position: relative and then set the canvas and the other element to position: absolute .

What is HTML canvas context?

The canvas context is an object with properties and methods that you can use to render graphics inside the canvas element. The context can be 2d or webgl (3d). Each canvas element can only have one context. If we use the getContext() method multiple times, it will return a reference to the same context object.

What is the function of the new HTML5 canvas element?

The CANVAS element allows you to add so much more interactivity to your web pages because now you can control the graphics, images, and text dynamically with a scripting language. The CANVAS element helps you turn images, photos, charts, and graphs into animated elements.


Video Answer


1 Answers

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. 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/

Just for fun. Tiled version just change "numberofTiles"

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

like image 189
Jordan Maduro Avatar answered Oct 08 '22 05:10

Jordan Maduro