【问题标题】:Animating an image to create rain effect动画图像以创建雨效果
【发布时间】:2016-10-23 10:16:58
【问题描述】:

我正在尝试使用 HTML5 画布使用 javascript 编写一个足够简单的动画。这是一张雨滴的图像,我想无缝地制作动画。这是图片:

https://s31.postimg.org/475z12nyj/raindrops.png

这就是我目前制作动画的方式:

function Background() {
        this.x = 0, this.y = 0, this.w = bg.width, this.h = bg.height;
        this.render = function() {
            ctx.drawImage(bg, 0, this.y++);
            if (this.y <= -199) { //If image moves out of canvas, reset position to 0
                this.y = 0;
            }
        }
    }

不过,我面临两个问题。

  • 我根本无法让图像循环播放。它只是掉了一次,我需要把它放在一个循环上,这样当它开始离开画布时它会再次继续。
  • 一旦我知道如何正确循环它,就会出现一个问题,即雨水不会完全垂直落下。如图中的雨滴所示,它需要沿对角线落下。

这就是它不再是一个足够简单的动画的地方。

这是我的fiddle,它包含了我所有的代码。非常感谢。

PS:我会在 Javascript 或 CSS 方面寻求任何帮助。但我确实需要雨效果才能只使用图像!不幸的是,我无法接受其他任何事情。

【问题讨论】:

  • this.y0 开始,并在增加this.y++,但你等到它变成&lt;0;甚至this.y &lt;= -199。您必须等待相当长的时间才能发生这种情况;)

标签: javascript html animation canvas


【解决方案1】:

我建议将您的循环拆分为分别调用 update() 和 draw() 的动画循环。在 update() 中更新您的状态,然后在 draw() 中渲染该状态。

类似这样的东西(有点破烂,但你也许可以做得更好:)):

var lastTick = 0;
var position = { x:0, y:0 };
var bg = document.getElementById('bg');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

function update(gameTime) {
	position.x += (70 * gameTime.diff / 1000);
	position.y += (110 * gameTime.diff / 1000);
	if (position.x > canvas.width) {
		position.x = 0;
	}

	if (position.y > canvas.height) {
		position.y = 0;
	}
}

function draw(gameTime) {
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	ctx.drawImage(bg, position.x, position.y, canvas.width, canvas.height);
	ctx.drawImage(bg, position.x - canvas.width, position.y, canvas.width, canvas.height);
	ctx.drawImage(bg, position.x, position.y - canvas.height, canvas.width, canvas.height);
	ctx.drawImage(bg, position.x - canvas.width, position.y - canvas.height, canvas.width, canvas.height);
}

function loop(tick) {
	var diff = tick - lastTick;
	var gameTime = { tick:tick, diff:diff };
	update(gameTime);
	draw(gameTime);
	requestAnimationFrame(loop);
	lastTick = tick;
}

requestAnimationFrame(loop);
<title>Rain</title>
<meta charset="UTF-8">
<style>
	canvas {
		width:100vw;
		height:100vh;
	}
</style>
<img id="bg" src="https://s31.postimg.org/475z12nyj/raindrops.png" style="display:none;">
<canvas id="canvas"><h1>Canvas not supported</h1></canvas>

【讨论】:

  • 您将如何将您的代码实现到我的代码中?你能用它更新小提琴吗? jsfiddle.net/qafqyLsy我不知道哪些位该粘贴哪些不该粘贴。
  • 我所做的只是更新 x 的值,为您提供一些对角线类型的运动。然后添加了对requestAnimationFrame的调用,让render函数循环。更好的是在此之外创建您的循环。在您的循环中,只需调用 update()、draw() 和 requestAnimationFrame(nameOfYourAnimationLoop)。然后,在您的 update() 函数中,您可以更新 Background 对象的状态。在 draw 中,你可以调用 Background 对象的 render() 方法(当然没有上面的 requestAnimationFrame)。
  • 好的,谢谢,感谢您的帮助。但是关于对角线运动指针,使用您对我的代码的更新仍然对雨的运动没有任何作用,而只是以垂直方式落下。你确定你不能更新我的小提琴它是如何为你工作的,因为我不认为我做错了吗?很抱歉一直坚持。
  • 没关系。我已经用一个完整的示例更新了我的答案,向您展示了如何使用可运行的代码 sn-p :)
  • 非常感谢,这正是我所希望的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
相关资源
最近更新 更多