Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update area clearing during rendering #3016

Merged
merged 12 commits into from
May 30, 2016
55 changes: 29 additions & 26 deletions src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
}
},
onChange: function() {
if (obj.canvas) {
// we do not want to animate a selection, only cursor
if (obj.canvas && obj.selectionStart === obj.selectionEnd) {
obj.renderCursorOrSelection();
}
},
Expand Down Expand Up @@ -145,17 +146,8 @@
var _this = this,
delay = restart ? 0 : this.cursorDelay;

this._currentTickState && this._currentTickState.abort();
this._currentTickCompleteState && this._currentTickCompleteState.abort();
clearTimeout(this._cursorTimeout1);
this.abortCursorAnimation();
this._currentCursorOpacity = 1;
if (this.canvas) {
this.canvas.clearContext(this.canvas.contextTop || this.ctx);
this.renderCursorOrSelection();
}
if (this._cursorTimeout2) {
clearTimeout(this._cursorTimeout2);
}
this._cursorTimeout2 = setTimeout(function() {
_this._tick();
}, delay);
Expand All @@ -172,6 +164,8 @@
clearTimeout(this._cursorTimeout2);

this._currentCursorOpacity = 0;
// to clear just itext area we need to transform the context
// it may not be worth it
this.canvas && this.canvas.clearContext(this.canvas.contextTop || this.ctx);
},

Expand Down Expand Up @@ -382,22 +376,30 @@
* Initializes "mousemove" event handler
*/
initMouseMoveHandler: function() {
var _this = this;
this.canvas.on('mouse:move', function(options) {
if (!_this.__isMousedown || !_this.isEditing) {
return;
}
this.canvas.on('mouse:move', this.mouseMoveHandler.bind(this));
},

var newSelectionStart = _this.getSelectionStartFromPointer(options.e);
if (newSelectionStart >= _this.__selectionStartOnMouseDown) {
_this.setSelectionStart(_this.__selectionStartOnMouseDown);
_this.setSelectionEnd(newSelectionStart);
}
else {
_this.setSelectionStart(newSelectionStart);
_this.setSelectionEnd(_this.__selectionStartOnMouseDown);
}
});
/**
* @private
*/
mouseMoveHandler: function(options) {
if (!this.__isMousedown || !this.isEditing) {
return;
}

var newSelectionStart = this.getSelectionStartFromPointer(options.e);
if (newSelectionStart === this.__selectionStartOnMouseDown) {
return;
}
if (newSelectionStart > this.__selectionStartOnMouseDown) {
this.setSelectionStart(this.__selectionStartOnMouseDown);
this.setSelectionEnd(newSelectionStart);
}
else {
this.setSelectionStart(newSelectionStart);
this.setSelectionEnd(this.__selectionStartOnMouseDown);
}
this.renderCursorOrSelection();
},

/**
Expand Down Expand Up @@ -510,6 +512,7 @@
this.fire('editing:exited');
isTextChanged && this.fire('modified');
if (this.canvas) {
this.canvas.off('mouse:move', this.mouseMoveHandler);
this.canvas.fire('text:editing:exited', { target: this });
isTextChanged && this.canvas.fire('object:modified', { target: this });
}
Expand Down
13 changes: 9 additions & 4 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
this.oldWidth = this.width;
this.oldHeight = this.height;
this.callSuper('_render', ctx);
this.ctx = ctx;
this.isEditing && this.renderCursorOrSelection();
Expand All @@ -308,7 +310,7 @@
* Renders cursor or selection (depending on what exists)
*/
renderCursorOrSelection: function() {
if (!this.active) {
if (!this.active || !this.isEditing) {
return;
}

Expand All @@ -321,12 +323,12 @@
ctx.transform.apply(ctx, this.canvas.viewportTransform);
this.transform(ctx);
this.transformMatrix && ctx.transform.apply(ctx, this.transformMatrix);
this._clearTextArea(ctx);
}
else {
ctx = this.ctx;
ctx.save();
}

if (this.selectionStart === this.selectionEnd) {
boundaries = this._getCursorBoundaries(chars, 'cursor');
this.renderCursor(boundaries, ctx);
Expand All @@ -339,6 +341,11 @@
ctx.restore();
},

_clearTextArea: function(ctx) {
// we add 4 pixel, to be sure to do not leave any pixel out
var width = this.oldWidth + 4, height = this.oldHeight + 4;
ctx.clearRect(-width / 2, -height / 2, width, height);
},
/**
* Returns 2d representation (lineIndex and charIndex) of cursor (or selection start)
* @param {Number} [selectionStart] Optional index. When not given, current selectionStart is used.
Expand Down Expand Up @@ -488,7 +495,6 @@

ctx.fillStyle = this.getCurrentCharColor(lineIndex, charIndex);
ctx.globalAlpha = this.__isMousedown ? 1 : this._currentCursorOpacity;
ctx.clearRect(-this.width / 2, -this.height / 2, this.width, this.height);
ctx.fillRect(
boundaries.left + leftOffset,
boundaries.top + boundaries.topOffset,
Expand All @@ -510,7 +516,6 @@
end = this.get2DCursorLocation(this.selectionEnd),
startLine = start.lineIndex,
endLine = end.lineIndex;
ctx.clearRect(-this.width / 2, -this.height / 2, this.width, this.height);
for (var i = startLine; i <= endLine; i++) {
var lineOffset = this._getLineLeftOffset(this._getLineWidth(ctx, i)) || 0,
lineHeight = this._getHeightOfLine(this.ctx, i),
Expand Down
12 changes: 12 additions & 0 deletions test/unit/itext.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@
ok(!iText.isEditing);
});

/* test('enterEditing, exitEditing eventlistener counts', function() {
var iText = new fabric.IText('test');
canvas.add(iText);
equal(typeof iText.enterEditing, 'function');
equal(typeof iText.exitEditing, 'function');
iText.enterEditing();
var length = iText.canvas.__eventListeners["mouse:move"].length;
equal(iText.canvas.__eventListeners["mouse:move"], length);
iText.exitEditing();
equal(iText.canvas__eventListeners["mouse:move"].length, length - 1);
});*/

test('event firing', function() {
var iText = new fabric.IText('test'),
enter = 0, exit = 0, modify = 0;
Expand Down