<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<canvas id="spriteCanvas" width="500" height="500">
<img id="coin" width="440" height="40" src="coin.png">
</canvas>
</body>
</html>
이미지를 캔버스 요소 안에 넣으려고했지만 브라우저에 표시되지 않습니다. Canvas 요소 외부에 배치하면 이미지 태그가 표시되므로 이미지 태그가 작동하는 것을 알고 있습니다. 또한 캔버스 요소를 검사하면 이미지가 내부에 있음을 알 수 있지만 치수는 0입니다. 왜 이것이 작동하지 않는지 설명 할 수 있습니까?캔버스 내부에 이미지가 표시되지 않음
편집 : 내 원본 코드는 자바 스크립트를 통해 이미지를 추가했지만 캔버스에 표시되지 않습니다. 위와 똑같은 문제를 일으켰습니다. 하지만 방금 "onload"가 누락되었다는 것을 깨달았습니다.
원래 코드 :
var coinImage = new Image();
coinImage.src = "coin.png";
var sCanvas = document.getElementById('spriteCanvas');
function Sprite(canvas, width, height, image){
this.context = canvas.getContext("2d");
this.width = width;
this.height = height;
this.image = image;
}
Sprite.prototype.render = function() {
var that = this;
this.context.drawImage(that.image, 0, 0);
}
function init() {
var coin = new Sprite(sCanvas, 100, 100, coinImage);
coin.render();
}
init();
편집 해 코드하십시오 <canvas>
태그가 어떻게 작동하는지
var coinImage = new Image();
coinImage.src = "coin.png";
var sCanvas = document.getElementById('spriteCanvas');
function Sprite(canvas, width, height, image){
this.context = canvas.getContext("2d");
this.width = width;
this.height = height;
this.image = image;
}
Sprite.prototype.render = function() {
var that = this;
this.context.drawImage(that.image, 0, 0);
}
coinImage.onload = function() {
var coin = new Sprite(sCanvas, 100, 100, coinImage);
coin.render();
}
'