MCPcopy Index your code
hub / github.com/ampproject/amphtml / selectByWidth_

Method selectByWidth_

src/core/dom/srcset.js:172–194  ·  view source on GitHub ↗

* @param {number} width * @return {number} * @private

(width)

Source from the content-addressed store, hash-verified

170 * @private
171 */
172 selectByWidth_(width) {
173 const sources = /** @type {Array<WidthSourceDef>} */ (this.sources_);
174 let minIndex = 0;
175 let minScore = Infinity;
176 let minWidth = Infinity;
177
178 for (let i = 0; i < sources.length; i++) {
179 const sWidth = sources[i].width ?? 0;
180 const score = Math.abs(sWidth - width);
181
182 // Select the one that is closer with a slight preference toward larger
183 // widths. If smaller size is closer, enforce minimum ratio to ensure
184 // image isn't too distorted.
185 if (score <= minScore * 1.1 || width / minWidth > 1.2) {
186 minIndex = i;
187 minScore = score;
188 minWidth = sWidth;
189 } else {
190 break;
191 }
192 }
193 return minIndex;
194 }
195
196 /**
197 * @param {number} dpr

Callers 1

selectMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected