1
0

Overlay.coffee 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class Overlay extends Class
  2. constructor: ->
  3. @visible = false
  4. @called = false
  5. @height = 0
  6. @image_top = 0
  7. @image_left = 0
  8. @image_width = 0
  9. @image_height = 0
  10. @background_image = ""
  11. @image_transform = ""
  12. @style = ""
  13. @pos = null
  14. @tag = null
  15. zoomImageTag: (tag, target_width, target_height) =>
  16. @log "Show", target_width, target_height
  17. @background_image = tag.style.backgroundImage
  18. @height = document.body.scrollHeight
  19. pos = tag.getBoundingClientRect()
  20. @original_pos = pos
  21. @image_top = parseInt(pos.top + window.scrollY) + "px"
  22. @image_left = parseInt(pos.left)+"px"
  23. @image_width = target_width
  24. @image_height = target_height
  25. ratio = pos.width/target_width
  26. @image_transform = "scale(#{ratio}) "
  27. @image_margin_left = parseInt((pos.width - target_width) / 2)
  28. @image_margin_top = parseInt((pos.height - target_height) / 2)
  29. @style = ""
  30. #@image_transform += "translateX(#{((pos.width / ratio) - (target_width / ratio)) / 2}px) "
  31. #@image_transform += "translateY(#{((pos.height / ratio) - (target_height / ratio)) / 2}px) "
  32. @called = true
  33. @tag = tag
  34. @visible = true
  35. window.requestAnimationFrame ( =>
  36. #@image_width = target_width
  37. #@image_height = target_height
  38. ratio = 1
  39. @image_transform = "scale(#{ratio}) "
  40. #@image_transform += "translateX(#{((pos.width / ratio) - (target_width / ratio)) / 2}px) "
  41. #@image_transform += "translateY(#{((pos.height / ratio) - (target_height / ratio)) / 2}px) "
  42. #@image_top = (pos.top + pos.height / 2 - target_height / 2) + "px"
  43. #@image_margin_left = 0-target_width/2
  44. Page.projector.scheduleRender()
  45. )
  46. handleClick: =>
  47. @log "Hide"
  48. #@image_width = @original_pos.width
  49. #@image_height = @original_pos.height+1
  50. #@image_left = @original_pos.left+"px"
  51. #@image_margin_left = 0
  52. ratio = @original_pos.width/@image_width
  53. @image_transform = "scale(#{ratio}) "
  54. #@image_transform += "translateX(#{((@original_pos.width / ratio) - (@image_width / ratio)) / 2}px) "
  55. #@image_transform += "translateY(#{((@original_pos.height / ratio) - (@image_height / ratio)) / 2}px) "
  56. @image_margin_left = Math.floor((@original_pos.width - @image_width) / 2)
  57. @image_margin_top = Math.floor((@original_pos.height - @image_height) / 2)
  58. @log @image_margin_top, @image_margin_left, @image_width, @image_height
  59. @visible = false
  60. setTimeout ( =>
  61. @log "opacity", @visible
  62. if not @visible
  63. @style = "opacity: 0"
  64. Page.projector.scheduleRender()
  65. ), 400
  66. setTimeout ( =>
  67. if not @visible
  68. @called = false
  69. Page.projector.scheduleRender()
  70. ), 900
  71. return false
  72. render: =>
  73. if not @called
  74. return h("div#Overlay", {classes: {visible: @visible}, onclick: @handleClick})
  75. h("div#Overlay", {classes: {visible: @visible}, onclick: @handleClick, style: "height: #{@height}px"}, [
  76. h("div.img", {style: """
  77. transform: #{@image_transform}; margin-left: #{@image_margin_left}px; margin-top: #{@image_margin_top}px;
  78. top: #{@image_top}; left: #{@image_left};
  79. width: #{@image_width}px; height: #{@image_height}px;
  80. background-image: #{@background_image};
  81. #{@style}"""
  82. })
  83. ])
  84. window.Overlay = Overlay