-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-multi-resolution-images.less
148 lines (112 loc) · 4.14 KB
/
auto-multi-resolution-images.less
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// =================================
// SETTINGS
// =================================
// Image Path
// =================================
// Set the basepath to your images dir.
// Absolute paths are okay too!
@base_url: "./images/";
// Resolution Break-points
// =================================
// Update as you see fit for your project
// Standard Resolution (anything up to 165dpi)
@mq_resolution_1x: ~"(max-resolution: 165dpi), (-webkit-max-device-pixel-ratio: 1.0)";
// 2x Resolution (up to 335dpi)
@mq_resolution_2x: ~"(max-resolution: 335dpi), (-webkit-max-device-pixel-ratio: 2)";
// 3x Resolution (should probably go up to 495dpi)
@mq_resolution_3x: ~"(min-resolution: 336dpi), (-webkit-min-device-pixel-ratio: 2.01)";
// Mimetypes
// =================================
// Not presently used.
// Will be needed for LESS 1.4
// Update as needed for your image files.
.mimetype(png){@mimetype:"image/png";}
.mimetype(jpg){@mimetype:"image/jpeg";}
.mimetype(jpeg){@mimetype:"image/jpeg";}
.mimetype(gif){@mimetype:"image/gif";}
// =================================
// STOP EDITING
// =================================
// Filename Resolution Identifiers
// =================================
// The part of the filename that
// indicates its resolution.
.res_string(@default){ @res_string:"";}
.res_string(2){ @res_string: "@2x";}
.res_string(3){ @res_string: "@3x";}
// CSS Value Constructors (x3)
// =================================
// A helper mixin used by the
// background-image mixin for
// creating the filename from
// the parameters provided.
.construct_image(@basename, @ext, @resolution:1){
// Returns @dui_data string or number zero
// when there is no defined data-uri image.
.get_dui_data(@basename, @resolution);
// Converts numeric resolution factor to
// a string that can be used in the filename.
.res_string(@resolution);
// If no data-uri image was found, construct uri path
.construct_file_img_value(@basename, @ext, @res_string);
// If there is a data-uri defined at the proper resolution
.construct_datauri_img_value(@dui_data, @resolution);
}
// =================================
// A helper mixin used by the
// .construct_image mixin. When there
// is no data uri image found, it
// constructs the entire value for the
// CSS background-image property,
// including the url() wrapper.
.construct_file_img_value(@basename, @ext, @res_string) when not (isstring(@dui_data)){
// URL-escape the base name.
@e_basename: escape(@basename);
// This is basically the "return" value. This variable becomes
// available in the scope of the parent that called this mixin.
@constructed_img: ~"url('@{base_url}@{e_basename}@{res_string}.@{ext}')";
// LESS 1.4.0 method
// .mimetype(@ext);
// @full_url: "@{base_url}@{e_basename}@{res_string}.@{ext}";
// @constructed_img: data-uri(@full_url);
}
// =================================
// A helper mixin used by the
// .construct_image mixin. When a
// data uri image is found, it
// constructs the entire value for the
// CSS background-image property,
// including the url() wrapper.
.construct_datauri_img_value(@data, @res) when (isstring(@dui_data)){
// Assumes that the data for this image already contains
// mimetype, etc. and only needs to be wrapped in the url.
@constructed_img: ~"url('@{data}')";
}
// =================================
// Mixins as Property Aliases
// =================================
// Notice PNG is the default filetype.
// Set it to what works for you.
// Only one mixin at this time. But you
// can easily duplicate it for other
// properties, such as .border-image, or
// .list-style-image.
.background-image(@basename, @ext:png){
@media @mq_resolution_1x{
// Standard Resolution (anything up to 165dpi)
.construct_image(@basename, @ext, 1);
background-image:@constructed_img;
// LESS 1.4.0 will support data-uri automatically, like so...
// background-image:data-uri(@mimetype, @constructed_img);
}
@media @mq_resolution_2x {
// 2x Resolution
.construct_image(@basename, @ext, 2);
background-image: @constructed_img;
}
@media @mq_resolution_3x {
// 3x Resolution
.construct_image(@basename, @ext, 3);
background-image: @constructed_img;
}
}