curtain.js 1.5 KB

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