retinajs-rails 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +83 -0
- data/app/assets/javascripts/retina.js +182 -0
- data/app/assets/stylesheets/retina.scss +20 -0
- data/lib/retinajs-rails.rb +12 -0
- data/retinajs-rails.gemspec +16 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b543d7cfda51df21fe2c7633829f99e8187e0d8d
|
4
|
+
data.tar.gz: 278d5c80eca30acf827d357a26d7abf2a7fbdcaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 121d8b255c47c3e5d561cc77f272ad244ec0bc31ff2789b198965720f52029d38657f16a53502db3e6710de58800a176c60f38a3ba0d987a0c6f8a132d98e2ef
|
7
|
+
data.tar.gz: e2d1b3be8a6d4e9ed9f10469d38e089284daa6e4f226d83dcbfe1d2fe5445fbf10c7de510c6a717b341c36427c047d7f092381442940377e5d8c3dd9a05734a6
|
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# retinajs-rails
|
2
|
+
|
3
|
+
Integrates [Retina.js](http://imulus.github.io/retinajs/) with the rails asset pipeline.
|
4
|
+
|
5
|
+
Retina.js is an awesome and simple plugin for rendering retina images for displays that support this.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem "retinajs-rails", "~> 0.1"
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
To import the js plugin, add the following line to your application.js:
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
//= require retina
|
23
|
+
```
|
24
|
+
|
25
|
+
### Image helper
|
26
|
+
|
27
|
+
Now you will be able to render retina images by using the `image_tag_with_at2x` helper. Example:
|
28
|
+
```ruby
|
29
|
+
# Replace:
|
30
|
+
image_tag "logo.png"
|
31
|
+
|
32
|
+
# With:
|
33
|
+
image_tag_with_at2x "logo.png"
|
34
|
+
```
|
35
|
+
|
36
|
+
This will output the HTML similar to::
|
37
|
+
```html
|
38
|
+
<img data-at2x="/assets/logo@2x.png" src="/assets/logo.png">
|
39
|
+
```
|
40
|
+
|
41
|
+
### SCSS mixin
|
42
|
+
|
43
|
+
If you'd like to use the SCSS mixin, you need to import `retina.scss`, add the following line to your application.scss:
|
44
|
+
|
45
|
+
``` ruby
|
46
|
+
@import "retina"
|
47
|
+
```
|
48
|
+
|
49
|
+
Now you're able to use the `.at2x` mixin in your stylesheets.
|
50
|
+
|
51
|
+
For example:
|
52
|
+
|
53
|
+
```scss
|
54
|
+
.logo {
|
55
|
+
@include at2x('logo', 'png', 200px, 100px);
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
will compile to:
|
60
|
+
|
61
|
+
```scss
|
62
|
+
.logo {
|
63
|
+
background-image: url('/assets/logo.png');
|
64
|
+
}
|
65
|
+
|
66
|
+
@media all and (-webkit-min-device-pixel-ratio: 1.5) {
|
67
|
+
.logo {
|
68
|
+
background-image: url('/assets/logo@2x.png');
|
69
|
+
background-size: 200px 100px;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
```
|
73
|
+
|
74
|
+
For more information about Retina.js, see:
|
75
|
+
[http://imulus.github.io/retinajs/](http://imulus.github.io/retinajs/)
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create new Pull Request
|
@@ -0,0 +1,182 @@
|
|
1
|
+
/*!
|
2
|
+
* Retina.js v1.3.0
|
3
|
+
*
|
4
|
+
* Copyright 2014 Imulus, LLC
|
5
|
+
* Released under the MIT license
|
6
|
+
*
|
7
|
+
* Retina.js is an open source script that makes it easy to serve
|
8
|
+
* high-resolution images to devices with retina displays.
|
9
|
+
*/
|
10
|
+
|
11
|
+
(function() {
|
12
|
+
var root = (typeof exports === 'undefined' ? window : exports);
|
13
|
+
var config = {
|
14
|
+
// An option to choose a suffix for 2x images
|
15
|
+
retinaImageSuffix : '@2x',
|
16
|
+
|
17
|
+
// Ensure Content-Type is an image before trying to load @2x image
|
18
|
+
// https://github.com/imulus/retinajs/pull/45)
|
19
|
+
check_mime_type: true,
|
20
|
+
|
21
|
+
// Resize high-resolution images to original image's pixel dimensions
|
22
|
+
// https://github.com/imulus/retinajs/issues/8
|
23
|
+
force_original_dimensions: true
|
24
|
+
};
|
25
|
+
|
26
|
+
function Retina() {}
|
27
|
+
|
28
|
+
root.Retina = Retina;
|
29
|
+
|
30
|
+
Retina.configure = function(options) {
|
31
|
+
if (options === null) {
|
32
|
+
options = {};
|
33
|
+
}
|
34
|
+
|
35
|
+
for (var prop in options) {
|
36
|
+
if (options.hasOwnProperty(prop)) {
|
37
|
+
config[prop] = options[prop];
|
38
|
+
}
|
39
|
+
}
|
40
|
+
};
|
41
|
+
|
42
|
+
Retina.init = function(context) {
|
43
|
+
if (context === null) {
|
44
|
+
context = root;
|
45
|
+
}
|
46
|
+
|
47
|
+
var existing_onload = context.onload || function(){};
|
48
|
+
|
49
|
+
context.onload = function() {
|
50
|
+
var images = document.getElementsByTagName('img'), retinaImages = [], i, image;
|
51
|
+
for (i = 0; i < images.length; i += 1) {
|
52
|
+
image = images[i];
|
53
|
+
if (!!!image.getAttributeNode('data-no-retina')) {
|
54
|
+
retinaImages.push(new RetinaImage(image));
|
55
|
+
}
|
56
|
+
}
|
57
|
+
existing_onload();
|
58
|
+
};
|
59
|
+
};
|
60
|
+
|
61
|
+
Retina.isRetina = function(){
|
62
|
+
var mediaQuery = '(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)';
|
63
|
+
|
64
|
+
if (root.devicePixelRatio > 1) {
|
65
|
+
return true;
|
66
|
+
}
|
67
|
+
|
68
|
+
if (root.matchMedia && root.matchMedia(mediaQuery).matches) {
|
69
|
+
return true;
|
70
|
+
}
|
71
|
+
|
72
|
+
return false;
|
73
|
+
};
|
74
|
+
|
75
|
+
|
76
|
+
var regexMatch = /\.\w+$/;
|
77
|
+
function suffixReplace (match) {
|
78
|
+
return config.retinaImageSuffix + match;
|
79
|
+
}
|
80
|
+
|
81
|
+
function RetinaImagePath(path, at_2x_path) {
|
82
|
+
this.path = path || '';
|
83
|
+
if (typeof at_2x_path !== 'undefined' && at_2x_path !== null) {
|
84
|
+
this.at_2x_path = at_2x_path;
|
85
|
+
this.perform_check = false;
|
86
|
+
} else {
|
87
|
+
if (undefined !== document.createElement) {
|
88
|
+
var locationObject = document.createElement('a');
|
89
|
+
locationObject.href = this.path;
|
90
|
+
locationObject.pathname = locationObject.pathname.replace(regexMatch, suffixReplace);
|
91
|
+
this.at_2x_path = locationObject.href;
|
92
|
+
} else {
|
93
|
+
var parts = this.path.split('?');
|
94
|
+
parts[0] = parts[0].replace(regexMatch, suffixReplace);
|
95
|
+
this.at_2x_path = parts.join('?');
|
96
|
+
}
|
97
|
+
this.perform_check = true;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
root.RetinaImagePath = RetinaImagePath;
|
102
|
+
|
103
|
+
RetinaImagePath.confirmed_paths = [];
|
104
|
+
|
105
|
+
RetinaImagePath.prototype.is_external = function() {
|
106
|
+
return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) );
|
107
|
+
};
|
108
|
+
|
109
|
+
RetinaImagePath.prototype.check_2x_variant = function(callback) {
|
110
|
+
var http, that = this;
|
111
|
+
if (this.is_external()) {
|
112
|
+
return callback(false);
|
113
|
+
} else if (!this.perform_check && typeof this.at_2x_path !== 'undefined' && this.at_2x_path !== null) {
|
114
|
+
return callback(true);
|
115
|
+
} else if (this.at_2x_path in RetinaImagePath.confirmed_paths) {
|
116
|
+
return callback(true);
|
117
|
+
} else {
|
118
|
+
http = new XMLHttpRequest();
|
119
|
+
http.open('HEAD', this.at_2x_path);
|
120
|
+
http.onreadystatechange = function() {
|
121
|
+
if (http.readyState !== 4) {
|
122
|
+
return callback(false);
|
123
|
+
}
|
124
|
+
|
125
|
+
if (http.status >= 200 && http.status <= 399) {
|
126
|
+
if (config.check_mime_type) {
|
127
|
+
var type = http.getResponseHeader('Content-Type');
|
128
|
+
if (type === null || !type.match(/^image/i)) {
|
129
|
+
return callback(false);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
RetinaImagePath.confirmed_paths.push(that.at_2x_path);
|
134
|
+
return callback(true);
|
135
|
+
} else {
|
136
|
+
return callback(false);
|
137
|
+
}
|
138
|
+
};
|
139
|
+
http.send();
|
140
|
+
}
|
141
|
+
};
|
142
|
+
|
143
|
+
|
144
|
+
function RetinaImage(el) {
|
145
|
+
this.el = el;
|
146
|
+
this.path = new RetinaImagePath(this.el.getAttribute('src'), this.el.getAttribute('data-at2x'));
|
147
|
+
var that = this;
|
148
|
+
this.path.check_2x_variant(function(hasVariant) {
|
149
|
+
if (hasVariant) {
|
150
|
+
that.swap();
|
151
|
+
}
|
152
|
+
});
|
153
|
+
}
|
154
|
+
|
155
|
+
root.RetinaImage = RetinaImage;
|
156
|
+
|
157
|
+
RetinaImage.prototype.swap = function(path) {
|
158
|
+
if (typeof path === 'undefined') {
|
159
|
+
path = this.path.at_2x_path;
|
160
|
+
}
|
161
|
+
|
162
|
+
var that = this;
|
163
|
+
function load() {
|
164
|
+
if (! that.el.complete) {
|
165
|
+
setTimeout(load, 5);
|
166
|
+
} else {
|
167
|
+
if (config.force_original_dimensions) {
|
168
|
+
that.el.setAttribute('width', that.el.offsetWidth);
|
169
|
+
that.el.setAttribute('height', that.el.offsetHeight);
|
170
|
+
}
|
171
|
+
|
172
|
+
that.el.setAttribute('src', path);
|
173
|
+
}
|
174
|
+
}
|
175
|
+
load();
|
176
|
+
};
|
177
|
+
|
178
|
+
|
179
|
+
if (Retina.isRetina()) {
|
180
|
+
Retina.init(root);
|
181
|
+
}
|
182
|
+
})();
|
@@ -0,0 +1,20 @@
|
|
1
|
+
// retina.sass
|
2
|
+
// A helper mixin for applying high-resolution background images (http://www.retinajs.com)
|
3
|
+
|
4
|
+
// Submitted by Nathan Crank
|
5
|
+
// nathancrank.com
|
6
|
+
|
7
|
+
@mixin at2x($path, $ext: "jpg", $w: auto, $h: auto) {
|
8
|
+
$at1x_path: "#{$path}.#{$ext}";
|
9
|
+
$at2x_path: "#{$path}@2x.#{$ext}";
|
10
|
+
|
11
|
+
background-image: image-url("#{$at1x_path}");
|
12
|
+
|
13
|
+
@media all and (-webkit-min-device-pixel-ratio : 1.5),
|
14
|
+
all and (-o-min-device-pixel-ratio: 3/2),
|
15
|
+
all and (min--moz-device-pixel-ratio: 1.5),
|
16
|
+
all and (min-device-pixel-ratio: 1.5) {
|
17
|
+
background-image: image-url("#{$at2x_path}");
|
18
|
+
background-size: $w $h;
|
19
|
+
}
|
20
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module RetinaJs
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
module ImageHelper
|
7
|
+
def image_tag_with_at2x(name_at_1x, options={})
|
8
|
+
name_at_2x = name_at_1x.gsub(%r{\.\w+$}, '@2x\0')
|
9
|
+
image_tag(name_at_1x, options.merge("data-at2x" => asset_path(name_at_2x)))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
ActionView::Base.send :include, ImageHelper
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'retinajs-rails'
|
5
|
+
gem.version = '0.2'
|
6
|
+
gem.date = Date.today
|
7
|
+
gem.authors = ["Joshua Jansen"]
|
8
|
+
gem.email = ["joshuajansen88@gmail.com"]
|
9
|
+
gem.description = %q{Adds retina.js assets and image helpers to your Rails app}
|
10
|
+
gem.summary = %q{This gem adds the retina.js assets to your asset pipeline and adds helper methods for rendering retina images.}
|
11
|
+
gem.homepage = "https://github.com/joshuajansen/retinajs-rails"
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.license = 'MIT'
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retinajs-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Jansen
|
@@ -16,7 +16,13 @@ email:
|
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
|
-
files:
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- README.md
|
22
|
+
- app/assets/javascripts/retina.js
|
23
|
+
- app/assets/stylesheets/retina.scss
|
24
|
+
- lib/retinajs-rails.rb
|
25
|
+
- retinajs-rails.gemspec
|
20
26
|
homepage: https://github.com/joshuajansen/retinajs-rails
|
21
27
|
licenses:
|
22
28
|
- MIT
|