PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

FabricJS – 查找转换为 HTMLCanvasElement 的 Polygon 对象的绘图上下文?

WBOY
WBOY 转载
2023-08-30 17:33:06 490浏览

FabricJS – 查找转换为 HTMLCanvasElement 的 Polygon 对象的绘图上下文?

我们可以通过创建fabric.Polygon的实例来创建Polygon对象。多边形对象的特征可以是由一组连接的直线段组成的任何闭合形状。由于它是 FabricJS 的基本元素之一,因此我们还可以通过应用角度、不透明度等属性轻松自定义它。

为了将多边形对象转换为 HTMLCanvasElement,我们使用 toCanvasElement 方法。它返回 HTMLCanvasElement 类型的 DOM 元素,该接口从 HTMLElement 接口继承其属性和方法。我们使用 getContext 方法在画布上查找绘图上下文。如果不支持上下文 ID,则返回 null 值。

语法

HTMLCanvasElement.getContext():

示例 1:使用 toCanvasElement 方法

让我们看一个代码示例,以查看使用 toCanvasElement 方法时记录的输出。使用 toCanvasElement 方法时,将返回 HTMLCanvasElement 类型的 DOM 元素。您可以从开发工具打开控制台以查看正在返回 HTMLCanvasElement 类型的 DOM 元素。

<!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>Using the toCanvasElement method</h2>
   <p>You can open console from dev tools to see the logged output</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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      console.log(
         "The output on using toCanvasElement method is:",
         polygon.toCanvasElement()
      );
   </script>
</body>
</html>

示例2:使用getContext方法

让我们看一个代码示例,看看使用 getContext 方法和 toCanvasElement 方法查找转换为 HTMLCanvasElement 的 Polygon 对象的绘图上下文时记录的输出。绘图上下文让我们可以在画布上绘图。由于我们向 getContext 方法传递了值“2d”,因此会返回一个 CanvasRenderingContext2D 对象。

<!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>Using getContext method</h2>
   <p>
      You can open console from dev tools to see that the CanvasRenderingContext2D object is being returned
   </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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      var polygonCanvas = polygon.toCanvasElement({
         width: 200,
      });
      
      // Using getContext method
      console.log(
         "The drawing context of a Polygon object converted to HTMLCanvasElement is as follows:",
         polygonCanvas.getContext("2d")
      );
   </script>
</body>
</html> 

结论

在本教程中,我们使用两个简单的示例来演示如何找到使用 FabricJS 转换为 HTMLCanvasElement 的 Polygon 对象的绘图上下文。

以上就是FabricJS – 查找转换为 HTMLCanvasElement 的 Polygon 对象的绘图上下文?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除