2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 `core-image` is an element for displaying an image that provides useful sizing and
12 preloading options not found on the standard `<img>` tag.
14 The `sizing` option allows the image to be either cropped (`cover`) or
15 letterboxed (`contain`) to fill a fixed user-size placed on the element.
17 The `preload` option prevents the browser from rendering the image until the
18 image is fully loaded. In the interim, either the element's CSS `background-color`
19 can be be used as the placeholder, or the `placeholder` property can be
20 set to a URL (preferably a data-URI, for instant rendering) for an
23 The `fade` option (only valid when `preload` is set) will cause the placeholder
24 image/color to be faded out once the image is rendered.
28 Basically identical to <img src="..."> tag:
30 <core-image src="http://lorempixel.com/400/400"></core-image>
32 Will letterbox the image to fit:
34 <core-image style="width:400px; height:400px;" sizing="contain"
35 src="http://lorempixel.com/600/400"></core-image>
37 Will crop the image to fit:
39 <core-image style="width:400px; height:400px;" sizing="cover"
40 src="http://lorempixel.com/600/400"></core-image>
42 Will show light-gray background until the image loads:
44 <core-image style="width:400px; height:400px; background-color: lightgray;"
45 sizing="cover" preload src="http://lorempixel.com/600/400"></core-image>
47 Will show a base-64 encoded placeholder image until the image loads:
49 <core-image style="width:400px; height:400px;" placeholder="data:image/gif;base64,..."
50 sizing="cover" preload src="http://lorempixel.com/600/400"></core-image>
52 Will fade the light-gray background out once the image is loaded:
54 <core-image style="width:400px; height:400px; background-color: lightgray;"
55 sizing="cover" preload fade src="http://lorempixel.com/600/400"></core-image>
58 @group Polymer Core Elements
63 <link rel=
"import" href=
"../polymer/polymer.html">
65 <polymer-element name=
"core-image">
67 <link rel=
"stylesheet" href=
"core-image.css">
68 <template if=
"{{!sizing}}">
71 <template if=
"{{preload && fade}}">
72 <div id=
"placeholder" fit
></div>
83 * The URL of an image.
92 * When false, the image is prevented from loading and any placeholder is
93 * shown. This may be useful when a binding to the src property is known to
94 * be invalid, to prevent 404 requests.
103 * Sets a sizing option for the image. Valid values are `contain` (full
104 * aspect ratio of the image is contained within the element and
105 * letterboxed) or `cover` (image is cropped in order to fully cover the
106 * bounds of the element), or `null` (default: image takes natural size).
115 * When a sizing option is uzed (`cover` or `contain`), this determines
116 * how the image is aligned within the element bounds.
118 * @attribute position
125 * When `true`, any change to the `src` property will cause the `placeholder`
126 * image to be shown until the
135 * This image will be used as a background/placeholder until the src image has
136 * loaded. Use of a data-URI for placeholder is encouraged for instant rendering.
138 * @attribute placeholder
145 * When `preload` is true, setting `fade` to true will cause the image to
155 * Read-only value that tracks the loading state of the image when the `preload`
165 * Can be used to set the width of image (e.g. via binding); size may also be
175 * Can be used to set the height of image (e.g. via binding); size may also be
187 'preload color sizing position src fade': 'update'
190 widthChanged: function() {
191 this.style
.width
= isNaN(this.width
) ? this.width
: this.width
+ 'px';
194 heightChanged: function() {
195 this.style
.height
= isNaN(this.height
) ? this.height
: this.height
+ 'px';
199 this.style
.backgroundSize
= this.sizing
;
200 this.style
.backgroundPosition
= this.sizing
? this.position
: null;
201 this.style
.backgroundRepeat
= this.sizing
? 'no-repeat' : null;
204 if (!this._placeholderEl
) {
205 this._placeholderEl
= this.shadowRoot
.querySelector('#placeholder');
207 this._placeholderEl
.style
.backgroundSize
= this.sizing
;
208 this._placeholderEl
.style
.backgroundPosition
= this.sizing
? this.position
: null;
209 this._placeholderEl
.style
.backgroundRepeat
= this.sizing
? 'no-repeat' : null;
210 this._placeholderEl
.classList
.remove('fadein');
211 this._placeholderEl
.style
.backgroundImage
= (this.load
&& this.placeholder
) ? 'url(' + this.placeholder
+ ')': null;
213 this._setSrc(this.placeholder
);
215 if (this.load
&& this.src
) {
216 var img
= new Image();
219 img
.onload = function() {
220 this._setSrc(this.src
);
221 this.loading
= false;
223 this._placeholderEl
.classList
.add('fadein');
228 this._setSrc(this.src
);
232 _setSrc: function(src
) {
234 this.style
.backgroundImage
= src
? 'url(' + src
+ ')': '';
236 this.$.img
.src
= src
|| '';