百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

offthread-image

> 编程语言
开源

一个辅助库,用于在主线程中使用 createImageBitmap 解码图像。

194 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个辅助库,用于在主线程中使用 createImageBitmap 解码图像。

Offthread Image

A helper library that decodes images off-main thread using createImageBitmap in a worker, and then transfers them back to the main thread and draws them into a canvas.

Status: 0.1.0-alpha

I would like to see it now, please!

Of course. Firstly you will need Chrome Canary, and you should switch on "Enable experimental canvas features" in about:flags. Then head to the test page.

Docs & Usage

If you want to create a single off-thread image, you can do so by instantiating it as a wrapper around an existing element.

javascript
let target = document.querySelector('.offthread-img');
let img = new OffthreadImage(target);
target.addEventListener('decoded', function () {
  // The image has been decoded... remove the spinner.
  target.classList.remove('spinner');
});
img.src = 'cat.png';

The element itself would look something like this:

xml

However you can also add the src attribute to the element if you prefer:

xml

And now you no longer need to set the src in your JavaScript as the library will pick it up.

Other attributes

In addition to setting src you can also set width and height attributes, much as you would do with an `` element.

Both width and height attributes are optional. If only one is provided, the other is calculated based on the one provided, and the image will be drawn in proportion. If both are provided they will be used as-is, which may cause distortion.

xml

Creating multiple OffthreadImages

OffthreadImage also provides a factory method to bootstrap many images at once. This is done by calling createFromSelector():

javascript
OffthreadImage.createFromSelector();

You don't need to provide a selector (it defaults to '.offthread-img'), but you can pass one if you want to.

Background Images

If you want to use an OffthreadImage as a background image, you can do that, too. Instead of setting src use bg-src instead. The canvas will then be sized to the parent element exactly:

xml

If you use a background image, the styles of the target element will be inspected for the background-size style, though only cover and contain are supported right now:

css
.bg {
  width: 400px;
  height: 320px;
  background-size: cover;
}

Finally, if you want to read through the API, you can check out the docs.

Support & Issues

The library depends on createImageBitmap, which is behind a flag in Chrome (--enable-experimental-canvas-features, or "Enable experimental canvas features" in chrome://flags).

Support is therefore currently for:

  • Chrome 50+.
  • Firefox 42+.

For other browsers there is no fallback provided, but there is an issue filed to do that.

There are other todos and missing features:

  • Support background-size more completely.
  • Support background-position more completely.
  • Support background-repeat.

License: Apache 2.0 - See /LICENSE.

Author: paullewis.

Please note: this is not an official Google product.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言