Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 在画布的不同x、y位置重新绘制矩形_Javascript_Jquery_Html_Css_Canvas - Fatal编程技术网

Javascript 在画布的不同x、y位置重新绘制矩形

Javascript 在画布的不同x、y位置重新绘制矩形,javascript,jquery,html,css,canvas,Javascript,Jquery,Html,Css,Canvas,我在一个div上有一个画布,我试图在加载时在位置0,0绘制一个矩形,并在需要时将其移动到另一个位置x,y。我需要的x,y位置正在完美返回,我正在使用clearRect(0,0,canvas.width,canvas.height)方法在需要移动时清除画布,并再次使用fillRect(x,y,width,height)在这些特定位置重新绘制。然而,尽管x,y位置很好,并且正在调用fillRect(..)(我在chrome中调试过),但只有当我在位置0,0处重新绘制矩形时,才会删除并绘制矩形。否则它

我在一个div上有一个画布,我试图在加载时在位置0,0绘制一个矩形,并在需要时将其移动到另一个位置x,y。我需要的x,y位置正在完美返回,我正在使用
clearRect(0,0,canvas.width,canvas.height)
方法在需要移动时清除画布,并再次使用
fillRect(x,y,width,height)
在这些特定位置重新绘制。然而,尽管x,y位置很好,并且正在调用fillRect(..)(我在chrome中调试过),但只有当我在位置0,0处重新绘制矩形时,才会删除并绘制矩形。否则它只会删除。起初,我以为它正在被绘制,但可能div和canvas的分层正在丢失,但我将它放置在其他地方,不,这不是问题所在

这是我的代码,也许有人能看到我的代码有问题!谢谢

function removeCursor(connectionId) {
     var canvas = document.getElementById(connectionId);
     var ctx = canvas.getContext('2d');

     ctx.clearRect(0, 0, canvas.width, canvas.height); 
}

function paintCursor(x, y, connectionId, color) {
     var canvas = document.getElementById(connectionId);
     var context = canvas.getContext('2d');
     context.fillStyle = color;
     context.fillRect(x, y, 0.75, 5);
}

// the canvas is created on someone connecting to a specific page
if (someoneConnected) {
    var canvas = document.createElement("canvas");
    canvas.id = connectionId;
    canvas.className = 'canvases';
    canvas.style.zIndex = zindex;
    zindex++;

    var parentDiv = document.getElementById("editor-section");
    parentDiv.appendChild(canvas);

    paintCursor(0, 0, connectionId, color);

} else { // someone disconnected

    var canvas = document.getElementById(connectionId);
    canvas.parentNode.removeChild(canvas);
}
我对用户事件(如按键和单击)调用方法
removeCursor(connectionId)
paintCursor(x,y,connectionId,color)
。十、 Y是当前选择的坐标


你知道这里怎么了吗

你为什么不考虑一下

function rePaintCursor(x, y, connectionId, color) {
    var canvas = document.getElementById(connectionId);
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height); 
    context.fillStyle = color;
    context.fillRect(x, y, 0.75, 5);
}   

我的猜测是,如果x和y是正确的,那么执行顺序可能会妨碍您。

通过:“x,y是当前选择的坐标”,您是指光标坐标吗?X和Y是如何确定的,与什么元素有关?实际上这就是问题所在@Nikki!x和y坐标没有正确返回,因为它们是根据视口而不是内容可编辑div计算的。请查看此问题,也许您可以回答: