Legebatterie

p5js--StarField

Mar 29th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. var number= 1000;
  2.  
  3.  
  4. class Star{
  5.  
  6. constructor(){
  7. this.x=random(-0.006*width,0.006*width)
  8. this.y=random(-0.006*height,0.006*height)
  9.  
  10. this.ox=random(-0.006*width,0.006*width)
  11. this.oy=random(-0.006*height,0.006*height)
  12.  
  13.  
  14. this.size=1
  15. this.vel=1
  16. }
  17.  
  18. show(){
  19. fill(255);
  20. translate(width/2,height/2);
  21. ellipse(this.x,this.y,this.size,this.size);
  22. stroke(255)
  23. strokeWeight(0.01)
  24. line(this.x,this.y,0,0)
  25. translate(-width/2,-height/2);
  26.  
  27.  
  28.  
  29. }
  30. move(){
  31. this.x=(this.x*this.vel)
  32. this.y=(this.y*this.vel)
  33. this.vel+=this.vel/(width+height)/1
  34. this.size=map(abs(this.x)+abs(this.y)/2,0,width/2+height/42,2,10)
  35. }
  36.  
  37. reborn(i){
  38. if(this.x>width/2||this.x<-width/2||this.y>height/2||this.y<-height/2){
  39. s[i]=new Star()
  40. }
  41. }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. let s= [];
  48. let start=true;
  49.  
  50. function setup() {
  51.  
  52. createCanvas(600, 400);
  53. frameRate(120);
  54.  
  55. for(let i=0;i<number;i++){
  56. s[i]=new Star();
  57. }
  58.  
  59.  
  60. }
  61.  
  62. function draw() {
  63.  
  64. background(0);
  65. if(start){
  66. for(let i=0;i<10000;i++){
  67. for(let i=0;i<number;i++){
  68.  
  69. s[i].move()
  70. s[i].reborn(i)
  71. }
  72. }
  73. start=false
  74. }
  75. for(let i=0;i<number;i++){
  76. s[i].show()
  77. s[i].move()
  78. s[i].reborn(i)
  79. }
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment