How scottjehl/picturefill works
Purpose and delivery
Picturefill is an archived JavaScript polyfill for responsive-image markup, primarily <picture>, srcset, sizes, and media/type-based source selection. The distributable browser entry point is dist/picturefill.js; source development centers on src/picturefill.js. It is intended to run directly in browsers, not as a server application.
Core polyfill engine
src/picturefill.js owns responsive-image evaluation and candidate selection. It scans applicable images/picture elements, parses responsive source metadata, evaluates viewport/device conditions, and assigns the selected image resource to the browser. The built artifact mirrors this in dist/picturefill.js.
Markup-to-resource selection flow
The principal flow is:
1. Browser startup or explicit reevaluation invokes Picturefill.
2. Image and <picture><source> markup is collected.
3. Media conditions, MIME support, sizes, and srcset candidates are evaluated against the current environment.
4. The best resource candidate is selected and applied to the target image.
The key boundary is DOM markup and browser capabilities in, selected image URL and image-element state out. Native responsive-image support may handle parts of this flow; the polyfill supplies fallback/compatibility behavior.
Environment and compatibility integration
The engine relies on browser DOM APIs, media-query evaluation, viewport metrics, image loading behavior, and type support. A Gecko-specific compatibility layer is maintained in src/plugins/gecko-picture/pf.gecko-picture.js; release plugins are separately shipped under dist/plugins/.
Optional plugin stages
Plugins extend the core at lifecycle boundaries rather than replacing selection logic:
• Mutation-driven rescans for dynamically changed markup: src/plugins/mutation/pf.mutation.js
• Print-mode reevaluation: src/plugins/print/pf.print.js
• Intrinsic-dimension handling for responsive images: src/plugins/intrinsic-dimension/pf.intrinsic.js
• Image MIME/type support handling: src/plugins/typesupport/pf.type.js
• Legacy-browser accommodations: src/plugins/oldie/pf.oldie.js
Corresponding browser-consumable plugin files live under dist/plugins/.
Distribution and consumption boundary
Consumers can load the standalone bundle at dist/picturefill.js and selectively add compatible plugin scripts from dist/plugins/. Source files under src/ are the implementation boundary; dist/ is the publication boundary. Package metadata supports JavaScript package ecosystems through package.json and bower.json, with composer.json also present.
Validation and examples
Browser-based QUnit test pages exercise core selection and mutation behavior through tests/index.html, tests/index-functional.html, and tests/index-mutation.html. Runnable markup examples in examples/demo-01.html through examples/demo-05.html demonstrate supported responsive-image patterns and provide manual integration fixtures.
Main entry points: src/picturefill.js Picturefill engine · dist/picturefill.js Picturefill bundle · pf.mutation.js Mutation rescan · pf.print.js Print reevaluation
Components 15
Source implementation
- Picturefill engine · Scans markup, evaluates sources, and applies the best candidate.
src/picturefill.js - Gecko compatibility
src/plugins/gecko-picture/pf.gecko-picture.js
Optional plugins
Lifecycle extensions compiled for selective browser use.
- Mutation rescan · Rescans dynamically changed markup.
src/plugins/mutation/pf.mutation.js - Print reevaluation
src/plugins/print/pf.print.js - Intrinsic dimensions
src/plugins/intrinsic-dimension/pf.intrinsic.js - Type support · Handles image MIME/type support.
src/plugins/typesupport/pf.type.js - Legacy browser support
src/plugins/oldie/pf.oldie.js
Published browser assets
- Picturefill bundle · Standalone browser-consumable core artifact.
dist/picturefill.js - Plugin bundles · Selectable compatible plugin scripts.
- Package metadata · Supports JavaScript package ecosystems.
package.json
Browser environment
- Responsive image markup · Images, picture sources, srcset, sizes, media, and type attributes.
- Browser capabilities · DOM, media queries, viewport metrics, image loading, and MIME support.
- Selected image resource · Chosen URL and target image-element state.
Other components
- Browser QUnit tests · Exercises core selection and mutation behavior.
tests/index.html - Responsive-image demos · Manual integration fixtures for supported markup patterns.
examples/demo-01.html
Connections 21
- Responsive image markup → Picturefill engine: collected by
- Browser capabilities → Picturefill engine: evaluates against
- Picturefill engine → Selected image resource: selects and applies
- Gecko compatibility → Picturefill engine: adds compatibility
- Mutation rescan → Picturefill engine: triggers rescans
- Print reevaluation → Picturefill engine: triggers reevaluation
- Intrinsic dimensions → Picturefill engine: extends lifecycle
- Type support → Picturefill engine: extends type handling
- Legacy browser support → Picturefill engine: adds legacy support
- Picturefill engine → Picturefill bundle: published as
- Mutation rescan → Plugin bundles: published with
- Print reevaluation → Plugin bundles: published with
- Intrinsic dimensions → Plugin bundles: published with
- Type support → Plugin bundles: published with
- Legacy browser support → Plugin bundles: published with
- Picturefill bundle → Responsive image markup: runs against
- Picturefill bundle → Browser capabilities: uses
- Plugin bundles → Picturefill bundle: optionally extends
- Browser QUnit tests → Picturefill bundle: exercises
- Responsive-image demos → Picturefill bundle: loads
- Package metadata → Picturefill bundle: distributes