我制作了几乎没有闪烁的随机恒星粒子,由于某种原因它无法在Firefox上运行,我可以渲染出我的图像,但是恒星无法渲染。我也没有错误消息。

我把它发送给了一个朋友,由于某种原因,它也不能在他的chrome浏览器中使用,但是在我的chrome浏览器中,它却不能运行,只是无法使用Firefox,因为我在这两个浏览器中都没有错误报告,所以我完全不知道。将不胜感激:)

作为一个侧面说明,im渲染这样的图像只是为了表明出于某种原因它们将渲染而不是星星,通常我会拥有一个数组和几个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...");
  }

最佳答案

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

更正此:

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


对此:

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

09-10 03:00
查看更多