Skip to content

Instantly share code, notes, and snippets.

@async3619
Created July 1, 2022 09:48
Show Gist options
  • Save async3619/11c29cae088f467c4d524bcf3587a504 to your computer and use it in GitHub Desktop.
Save async3619/11c29cae088f467c4d524bcf3587a504 to your computer and use it in GitHub Desktop.
Image 객체 Canvas 에 그리기
<!DOCTYPE HTML>
<html lang="ko">
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<button>load image</button>
<script>
function loadImage(src) {
return new Promise(res => {
const img = new Image();
img.addEventListener("load", () => {
res(img);
});
img.src = src;
})
}
const canvas = document.querySelector("#canvas");
const context = canvas.getContext("2d");
const button = document.querySelector("button");
button.addEventListener("click", async () => {
const image = await loadImage("./1622768267.jpg");
context.drawImage(image, 0, 0);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment