如何使用 FabricJS 停用透過拖動選擇物件?
在這篇文章中,我們將演示如何使用 FabricJS 停用透過拖動選擇物件。在 FabricJS 畫布中,我們基本上可以點選任意位置並選擇一個區域,該區域內的任何物件都將被選中。在這篇文章中,我們將瞭解如何禁止這種行為。
語法
new fabric.Canvas(element: HTMLElement|String, {selection: boolean}: Object)引數
element − 此引數是<canvas> 元素本身,可以使用document.getElementById() 或<canvas> 元素本身的 id 獲取。FabricJS 畫布將在此元素上初始化。
options (可選) − 此引數是一個物件,它為我們的畫布提供額外的自定義選項。使用此引數,可以更改與畫布相關的許多屬性,例如顏色、游標、邊框寬度等。selection 引數指示是否應啟用選擇。此鍵的預設值為 True。
示例 1
讓我們首先看看啟用選擇時拖動選擇是什麼樣的。對於此示例,我們將 selection 鍵設定為 True,這也是預設值。讓我們看看啟用選擇後畫布的行為。
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Disabling the selection of objects on a canvas</h2>
<p>Here you can select the object as the selection key is True</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selection: true
});
// Creating an instance of the fabric.Circle class
var cir = new fabric.Circle({
radius: 40,
fill: "#87a96b",
left: 30,
top: 20,
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
selection 鍵指定是否應啟用或停用透過拖動選擇畫布中物件的選項。如果我們將該鍵設定為 False,那麼我們將無法再透過拖動來選擇物件。
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Disabling the selection of objects on a canvas</h2>
<p> Here you cannot select an area around the object as the selection key is set to False.</p>
<canvas id="canvas"></canvas>
<script>
//Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selection: false
});
//creating an instance of the fabric.Circle class
var cir = new fabric.Circle({
radius: 40,
fill: "#87a96b",
left: 30,
top: 20,
});
//adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>現在我們將 selection 設定為 False,我們將無法再選擇物件周圍的部分來拖動它。但是,我們仍然可以手動點選並選擇物件。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP