https://jsfiddle.net/YashChapani/g0xm9tqv/2/
无论高度如何,矩形的边框半径都应保持比例且平滑圆润。
当矩形的高度减小时,边框半径会出现扭曲并表现出意外。
代码:
const canvas = new fabric.Canvas('canvas');
let canvasWidth = 640;
let canvasHeight = 640;
canvas.setWidth(canvasWidth);
canvas.setHeight(canvasHeight);
const rect = new fabric.Rect({
left: 145, // Center the rectangle within the canvas
top: 145, // Center the rectangle within the canvas
fill: 'blue',
width: 350,
height: 350,
rx: 20, // Horizontal border radius
ry: 20 // Vertical border radius
});
// Add rectangle to the canvas
canvas.add(rect);
canvas.renderAll();
版本信息:
您可以使用矩形
scaling
事件来更新width
和height
,以便将缩放设置为1
并且更新矩形height
和width
。
const canvas = new fabric.Canvas("canvas")
let canvasWidth = 640
let canvasHeight = 640
canvas.setWidth(canvasWidth)
canvas.setHeight(canvasHeight)
const rect = new fabric.Rect({
left: 145, // Center the rectangle within the canvas
top: 145, // Center the rectangle within the canvas
fill: "blue",
width: 350,
height: 350,
rx: 20, // Horizontal border radius
ry: 20, // Vertical border radius
})
rect.on("scaling", function () {
this.set({
width: this.width * this.scaleX,
height: this.height * this.scaleY,
scaleX: 1,
scaleY: 1,
})
})
// Add rectangle to the canvas
canvas.add(rect)
canvas.renderAll()
<!DOCTYPE html>
<html>
<head>
<title>Fabric.js Adjustable Dashed Line</title>
<!-- Using Fabric.js version 6.4.0 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>