Curtain.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class Curtain {
  2. constructor() {
  3. this.system = null;
  4. this.canvas = null;
  5. this.duration = 5000;
  6. this.timer = 0;
  7. this.opacity = 0;
  8. this.onComplete = null;
  9. this.state = "idle";
  10. }
  11. init(systemObject, canvas) {
  12. this.system = systemObject;
  13. this.canvas = canvas;
  14. }
  15. close(duration, onComplete) {
  16. this.duration = duration;
  17. this.onComplete = onComplete;
  18. this.opacity = 1;
  19. this.timer = 0;
  20. this.state = "closing";
  21. }
  22. open(duration, onComplete) {
  23. this.duration = duration;
  24. this.onComplete = onComplete;
  25. this.opacity = 0;
  26. this.timer = 0;
  27. this.state = "opening";
  28. }
  29. updateDelta(delta) {
  30. if (this.state == "idle") {
  31. return;
  32. }
  33. this.timer += delta * (1000 / 60);
  34. if (this.timer - (this.duration / 8) >= this.duration) {
  35. this.state = "idle";
  36. this.onComplete();
  37. return;
  38. }
  39. this.opacity = (this.timer / (this.duration - (this.duration / 8)));
  40. }
  41. draw(context) {
  42. if (this.state == "idle") {
  43. return;
  44. }
  45. if (this.state == "closing") {
  46. context.fillStyle = "#000000";
  47. context.globalAlpha = Math.min(1, Math.max(0, this.opacity));
  48. context.beginPath();
  49. context.rect(0, 0, this.canvas.width, this.canvas.height);
  50. context.fill();
  51. }
  52. if (this.state == "opening") {
  53. context.fillStyle = "#000000";
  54. context.globalAlpha = Math.min(1, Math.max(0, 1 - this.opacity));
  55. context.beginPath();
  56. context.rect(0, 0, this.canvas.width, this.canvas.height);
  57. context.fill();
  58. }
  59. context.globalAlpha = 1;
  60. }
  61. };