我只想添加自定义标签属性到现有的图像对象与_render。fabric.Image.prototype具有_render方法,因此我从fabric.js库中复制并将其覆盖为
fabric.util.object.extend(fabric.Image.prototype, {
_render: function(ctx, noTransform) {
var x, y, imageMargins = this._findMargins(), elementToDraw;
x = (noTransform ? this.left : -this.width / 2);
y = (noTransform ? this.top : -this.height / 2);
if (this.meetOrSlice === 'slice') {
ctx.beginPath();
ctx.rect(x, y, this.width, this.height);
ctx.clip();
}
if (this.isMoving === false && this.resizeFilters.length && this._needsResize()) {
this._lastScaleX = this.scaleX;
this._lastScaleY = this.scaleY;
elementToDraw = this.applyFilters(null, this.resizeFilters, this._filteredEl || this._originalElement, false);
}else {
elementToDraw = this._element;
}
elementToDraw && ctx.drawImage(elementToDraw,
x + imageMargins.marginX,
y + imageMargins.marginY,
imageMargins.width,
imageMargins.height
);
this._renderStroke(ctx);
// Custom code added
ctx.font = '12px Ubuntu';
ctx.fillStyle = 'grey';
ctx.style = '1px dashed';
ctx.fillText(this.label, this.width/2-10, -(this.height/2) - 10);
}
}); 我知道这不是正确的做法。有没有一种方法可以扩展当前的_render函数并只添加
ctx.font = '12px Ubuntu';
ctx.fillStyle = 'grey';
ctx.style = '1px dashed';
ctx.fillText(this.label, this.width/2-10, -(this.height/2) - 10);使用JavaScript原型继承
任何帮助都是好的
发布于 2015-06-12 19:45:29
在扩展类中,首先在被覆盖的_render函数中添加一个callSuper,然后添加您自己的代码。像这样..。
_render: function(ctx) {
this.callSuper('_render', ctx);
/* your code */
}https://stackoverflow.com/questions/27265245
复制相似问题