1

/Users/demo/canvas/node_modules/canvas/lib/image.js:91 SetSource.call(img, src); ^

Error: node-canvas was built without JPEG support
at setSource (/Users/demo/canvas/node_modules/canvas/lib/image.js:91:13)
at Image.set (/Users/demo/canvas/node_modules/canvas/lib/image.js:62:9)
at /Users/demo/canvas/node_modules/canvas/index.js:34:15
at new Promise ()
at loadImage (/Users/demo/canvas/node_modules/canvas/index.js:23:10)
at Object. (/Users/demo/canvas/index.js:19:1)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    const { createCanvas, loadImage } = require('canvas')
    const canvas = createCanvas(200, 200)
    const ctx = canvas.getContext('2d')
    ctx.font = '30px Impact'
    ctx.rotate(0.1)
    ctx.fillText('Awesome!', 50, 100)
    var text = ctx.measureText('Awesome!')
    ctx.strokeStyle = 'rgba(0,0,0,0.5)'
    ctx.beginPath()
    ctx.lineTo(50, 102)
    ctx.lineTo(50 + text.width, 102)
    ctx.stroke()
    loadImage('./2.jpg').then((image) => {
      ctx.drawImage(image, 50, 0, 70, 70)
    
      console.log('<img src="' + canvas.toDataURL() + '" />')
    })

环境: canvas@2.7.0 noede@15.14.0 macOS Big Sur 11.1 MacBook Pro(13英寸,M1,2020) Apple M1

4

1 回答 1

3

我遇到了同样的问题。幸运的是,在 2021 年 11 月 11 日(11 天前)已经解决了这个问题。

编辑(2022 年 1 月 25 日):

新的画布版本 (2.9.0) 已发布,因此该问题不应再出现。

npm install canvas@2.9.0

原始答案:

只需运行以下命令即可直接从 Github 安装画布。此提交是(今天)对 master 的最新提交,并明确修复了此问题。

npm install canvas@github:Automattic/node-canvas#198080580a0e3938c48daae357b88a1638a9ddcd

提交:

我使用的其他来源:

其他一些人建议也设置一些 ENV 变量,但在我的情况下这不是必需的。我将 MacBook Pro 与(Apple M1 Pro)一起使用。

我必须做的另一件事是安装画布依赖项以从源代码构建画布(您可能已经这样做了):

brew install pkg-config cairo pango libpng jpeg giflib librsvg libjpeg-turbo
于 2021-11-22T11:07:38.453 回答