You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Shaker.js 570B

12345678910111213141516171819202122232425262728293031
  1. import Vec2 from "./Vec2.js";
  2. export default class Shaker {
  3. constructor(decay = 7) {
  4. this.vec = new Vec2();
  5. this.decay = decay;
  6. this.shakeX = 0;
  7. this.shakeY = 0;
  8. }
  9. shake(x, y = x) {
  10. this.shakeX = x;
  11. this.shakeY = y;
  12. }
  13. update(dt) {
  14. if (this.shakeX > 0.1)
  15. this.vec.x = (Math.random() - 0.5) * this.shakeX * dt;
  16. else
  17. this.vec.x = 0;
  18. if (this.shakeY > 0.1)
  19. this.vec.y = (Math.random() - 0.5) * this.shakeY * dt;
  20. else
  21. this.vec.y = 0;
  22. var xRatio = 1 / (1 + (dt * this.decay));
  23. this.shakeX *= xRatio;
  24. this.shakeY *= xRatio;
  25. }
  26. }