gpt4 book ai didi

javascript - 无法在 'CanvasRenderingContext2D 上执行 'drawImage'

转载 作者:行者123 更新时间:2023-12-04 17:42:08 25 4
gpt4 key购买 nike

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)' at drawCanvas (userscript.html?id=0e769594-f15d-490f-a75c-bac819525aab:56) at setTimeout (userscript.html?id=0e769594-f15d-490f-a75c-bac819525aab:59)



我正在尝试根据 url 输入将图像加载到 Canvas 上,我尝试添加事件监听器和记录器,但似乎都不起作用。
setTimeout(() => {

let startpanel = document.getElementById('startpanel')

// image for skin
var img = document.createElement("img");
img.style.width = "220px";
img.style.height = "177px";

// input custom skin
var skinInput = document.createElement("input");
skinInput.placeholder = "skin url";
skinInput.style.width = "150px";
skinInput.style.height = "35px";

skinInput.onblur = function() {
const newImage = new Image();
newImage.src = skinInput.value;
img.src = skinInput.value;
drawCanvas(newImage);
};

startpanel.appendChild(skinInput)

// draw skin
var appearance = document.createElement("canvas");
appearance.id = "appearance";
// appearance.style.position = "absolute";
appearance.style.top = "100px";
appearance.style.width = "200px";
appearance.style.height = "177px";
appearance.style.border = "1px solid #d3d3d3";
startpanel.appendChild(appearance)

const drawCanvas = (newImage) => {
var ctx = appearance.getContext("2d");
ctx.drawImage(newImage, 10, 10);
}

drawCanvas();

}, 3000);

最佳答案

我认为抛出此错误是因为您调用了自己的函数 "drawCanvas(newImage)"定义后没有参数: "drawCanvas()" .

因此,您的函数调用上下文函数 绘制图像 第一个参数为空: "ctx.drawImage(newImage, 10, 10)"

如果删除行 "drawCanvas()"错误不会被抛出。

关于javascript - 无法在 'CanvasRenderingContext2D 上执行 'drawImage',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com