だから私はキャンバスで遊んでいます。ランダムに落下するオブジェクトをシミュレートしようとすると、背景画像を描画しても問題はなく、雨滴をシミュレートすることになっている 2 番目の画像ではありません。
ドロップをランダムな x で降ろすことはできますが、変数 noOfDrops によって設定されたドロップ イメージ x 回をループする方法がよくわかりません。
ループをコメントアウトしたままにしました。ランダムにドロップするドロップが1つだけの作業コードは次のとおりです。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Canvas Regn</title>
<script type="text/javascript">
var ctx;
var imgBg;
var imgDrops;
var x = 0;
var y = 0;
var noOfDrops = 50;
var fallingDrops = [];
function setup() {
var canvas = document.getElementById('canvasRegn');
if (canvas.getContext) {
ctx = canvas.getContext('2d');
setInterval('draw();', 36);
imgBg = new Image();
imgBg.src = 'dimma.jpg';
imgDrops = new Image();
imgDrops.src = 'drop.png';
/*for (var i = 0; i < noOfDrops; i++) {
var fallingDr = imgDrops[i];
fallingDr.x = Math.random() * 600;
fallingDrops.push(fallingDr);
}*/
}
}
function draw() {
drawBackground();
ctx.drawImage (imgDrops, x, y); //The rain drop
y += 3; //Set the falling speed
if (y > 450) { //Repeat the raindrop when it falls out of view
y = -25 //Account for the image size
x = Math.random() * 600; //Make it appear randomly along the width
}
}
function drawBackground(){
ctx.drawImage(imgBg, 0, 0); //Background
}
</script>
</head>
<body onload="setup();">
<canvas id="canvasRegn" width="600" height="450"style="margin:100px;"></canvas>
</body>
</html>
誰かがそれを実装する方法についていくつかの良いアイデアを持っているなら、私はそれを大いに感謝します.