캔버스를 처음 사용하여 간단한 선형 모션 애니메이션 예제를 HTML Canvas Tutorials에서 시작했습니다.HTML5 캔버스 선형 이미지 애니메이션
대신 이미지를 표시하도록 사각형을 대체 할 수 있습니까?
코드를 편집하려고 할 때마다 출력되는 이미지가 정적이므로 분명히 잘못된 것이 있습니다.
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000/60);
};
})();
function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
context.drawImage = 'images/player.png';
}
function animate(myRectangle, canvas, context, startTime) {
// update
var time = (new Date()).getTime() - startTime;
var linearSpeed = 200;
// pixels/second
var newX = linearSpeed * time/1000;
if (newX < canvas.height - myRectangle.height - myRectangle.borderWidth/2) {
myRectangle.y = newX;
}
// clear
context.clearRect(0, 0, canvas.width, canvas.height);
drawRectangle(myRectangle, context);
// request new frame
requestAnimFrame(function() {
animate(myRectangle, canvas, context, startTime);
});
}
var canvas = document.getElementById('monopoly-piece');
var context = canvas.getContext('2d');
var img = new Image;
var myRectangle = {
x: 20,
y: 0,
width: 50,
height: 50,
borderWidth: 5,
img: 'images/player.png'
};
drawRectangle(myRectangle, context);
// wait one second before starting animation
setTimeout(function() {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 1000);
어떤 도움을 주시면 감사하겠습니다. 미리 감사드립니다. :) jsfiddle에서
작동 할 수이 도움이 될 수 있습니다 : http://www.w3schools.com/tags/canvas_drawimage.asp 응답 @snies에 대한 – snies
덕분에 내가 알고 있어요 drawImage 메서드를 사용하지만 캔버스의 완전한 초보자로서 나는 그것을 기존 코드로 구현하는 방법을 완전히 모릅니다 ... – user2879183