语法new fabric.rect({ flipx: boolean }: object)
参数选项(可选) - 此参数是一个提供额外自定义的对象到我们的矩形。使用此参数,可以更改与以 flipx 为属性的对象相关的颜色、光标、描边宽度等属性。
选项键flipx - 此属性接受一个布尔值,该值允许我们水平翻转对象。示例 1将 flipx 作为具有“false”值的键进行传递让我们看一个代码示例,它向我们展示了 fabricjs 中矩形对象的默认方向。由于我们向 flipx 属性传递了 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>passing flipx as key with a false value</h2> <p>you can see that the object is not flipped horizontally</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 rectangle object var rect = new fabric.rect({ left: 125, top: 90, width: 170, height: 70, bordercolor: "purple", borderscalefactor: 3, flipx: false, }); // create gradient fill rect.set( "fill", new fabric.gradient({ type: "linear", coords: { x1: 0, y1: 0, x2: 50, y2: 0 }, colorstops: [ { offset: 0, color: "pink" }, { offset: 1, color: "blue" }, ], }) ); // add it to the canvas canvas.add(rect); </script></body></html>
示例 2将 flipx 属性作为具有“true”值的键传递
在此示例中,我们有一个宽度为 170、高度为 70 且具有水平线性渐变填充的矩形对象。当我们将 flipx 属性应用于矩形对象时,它会水平翻转,因此我们看到渐变也翻转了。
<!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 flipx property as key with a true value</h2> <p>you can see now that the object has flipped horizontally</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 rectangle object var rect = new fabric.rect({ left: 125, top: 90, width: 170, height: 70, bordercolor: "purple", borderscalefactor: 3, flipx: true, }); // create gradient fill rect.set( "fill", new fabric.gradient({ type: "linear", coords: { x1: 0, y1: 0, x2: 50, y2: 0 }, colorstops: [ { offset: 0, color: "pink" }, { offset: 1, color: "blue" }, ], }) ); // add it to the canvas canvas.add(rect); </script></body></html>
以上就是如何使用 fabricjs 水平翻转矩形?的详细内容。
