在画布上调整图像大小的问题

时间:2017-11-03 07:49:30

标签: javascript html5 canvas

我正在尝试在画布中导入图像,但生成的图像格式错误(画布和图像应为300x300像素)。 如何在这些尺寸上强制使用画布和图像?



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)
    })




3 个答案:

答案 0 :(得分:0)



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.width = 300;
canvas.height = 300;

答案 1 :(得分:0)

此解析(canvas.toDataURL())不起作用(跨域安全性错误)。从画布宽度和高度设置时不需要样式。也不需要'px':

    canvas.width = '300'
    canvas.height = '300'

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)
    })

答案 2 :(得分:0)

如前所述,使用以下方法设置画布大小:

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

要将导入图像的大小设置为300 x 300,请调用第二个drawImage,如下所示:

ctx.drawImage(imgFigure, 40, 130, 300, 300)