Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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 使用JQuery在画布中绘制图像 $.fn.colorPicker=函数(){ const$div=这个; 常量$colorPickerIcon=$(“_Javascript_Jquery_Canvas - Fatal编程技术网

Javascript 使用JQuery在画布中绘制图像 $.fn.colorPicker=函数(){ const$div=这个; 常量$colorPickerIcon=$(“

Javascript 使用JQuery在画布中绘制图像 $.fn.colorPicker=函数(){ const$div=这个; 常量$colorPickerIcon=$(“,javascript,jquery,canvas,Javascript,Jquery,Canvas,我尝试了这段代码,经过多次更改后,我决定在这里提问以寻求解决方案。我在获取上下文方面遇到了问题。我总是收到这样一个错误:“TypeError:$canvas.getContext不是函数。”“。我使用文章对此进行了编程,但它仍然不起作用。您正在尝试访问jQuery对象的getContext-方法。但是getContext是一种HTMLCanvasElement方法,可以在$(“”)[0]下访问。getContext(“2d”)查看我所知的getContext(“2d”)的可能重复项。”不是一个j

我尝试了这段代码,经过多次更改后,我决定在这里提问以寻求解决方案。我在获取上下文方面遇到了问题。我总是收到这样一个错误:“TypeError:$canvas.getContext不是函数。”“。我使用文章对此进行了编程,但它仍然不起作用。

您正在尝试访问jQuery对象的
getContext
-方法。但是
getContext
是一种HTMLCanvasElement方法,可以在
$(“”)[0]下访问。getContext(“2d”)

查看我所知的
getContext(“2d”)的可能重复项。”
不是一个
jQuery
函数,而是一个
JavaScript
。为了获得所需内容,请使用
const$context=$canvas[0]
。当您
控制台.log($('[0])
时,您可以在
\uuu proto\uuu
方法中找到它
$.fn.colorPicker = function () {
    const $div = this;
    const $colorPickerIcon = $("<img></img");
    const $canvas = $("<canvas></canvas>").addClass("canvas");
    const $context = $canvas.getContext("2d");
    const $closeButton = new Image();
    $closeButton.src = "./imgs/close.png";
    $($colorPickerIcon).attr("src", "./imgs/icon.jpg").attr("id", "icon").appendTo($div);
    $($colorPickerIcon).on("click", function () {
        $($colorPickerIcon).hide();
        $($closeButton).on("load", function () {
            $context.drawImage($closeButton, 0, 0);
        });
    });
}