在组上添加一个圆圈,单击未正确显示 FabricJS
add a circle on a group on click not appearing correctly with fabricjs
我有以下代码用于名为 App 的 cusomt 对象
App.prototype.createCanvas = function (id){
var domElement, canvas;
domElement = document.getElementById(id);
this.imageUrl = domElement.getAttribute('data-url');
this.canvas = new fabric.Canvas(id);
this.canvas.setWidth(800);
this.canvas.setHeight(600);
this.loadMainImage();
};
App.prototype.loadMainImage = function(){
var width, height, aspectRatio;
var _that = this;
fabric.Image.fromURL(this.imageUrl, function(oImg){
_that.mainImage = oImg;
console.log('loading image');
if (_that.mainImage.getWidth() > _that.mainImage.getHeight()){
_that.mainImage.scaleToWidth(600);
}else{
_that.mainImage.scaleToHeight(600);
}
_that.group = new fabric.Group([_that.mainImage]);
_that.group.hasRotatingPoint = false;
//_that.group.add(oImg);
_that.group.selectable = true;
_that.group.angle = 0;
_that.canvas.add(_that.group);
_that.group.center();
_that.group.setCoords();
_that.canvas.renderAll();
console.log(_that.group.getTop());
console.log(_that.group.getLeft());
console.log(_that.group.getWidth());
console.log(_that.group.getHeight());
});
};
App.prototype.startCalibration = function(){
console.log("circle A:"+ this.circleA);
console.log("circle B"+ this.circleB);
var _that = this;
alert("Click on xray to add two points for calibrations");
var distance;
this.pointA = null;
this.pointB = null;
if (this.calibrate){
setPoints = function (e){
var event = e.e;
console.log(this.group.getLocalPointer(event));
if (this.pointA == null){
this.pointA = {
x: this.group.getLocalPointer(event).x,
y: this.group.getLocalPointer(event).y
};
console.log("pointA:{x:"+this.pointA.x+",y:"+this.pointA.y+"}");
this.circleA = new fabric.Circle({
radius:5,
fill:"red",
left:this.pointA.x,
top: this.pointA.y
});
console.log("circleA top"+this.circleA.getTop());
console.log("circleA left"+this.circleA.getLeft());
this.group.addWithUpdate(this.circleA);
}else if (this.pointB == null) {
this.pointB = {
x: this.group.getLocalPointer(event).x,
y: this.group.getLocalPointer(event).y
};
this.circleB = new fabric.Circle({
radius:5,
fill:"red",
top:this.pointB.y,
left: this.pointB.x
});
this.group.addWithUpdate(this.circleB);
}
this.canvas.renderAll();
if (this.pointB !==null && this.pointA !== null){
$('#distanceValue').attr('disabled', false);
}
}.bind(this);
this.group.selectable = false;
this.canvas.renderAll();
this.group.on('mouseup', setPoints);
}
};
最后一个函数由外部 JavaScript 文件调用。我想做的是添加到组中加载到画布上的图像上的点,并校准点之间的距离。用户将提供点之间的物理距离,程序将进行校准。圆圈只是为了提供视觉效果,但它们不会出现在我单击的位置。控制台的输出.log为
groupTop: 150
groupLeft: 100
groupWidth:600
groupHeight:300
circle A:null
circle B: null
Object {x: 37, y: 28}
pointA:{x:37,y:28}
circleA top 37
circleA left 28
因此,尽管圆的顶部和左侧对组是正确的,但坐标圆不会按预期显示。它们看起来更靠右,但高度相同。我应该改用 originX 和 originY 吗?
如果可以在此处解决或打开一个新问题,第二个问题是删除功能不起作用
自1.4.0版本以来,FabricJS使用左上角进行定位。
如果你想让圆圈准确地出现在你点击的位置,首先要尝试的是添加到圆圈选项"originX: 'center', originY: 'center'"
因此,圆圈将以您单击的点为中心。
相关文章:
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 单击F5时如何停止页面加载
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 单击jquery清除输入值
- 单击按钮以等待单击按钮
- 在单击href链接的同时下载文件
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 使图像在单击时展开到不大于浏览器
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何在单击复选框后调用控制器方法
- 单击页面上的链接后高度发生变化
- 使用jquery在单击时在单元格中输入值
- 将纯文本URL转换为可单击链接
- ASP.NET通过单击JavaScript按钮触发c#事件
- 单击更改图标并通过javascript添加一个css类
- 阻止在select2单击时调用ajax
- 在组上添加一个圆圈,单击未正确显示 FabricJS
- 我如何选择和拖动不同的对象,而不是在fabricjs中单击的对象