javascript - 在Typescript中子类化Fabric JS类型反序列化时导致错误
问题描述
我扩展了 Fabric Js 默认类型:
export class Button extends fabric.Rect {
public text: string;
public textColor: string;
public font = '30px Arial';
public action: ActionEnum;
public type = 'button';
constructor(options: IButttonOptions) {
super(options);
if (options.font) {
this.font = options.font;
}
this.text = options.text;
this.textColor = options.textColor;
this.action = options.action;
}
toDatalessObject(propertiesToInclude?: string[]): any {
propertiesToInclude = (propertiesToInclude || []).concat(
['text', 'textColor', 'font', 'action']
);
return super.toDatalessObject(propertiesToInclude);
}
_render(ctx: CanvasRenderingContext2D) {
super._render(ctx);
console.log('test');
ctx.font = this.font;
ctx.fillStyle = this.textColor;
ctx.textAlign = 'center';
ctx.fillText(this.text, 0, 10);
}
我想要像这样序列化/反序列化画布:
var json = canvas.toDatalessJSON();
canvas.clear();
canvas.loadFromJSON(json); // cause error
但是当我想用上面定义的对象加载画布时,我有异常“klass is undefined”
我不知道如何解决这个问题。有人可以帮忙吗?
解决方案
推荐阅读
- sql - 在 SQL 的 where 子句中获取多个中间语句的多列
- asp.net-core - IdentityServer4.AccessTokenValidation 是否执行自动用户信息自检?
- c# - 在移动检查时连接四个控制台应用程序滞后
- android - CGAL-for-Android 交叉编译中的问题
- javascript - 在嵌套组件中反应组件道具
- r - 在 R 中使用条件概率分析学生的测试结果
- machine-learning - 为 CNN 构建二元分类器的正确方法
- wordpress - 内置 WP_USERS 与自定义用户表
- python - 无法继承正确的父类,初始化需要 1 个位置参数,但给出了 2 个
- python - 使用 update() 方法时,Django ImageField 不更新