首页 > 解决方案 > ColorThief.getColor() TypeError: url.indexOf 不是函数

问题描述

我正在尝试在 node.js 环境中使用 ColorThief。调用 ColorThief.getColor() 时出现以下错误:

TypeError:url.indexOf 不是函数

错误来自模块中的这行代码。

if(url.indexOf('data:image/gif;') 

这是我的代码:

const ColorThief = require('colorthief');
const img = document.querySelector('#image');
ColorThief.getColor(img)
    .then(color => { console.log(color) })
    .catch(err => { console.log(err) })

任何解决此问题的帮助表示赞赏。

标签: node.jscolor-thief

解决方案


来自https://lokeshdhakar.com/projects/color-thief文档

getColor(图像[,质量])

image - 在浏览器中调用时,此参数需要一个 HTML 图像元素,而不是 URL。在 Node 中运行时,此参数需要图像的路径。


推荐阅读