ruby snippet

C++ code posted
created at 14 Aug 08:55, updated at 12 Sep 07:54

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# View (Slim)
= image_tag_2x('image.png')

# Helper
def image_tag_2x(name_at_1x, options={})
  name_at_2x = ActionController::Base.helpers.asset_path( name_at_1x.gsub(%r{\.\w+$}, '@2x\0') )
  image_tag(name_at_1x, options.merge("data-at2x" => name_at_2x))
end

# Coffee script:
@isRetinaDisplay = ->
  if window.matchMedia
    mq = window.matchMedia("only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen  and (min-device-pixel-ratio: 1.3), only screen and (min-resolution: 1.3dppx)");    
    return (mq && mq.matches || (window.devicePixelRatio > 1));

$(document).ready ->
  if isRetinaDisplay()
    $.each $('img[data-at2x]'), (key, value) ->
      $(this).attr('src', $(this).attr('data-at2x'))

# Don't forget put your files image.png and image@2x.png same folder
906 Bytes in 5 ms with coderay