javascript - 使用 Fabric 拖放在 Angular 中不起作用
问题描述
我正在尝试将 Fabric.js 用于我的图像编辑器。
将图像从加载区域拖放到画布上。
这是我的 HTML 代码
<div class="panel panel-default panel-upload">
<div class="panel-heading">Upload Picture</div>
<div class="panel-body text-center">
<img id="testImage" draggable="true" class="images-item-upload" *ngFor='let url of urls' [src]="url?.url" (click)="addImageOnCanvas(url.url);"/>
<label class="btn btn-default btn-block file-container">
<i class="fa fa-fw fa-upload"></i> Choose a file
<input type='file' [disabled]="urls.length >= 16" multiple (change)="readUrl($event);">
</label>
</div>
</div>
这是我的 ts 代码
addImageOnCanvas(urls): void {
if (urls) {
fabric.Image.fromURL(urls, (image) => {
image.set({
left: 10,
top: 10,
angle: 0,
padding: 10,
cornersize: 10,
hasRotatingPoint: true,
title: this.urlName
});
image.scaleToWidth(Math.round(this.size.width / 2));
this.extend(image, this.randomId());
this.canvas.add(image);
this.selectItemAfterAdded(image);
});
this.updateLayers();
}
}
当我尝试拖放时出现错误
Cannot read property 'src' of undefined
at EditorComponent.push.xD4D.EditorComponent.handleDropOnCanvas (editor.component.ts:1036)
接下来我可以尝试什么?
解决方案
为什么不检查传递给回调的第二个参数?
Second argument is a boolean indicating if an error occurred or not.
http://fabricjs.com/docs/fabric.Image.html#.fromURL
我会从那里开始。
推荐阅读
- java - 将 compareTo 与 arrayList 一起使用的问题
- python-3.x - 函数中的“如果语句”基于 while 循环设置持续时间输出太多字符
- java - 我并行配置了 TestNG 测试,但它不起作用
- node.js - 为什么要选择独立的 socket.io 服务器 vs. Express vs. node http?
- asp.net - 如何让 ASP.NET 网站和 ASP.NET Core 网站在同一台服务器上工作
- git - 有没有办法根据项目文件夹/文件的最后修改时间提交?
- python - 根据熊猫中的不同列条件选择移位的列值
- python - 如何在气流中基于另一个 AWS-glue 任务成功完成在气流中启动 python 操作员 boto3 AWS-glue 任务?
- r - 使用交叉验证功能的 Knn
- python - SyntaxError:在将 matlab 代码转换为 Python 时无法分配给函数调用