使用JavaScript和画布进行视频颜色检测

发布于 2025-01-20 06:46:08 字数 2280 浏览 2 评论 0原文

我使用JavaScript和画布来检测网站上的RGB值,但是当我在Chrome/Edge中测试时,包装器Div背景颜色可以匹配视频中的效果很好,但是当我在测试相同的代码中时iPhone X上的Chrome,它们的出现略有不同。

MP4视频文件似乎在这里存在跨域问题,但是该代码在我所描述的PC上正常工作,这是显示我遇到的内容的屏幕截图:

我不知道如何避免这个问题,请帮助并感谢!

video = document.getElementById('cat_video');
video.muted = true;
video.autoplay = true;
video.loop = true;
video.setAttribute('defaultMuted', '');
video.setAttribute('playsinline', '');

function setVideoBgColor(vid, backgroundElement) {
  const ratio = window.devicePixelRatio || 1;
  const canvas = document.createElement("canvas");
  //size of canvas space to copy pixel onto
  videoWidth = 400;
  videoHeight = 300;
  canvas.width = videoWidth * ratio;
  canvas.height = videoHeight * ratio;
  canvas.style.width = `${videoWidth}px`;
  canvas.style.height = `${videoHeight}px`;
  const ctx = canvas.getContext("2d");
  ctx.scale(ratio, ratio);
  ctx.drawImage(vid, 0, 0, canvas.width, canvas.height);
  const p = ctx.getImageData(0, 0, 100, 100).data;
  const colorBoost = 0;
  const colorR = p[0] + colorBoost;
  const colorG = p[1] + colorBoost;
  const colorB = p[2] + colorBoost;
  //colorLayer.data[pixelPos];
  backgroundElement.style.backgroundColor = "rgb(" + colorR + "," + colorG + "," + colorB + ")";
}
document.getElementById('cat_video').addEventListener("play", function() {
  setVideoBgColor(document.getElementById('cat_video'), this.parentElement);
})
.wrapper {
  display: flex;
  align-item: center;
  justify-item: center;
  width: 100vw;
  height: 500px;
  background: pink;
}

#cat_video {
  width: 400px;
  height: 300px;
}
<div class="wrapper">
  <video id="cat_video" src="https://www.purrfectland.com/wp-content/uploads/2022/04/mouse_playing_cat.mp4"></video>
</div>

I use the JavaScript and canvas to detect the RGB value on my website, but when I test in the Chrome/Edge, it works fine that the wrapper div background color could match that in the video, but when I test the same code in the Chrome on iPhone X, it comes out they have slightly different.

The MP4 video file seems to have the cross-domain issue here, but the code works fine on the PC as I described, here are the screenshots showing what I met:

I don't know how to avoid this issue, pls help and thank you!

video = document.getElementById('cat_video');
video.muted = true;
video.autoplay = true;
video.loop = true;
video.setAttribute('defaultMuted', '');
video.setAttribute('playsinline', '');

function setVideoBgColor(vid, backgroundElement) {
  const ratio = window.devicePixelRatio || 1;
  const canvas = document.createElement("canvas");
  //size of canvas space to copy pixel onto
  videoWidth = 400;
  videoHeight = 300;
  canvas.width = videoWidth * ratio;
  canvas.height = videoHeight * ratio;
  canvas.style.width = `${videoWidth}px`;
  canvas.style.height = `${videoHeight}px`;
  const ctx = canvas.getContext("2d");
  ctx.scale(ratio, ratio);
  ctx.drawImage(vid, 0, 0, canvas.width, canvas.height);
  const p = ctx.getImageData(0, 0, 100, 100).data;
  const colorBoost = 0;
  const colorR = p[0] + colorBoost;
  const colorG = p[1] + colorBoost;
  const colorB = p[2] + colorBoost;
  //colorLayer.data[pixelPos];
  backgroundElement.style.backgroundColor = "rgb(" + colorR + "," + colorG + "," + colorB + ")";
}
document.getElementById('cat_video').addEventListener("play", function() {
  setVideoBgColor(document.getElementById('cat_video'), this.parentElement);
})
.wrapper {
  display: flex;
  align-item: center;
  justify-item: center;
  width: 100vw;
  height: 500px;
  background: pink;
}

#cat_video {
  width: 400px;
  height: 300px;
}
<div class="wrapper">
  <video id="cat_video" src="https://www.purrfectland.com/wp-content/uploads/2022/04/mouse_playing_cat.mp4"></video>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文