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

Control customization part 4 - draw selection background #2950

Merged
merged 13 commits into from
May 8, 2016
22 changes: 22 additions & 0 deletions src/mixins/object_interactivity.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@
return fabric.util.transformPoint(new fabric.Point(w, h), vpt, true);
},

/**
* Draws a colored layer behind the object, inside its selection borders.
* Requires public options: padding, selectionBackgroundColor
* this function is called when the context is transformed
* @param {CanvasRenderingContext2D} ctx Context to draw on
* @return {fabric.Object} thisArg
* @chainable
*/
drawSelectionBackground: function(ctx) {
if (!this.selectionBackgroundColor || !this.active || this.group) {
return this;
}
ctx.save();
var center = this.getCenterPoint(), wh = this._calculateCurrentDimensions();
ctx.translate(center.x, center.y);
ctx.rotate(degreesToRadians(this.angle));
ctx.fillStyle = this.selectionBackgroundColor;
ctx.fillRect(-wh.x/2, -wh.y/2, wh.x, wh.y);
ctx.restore();
return this;
},

/**
* Draws borders of an object's bounding box.
* Requires public properties: width, height
Expand Down
9 changes: 9 additions & 0 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,14 @@
*/
backgroundColor: '',

/**
* Selection Background color of an object. colored layer behind the object when it is active.
* does not mix good with globalCompositeOperation methods.
* @type String
* @default
*/
selectionBackgroundColor: '',

/**
* When defined, an object is rendered via stroke and this property specifies its color
* @type String
Expand Down Expand Up @@ -1059,6 +1067,7 @@

//setup fill rule for current object
this._setupCompositeOperation(ctx);
this.drawSelectionBackground(ctx);
if (!noTransform) {
this.transform(ctx);
}
Expand Down
1 change: 1 addition & 0 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@
if (this._shouldClearCache()) {
this._initDimensions(ctx);
}
this.drawSelectionBackground(ctx);
if (!noTransform) {
this.transform(ctx);
}
Expand Down