【发布时间】:2014-04-14 18:28:11
【问题描述】:
我正在画布上绘制一些对象(例如矩形)。它也有背景图片。
但是当我使用鼠标事件绘制矩形时,屏幕会闪烁很多。 当我改变对象的位置时,如何停止鼠标移动/鼠标向下的闪烁,以便一次又一次地重新绘制画布。 我们可以渲染它一段时间或任何其他解决方案吗?
我听说在 java 和 C# 中有一些函数为“Redraw = false”,但我的代码是在 JavaScript 中。
http://jsfiddle.net/G6tLn/7/`function myMove(e) { 如果(isDrag){ getMouse(e);
mySel.x = mx - offsetx;
mySel.y = my - offsety;
// something is changing position so we better invalidate the canvas!
invalidate();
} else if (isResizeDrag) {
// time ro resize!
var oldx = mySel.x;
var oldy = mySel.y;
// 0 1 2
// 3 4
// 5 6 7
switch (expectResize) {
case 0:
mySel.x = mx;
mySel.y = my;
mySel.w += oldx - mx;
mySel.h += oldy - my;
break;
case 1:
mySel.y = my;
mySel.h += oldy - my;
break;
case 2:
mySel.y = my;
mySel.w = mx - oldx;
mySel.h += oldy - my;
break;
case 3:
mySel.x = mx;
mySel.w += oldx - mx;
break;
case 4:
mySel.w = mx - oldx;
break;
case 5:
mySel.x = mx;
mySel.w += oldx - mx;
mySel.h = my - oldy;
break;
case 6:
mySel.h = my - oldy;
break;
case 7:
mySel.w = mx - oldx;
mySel.h = my - oldy;
break;
}
invalidate();
}
getMouse(e);
// if there's a selection see if we grabbed one of the selection handles
if (mySel !== null && !isResizeDrag) {
for (var i = 0; i < 8; i++) {
// 0 1 2
// 3 4
// 5 6 7
var cur = selectionHandles[i];
// we dont need to use the ghost context because
// selection handles will always be rectangles
if (mx >= cur.x && mx <= cur.x + mySelBoxSize &&
my >= cur.y && my <= cur.y + mySelBoxSize) {
// we found one!
expectResize = i;
invalidate();
switch (i) {
case 0:
this.style.cursor = 'nw-resize';
break;
case 1:
this.style.cursor = 'n-resize';
break;
case 2:
this.style.cursor = 'ne-resize';
break;
case 3:
this.style.cursor = 'w-resize';
break;
case 4:
this.style.cursor = 'e-resize';
break;
case 5:
this.style.cursor = 'sw-resize';
break;
case 6:
this.style.cursor = 's-resize';
break;
case 7:
this.style.cursor = 'se-resize';
break;
}
return;
}
}
// not over a selection box, return to normal
isResizeDrag = false;
expectResize = -1;
this.style.cursor = 'auto';
}
}
`
【问题讨论】:
-
编辑了问题。请帮助停止由于鼠标移动而导致的闪烁
-
我试过你的小提琴,它没有闪烁。我正在使用 Chrome。
-
在小提琴中它没有闪烁,但是当我将它添加到我的应用程序时。当我改变矩形的位置时它会闪烁。
标签: javascript jquery canvas