Snapshot của bbc/Imager.js: 3.8k★ · JavaScript. Responsive images while we wait for srcset to finish cooking
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
[!IMPORTANT]
This repository is no longer maintained. It remains available for reference, but issues and pull requests are not monitored.
Imager.js
Imager.js is an alternative solution to the issue of how to handle responsive image loading, created by developers at BBC News.
Why?
There are many responsive image solutions in the wild: srcset, src-n, PictureFill and so on. They are either
verbose or hard to debug (and to maintain/integrate). Some of them don't deal well with pixel density
and suffer from double asset payload (meaning you end up downloading assets unnecessarily).
We wanted something simple, which works and which is fast as well as network friendly (only download what you need, when you need it).
Imager implements the BBC Responsive News technique which incorporates:
loading any image once
loading the most suitable sized image
How does it work?
Imager runs through the following workflow:
lookup placeholder elements
replace placeholders with transparent images
update src attribute for each image and assign the best quality/size ratio URL
Finally, it will lazy load images to speed up page load time even further.
Compatibility
Imager is tested against the following mobile and desktop browsers:
The img[src] will be computed as following (according to the reported window.devicePixelRatio value by the device):
http://example.com/assets/260/imgr.png if no pixel ratio is detected, or advertised as 1
http://example.com/assets/260/imgr-2x.png if pixel ratio is advertised as 2 (or any value greater than 2)
http://example.com/assets/260/imgr-1.3x.png if pixel ratio is advertised as 1.3
Head to this device pixel density test resource to learn more about the available pixel ratio for your device.
Interpolating {width} value
Imager has the ability to replace {width} with a non-numeric value if you
provide the widthInterpolator option, which is a function that returns the
string to be injected into the image URL for a given width. This feature allows you to use a human readable name or integrate with third-party image providers.
The img[src] will be computed as http://example.com/assets/imgr-small.png instead of http://example.com/assets/imgr-260.png.
Mixing various configurations
You might want to generate HiDPI responsive images. But what if you also include images from another provider which
serves a totally different set of sizes, without pixel ratio?
Here is an example to serve your own images alongside Flickr images.
<div style="width: 240px">
<div class="delayed-image-load" data-src="http://placehold.it/{width}" data-alt="alternative text 1"></div>
<div class="delayed-flickr-image-load" data-src="//farm5.staticflickr.com/4148/4990539658_a38ed4ec6e_{width}.jpg" data-alt="alternative text 2"></div>
</div>
<script>
var imgrPlaceholder = new Imager('.delayed-image-load', {
availableWidths: [200, 260, 320, 600]
});
var imgrFlickr = new Imager('.delayed-flickr-image-load', {
availableWidths: {
150: 't_d',
500: 'd',
640: 'z_d'
}
});
</script>
This will result in the following HTML output:
<div style="width: 240px">
<img src="http://placehold.it/260" data-src="http://placehold.it/{width}" alt="alternative text 1" class="image-replace">
<img src="//farm5.staticflickr.com/4148/4990539658_a38ed4ec6e_d.jpg" data-src="//farm5.staticflickr.com/4148/4990539658_a38ed4ec6e_{width}.jpg" alt="alternative text 2" class="image-replace">
</div>
Passing a collection of elements
You might want to pass a NodeList or an array of placeholder elements as the first argument rather than a class selector.
<div style="width: 240px">
<div class="delayed-image-load" data-src="http://placehold.it/{width}" data-alt="alternative text"></div>
</div>
<script>
var placeholderElems = document.querySelectorAll('.delayed-image-load');
var imgrPlaceholder = new Imager(placeholderElems, {
availableWidths: [200, 260, 320, 600]
});
</script>
Browse Imager public APIs and options – versioned alongside the source code of the project:
HTML options
JavaScript options
JavaScript API
Demos
Additional and fully working examples lie in the demos folder.
They are using it
BBC News
BBC Sport
The Guardian
x-imager Web Component
Imager.jsx React Component
Background
This is an experiment in offering developers an interim solution to responsive images based on the ImageEnhancer concept researched and developed by the team at BBC News.
At present, support for srcset and PictureFill are not widespread and the polyfills for these solutions also come with a number of drawbacks.
Mark McDonnell (@integralist) documented the process and rewrote the original code so it could be evolved and improved with the help of the open-source community.
The goal of this project is to automate the process with the help of the Grunt JavaScript task runner (potentially via grunt-responsive-images for image generation based on a source directory).
Much of this work can be repurposed to work with a more standards-based approach once support improves in modern browsers.
For the purposes of maintaining a distinguishment between the ImageEnhancer concept built by BBC News and this project, we're calling it Imager.js
Read more on BBC Responsive News blog.
Credits
Mark McDonnell
Tom Maslen
Thomas Parisot
Addy Osmani
Licence
Copyright 2015 British Broadcasting Corporation
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Có — bbc/Imager.js phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/bbc/Imager.js.
bbc/Imager.js có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho bbc/Imager.js. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
bbc/Imager.js dùng license gì?
bbc/Imager.js phát hành theo license Apache-2.0. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
bbc/Imager.js là gì?
bbc/Imager.js (bbc/Imager.js) là dự án JavaScript trên GitHub. Theo mô tả gốc: Responsive images while we wait for srcset to finish cooking
Đọc thêm về bbc/Imager.js ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/bbc/Imager.js là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc Imager.js có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về Imager.js.