javascript - 调整 Canvas 上图像大小的问题

标签 javascript html canvas

我试图在 Canvas 中导入图像,但生成的图像格式错误( Canvas 和图像应为 300x300 像素)。 如何强制 Canvas 及其图像达到这些尺寸?

const createImage = (articleNumber, description, name, dateCreated, src) =>
      new Promise(resolve => {
        const canvas = document.createElement('canvas')
        canvas.style.width = '300px'
        canvas.style.height = '300px'
        // canvas.style.display = 'none'
        document.body.appendChild(canvas)
        const ctx = canvas.getContext('2d')

        const imgBase = new Image()
        imgBase.src = 'http://makepixelart.com/pixelpixapp/celebs/kirk.PNG'

        imgBase.onload = () => {
          ctx.drawImage(imgBase, 0, 0, 300, 300)
          ctx.fillStyle = '#000000'
          ctx.font = 'bold 50px Arial'
          ctx.fillText(articleNumber, 475, 225)
          ctx.fillText(description, 475, 380)
          ctx.fillText(name, 475, 495)
          ctx.fillText(dateCreated, 475, 610)
          const imgFigure = new Image()
          imgFigure.src = src
          imgFigure.onload = () => {
            ctx.drawImage(imgFigure, 40, 130, 390, 470)
            resolve(canvas.toDataURL())
          }
        }
      })
    createImage('xyz', 'desc', 'name item', '20170101', 'https://i.pinimg.com/736x/3e/23/92/3e2392ecff7d230cc8ae82e462f2a690--pixel-art-templates-change-background.jpg').then(base64 => {
      console.log(base64)
    })

最佳答案

const createImage = (articleNumber, description, name, dateCreated, src) =>
  new Promise(resolve => {
    const canvas = document.createElement('canvas')
    canvas.width = 300;
    canvas.height = 300;
    // canvas.style.display = 'none'
    document.body.appendChild(canvas)
    const ctx = canvas.getContext('2d')

    const imgBase = new Image()
    imgBase.src = 'http://makepixelart.com/pixelpixapp/celebs/kirk.PNG'

    imgBase.onload = () => {
      ctx.drawImage(imgBase, 0, 0, 300, 300)
      ctx.fillStyle = '#000000'
      ctx.font = 'bold 50px Arial'
      ctx.fillText(articleNumber, 475, 225)
      ctx.fillText(description, 475, 380)
      ctx.fillText(name, 475, 495)
      ctx.fillText(dateCreated, 475, 610)
      const imgFigure = new Image()
      imgFigure.src = src
      imgFigure.onload = () => {
        ctx.drawImage(imgFigure, 40, 130, 390, 470)
        resolve(canvas.toDataURL())
      }
    }
  })
createImage('xyz', 'desc', 'name item', '20170101', 'https://i.pinimg.com/736x/3e/23/92/3e2392ecff7d230cc8ae82e462f2a690--pixel-art-templates-change-background.jpg').then(base64 => {
  console.log(base64)
})

使用此设置 Canvas 的宽度和高度

canvas.width = 300;
canvas.height = 300;

关于javascript - 调整 Canvas 上图像大小的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47090839/

相关文章:

javascript - Google Drive API 和从浏览器上传文件

html - IE6 : How to get inline base64 images to work with IE6?

javascript - 如何使用 fabric.js Canvas 切换/选择/删除自定义形状

Javascript 检查 url 中是否没有有效方案

javascript - MVC - Html.Action 使用 Javascript 检索元素,然后将其作为参数传递给 Controller,然后返回 PartialView

javascript - 从 Node.js 订阅 SalesForce 主题时出错

html - asp.net 如何使用超链接将数据从一页显示到另一页

javascript - 我怎样才能让我网站上的这个按钮重定向到网站上的另一个页面?

Javascript:在 Canvas 上绘制矩形在 IE 上不起作用

Android,将位图绘制到 Canvas 的最快方法