나는 팝콘을 터뜨렸고, 네가 할 수있어. Image
객체를 만들고 그 객체의 src
속성을 설정하여 팝콘 이미지를로드하십시오. 이미지의 onload
속성을 사용하여 애니메이션을 시작하십시오. 애니메이션의 지속 시간이 k 인 경우, 사인 곡선 sin(x/(k/π))
을 사용하여 프레임 당 표시 할 커널 수를 계산하십시오.
여기 그것을하는 하나의 방법입니다, 여기 데모 : http://jsfiddle.net/uyk63/8/
var IMAGE_URL = 'http://i.istockimg.com/file_thumbview_approve/959519/2/stock-photo-959519-isolated-single-popcorn.jpg';
var DURATION = 10 * 1000, FRAMES = 30, KERNELS = 10;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var frame = 0,
start = new Date().getTime(),
image = new Image;
image.src = IMAGE_URL;
image.onload = function() {
function pop() {
ctx.drawImage(image,
Math.floor(Math.random() * canvas.width),
Math.floor(Math.random() * canvas.height),
100, 50);
}
// A little overcomplicated. You could probably do this in a single loop.
// (It's late and I'm tired, though. Sorry.)
function animate() {
var i, delay,
count = Math.floor(Math.sin(frame/(FRAMES/Math.PI)) * KERNELS);
for (i = 0; i < count; i++) {
delay = (DURATION/FRAMES)/count * i;
setTimeout(pop, delay);
}
if (++frame < FRAMES) {
setTimeout(animate, DURATION/FRAMES);
}
}
animate();
};