安装 npm 时出错 canvas

error while installing npm canvas

我正在尝试使用这个存储库: https://github.com/lokesh/color-thief/

在尝试使用它时我意识到我需要安装 npm canvas 包。 我去了https://www.npmjs.com/package/canvas, but that didn't work so I went to the Installation Wiki at https://github.com/Automattic/node-canvas/wiki/Installation---Windows.

我已经按照说明安装了 Python 2.7、Express Visual Studio 软件包和 GTK 2。我还在 cmd 中 运行 npm install -g node-gyp,它似乎没有错误。

当我尝试 运行 npm install canvas 之后,我仍然遇到错误。我在这里附上 npm-debug.log:

http://www.filedropper.com/npm-debug

翻阅图书馆,我发现了以下信息:

  1. 您需要使用图像对象来加载主色。
  2. 这里你真的不需要 NodeJS,它可以直接在网站上运行
  3. 你只需要 5 个 LOC 就可以使用它。

这是最简单的代码片段:

var image = new Image; //create the image object
image.onload = function () {
    var colorThief = new ColorThief(); //create the lib's object
    var dominantColor = colorThief.getColor(image); //get the dominant color
}
image.src = "source/to/image";
  • 主色getColor()函数将return一个RGB数组([R, G, B])

您可以查看 color-thief.js 文件以了解所有功能 - 此文件有详细的文档记录。