Javascript 需要CanvasRenderingContext2D作为父对象才能运行

Javascript 需要CanvasRenderingContext2D作为父对象才能运行,javascript,function,canvas,html5-canvas,parent,Javascript,Function,Canvas,Html5 Canvas,Parent,有没有办法使函数需要CanvasRenderingContext2D对象作为父对象?我想用它来创建额外的CanvasRenderingContext2D函数,就像 CanvasRenderingContext2D.__proto__.strokeRoundedRect = ( x, y, width, height, cornerRadius ) => { // Code here... } 编辑: 如果我运行let context=document.getElementBy

有没有办法使函数需要CanvasRenderingContext2D对象作为父对象?我想用它来创建额外的CanvasRenderingContext2D函数,就像

CanvasRenderingContext2D.__proto__.strokeRoundedRect = ( x, y, width, height, cornerRadius ) => {

    // Code here...

}
编辑:

如果我运行let context=document.getElementById canvas.getContext 2d;context.strokeRoundedRect 0,0,0,0,0;,系统提示我出现此错误Uncaught TypeError:context.strokeRoundedRect不是函数


显然,这个函数不存在,那么有什么方法可以实现它呢

我很抱歉突然问了一个问题。在发布此帖子之前,我没有彻底研究这个问题

对于任何有兴趣解决此问题的人,请阅读此评论

我发现,通过使用document.getElementsByTagName画布所需的所有画布的迭代,可以将所需的函数分配给每个画布的。我仍然不确定是否有更好的方法来实现这一点,但在下面您将找到原始JavaScript

let xcd = {};

xcd.initialize = ( context ) => {

    context.strokeRoundedRect = ( x, y, width, height, cornerRadius ) => {

        console.log( "strokeRoundedRect:: ", "X: " + x, "Y: " + y, "Width: " + width, "Height: " + height, "Corner radius: " + cornerRadius );

    }

    context.fillRoundedRect = ( x, y, width, height, cornerRadius ) => {

        console.log( "fillRoundedRect:: ", "X: " + x, "Y: " + y, "Width: " + width, "Height: " + height, "Corner radius: " + cornerRadius );

    }

}

// Get HTMLCollection of all canvases
let canvases = document.getElementsByTagName( "canvas" );

for( let i = 0; i < canvases.length; i++)

    xcd.initialize( canvases[ i ].getContext( "2d" ) );

你当前的代码有什么问题?我编辑了这篇文章^^^