Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调整画布上图像的大小时出现问题_Javascript_Html_Canvas - Fatal编程技术网

Javascript 调整画布上图像的大小时出现问题

Javascript 调整画布上图像的大小时出现问题,javascript,html,canvas,Javascript,Html,Canvas,我试图在画布中导入图像,但结果图像格式错误(画布和图像应为300x300像素)。 如何强制画布在这些尺寸上显示其图像 const createImage=(articlernumber、description、name、dateCreated、src)=> 新承诺(解决=>{ const canvas=document.createElement('canvas') canvas.style.width='300px' canvas.style.height='300px' //canvas.

我试图在画布中导入图像,但结果图像格式错误(画布和图像应为300x300像素)。 如何强制画布在这些尺寸上显示其图像

const createImage=(articlernumber、description、name、dateCreated、src)=>
新承诺(解决=>{
const canvas=document.createElement('canvas')
canvas.style.width='300px'
canvas.style.height='300px'
//canvas.style.display='none'
document.body.appendChild(画布)
const ctx=canvas.getContext('2d')
const imgBase=新映像()
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(条款号475225)
ctx.fillText(说明,475380)
ctx.fillText(名称475495)
ctx.fillText(创建日期,475610)
const imgFigure=新图像()
imgFigure.src=src
imgFigure.onload=()=>{
ctx.drawImage(图40、130、390、470)
解析(canvas.toDataURL())
}
}
})
createImage('xyz','desc','name item','20170101','https://i.pinimg.com/736x/3e/23/92/3e2392ecff7d230cc8ae82e462f2a690--pixel-art-templates-change-background.jpg)。然后(base64=>{
console.log(base64)
})
const createImage=(articlernumber、description、name、dateCreated、src)=>
新承诺(解决=>{
const canvas=document.createElement('canvas')
画布宽度=300;
帆布高度=300;
//canvas.style.display='none'
document.body.appendChild(画布)
const ctx=canvas.getContext('2d')
const imgBase=新映像()
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(条款号475225)
ctx.fillText(说明,475380)
ctx.fillText(名称475495)
ctx.fillText(创建日期,475610)
const imgFigure=新图像()
imgFigure.src=src
imgFigure.onload=()=>{
ctx.drawImage(图40、130、390、470)
解析(canvas.toDataURL())
}
}
})
createImage('xyz','desc','name item','20170101','https://i.pinimg.com/736x/3e/23/92/3e2392ecff7d230cc8ae82e462f2a690--pixel-art-templates-change-background.jpg)。然后(base64=>{
console.log(base64)
})
const createImage=(articlernumber、description、name、dateCreated、src)=>
新承诺(解决=>{
const canvas=document.createElement('canvas')
画布宽度=300;
帆布高度=300;
//canvas.style.display='none'
document.body.appendChild(画布)
const ctx=canvas.getContext('2d')
const imgBase=新映像()
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(条款号475225)
ctx.fillText(说明,475380)
ctx.fillText(名称475495)
ctx.fillText(创建日期,475610)
const imgFigure=新图像()
imgFigure.src=src
imgFigure.onload=()=>{
ctx.drawImage(图40、130、390、470)
解析(canvas.toDataURL())
}
}
})
createImage('xyz','desc','name item','20170101','https://i.pinimg.com/736x/3e/23/92/3e2392ecff7d230cc8ae82e462f2a690--pixel-art-templates-change-background.jpg)。然后(base64=>{
console.log(base64)
})
此解析(canvas.toDataURL())无效(跨域安全错误)。从画布宽度和高度设置时,不需要样式。也不需要“px”,只需:

    canvas.width = '300'
    canvas.height = '300'
const createImage=(articlernumber、description、name、dateCreated、src)=>
新承诺(解决=>{
const canvas=document.createElement('canvas')
canvas.width='300'
canvas.height='300'
//canvas.style.display='none'
document.body.appendChild(画布)
const ctx=canvas.getContext('2d')
const imgBase=新映像()
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(条款号475225)
ctx.fillText(说明,475380)
ctx.fillText(名称475495)
ctx.fillText(创建日期,475610)
const imgFigure=新图像()
imgFigure.src=src
imgFigure.onload=()=>{
ctx.drawImage(图40、130、390、470)
解析(canvas.toDataURL())
}
}
})
createImage('xyz','desc','name item','20170101','https://i.pinimg.com/736x/3e/23/92/3e2392ecff7d230cc8ae82e462f2a690--pixel-art-templates-change-background.jpg)。然后(base64=>{
console.log(base64)
})
此解析(canvas.toDataURL())无效(跨域安全错误)。从画布宽度和高度设置时,不需要样式。也不需要“px”,只需:

    canvas.width = '300'
    canvas.height = '300'
const createImage=(articlernumber、description、name、dateCreated、src)=>
新承诺(解决=>{
const canvas=document.createElement('canvas')
canvas.width='300'
canvas.height='300'
//canvas.style.display='none'
document.body.appendChild(画布)
const ctx=canvas.getContext('2d')
const imgBase=新映像()
imgBase.src=http://makepixelart.com/pixelpixapp/celebs/kirk.PNG'
imgBase.onload=()=>{
ctx.drawImage(imgBase,0,0,300,300)
ctx.drawImage(imgFigure, 40, 130, 300, 300)