首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >fabric.js扩展fabric.Image.prototype_render子类

fabric.js扩展fabric.Image.prototype_render子类
EN

Stack Overflow用户
提问于 2014-12-03 14:40:41
回答 1查看 1.2K关注 0票数 3

我只想添加自定义标签属性到现有的图像对象与_render。fabric.Image.prototype具有_render方法,因此我从fabric.js库中复制并将其覆盖为

代码语言:javascript
运行
复制
       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函数并只添加

代码语言:javascript
运行
复制
  ctx.font = '12px Ubuntu';
  ctx.fillStyle = 'grey';
  ctx.style = '1px dashed';
  ctx.fillText(this.label, this.width/2-10, -(this.height/2) - 10);

使用JavaScript原型继承

任何帮助都是好的

EN

回答 1

Stack Overflow用户

发布于 2015-06-12 19:45:29

在扩展类中,首先在被覆盖的_render函数中添加一个callSuper,然后添加您自己的代码。像这样..。

代码语言:javascript
运行
复制
_render: function(ctx) {
  this.callSuper('_render', ctx);
  /* your code */
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27265245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档