当前位置:网站首页 > R语言数据分析 > 正文

使用fabric画一个图形-练习

一,效果

在这里插入图片描述

二,代码

<template> <div id="app"> <!-- 1、创建 canvas 元素 --> <canvas width="500" height="500" id="canvas" style="border: 1px solid #ccc" ></canvas> </div> </template> <script> //引入fabric import { 
    fabric } from "fabric"; export default { 
    name: "App", components: { 
   }, mounted() { 
    //新建画布对象 const canvas = new fabric.Canvas("canvas", { 
    backgroundColor: "pink", }); // 多边形 const polygon1 = new fabric.Polygon( [ { 
    x: 30, y: 0, }, { 
    x: 30, y: 80, }, { 
    x: 0, y: 75, }, ], { 
    top: 20, left: 220, fill: "red", } ); const gradient1 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: 0, y1: 75, x2: 30, y2: 0 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#e23c92" }, { 
    offset: 1, color: "#f4609e" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon1.set("fill", gradient1); // 多边形 const polygon2 = new fabric.Polygon( [ { 
    x: 30, y: 0, }, { 
    x: 30, y: 80, }, { 
    x: 60, y: 75, }, ], { 
    top: 20, left: 250, fill: "red", } ); const gradient2 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: 30, y1: 80, x2: 60, y2: 80 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#f4609e" }, { 
    offset: 1, color: "#f286ba" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon2.set("fill", gradient2); const group1 = new fabric.Group([polygon1, polygon2], { 
    //公用的属性可以设置到这里来 left: 220, top: 0, }); // 多边形 const polygon3 = new fabric.Polygon( [ { 
    x: 36, y: 0, }, { 
    x: 72, y: 8, }, { 
    x: 36, y: 16, }, { 
    x: 0, y: 8, }, ], { 
    top: 80, left: 214, fill: "red", } ); const gradient3 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: 36, y1: 16, x2: 36, y2: 0 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#6a43b8" }, { 
    offset: 1, color: "#a17ce2" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon3.set("fill", gradient3); // 多边形 const polygon4 = new fabric.Polygon( [ { 
    x: 0, y: 8, }, { 
    x: 36, y: 16, }, { 
    x: 36, y: 85, }, { 
    x: -25, y: 70, }, ], { 
    top: 88, left: 189, fill: "red", } ); const gradient4 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: -25, y1: 70, x2: 0, y2: 8 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#7950c6" }, { 
    offset: 1, color: "#b16feb" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon4.set("fill", gradient4); // 多边形 const polygon5 = new fabric.Polygon( [ { 
    x: 72, y: 8, }, { 
    x: 36, y: 16, }, { 
    x: 36, y: 85, }, { 
    x: 97, y: 70, }, ], { 
    top: 88, left: 250, fill: "red", } ); const gradient5 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: 97, y1: 70, x2: 72, y2: 8 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#6841b8" }, { 
    offset: 1, color: "#aa6de4" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon5.set("fill", gradient5); const group2 = new fabric.Group([polygon3, polygon4, polygon5], { 
    //公用的属性可以设置到这里来 left: 189, top: 80, }); // 多边形 const polygon6 = new fabric.Polygon( [ { 
    x: 36, y: 0, }, { 
    x: 72, y: 8, }, { 
    x: 36, y: 16, }, { 
    x: 0, y: 8, }, ], { 
    top: 80, left: 214, fill: "red", } ); const gradient6 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: 36, y1: 16, x2: 36, y2: 0 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#18c9d9" }, { 
    offset: 1, color: "#a17ce2" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon6.set("fill", gradient6); // 多边形 const polygon7 = new fabric.Polygon( [ { 
    x: 0, y: 8, }, { 
    x: 36, y: 16, }, { 
    x: 36, y: 85, }, { 
    x: -25, y: 70, }, ], { 
    top: 88, left: 189, fill: "red", } ); const gradient7 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: -25, y1: 70, x2: 0, y2: 8 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#5ca6f2" }, { 
    offset: 1, color: "#18c9d9" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon7.set("fill", gradient7); // 多边形 const polygon8 = new fabric.Polygon( [ { 
    x: 72, y: 8, }, { 
    x: 36, y: 16, }, { 
    x: 36, y: 85, }, { 
    x: 97, y: 70, }, ], { 
    top: 88, left: 250, fill: "red", } ); const gradient8 = new fabric.Gradient({ 
    type: "linear", //线性渐变,可取值:linear or radial gradientUnits: "pixels", //默认为像素,但可以指定为百分比:pixels or pencentage coords: { 
    x1: 97, y1: 70, x2: 72, y2: 8 }, //线性渐变的起点和终点 colorStops: [ { 
    offset: 0, color: "#5ca6f2" }, { 
    offset: 1, color: "#10a5dd" }, ], //线性渐变的颜色分层,offset:0是起点,1是终点,中间可以多层 }); polygon8.set("fill", gradient8); const group3 = new fabric.Group([polygon6, polygon7, polygon8], { 
    //公用的属性可以设置到这里来 left: 189, top: 160, scale: 2, }); canvas.add(group3, group2, group1); }, data() { 
    return { 
   }; }, methods: { 
   }, }; </script> <style scoped lang="scss"></style> 
到此这篇使用fabric画一个图形-练习的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • postcss-pxtorem 插件自动转换 rem 单位的配置2024-11-27 20:45:04
  • element Transfer 穿梭框 内容太长显示不全,鼠标移动上去显示全部2024-11-27 20:45:04
  • 从window.history理解浏览器返回不触发页面刷新问题2024-11-27 20:45:04
  • 从异步到promise2024-11-27 20:45:04
  • 从promise到await2024-11-27 20:45:04
  • fabric操作canvas绘图-(四)事件绑定2024-11-27 20:45:04
  • fabric操作canvas绘图-(三)渐变2024-11-27 20:45:04
  • fabric操作canvas绘图-(二)动画2024-11-27 20:45:04
  • ES6的export和import2024-11-27 20:45:04
  • UML图及在drawio中的绘制2024-11-27 20:45:04
  • 全屏图片