JS粒子(星星)在chrome中工作但不是firefox

时间:2015-04-18 14:42:47

标签: javascript html google-chrome firefox particles

我制作了一些闪烁的随机星形粒子,由于某些原因它不能在Firefox上运行,我可以渲染出我的图像,但是星星不会渲染。我也没有错误消息。

我把它发送给朋友,由于某种原因,它也没有在他的Chrome浏览器中工作,但在我的Chrome浏览器上它工作,只是没有firefox,我完全无能为力,因为我在任一浏览器中都没有错误报告。会很感激任何帮助:)

作为旁注,即时渲染图像只是为了表明由于某种原因它们会渲染而不是星星,通常我会有一个数组和几个for循环,但是现在我想得到星星渲染。谢谢。

编辑:我只在我提到的这两个浏览器中进行过测试。

  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...");
  }

1 个答案:

答案 0 :(得分:1)

颜色字符串不是命令,因此请删除;

Corect this:

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

对此:

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