如何使画布对象(矩形)的选择在mouse:down事件之后始终处于活动状态?我尝试过使用subTargetCheck:true属性,但该属性不符合上述要求。
this.fabricObject = new fabric.Rect({
left: this.objectSize.left,
top: this.objectSize.top,
width: this.objectSize.width,
height: this.objectSize.height,
fill: '#ccc',
padding: 10,
subTargetCheck: true
});
this.canvas.add(this.fabricObject);
this.fabricObject.center().setCoords();
this.canvas.renderAll();用于set始终活动的=>代码
this.canvas.on('mouse:down', (e) => {
console.log(e.target)
this.canvas.getObjects().map((object:any) => {
object.set('active', true);
this.canvas.renderAll();
})
});=>,如果我这样做的颜色填充,那么它是运行良好的鼠标出事件。
this.canvas.on('mouse:down', (e) => {
console.log(e.target)
this.canvas.getObjects().map((object:any) => {
object.set('fill', 'green');
this.canvas.renderAll();
})
});参考文献:https://jsfiddle.net/jasie/r3fqu4p6/12/
就像在琴键中,当点击正方形时,它将处于活动状态,而当单击方块的外侧时,它将停用.所以我想要激活的同时点击外面的方块。
发布于 2020-03-05 19:52:34
您需要使用canvas.setActiveObject()来手动设置画布上的活动对象。
var canvas = new fabric.Canvas("canvas");
var ourRect = new fabric.Rect({
left: 100,
top: 100,
width: 75,
height: 50,
fill: "green",
padding: 10,
subTargetCheck: true
});
canvas.add(ourRect);
canvas.on("mouse:down", object => {
canvas.setActiveObject(ourRect);
});请查看此CodeSansbox演示:https://codesandbox.io/s/stackoverflow-60522500-fabric-js-1720-ekocp
https://stackoverflow.com/questions/60522500
复制相似问题