首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用JavaScript检测AVIF图像是否为动画

使用JavaScript检测AVIF图像是否为动画
EN

Stack Overflow用户
提问于 2022-11-03 06:16:30
回答 1查看 48关注 0票数 0

是否有一种方法来检测一个AVIF图像是否使用JavaScript动画?

绝对没有框架或库。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-03 08:25:31

新的API接口可以告诉你这个。

您可以将数据的ReadableStream传递给它,然后检查解码器的tracks是否将其animated元数据设置为true:

代码语言:javascript
运行
复制
if (!window.ImageDecoder) {
  console.warn("Your browser doesn't support the ImageDecoder API yet, we'd need to load a library");
}
// from https://colinbendell.github.io/webperf/animated-gif-decode/avif.html
fetch("https://colinbendell.github.io/webperf/animated-gif-decode/6.avif").then((resp) => test("animated", resp.body));
// from https://github.com/link-u/avif-sample-images cc-by-sa 4.0 Kaede Fujisaki
fetch("https://raw.githubusercontent.com/link-u/avif-sample-images/master/fox.profile1.8bpc.yuv444.avif").then((resp) => test("static", resp.body));

document.querySelector("input").onchange = ({target}) => test("your image", target.files[0].stream());


async function test(name, stream) {
  const decoder = new ImageDecoder({ data: stream, type: "image/avif" });
  // wait for we have some metadata
  await decoder.tracks.ready;
  // log if one of the tracks is animated
  console.log(name, [...decoder.tracks].some((track) => track.animated));
}
代码语言:javascript
运行
复制
<input type=file>

但是,注意这个API仍然不被广泛支持,因为只有基于铬的浏览器有一个实现目前。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74298761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档