在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 禁用三角形的居中縮放。三角形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個三角形,我們必須創(chuàng)建一個 Fabric.Triangle 類的實(shí)例并將其添加到畫布中。
當(dāng)通過控件縮放對象時,分配一個centeredScaling 屬性的真實(shí)值,變換的原點(diǎn)是其中心。
語法
new fabric.Triangle({ centeredScaling: Boolean }: Object)
登錄后復(fù)制
參數(shù)
選項(xiàng)(可選) – 此參數(shù)是一個對象 為我們的三角形提供額外的定制。使用此參數(shù),可以更改與 centeredScaling 屬性相關(guān)的對象的顏色、光標(biāo)、描邊寬度等屬性。
選項(xiàng)鍵
centeredScaling – 該屬性接受一個布爾值并允許我們控制對象是否應(yīng)使用其中心作為變換原點(diǎn)。
示例 1
傳遞 centeredScaling 作為 key 并為其分配一個“true”值
讓我們看一個代碼示例,了解啟用 centeredScaling 屬性時三角形對象的行為。當(dāng)我們放大對象時,變換的原點(diǎn)是三角形的中心。
<!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 centeredScaling as key and assigning it a "true" value</h2>
<p>Try scaling the triangle to see that it is using its center as the center of transformation.</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#5f9ea0",
centeredScaling: true,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
登錄后復(fù)制
示例 2
禁用 centeredScaling 屬性
我們可以通過為其指定 False 值來禁用 centeredScaling 屬性。這將不再使用三角形的中心作為變換的中心。這是一個代碼示例來演示
<!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 centeredScaling property</h2>
<p>Try scaling the triangle to see that it is using one of its corners as the center of transformation.</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#5f9ea0",
centeredScaling: false,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
登錄后復(fù)制
以上就是如何使用 FabricJS 禁用三角形的中心縮放?的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!






