我正在尝试从YouTube缩略图中获取调色板,以更好地将YouTube视频与整个网站设计融合在一起,我想知道是否有任何方法可以做到这一点?
我试过使用color-thief(https://github.com/lokesh/color-thief)和此代码(没有运气):
var img = new Image();
img.onload = function () {
var colorThief = new ColorThief();
colorThief.getColor(img);
};
img.crossOrigin = 'Anonymous';
img.src = 'http://img.youtube.com/vi/NuEfvIca0XU/mqdefault.jpg
从这我只是得到这个错误:
Image from origin 'http://img.youtube.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access
有什么方法可以绕过此过程,或者在不从YouTube下载缩略图并将其保存到我的服务器的情况下获取调色板?
解决方法:
找到了解决方案,但涉及到将图像链接传递到http://cors-anywhere.herokuapp.com/
这给我们留下了:
var img = new Image();
img.onload = function () {
var colorThief = new ColorThief();
colorThief.getColor(img);
};
img.crossOrigin = 'Anonymous';
img.src = 'http://cors-anywhere.herokuapp.com/http://img.youtube.com/vi/'+id+'/mqdefault.jpg';