【问题标题】:Modifying an existing CodePen Galaxy affect (Zoom out to Zoom In)修改现有 CodePen Galaxy 效果(缩小到放大)
【发布时间】:2022-02-12 12:41:03
【问题描述】:

最近我在 codepen 上发现了这个很棒的星系效果: https://codepen.io/zeztron/pen/MPNxxR

我尝试修改 JavaScript,但找不到改变它的方法,所以感觉就像缩小,让它感觉像放大。

有人可以帮忙吗?谢谢!!

代码如下:

<body>
  <canvas id="canvas"></canvas>
</body>
body {
    background: black;
    height: 100%;
    min-height: 100%;
}


#canvas {
    height: 100%;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    opacity: 0;
}
var Space = {
  init: function(){
    var self = this;
    this.config = {
      perspective: 3,
      star_color: '255, 255, 255',
      speed: 1,
      stars_count: 2
    };
    this.canvas = document.getElementById('canvas');
    this.context = canvas.getContext('2d');
    this.start();
    window.onresize = function(){
      self.start();
    };
  },

  start: function(){
    var self = this;

    this.canvas.width  = this.canvas.offsetWidth;
    this.canvas.height = this.canvas.offsetHeight;
    this.canvas_center_x = this.canvas.width / 2;
    this.canvas_center_y = this.canvas.height / 2;

    this.stars_count = this.canvas.width / this.config.stars_count;
    this.focal_length = this.canvas.width / this.config.perspective;
    this.speed = this.config.speed * this.canvas.width / 2000;

    this.stars = [];

    for(i = 0; i < this.stars_count; i++){
      this.stars.push({
        x: Math.random() * this.canvas.width,
        y: Math.random() * this.canvas.height,
        z: Math.random() * this.canvas.width,
      });
    }

    window.cancelAnimationFrame(this.animation_frame);
    this.canvas.style.opacity = 1;

    this.cow = new Image();
    this.cow.src = 'https://gallery.yopriceville.com/var/resizes/Free-Clipart-Pictures/Fast-Food-PNG-Clipart/Hamburger_PNG_Vector_Picture.png?m=1507172108';
    this.cow.onload = function(){
      self.render();
    }
  },

  render: function(){
    var self = this;
    this.animation_frame = window.requestAnimationFrame(function(){
      self.render();
    });
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    for(var i = 0, length = this.stars.length; i < length; i += 1){
      var star = this.stars[i];
      star.z -= this.speed;
      if(star.z <= 0) {
        this.stars[i] = {
          x: Math.random() * this.canvas.width,
          y: Math.random() * this.canvas.height,
          z: this.canvas.width,
        };
      }

      var star_x = (star.x - this.canvas_center_x) * (this.focal_length / star.z) + this.canvas_center_x;
      var star_y = (star.y - this.canvas_center_y) * (this.focal_length / star.z) + this.canvas_center_y;
      var star_radius  = Math.max(0, 1.4 * (this.focal_length / star.z) / 2);
      var star_opacity = 1.2 - star.z / this.canvas.width;
      var cow_width = Math.max(0.1, 100 * (this.focal_length / star.z) / 2);

      if(star.cow){
        this.context.save();
        this.context.translate((star_x-cow_width)+(cow_width/2), (star_y-cow_width)+(cow_width/2));
        this.context.rotate(star.z/star.rotation_speed);
        this.context.translate(-((star_x-cow_width)+(cow_width/2)), -((star_y-cow_width)+(cow_width/2)));
        this.context.globalAlpha = star_opacity;
        this.context.drawImage(this.cow, 0, 0, this.cow.width, this.cow.width, star_x-cow_width, star_y-cow_width, cow_width, cow_width);
        this.context.restore();
      } else {
        this.context.fillStyle = 'rgba(' + this.config.star_color + ',' + star_opacity + ')';
        this.context.beginPath();
        this.context.arc(star_x, star_y, star_radius, 0, Math.PI * 2);
        this.context.fill();
      }
    }
  }
};

window.onload = function(){
  Space.init();
};

【问题讨论】:

  • 你尝试了什么?
  • 只需在javascript顶部的配置中将speed1更改为-1
  • @GabrielePetrioli 是的,我试过了,但它确实改变了,它不会无限运行......

标签: javascript css animation codepen


【解决方案1】:

每颗星的z-axisrandom point inside the canvas 减少到0 以创建缩小效果。要反转它,只需增加z-axis,而不是速度。

这是缩小:

var star = this.stars[i];
star.z -= this.speed; // decreasing
if(star.z <= 0) {
    this.stars[i] = {
        x: Math.random() * this.canvas.width,
        y: Math.random() * this.canvas.height,
        z: this.canvas.width, // reset the z-axis
    };
}

这是放大:

star.z += this.speed; // increasing
if(star.z > this.canvas.width){
    this.stars[i] = {
        x: Math.random() * this.canvas.width,
        y: Math.random() * this.canvas.height,
        z: Math.random() * this.canvas.width, // reset to a random point
    };
}

z-axis到达某个点时,您需要重置它,以便它可以无限运行。

var Space = {
  init: function(){
    var self = this;
    this.config = {
      perspective: 3,
      star_color: '255, 255, 255',
      speed: 1,
      stars_count: 2
    };
    this.canvas = document.getElementById('canvas');
    this.context = canvas.getContext('2d');
    this.start();
    window.onresize = function(){
      self.start();
    };
  },

  start: function(){
    var self = this;

    this.canvas.width  = this.canvas.offsetWidth;
    this.canvas.height = this.canvas.offsetHeight;
    this.canvas_center_x = this.canvas.width / 2;
    this.canvas_center_y = this.canvas.height / 2;

    this.stars_count = this.canvas.width / this.config.stars_count;
    this.focal_length = this.canvas.width / this.config.perspective;
    this.speed = this.config.speed * this.canvas.width / 2000;

    this.stars = [];

    for(i = 0; i < this.stars_count; i++){
      this.stars.push({
        x: Math.random() * this.canvas.width,
        y: Math.random() * this.canvas.height,
        z: Math.random() * this.canvas.width,
      });
    }

    window.cancelAnimationFrame(this.animation_frame);
    this.canvas.style.opacity = 1;

    this.cow = new Image();
    this.cow.src = 'https://gallery.yopriceville.com/var/resizes/Free-Clipart-Pictures/Fast-Food-PNG-Clipart/Hamburger_PNG_Vector_Picture.png?m=1507172108';
    this.cow.onload = function(){
      self.render();
    }
  },

  render: function(){
    var self = this;
    this.animation_frame = window.requestAnimationFrame(function(){
      self.render();
    });
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    for(var i = 0, length = this.stars.length; i < length; i += 1){
      var star = this.stars[i];
      star.z += this.speed;
      if(star.z > this.canvas.width) {
        this.stars[i] = {
          x: Math.random() * this.canvas.width,
          y: Math.random() * this.canvas.height,
          z: Math.random() * this.canvas.width,
        };
      }

      var star_x = (star.x - this.canvas_center_x) * (this.focal_length / star.z) + this.canvas_center_x;
      var star_y = (star.y - this.canvas_center_y) * (this.focal_length / star.z) + this.canvas_center_y;
      var star_radius  = Math.max(0, 1.4 * (this.focal_length / star.z) / 2);
      var star_opacity = 1.2 - star.z / this.canvas.width;
      var cow_width = Math.max(0.1, 100 * (this.focal_length / star.z) / 2);

      if(star.cow){
        this.context.save();
        this.context.translate((star_x-cow_width)+(cow_width/2), (star_y-cow_width)+(cow_width/2));
        this.context.rotate(star.z/star.rotation_speed);
        this.context.translate(-((star_x-cow_width)+(cow_width/2)), -((star_y-cow_width)+(cow_width/2)));
        this.context.globalAlpha = star_opacity;
        this.context.drawImage(this.cow, 0, 0, this.cow.width, this.cow.width, star_x-cow_width, star_y-cow_width, cow_width, cow_width);
        this.context.restore();
      } else {
        this.context.fillStyle = 'rgba(' + this.config.star_color + ',' + star_opacity + ')';
        this.context.beginPath();
        this.context.arc(star_x, star_y, star_radius, 0, Math.PI * 2);
        this.context.fill();
      }
    }
  }
};

window.onload = function(){
  Space.init();
};
body {
    background: black;
    height: 100%;
    min-height: 100%;
}


#canvas {
    height: 100%;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    opacity: 0;
}
<body>
  <canvas id="canvas"></canvas>
</body>

【讨论】:

  • 非常感谢!它就像一个魅力!我发现当我做放大版的时候感觉点变小了,所以我增加了它的半径大小。再次非常感谢您的知识!
  • 不客气。我很高兴它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
相关资源
最近更新 更多