【发布时间】:2018-01-05 22:04:53
【问题描述】:
已编辑:感谢大家宝贵的时间和精力。最后我做了这个))JSfiddle
我只是在玩画布并制作了这个。小提琴链接here.
... some code here ...
var cords = [];
for(var i = 50; i <= width; i += 100) {
for(var j = 50; j <= height; j += 100) {
cords.push({ cor: i+','+j});
}
}
console.log(cords);
var offset = 15,
speed = 0.01,
angle = 0.01;
cords.forEach(function(e1) {
e1.base = parseInt(Math.random()*25);
e1.rgb = 'rgb('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+')';
});
setInterval(function() {
cords.forEach(function(e1) {
e1.base = parseInt(Math.random()*25);
e1.rgb = 'rgb('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+')';
});
},5000);
function render() {
ctx.clearRect(0,0,width,height);
cords.forEach(function(e1) {
//console.log(e1);
ctx.fillStyle = e1.rgb;
ctx.beginPath();
var r = e1.base + Math.abs(Math.sin(angle)) * offset;
var v = e1.cor.split(',');
ctx.arc(v[0],v[1],r,0,Math.PI * 2, false);
ctx.fill();
});
angle += speed;
requestAnimationFrame(render);
}
render();
想知道如果 -
- 坐标可以是随机的,现在它们是固定的,如您所见。 5000 百万之后,球会出现在各种随机的绳索中,但即使在最大的时候,它们也不会相互接触。
- 每个球都有相同的速度来改变大小,我也希望那是不同的。这意味着,在 50 亿之后,它们也会以不同的动画速度出现。
也非常感谢任何有关改进代码并使其更好/更快/更轻的建议。谢谢!
【问题讨论】:
-
在圆圈对象中定义速度和角度jsfiddle.net/v9x1gpLq/5
-
@Kaiido 谢谢,但我希望绳索在每次初始化时都是随机的。见this
-
好吧,添加它。这只是一个评论,让您了解如何处理一般问题的基础知识。现在您知道了,您可以为您的对象添加任何单一属性。
标签: javascript html animation canvas