在上一篇文章里我绘制了一幅夜空下的大巴
但是既然是夜空,没有星星就会有些光秃秃的,为了让它动起来,看起来稍微好看一点,有趣味性一点,现在需要让他动起来,用代码在夜空上加入明灭的星空效果,动图如下:
在上一篇代码的基础上改变如下:
function setup() {
createCanvas(208*4,154*4);//坐标乘5
angleMode(DEGREES);
noStroke();
var bgStars = ~~(Math.max(208*4, 154*4) / 20) + 20;
while (bgStars--) {
createBgStar(bgStars)
}
}
添加
var V = p5.Vector
var stars = []
var lastPos = {
x: null,
y: null
}
function createBgStar(index) {
var s = new Star(new V(random(0, 208*4), random(0, 60*4)))
s.maxSize = (index % 4) + 1
stars[index] = s
s.o