Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy from one canvas to other canvas

here is the jsfiddle

i have this as my source canvas

HTML

 <h1>Source Canvas</h1>
 <canvas id="source" width=436 height=567></canvas>
 <h1>Destination Canvas</h1>
 <canvas id="destination" width=436 height=567></canvas>

javascript

 var sourceImage, ctx, sourceCanvas, destinationCanvas;
        //get the canvases
        sourceCanvas = document.getElementById('source');
        destinationCanvas = document.getElementById('destination');

    //draw the source image to the source canvas
    ctx = sourceCanvas.getContext('2d');

     function start() {

                ctx.drawImage(img1, 0, 0);

                ctx.globalCompositeOperation = "source-atop";

                var pattern = ctx.createPattern(img, 'repeat');
                ctx.rect(0, 0, sourceCanvas.width, sourceCanvas.height);
                ctx.fillStyle = pattern;
                ctx.fill();

                ctx.globalAlpha = .10;
                ctx.drawImage(img1, 0, 0);
                ctx.drawImage(img1, 0, 0);
                ctx.drawImage(img1, 0, 0);
                 //ctx.globalAlpha = 1;
            }
     var img1 = new Image();
     var img = new Image();
    img.onload = function () {

              img1.onload = function () {
                  start();
              }
img1.src = "https://dl.dropboxusercontent.com/u/139992952/stackoverflow/4jiSz1.png";
}
img.src = "https://dl.dropboxusercontent.com/u/139992952/stackoverflow/BooMu1.png";

i want to to show what is in source canvas in my destination canvas.

i tired

var image, destinationCtx;

//create the image
image = new Image();

//get the base64 data
image.src = sourceCanvas.toDataURL('image/png');

//get the destination context
destinationCtx = destinationCanvas.getContext('2d');

//copy the data
destinationCtx.drawImage(image, 0, 0);

//done

but having no luck. am i missing something? Copy via imageData,Copy via Base64 data,Copy via direct draw any method will do my job. when i try with

http://jsperf.com/copying-a-canvas-element it copies but when i put my source canvas writer it does not work ? am i missing something?

like image 518
MD TAHMID HOSSAIN Avatar asked Dec 15 '22 08:12

MD TAHMID HOSSAIN


2 Answers

You can directly copy one canvas over other. Like this...

var destinationCtx;

//get the destination context
destinationCtx = destinationCanvas.getContext('2d');

//copy the data
destinationCtx.drawImage(sourceCanvas, 0, 0);
like image 110
mohkhan Avatar answered Dec 17 '22 22:12

mohkhan


You can use getImageData from the source canvas and putImageData to the destination canvas.This is the fastest one compare to other ways.

var sourceCtx, destinationCtx, imageData;

sourceCtx = sourceCanvas.getContext('2d');
destinationCtx = destinationCanvas.getContext('2d');

imageData = sourceCtx.getImageData(0, 0, sourceCanvas.width - 1, sourceCanvas.height - 1);

destinationCtx.putImageData(imageData, 0, 0);

source:/ https://jsperf.com/copying-a-canvas-element

like image 35
lalithkumar Avatar answered Dec 17 '22 20:12

lalithkumar