Skip to content

Instantly share code, notes, and snippets.

@magemore
Created March 24, 2023 15:56
Show Gist options
  • Save magemore/9bf2defa5374c2f3ec56cb99f754cfc0 to your computer and use it in GitHub Desktop.
Save magemore/9bf2defa5374c2f3ec56cb99f754cfc0 to your computer and use it in GitHub Desktop.
Canvas draw image from one to other
const canvas1 = document.getElementById("canvas1");
const ctx1 = canvas1.getContext("2d");
const canvas2 = document.createElement("canvas");
canvas2.width = canvas1.width;
canvas2.height = canvas1.height;
const ctx2 = canvas2.getContext("2d");
const img = new Image();
img.onload = function() {
ctx2.drawImage(img, 0, 0);
const imageData = ctx2.getImageData(0, 0, canvas2.width, canvas2.height);
const data = imageData.data;
// modify the pixel data here
ctx2.putImageData(imageData, 0, 0);
ctx1.drawImage(canvas2, 0, 0);
};
img.src = "path/to/image.jpg";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment