在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 禁用 Ellipse 的居中縮放。橢圓形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個(gè)橢圓,我們必須創(chuàng)建一個(gè) Fabric.Ellipse 類的實(shí)例并將其添加到畫布中。通過(guò)控件進(jìn)行縮放時(shí),為 centeredScaling 屬性分配“true”值,使用中心作為對(duì)象的變換原點(diǎn)。
語(yǔ)法
new fabric.Ellipse({ centeredScaling: Boolean }: Object)
登錄后復(fù)制
參數(shù)
選項(xiàng)(可選)- 此參數(shù)是一個(gè)對(duì)象 為我們的橢圓提供額外的定制。使用此參數(shù),可以更改與 centeredScaling 屬性相關(guān)的對(duì)象的顏色、光標(biāo)、描邊寬度和許多其他屬性。
選項(xiàng)鍵
centeredScaling – 此屬性接受布爾值 值。當(dāng)此屬性為True時(shí),對(duì)象使用中心作為變換原點(diǎn)。
示例 1
將centeredScaling作為鍵傳遞并為其分配一個(gè)“true”值
以下示例演示了橢圓對(duì)象在centeredScaling時(shí)的行為方式屬性已啟用。當(dāng)我們放大對(duì)象時(shí),變換的原點(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>How to disable the centered scaling of Ellipse using FabricJS?</h2>
<p>Select the object and stretch it from its corners. The ellipse will scale up from its center. This is the default behavior.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 215,
top: 100,
fill: "white",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
centeredScaling: true,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復(fù)制
示例 2
禁用 centeredScaling 屬性
我們可以通過(guò)為其指定“false”值來(lái)禁用 centeredScaling 屬性。這將不再使用橢圓的中心作為變換中心。這是一個(gè)代碼,用于演示 –
<!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>How to disable the centered scaling of Ellipse using FabricJS?</h2>
<p>Select the object and stretch it from its corners. You will notice the object scales up but not from its center because we have set <b>centeredScaling</b> as False. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 215,
top: 100,
fill: "",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
centeredScaling: false,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復(fù)制
以上就是如何使用 FabricJS 禁用 Ellipse 的居中縮放?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






