简体   繁体   English

在Fabric.js中删除选定的对象矩形

[英]Removing selected object rectangle in Fabric.js

When I click a circle, the rectangle is created around of circle.How can I remove it? 单击圆时,将在圆周围创建矩形,如何将其删除? http://jsfiddle.net/yrL4eLsn/4/ http://jsfiddle.net/yrL4eLsn/4/

var canvas = new fabric.Canvas(document.getElementById('c'));

var rect = new fabric.Rect({
    width: 100,
    height: 100,
    left: 50,
    top: 50,
    fill: 'rgba(255,0,0,0.5)'
});
var circle = new fabric.Circle({
    radius: 50,
    left: 175,
    top: 75,
    fill: '#aac'
});


canvas.add(rect);
canvas.add(circle);

In FabricJS, the rectangle you are referring called the Border . 在FabricJS中,您引用的矩形称为Border On the corners of the border are smaller controls (squares) called the Controls that allow you to manipulate the object. 在边框的角上是称为控件的较小控件(正方形),可用于操作对象。

Both of these can be toggled on or off in the array of settings when you setup your object. 设置对象时,可以在设置数组中打开或关闭这两个选项。

hasBorders hasBorders

Default: true 默认值:true

When set to false , object's controlling borders are not rendered 设置为false ,不呈现对象的控制边界

hasControls hasControls

Default: true 默认值:true

When set to false , object's controls are not displayed and can not be used to manipulate object 设置为false ,不显示对象的控件,并且不能将其用于操作对象

So to remove both the borders and the controls, update your circle declaration to this: 因此,要同时删除边框和控件,请将您的圆形声明更新为:

var circle = new fabric.Circle({
    radius: 50,
    left: 175,
    top: 75,
    fill: '#aac',
    hasControls: false,
    hasBorders: false
});

One other option would be to to modify the color of the Border. 另一种选择是修改边框的颜色。
borderColor 边框颜色

Default: "rgba(102,153,255,0.75)" 默认值:“ rgba(102,153,255,0.75)”

Color of controlling borders of an object (when it's active) 控制对象边界的颜色(处于活动状态时)

Styling of the Controls is through yet another option: 控件的样式可通过另一种选择:

cornerColor cornerColor

Default: "rgba(102,153,255,0.5)" 默认值:“ rgba(102,153,255,0.5)”

Color of controlling corners of an object (when it's active) 控制对象角的颜色(处于活动状态时)

cornerSize cornerSize

Default: 12 默认值:12

Size of object's controlling corners (in pixels) 对象控制角的大小(以像素为单位)

EDIT: 编辑:

Ok after looking at your JSFiddle, you are really asking how to use the jQuery canvas api. 好了,在查看了JSFiddle之后,您实际上是在问如何使用jQuery canvas API。 Can't help you there, but it looks like there are methods for moving/updating objects after they've been placed on a canvas. 在这里不能为您提供帮助,但是看起来有些方法可以将对象放置在画布上之后移动/更新对象。

The original answer (below) is for plain javascript on a plain canvas. 原始答案(如下)适用于纯画布上的纯javascript。

You don't. 你不知道 A single canvas doesn't have the concept of 'layers' or remember what objects were placed where. 单个画布没有“层”的概念,也没有记住什么对象放置在何处。

You have a few options: 您有几种选择:

1) Repaint the canvas and this time don't draw the rectangle. 1)重新绘制画布,这次不绘制矩形。

2) Draw two canvases layered on top of each other and toggle the visibility of the one containing the rectangle. 2)绘制两个彼此叠置的画布,并切换包含矩形的画布的可见性。

3) Draw a white rectangle on top of the rectangle you just drew (or whatever the background color is) 3)在您刚刚绘制的矩形上方绘制一个白色矩形(或其他背景颜色)

Option 1 is my preferred way. 选项1是我的首选方式。 Option 2 is more complexity than you need, unless you're going to start creating a very complex image. 选项2比您需要的要复杂得多,除非您要开始创建非常复杂的图像。 Option 3 seems hacky and easy to break. 选项3似乎很容易破解。

Regarding #1 - The painting of the canvas happens very, very fast, so it's safe to redraw the canvas several times per second (in the case of performing animation, eg,) without incurring much of a performance hit. 关于#1-画布的绘制非常非常快,因此可以安全地每秒重绘几次画布(例如,在执行动画的情况下),而不会造成很大的性能损失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM