* @param {number} width * @return {number} * @private
(width)
| 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 |