javascript-HTML画布上的雨滴动画
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-HTML画布上的雨滴动画,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2708字,纯文字阅读大概需要4分钟。
内容图文
![javascript-HTML画布上的雨滴动画](/upload/InfoBanner/zyjiaocheng/679/8ae61be6455f4df4be4835c20fc0423c.jpg)
我试图在画布上创建水滴效果.我使用array创建了一些示例水滴.对其进行动画处理我的意思是改变它们的位置,我引入了setTimeout函数.它适用于单个水滴.因为我的画布上有多个水滴它不再工作了.滴仍然保持.如何使用setTimeout函数为所有单独的滴设置动画?
这是小提琴jsfiddle
单个水滴的动画效果single drop animation
<html>
<head>
</head>
<body>
<script>
function makeit(){
var canvas=document.getElementById("mycanvas");
var ctx=canvas.getContext('2d');
canvas.width=600;
canvas.height=600;
canvas.style.border="1px solid black";
var drop=function (x,y){
ctx.save();
ctx.fillStyle="orange";
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.fill();
ctx.fillStyle="red";
ctx.moveTo(x-5,y);
ctx.lineTo(x,y-7);
ctx.lineTo(x+5,y);
ctx.arc(x,y,5,0,Math.PI);
ctx.closePath();
ctx.fill();
ctx.restore();
y=y+3;
if(y==canvas.height){
y=0;
}
var m=setTimeout('drop('+x+','+y+')',20);
}
var xpos=[10,20,30,40,50,60,70,70,76];
var ypos=[30,20,60,80,76,90,30,40,79];
for(i=0;i<xpos.length;i++){
drop(xpos[i],ypos[i]);
}
}
window.onload=makeit;
</script>
<canvas id="mycanvas" ></canvas>
</body>
</html>
解决方法:
这是我将要执行的操作:
function makeit() {
// These variables can be used in the drawing functions
var canvas = document.getElementById("mycanvas");
var ctx = canvas.getContext('2d');
var drops = [];
// Init canvas & drops
init();
// Make the 3rd drop falls
var fall = setInterval(function () {
updateDrop(3);
}, 200);
// Make the 7th drop falls
var fall2 = setInterval(function () {
updateDrop(7);
}, 400);
// Stop the 3rd drop at anytime with this code :
// clearInterval(fall);
// Functions
function init() {
canvas.width = 600;
canvas.height = 600;
canvas.style.border = "1px solid black";
// Draw background
drawBackground();
// Draw drops
var xpos = [10, 20, 30, 40, 50, 60, 70, 70, 76];
var ypos = [30, 20, 60, 80, 76, 90, 30, 40, 79];
for (i = 0; i < xpos.length; i++) {
drops.push(drawDrop(xpos[i], ypos[i]));
}
}
function drawBackground() {
ctx.fillStyle = "orange";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fill();
}
function drawDrop(x, y) {
ctx.beginPath();
ctx.fillStyle = "red";
ctx.moveTo(x - 5, y);
ctx.lineTo(x, y - 7);
ctx.lineTo(x + 5, y);
ctx.arc(x, y, 5, 0, Math.PI);
ctx.closePath();
ctx.fill();
return {
'x': x,
'y': y
};
}
function updateDrop(dropNumber) {
var dropNumber = dropNumber - 1; //Because 0 is first
// Update position
if (drops[dropNumber].y >= canvas.height) {
drops[dropNumber].y = 0;
} else {
drops[dropNumber].y += 3;
}
//Draw background
drawBackground();
//Draw drops
for (i = 0; i < drops.length; i++) {
drawDrop(drops[i].x, drops[i].y);
}
}
}
window.onload = makeit;
然后,如果要阻止第三滴下落,可以执行以下行:
clearInterval(fall);
只需看一下JSFiddle:http://jsfiddle.net/Oliboy50/QHRaS/5/
在绘制线条之前,请不要忘记ctx.beginPath(),否则您将无法清除它们.
内容总结
以上是互联网集市为您收集整理的javascript-HTML画布上的雨滴动画全部内容,希望文章能够帮你解决javascript-HTML画布上的雨滴动画所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。