https://stackoverflow.com/questions/36660108/how-to-create-custom-fabricjs-object

I have to create a custom fabricjs object(say, fabric.Demo) which extends fabric.Group object. fabric.Demo object will be used to grouped other two fabric.Group objects. I have searched on the Internet and found only these links as useful.

  1. Link 1
  2. Link 2

But I'm getting this error 'this._objects is undefined'. I know I haven't write _render(). But I don't understand that what to code in _render(). If anyone knows the answer, it will be appreciated.

Here is my code.

(function (global) {

    var fabric = global.fabric || (global.fabric = {}),
extend = fabric.util.object.extend,
clone = fabric.util.object.clone; var stateProperties = fabric.Text.prototype.stateProperties.concat();
stateProperties.push(
'top',
'left'
); fabric.Demo = fabric.util.createClass(fabric.Group, {
type: 'demo',
initialize: function () { this.grp = new fabric.Group([], {
selectable: false,
padding: 0
}); this.grp.add([
new fabric.Group([
new fabric.Text('A', {top: 200, left: 200}),
new fabric.Text('B', {top: 200, left: 200})
]),
new fabric.Group([
new fabric.Text('C', {top: 200, left: 200}),
new fabric.Text('D', {top: 200, left: 200})
])
]);
},
_render: function (ctx) { }
});
})(typeof exports !== 'undefined' ? exports : this);

 

---------------------------------------------------------------------------------------------------------------------------------------------------

3down voteaccepted

If you want to extend Group you have to respect its basic function, render a handfull of objects stored in the _objects array.

So when you initialize your class do not create a this.grp.

instead push your 2 groups inside a _objects array.

fabric.Demo = fabric.util.createClass(fabric.Group, {
type: 'demo',
initialize: function () { this.grp = new fabric.Group([], {
selectable: false,
padding: 0
}); this._objects.push(
new fabric.Group([
new fabric.Text('A', {top: 200, left: 200}),
new fabric.Text('B', {top: 200, left: 200})
]));
this._objects.push(
new fabric.Group([
new fabric.Text('C', {top: 200, left: 200}),
new fabric.Text('D', {top: 200, left: 200})
]));
}
});
})(typeof exports !== 'undefined' ? exports : this);

extend render functions thinking what you need different from standard group, and do not forget to put the fromObject function if you want to load and restore your canvas.

最新文章

  1. 冰冻三尺非一日之寒--web框架Django(三)
  2. PHP注册审核做法
  3. struts2将servlet对象注入到Action中
  4. Struts2拦截器原理以及实例
  5. 转 java 类 单例
  6. 菜鸟-手把手教你把Acegi应用到实际项目中(5)
  7. poj 3311(floyd+状态压缩)
  8. ps 快捷键
  9. Java Interface and Abstraction
  10. Kotlin封装RxJava与Retrofit
  11. 机器学习技法:14 Radial Basis Function Network
  12. Postman----模拟服务器返回数据
  13. 关于微信emoji 表情数据库存不了,或者显示为???的问题
  14. Clustering[Introduction]
  15. WARN conf.FlumeConfiguration: Could not configure sink sink1 due to: No channel configured for sink: sink1 org.apache.flume.conf.ConfigurationException: No channel configured for sink: sink1
  16. Win10下JDK环境变量的设置
  17. is_readable() 函数检查指定的文件是否可读。
  18. Linux下Tomcat的启动、关闭
  19. linux消息队列编程实例
  20. <dedecms>织梦内页调用会员信息

热门文章

  1. mysql 查询结果创建表
  2. Mybatis注解的使用
  3. WPF的webBrowser控件关键代码
  4. QML与Qt C++ 交互机制探讨与总结(转)
  5. VS2013 更改MFC标题栏图标和生成的执行文件图标
  6. 9.OpenStack安装web界面
  7. JAVA线程池调优
  8. centos7 挂载exfat格式的u盘
  9. PHP获取IP的方法
  10. Codeforces 1025D Recovering BST