简体   繁体   English

帆布烟雾颗粒不移动

[英]Canvas smoke particles not moving

I am building a fancy easteregg for our internal application. 我正在为我们的内部应用程序构建一个精美的Easteregg。 Therefor I have an image 'riding' through the screen. 因此,我在屏幕上有一个图像“骑行”。 To enhance fancyness I want to add smoke to the vehicle that follows the graphic. 为了提高想象力,我想向跟随图形的车辆添加烟雾。

For the smoke I implemented a modified version of this pen: http://codepen.io/CucuIonel/pen/hFJlr 对于烟雾,我实现了这支笔的修改版本: http : //codepen.io/CucuIonel/pen/hFJlr

Here's the result: http://jsfiddle.net/vc2d08bt/4/ 结果如下: http : //jsfiddle.net/vc2d08bt/4/

The important codesnippets (as I guess) can be found in the fiddle, search for: 可以在小提琴中找到重要的代码片段(据我猜),请搜索:

setInterval(function () {
    position.x = $('#crystalship_exhaust').offset().left;
    position.y = $('#crystalship_exhaust').offset().top;
}, 100);

Since the pen binds the smoke to the cursors offset, I tried binding it to a span "inside" the vehicle, to follow. 由于笔将烟雾绑定到光标偏移处,因此我尝试将其绑定到车辆“内部”的跨度。 But the Smoke is growing in both X-directions and is not even close to follow the vehicle. 但是,烟雾在两个X方向上都在增长,甚至无法跟随车辆。

What am I doing wrong here? 我在这里做错了什么? I am not very familiar with canvas but thankfull for any help. 我对画布不是很熟悉,但是非常感谢您的帮助。

Thanks in advance! 提前致谢!

I've added the movement to the car with smoke going out of the exhaust. 我将机芯加入了汽车,使废气中有烟冒出来。 On mouse move the travels across the screen but this is easily changeable if you'd like it. 用鼠标在屏幕上移动,但是如果您愿意,可以轻松更改。 I changed the emotion direction of the partials to Y instead of X so it goes backwards and moved the image of the car with the emitter. 我将零件的情感方向更改为Y而不是X,这样它向后移动并用发射器移动了汽车的图像。

http://codepen.io/anon/pen/JYapqr http://codepen.io/anon/pen/JYapqr

Javascript : Javascript

var canvas = document.createElement('canvas');
var w = canvas.width = 1200,
    h = canvas.height = 700;
var c = canvas.getContext('2d');

var img = new Image();

//Added a new Car Image //添加了新的汽车图片

var car = new Image();
img.src = 'http://oi41.tinypic.com/4i2aso.jpg';

//And set it's src property //并设置它的src属性

car.src = 'https://openmerchantaccount.com/img2/crystalship.png';

//Set the starting positions to -car length so it doesn't just appear on screen //将起始位置设置为-car length,以便它不仅出现在屏幕上

var position = {x : -250, y : h/2};

document.body.appendChild(canvas);

var particles = [];
var random = function(min, max){
  return Math.random()*(max-min)*min;
};

function Particle(x, y){
  this.x = x;
  this.y = y;

//Changed the direction of the partial to go right, not up. //将部分的方向更改为右移,而不是向上。

  this.velX = -2;
  this.velY = (random(1, 10)-5)/10;
  this.size = random(3, 5)/10;
  this.alpha = 1;
  this.update = function(){
    this.y += this.velY;
    this.x += this.velX;
    this.velY *= 0.99;
    if(this.alpha < 0)
      this.alpha = 0;
    c.globalAlpha = this.alpha;
    c.save();
    c.translate(this.x, this.y);
    c.scale(this.size, this.size);

    c.drawImage(img, -img.width/2, -img.height/2);
    c.restore();
    this.alpha *= 0.96;
    this.size += 0.02;//
  };
}

var draw = function(){

//To animate movement, on draw move the position 2px to the right //要设置动画,请在绘制时向右移动2px

  position.x+=2;
  position.y;

//If the car reaches the end I just move it back to the start //如果汽车到达终点,我只需将其移回起点

  if(position.x+250>w){
    position.x=-250;
  }

  var p = new Particle(position.x, position.y);
  particles.push(p);
  while(particles.length > 500) particles.shift();

  c.globalAlpha = 1;
  c.fillStyle = '#000';
  c.fillRect(0,0,w,h);

//Draw the image back on screen //在屏幕上绘制图像

  c.drawImage(car, position.x,position.y-80,250,100);
  for(var i = 0; i < particles.length; i++)
  {
    particles[i].update();
  }
};

setInterval(draw, 1000/60);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM