
我们可以通过创建fabric.Polygon的实例来创建一个Polygon对象。多边形对象的特征可以是由一组连接的直线段组成的任何闭合形状。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性来轻松自定义它。我们使用 skewing 事件来演示多边形对象在被操作时如何对用户做出反应。通过控制倾斜。
语法
polygon.on("skewing", callbackFunction);
示例 1:显示对象如何响应倾斜事件
让我们看一个代码示例,了解多边形对象在使用倾斜事件时如何反应。通过按 shift 键,然后沿水平或垂直方向拖动中间控件,可以在水平和垂直方向上倾斜对象。当对象倾斜时,倾斜事件会连续触发。
Displaying how the object reacts to the skewing event
You can keep skewing the object while the console from dev tools is open to see the logged output
// Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a polygon instance var polygon = new fabric.Polygon( [ { x: 500, y: 20 }, { x: 550, y: 60 }, { x: 550, y: 200 }, { x: 350, y: 200 }, { x: 350, y: 60 }, { x: 500, y: 20 }, ], { fill: "red", stroke: "blue", strokeWidth: 2, objectCaching: false, } ); // Adding it to the canvas canvas.add(polygon); // Using the skewing event polygon.on("skewing", () => { canvas.renderAll(); console.log("The polygon object is being skewed"); });
示例 2:发生倾斜时更改填充颜色
让我们看一个代码示例,以了解如何在发生倾斜事件时更改填充颜色。我们使用了 set 方法,它是一个允许我们指定要更改的属性的设置器。在这里,每当我们倾斜多边形时,填充颜色就会变为“绿色”。
标书对比王
标书对比王是一款标书查重工具,支持多份投标文件两两相互比对,重复内容高亮标记,可快速定位重复内容原文所在位置,并可导出比对报告。
58 查看详情
Changing the fill colour when skew happens
You can see that the fill colour changes when the polygon is skewed
// Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a polygon instance var polygon = new fabric.Polygon( [ { x: 500, y: 20 }, { x: 550, y: 60 }, { x: 550, y: 200 }, { x: 350, y: 200 }, { x: 350, y: 60 }, { x: 500, y: 20 }, ], { fill: "red", stroke: "blue", strokeWidth: 2, objectCaching: false, top: 50, left: 30, scaleX: 0.5, scaleY: 0.5 } ); // Adding it to the canvas canvas.add(polygon); // Using the skewing event polygon.on("skewing", () => { polygon.set("fill", "green") canvas.renderAll(); });
结论
在本教程中,我们使用两个简单的示例来演示如何使用 FabricJS 使多边形对象对倾斜事件做出反应。
以上就是如何使用 FabricJS 使多边形对象对倾斜事件做出反应?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/528564.html
微信扫一扫
支付宝扫一扫