Published
Edited
May 27, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
img.currentSrc === selectedResource.url
Insert cell
selectedResource = selectSrcset( candidates, dpr )
Insert cell
selectSmallestOverOrLargest( candidates )
Insert cell
selectClosest( candidates )
Insert cell
Insert cell
img.currentSrc
Insert cell
selectSrcset = function( annotatedCandidates, dpr ) {
if ( dpr > 1 ) {
return selectClosest( annotatedCandidates );
} else {
return selectSmallestOverOrLargest( annotatedCandidates );
}
}
Insert cell
selectClosest = function( annotatedCandidates ) {
const distances = annotatedCandidates.map( c => c.geometricDistance );
const minDistance = Math.min( ...distances );
return annotatedCandidates.find( c => c.geometricDistance === minDistance );
}
Insert cell
selectSmallestOverOrLargest = function( annotatedCandidates ) {
const distances = annotatedCandidates.map( c => c.linearDistance );
const larger = distances.filter( (d) => { return d >= 0 } );
let selectedDistance;
if ( larger.length === 0 ) {
selectedDistance = Math.max( ...distances );
} else {
selectedDistance = Math.min( ...larger );
}
return annotatedCandidates.find( c => c.linearDistance === selectedDistance );
}
Insert cell
addDensitiesAndDistances = function( parsedSrcset, sizesLengthInPx, dpr ) {
parsedSrcset.candidates.forEach( i => {
if ( i.hasOwnProperty( 'd' ) ) {
i.density = i.d;
} else if ( i.hasOwnProperty( 'w' ) && sizesLengthInPx ) {
i.density = i.w / sizesLengthInPx;
} else {
i.density = 1;
}
i.geometricDistance = geometricDistance( i.density, dpr );
i.linearDistance = i.density - dpr;
} );
return parsedSrcset;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more