首頁  >  文章  >  web前端  >  如何使用FabricJS設定Textbox允許的最小比例值?

如何使用FabricJS設定Textbox允許的最小比例值?

WBOY
WBOY轉載
2023-08-24 13:09:031031瀏覽

如何使用FabricJS設定Textbox允許的最小比例值?

在本教學中,我們將學習如何使用 FabricJS 設定 Textbox 的最小允許比例。我們可以自訂、拉伸或移動文字方塊中寫入的文字。為了建立文字框,我們必須建立fabric.Textbox類別的實例並將其新增到畫布中。同樣,我們也可以使用 minScaleLimit 屬性來設定其允許的最小比例。

文法

new fabric.Textbox(text: String, { minScaleLimit : Number }: Object)

參數

  • text - 此參數接受一個 String,它是我們要在文字方塊中顯示的文字字串。

  • 選項(可選) - 此參數是一個對象,它為我們的文字方塊提供額外的自訂。使用此參數,可以變更與 minScaleLimit 為屬性的物件相關的顏色、遊標、邊框寬度和許多其他屬性等屬性。

選項鍵

  • minScaleLimit - 此屬性接受Number 作為值,該值允許我們控製文字方塊允許的最小比例值.

範例 1

文字方塊物件的預設外觀

讓我們來看一個程式碼範例,看看當不使用 minScaleLimit 屬性時,我們的文字方塊物件是什麼樣子。在這種情況下,我們將能夠自由縮放對象,因為沒有設定最小限制。

<!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>Default appearance of the Textbox object</h2>
	<p>You can scale the textbox object to see that there is no minimum limit set</p>
	<canvas id="canvas"></canvas>
	<script>
		// Initiate a canvas instance
		var canvas = new fabric.Canvas("canvas");
		canvas.setWidth(document.body.scrollWidth);
		canvas.setHeight(250);
		
		// Initiate a textbox object
		var textbox = new fabric.Textbox("That which does not kill us makes us stronger.", {
			backgroundColor: "#e5e4e2",
			width: 400,
			left: 50,
			top: 70,
			fill: "#e1a95f",
		});
		
		// Add it to the canvas
		canvas.add(textbox);
	</script>
</body>
</html>

範例 2

minScaleLimit 屬性作為具有自訂值的鍵傳遞

#在此範例中,我們將了解為 minScaleLimit 屬性指派值如何變更畫布中文字方塊物件的最小允許比例值。這裡我們使用 0.8 作為值,這意味著我們將無法將物件縮小到寬度小於 240 像素,寬度由 寬度 * 限制 (0.8 *300 = 240 像素) 計算。

<!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>Passing the minScaleLimit property as key with a custom value</h2>
	<p>	You can scale the textbox object to see that there is a minimum limit set</p>
	<canvas id="canvas"></canvas>
	<script>
		// Initiate a canvas instance
		var canvas = new fabric.Canvas("canvas");
		canvas.setWidth(document.body.scrollWidth);
		canvas.setHeight(250);
		
		// Initiate a textbox object
		var textbox = new fabric.Textbox("That which does not kill us makes us stronger.", {
			backgroundColor: "#e5e4e2",
			width: 400,
			left: 50,
			top: 70,
			fill: "#e1a95f",
			minScaleLimit: 0.8,
		});
		
		// Add it to the canvas
		canvas.add(textbox);
	</script>
</body>
</html>

以上是如何使用FabricJS設定Textbox允許的最小比例值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除