Camera.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var Camera = function(aCanvas, aContext, x, y) {
  2. var camera = this;
  3. var canvas = aCanvas;
  4. var context = aContext;
  5. this.x = x;
  6. this.y = y;
  7. this.minZoom = 1.3;
  8. this.maxZoom = 1.8;
  9. this.zoom = this.minZoom;
  10. var backgroundColor = Math.random()*360;
  11. this.setupContext = function() {
  12. var translateX = canvas.width / 2 - camera.x * camera.zoom;
  13. var translateY = canvas.height / 2 - camera.y * camera.zoom;
  14. // Reset transform matrix
  15. context.setTransform(1,0,0,1,0,0);
  16. context.fillStyle = 'hsl('+backgroundColor+',50%,10%)';
  17. context.fillRect(0,0,canvas.width, canvas.height);
  18. context.translate(translateX, translateY);
  19. context.scale(camera.zoom, camera.zoom);
  20. if(debug) {
  21. drawDebug();
  22. }
  23. };
  24. this.update = function(model) {
  25. backgroundColor += 0.08;
  26. backgroundColor = backgroundColor > 360 ? 0 : backgroundColor;
  27. var targetZoom = (model.camera.maxZoom + (model.camera.minZoom - model.camera.maxZoom) * Math.min(model.userTadpole.momentum, model.userTadpole.maxMomentum) / model.userTadpole.maxMomentum);
  28. model.camera.zoom += (targetZoom - model.camera.zoom) / 60;
  29. var delta = {
  30. x: (model.userTadpole.x - model.camera.x) / 30,
  31. y: (model.userTadpole.y - model.camera.y) / 30
  32. }
  33. if(Math.abs(delta.x) + Math.abs(delta.y) > 0.1) {
  34. model.camera.x += delta.x;
  35. model.camera.y += delta.y;
  36. for(var i = 0, len = model.waterParticles.length; i < len; i++) {
  37. var wp = model.waterParticles[i];
  38. wp.x -= (wp.z - 1) * delta.x;
  39. wp.y -= (wp.z - 1) * delta.y;
  40. }
  41. }
  42. };
  43. // Gets bounds of current zoom level of current position
  44. this.getBounds = function() {
  45. return [
  46. {x: camera.x - canvas.width / 2 / camera.zoom, y: camera.y - canvas.height / 2 / camera.zoom},
  47. {x: camera.x + canvas.width / 2 / camera.zoom, y: camera.y + canvas.height / 2 / camera.zoom}
  48. ];
  49. };
  50. // Gets bounds of minimum zoom level of current position
  51. this.getOuterBounds = function() {
  52. return [
  53. {x: camera.x - canvas.width / 2 / camera.minZoom, y: camera.y - canvas.height / 2 / camera.minZoom},
  54. {x: camera.x + canvas.width / 2 / camera.minZoom, y: camera.y + canvas.height / 2 / camera.minZoom}
  55. ];
  56. };
  57. // Gets bounds of maximum zoom level of current position
  58. this.getInnerBounds = function() {
  59. return [
  60. {x: camera.x - canvas.width / 2 / camera.maxZoom, y: camera.y - canvas.height / 2 / camera.maxZoom},
  61. {x: camera.x + canvas.width / 2 / camera.maxZoom, y: camera.y + canvas.height / 2 / camera.maxZoom}
  62. ];
  63. };
  64. this.startUILayer = function() {
  65. context.setTransform(1,0,0,1,0,0);
  66. }
  67. var debugBounds = function(bounds, text) {
  68. context.strokeStyle = '#fff';
  69. context.beginPath();
  70. context.moveTo(bounds[0].x, bounds[0].y);
  71. context.lineTo(bounds[0].x, bounds[1].y);
  72. context.lineTo(bounds[1].x, bounds[1].y);
  73. context.lineTo(bounds[1].x, bounds[0].y);
  74. context.closePath();
  75. context.stroke();
  76. context.fillText(text, bounds[0].x + 10, bounds[0].y + 10);
  77. };
  78. var drawDebug = function() {
  79. debugBounds(camera.getInnerBounds(), 'Maximum zoom camera bounds');
  80. debugBounds(camera.getOuterBounds(), 'Minimum zoom camera bounds');
  81. debugBounds(camera.getBounds(), 'Current zoom camera bounds');
  82. };
  83. };