Lucian Xeno
Lucian Xeno

Reputation: 123

JS particles (stars) working in chrome but not firefox

i have made little random star particle that flashes and for some reason it does not run on Firefox, I can render out my image but the stars are not rendering. I have no error messages either.

I sent it to a friend and for some reason it didn't work in his chrome browser either, but on my chrome browser it works, just not firefox, im completely clueless since i have no error reporting in either browser. would appreciate any help :)

as a side note, im rendering the images like that just to show that for some reason they will render but not the stars, usually i would have an array and a couple of for loops, but for now i want to get the stars rendering. thanks.

edit: i have only tested in these two browsers i have mentioned.

  var canvas = document.getElementById("canvas"),
      context = canvas.getContext("2d");

  var stars = [],
      numberOfStars = 200,
      state = "town",
      tileMap = new Image();

  tileMap.src = "images/tileMap.png";

  function Star() {
    this.x = Math.random() * window.innerWidth;
    this.y = Math.random() * window.innerHeight;
    this.size = Math.random() * 3;
    this.alpha = Math.random();
    this.counter = 0;
    this.draw = function() {
      this.counter++;

      if (this.counter == 10) {
        var decrease = false;

        //check the brightness
        if(this.alpha >= 1) {
          decrease = true;
        } else if (this.alpha <= 0) {
          decrease = false;
        }

        //change brightness
        if(decrease) {
          this.alpha = 0.4;
        } else {
          this.alpha += 0.1;
        }

        //reset counter
        this.counter = 0;
      }

      //draw star
      context.fillStyle = "rgba(255,255,255," + this.alpha + ");";
      context.fillRect(this.x, this.y, this.size, this.size);
    }
  }

  function loop() {
    //que next frame
    window.requestAnimationFrame(loop);

    //set canvas size to window
    context.canvas.width  = window.innerWidth;
    context.canvas.height = window.innerHeight;

    //clear screen, set black background
    context.fillStyle = "black";
    context.fillRect(0, 0, window.innerWidth, window.innerHeight);

    //add stars
    for(i = 0; i < stars.length; i++) {
      stars[i].draw();

    //draw relavent map
    context.drawImage(tileMap, 64, 64, 64, 128);
    context.drawImage(tileMap, 64*2, 64, 64, 128);
    context.drawImage(tileMap, 64*3, 64, 64, 128);
    context.drawImage(tileMap, 64, 64*2, 64, 128);
    context.drawImage(tileMap, 64*2, 64*2, 64, 128);
    context.drawImage(tileMap, 64*3, 64*2, 64, 128);
    }
  }

  window.onload = function() {
    //create stars
    for (i = 0; i < numberOfStars; i++) {
      stars.push(new Star);
    }

    //request first frame
    window.requestAnimationFrame(loop);

    console.log("running...");
  }

Upvotes: 0

Views: 727

Answers (1)

GramThanos
GramThanos

Reputation: 3622

Color string is not a command so remove ;

Corect this:

context.fillStyle = "rgba(255,255,255," + this.alpha + ");";

To this:

context.fillStyle = "rgba(255,255,255," + this.alpha + ")";

Upvotes: 1

Related Questions