当前位置:   article > 正文

基于canvas画布的实用类Fabric.js的使用

fabric.js

目录

前言

一、Fabric.js简介

二、开始

1、引入Fabric.js

2、在main.js中使用

3、初始化画布

三、方法

四、事件

1、常用事件

2、事件绑定

3、事件解绑

五、canvas常用属性

六、对象属性

1、基本属性

2、扩展属性

七、图层层级操作

八、复制和粘贴

1、复制

2、粘贴

九、锁定

1、静止水平移动(lockMovementX)

2、静止垂直移动(lockMovementY)

3、静止旋转(lockRotation)

4、静止水平缩放(lockScalingX)

5、静止垂直缩放(lockScalingY)

6、限制拖动区域

十、分组

十一、动画

十二、图像滤镜

十三、渐变

1、线性渐变

2、径向渐变

十四、拖拽和缩放画布

1、拖拽画布

2、以画布中心点为基准手动缩放

3、以鼠标指针位置为基准缩放

十五、右键菜单删除

十六、自由绘画

1、开启绘图模式

2、关闭绘图模式

十七、绘制背景图片

1、方式一:通过img元素添加

2、方式二:通过图片路径添加

十八、绘制文本

1、基础用法

2、文本修饰

3、可编辑文本

十九、绘制线和路径

1、绘制直线

2、绘制虚线

3、绘制路径

二十、自由绘制矩形

二十一、自由绘制圆形

二十二、自由绘制椭圆形

二十三、自由绘制三角形

二十四、自由绘制多边形


前言

        演示Demo:前端可视化demo

        Demo源码:https://gitee.com/k21vin/front-end-data-visualization

        本文章所有的gif图片由于录屏软件问题,上面的鼠标位置都是错位显示的!!

一、Fabric.js简介

        Fabric.js是一个对canvas进行封装的Javascript库,在原生canvas之上提供了交互式对象模型,通过简洁的api就可以在画布上进行丰富的操作。

        它主要的功能包括在canvas上创建和填充图形,比如矩形、圆形、多边形;生成的图像自带缩放、旋转、拖拽等功能;还可以给图形填充渐变颜色;各个图形可以相互组合等等。

二、开始

1、引入Fabric.js

npm i fabric --save

2、在main.js中使用

  1. // main.js
  2. import fabric from "fabric"
  3. Vue.use(fabric)

3、初始化画布

  1. <template>
  2. <canvas id="canvas"></canvas>
  3. </template>
  4. <script>
  5. export default {
  6. data () {
  7. canvas: null, // 画布对象
  8. },
  9. mounted() {
  10. this.canvas = new fabric.Canvas('canvas', {
  11. width: 300,
  12. height: 200
  13. })
  14. }
  15. </script>

三、方法

  1. canvas.add(object) // 添加对象
  2. canvas.remove(object) // 删除对象
  3. canvas.setWidth(width) // 设置canvas宽度
  4. canvas.setHeight(height) // 设置canvas高度
  5. canvas.setDimensions({width, height})// 一键设置宽高
  6. canvas.getObjects() // 获取所有对象
  7. canvas.getActiveObject() // 获取选中的对象
  8. canvas.clear() // 清除画布中所有对象
  9. canvas.renderAll() // 重绘
  10. canvas.requestRenderAll() // 请求重新渲染
  11. canvas.getZoom() // 获取画布当前缩放值
  12. canvas.sendToBack(object) // 移到对象到最底层
  13. canvas.viewportCenterObjectH(object) // 水平居中对象
  14. canvas.viewportCenterObjectV(object) // 垂直居中对象
  15. canvas.viewportCenterObject(object) // 垂直水平居中对象
  16. canvas.fxCenterObjectH(object) // 动画水平居中对象
  17. canvas.fxCenterObjectV(object) // 动画垂直居中对象
  18. canvas.fxCenterObject(object) // 动画垂直水平居中对象
  19. let canvasJsonData = JSON.stringify(canvas.toJSON()) // 将画布序列化成json数据
  20. let canvasSvgData = canvas.toSVG() // 将画布序列化成svg数据
  21. canvas.loadFromJSON(canvasJsonData) // 反序列化Json数据

四、事件

1、常用事件

  1. mouse: down // 鼠标按下事件
  2. mouse: move // 鼠标移动事件
  3. mouse: up // 鼠标移动事件
  4. mouse: over // 鼠标移入事件
  5. mouse: out // 鼠标移出事件
  6. mouse: dblclick // 鼠标双击事件
  7. object: added // 对象被添加事件
  8. object: removed // 对象被删除事件
  9. object: modified // 对象被修改事件
  10. object: rotating // 对象被旋转事件
  11. object: scaling // 对象被缩放事件
  12. object: moving // 对象被移动事件

2、事件绑定

  1. canvas.on('mouse: wheel', (opt) => {
  2. console.log(opt)
  3. })

3、事件解绑

canvas.off('mouse: wheel')

五、canvas常用属性

  1. canvas.selection = true // 画布是否可选中 默认为true、false:不可选中
  2. canvas.selectionColor = 'transparent' // 画布鼠标框选时的背景色
  3. canvas.selectionBorderColor = 'transparent'// 画布鼠标框选时的边框颜色
  4. canvas.selectionLineWidth = 6 // 画布鼠标框选时的边框厚度
  5. canvas.selectionDashArray = [30, 4, 10] // 画布鼠标框选时边框虚线规则
  6. canvas.selectionFullyContained = true // 只选择完全包含在拖动选择矩形中的形状
  7. canvas.backgroundColor = '#2E3136' // 画布背景色
  8. canvas.hoverCursor = 'pointer' // 鼠标光标样式 default、pointer、move等
  9. canvas.skipTargetFind = true // 整个画板元素不能被选中
  10. canvas.fireRightClick = true // 启用右键,options.button的数字为3
  11. canvas.stopContextMenu = true // 禁止默认右键菜单

        Fabric.js 可以通过 viewportTransform 属性配置画布的视窗属性

  1. canvas.viewportTransform[4] = 100
  2. canvas.viewportTransform[5] = 100

        viewportTransform 是一个数组,里面有6个元素,默认值是 [1, 0, 0, 1, 0, 0]。从下标0开始,它们分别代表:

  1. [0]: 水平缩放(x轴方向)
  2. [1]: 水平倾斜(x轴方向)
  3. [2]: 垂直倾斜(y轴方向)
  4. [3]: 垂直缩放(y轴方向)
  5. [4]: 水平移动(x轴方向)
  6. [5]: 垂直移动(y轴方向)

六、对象属性

1、基本属性

  1. let circle = new fabric.Circle({
  2. top: 100, // y坐标
  3. left: 100, // x坐标
  4. fill: '#17b978', // 填充色
  5. radius: 50 // 半径
  6. })
  1. rect.top = 100 // y坐标
  2. rect.left = 100 // x坐标
  3. rect.width = 100 // 矩形宽度
  4. rect.height = 100 // 矩形高度
  5. circle.radius = 50 // 圆半径
  6. rect.fill = '#17b978' // 填充色
  7. rect.stroke = '#FE5332' // 线条颜色
  8. rect.strokeWidth = 10 // 线条宽度
  9. rect.strokeMiterLimit = index // 可以用来记录当前选中的rectList列表的索引!!!!!

2、扩展属性

  1. circle.set({
  2. hasBorders: false
  3. }
  1. circle.selectable = false // 控件不能被选择,不会被操作
  2. circle.hasControls = false // 只能移动不能(编辑)操作
  3. circle.hasBorders = false // 选中时,是否显示边,true:显示(默认)
  4. circle.borderColor = 'red' // 选中时,边的颜色
  5. circle.borderScaleFactor = 5 // 选中时,边的粗细
  6. circle.borderDashArray = [20, 5, 10, 7] // 选中时,虚线边的规则
  7. circle.transparentCorners = false // 选中时,角是否是空心 true:空心 false:实心
  8. circle.cornerColor = "#a1de93", // 选中时,角的颜色
  9. circle.cornerStrokeColor = 'pink' // 选中时,角的边框的颜色
  10. circle.cornerStyle = 'circle' // 选中时,角的属性 rect:矩形(默认)、circle:圆形
  11. circle.cornerSize = 20 // 选中时,角的大小为20
  12. circle.cornerDashArray = [10, 2, 6] // 选中时,虚线角的规则
  13. circle.selectionBackgroundColor = '#ffc300' // 选中时,选框的背景色
  14. circle.padding = 20 // 选中时,选框离图形的距离
  15. circle.borderOpacityWhenMoving = 0.6 // 当对象活动和移动时,对象控制边界的不透明度
  16. triangle.perPixelTargetFind = true // 选择三角形空白位置的时候无法选中,false:可以选中(默认)

七、图层层级操作

  1. canvas.bringToFront(rect) // 移到顶层
  2. canvas.sendToBack(rect) // 移到底层
  3. canvas.bringForward(rect) // 上移一层
  4. canvas.sendBackwards(rect) // 下移一层
  5. canvas.moveTo(0) // 移动到指定层

八、复制和粘贴

1、复制

  1. handleCopy() {
  2. if (!canvas.getActiveObject()) {
  3. this.$message.warning('请先选择元素')
  4. return
  5. }
  6. this.canvas.getActiveObject().clone(cloned => {
  7. this.cloneObjects = cloned
  8. })
  9. }

2、粘贴

  1. handlePaste() {
  2. if (!this.cloneObjects) {
  3. return this.$message.warning('还没复制过任何内容')
  4. }
  5. this.cloneObjects.clone(cloned => {
  6. this.canvas.discardActiveObject() // 取消选择
  7. // 设置新内容的坐标位置
  8. cloned.set({
  9. left: cloned.left + 10,
  10. top: cloned.top + 10,
  11. evented: true
  12. })
  13. if (cloned.type === 'activeSelection') { // 如果复制的是多个对象,则需要遍历克隆对象
  14. cloned.canvas = this.canvas;
  15. cloned.forEachObject(obj => {
  16. this.canvas.add(obj)
  17. })
  18. cloned.setCoords()
  19. } else {
  20. this.canvas.add(cloned)
  21. }
  22. this.cloneObjects.top += 10
  23. this.cloneObjects.left += 10
  24. this.canvas.setActiveObject(cloned)
  25. this.canvas.requestRenderAll()
  26. })
  27. }

九、锁定

        Fabric对象可以添加一些属性进行锁定,例如静止水平移动、静止垂直移动,静止缩放等等

1、静止水平移动(lockMovementX)

  1. let rect = new fabric.Rect({
  2. width: 100,
  3. height: 50,
  4. fill: '#ffde7d',
  5. top: 20,
  6. left: 20
  7. })
  8. rect.lockMovementX = true
  9. canvas.add(rect)

2、静止垂直移动(lockMovementY)

  1. let rect = new fabric.Rect({
  2. width: 100,
  3. height: 50,
  4. fill: '#ffde7d',
  5. top: 20,
  6. left: 20
  7. })
  8. rect.lockMovementY = true

3、静止旋转(lockRotation)

  1. let rect = new fabric.Rect({
  2. width: 100,
  3. height: 50,
  4. fill: '#ff9a3c',
  5. top: 60,
  6. left: 160
  7. })
  8. rect.lockRotation = true

4、静止水平缩放(lockScalingX)

  1. let rect = new fabric.Rect({
  2. width: 100,
  3. height: 50,
  4. fill: '#ffde7d',
  5. top: 20,
  6. left: 20
  7. })
  8. rect.lockScalingX = true

5、静止垂直缩放(lockScalingY)

  1. let rect = new fabric.Rect({
  2. width: 100,
  3. height: 50,
  4. fill: '#f95959',
  5. top: 20,
  6. left: 20
  7. })
  8. rect.lockScalingY = true

6、限制拖动区域

  1. let boundingBox = new fabric.Rect({
  2. top: 100,
  3. left: 100,
  4. width: 600,
  5. height: 400,
  6. fill: '#f95959',
  7. selectable: false
  8. })
  9. let movingBox = new fabric.Rect({
  10. top: 150,
  11. left: 150,
  12. width: 100,
  13. height: 100,
  14. fill: 'yellow',
  15. hasBorders: false,
  16. hasControls: false,
  17. hoverCursor: 'move'
  18. })
  19. this.canvas.add(boundingBox);
  20. this.canvas.add(movingBox);
  21. this.canvas.on("object:moving", (opt) => {
  22. let top = movingBox.top;
  23. let left = movingBox.left;
  24. let topBound = boundingBox.top;
  25. let bottomBound = topBound + boundingBox.height;
  26. let leftBound = boundingBox.left;
  27. let rightBound = leftBound + boundingBox.width;
  28. opt.target.left = Math.min(Math.max(left, leftBound), rightBound - movingBox.width)
  29. opt.target.top = Math.min(Math.max(top, topBound), bottomBound - movingBox.height)
  30. })

十、分组

        Groups是Fabric最强大的功能之一,它可以将任意数量的Fabric对象组合在一起,形成一个小组,分组后,所有对象都可以一起移动、修改、缩放、旋转甚至更改其外观等

  1. let group = new fabric.Group([circle, text], {
  2. left: 100,
  3. top: 100,
  4. angle: -10
  5. })
  6. canvas.add(group)

        修改分组的某个对象的属性:

  1. group.item(0).set("fill","red");
  2. group.item(1).set({
  3. text:"trololo",
  4. fill:"white"
  5. })

        分组时要记住的另一件事是对象的状态。例如,在与图像组成组时,需要确保这些图像已完全加载:

  1. fabric.Image.fromURL(logo, img => {
  2. let img1 = img.scale(0.3).set({left: 0, top: 0})
  3. fabric.Image.fromURL(logo, img => {
  4. let img2 = img.scale(0.3).set({left: 80, top: 0})
  5. fabric.Image.fromURL(logo, img => {
  6. let img3 = img.scale(0.3).set({left: 160, top: 0})
  7. let group = new fabric.Group([img1, img2, img3], {
  8. left: 10,
  9. top: 400
  10. })
  11. canvas.add(group)
  12. })
  13. })
  14. })

十一、动画

        每个Fabric对象都有一个animate方法,该方法可以动画化该对象,animate(动画属性,动画的结束值,[动画的详细信息])

  1. let rect = new fabric.Rect({
  2. left: 100,
  3. top: 100,
  4. width: 100,
  5. height: 100,
  6. fill: 'red'
  7. })
  8. rect.animate("angle", 45, {
  9. onChange: canvas.renderAll.bind(canvas)
  10. })
  11. canvas.add(rect)

        第一个参数是要设置动画的属性。第二个参数是动画的结束值。如果矩形具有-15°的角度,并且我们传递了45,则动画将从-15°变为45°。第三个参数是一个可选对象,用于指定动画的详细信息-持续时间,回调,缓动等

  1. rect.animate("angle", 45, {
  2. from: 0, // 允许指定可设置动画的属性的起始值(如果我们不希望使用当前值)
  3. duration: 1000, // 默认为500(ms),可用于更改动画的持续时间
  4. easing: fabric.util.ease.easeOutBounce, // 缓动功能 easeOutBounce、easeInCubic、easeOutCubic、easeInElastic、easeOutElastic、easeInBounce、easeOutExpo
  5. onChange: canvas.renderAll.bind(canvas), // 在每次刷新时都会执行
  6. onComplete: (e) => { console.log(e) } // 在动画结束时调用的回调
  7. })

        animate的一个方便之处在于它还支持相对值

  1. // 向右移动100px
  2. rect.animate('left', '+=100', {
  3. onChange: canvas.renderAll.bind(canvas)
  4. })
  5. // 逆时针旋转5度
  6. rect.animate('angle', '-=5', {
  7. onChange: canvas.renderAll.bind(canvas)
  8. })

十二、图像滤镜

        fabric.Image的每个实例都具有“ filters”属性,该属性是一个简单的过滤器数组。该阵列中的每个过滤器都是Fabric过滤器之一的实例。或您自己的自定义过滤器的实例。

  1. let url = 'http://localhost:82/public/img/5.png' // 图片url
  2. let base64Url = await this.imgUrlToBase64(url) // 图片base64 url
  3. // 正常照片
  4. fabric.Image.fromURL(url, img => {
  5. img.scale(0.3)
  6. canvas.add(img)
  7. })
  8. // 单个滤镜
  9. fabric.Image.fromURL(base64Url, img => {
  10. img.scale(0.3)
  11. img.left = 300
  12. // 添加滤镜
  13. img.filters.push(new fabric.Image.filters.Grayscale())
  14. // 图片加载完成之后,应用滤镜效果
  15. img.applyFilters()
  16. canvas.add(img)
  17. })
  18. // 叠加滤镜
  19. fabric.Image.fromURL(base64Url, img => {
  20. img.scale(0.3)
  21. img.set({
  22. left: 300,
  23. top: 250,
  24. })
  25. img.filters.push(
  26. new fabric.Image.filters.Grayscale(),
  27. new fabric.Image.filters.Sepia(), //色偏
  28. new fabric.Image.filters.Brightness({ brightness: 0.2 }) //亮度
  29. )
  30. img.applyFilters()
  31. canvas.add(img)
  32. })

        (说明:这里图片可能会出错,放本地图片地址会报“Cannot read property 'naturalWidth' of null”的错误,直接放网络图片地址会报“Failed to execute 'texImage2D' on 'WebGLRenderingContext': The image element contains cross-origin data, and may not be loaded.”的错误。解决方法就是将转为Base64格式

十三、渐变

        Fabric支持在所有对象上设置填充或描边属性的渐变,首先创建渐变,然后将其分配给填充或描边。

1、线性渐变

  1. // 圆
  2. let circle = new fabric.Circle({
  3. left: 100,
  4. top: 100,
  5. radius: 50,
  6. })
  7. let gradient = new fabric.Gradient({
  8. type: 'linear', // linear or radial
  9. gradientUnits: 'pixels', // pixels or pencentage 像素 或者 百分比
  10. coords: { x1: 0, y1: 0, x2: circle1.width, y2: 0 }, // 至少2个坐标对(x1,y1和x2,y2)将定义渐变在对象上的扩展方式
  11. colorStops:[ // 定义渐变颜色的数组
  12. { offset: 0, color: 'red' },
  13. { offset: 0.2, color: 'orange' },
  14. { offset: 0.4, color: 'yellow' },
  15. { offset: 0.6, color: 'green' },
  16. { offset: 0.8, color: 'blue' },
  17. { offset: 1, color: 'purple' },
  18. ]
  19. })
  20. circle.set('fill', gradient);
  21. canvas.add(circle)

2、径向渐变

  1. let circle = new fabric.Circle({
  2. left: 100,
  3. top: 100,
  4. radius: 50
  5. })
  6. let gradient = new fabric.Gradient({
  7. type: 'radial',
  8. coords: {
  9. r1: 50,
  10. r2: 0,
  11. x1: 50,
  12. y1: 50,
  13. x2: 50,
  14. y2: 50,
  15. },
  16. colorStops: [
  17. { offset: 0, color: '#fee140' },
  18. { offset: 1, color: '#fa709a' }
  19. ]
  20. })
  21. circle.set('fill', gradient);
  22. canvas.add(circle)

十四、拖拽和缩放画布

1、拖拽画布

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. lastPosX: 0, // 上次鼠标位置X坐标
  6. lastPosY: 0, // 上次鼠标位置Y坐标
  7. isDragging: false, // 是否可以拖拽画布
  8. }
  9. },
  10. mounted() {
  11. ... // 初始化canvas
  12. this.canvas.on('mouse:down', this.onMouseDown)
  13. this.canvas.on('mouse:move', this.onMouseMove)
  14. this.canvas.on('mouse:up', this.onMouseUp)
  15. },
  16. methods: {
  17. // 监听鼠标按下事件
  18. onMouseDown(opt) {
  19. this.lastPosX = opt.e.clientX
  20. this.lastPosY = opt.e.clientY
  21. this.isDragging = true
  22. },
  23. // 监听鼠标移动事件
  24. onMouseMove(opt) {
  25. if (this.isDragging) {
  26. this.canvas.viewportTransform[4] += opt.e.clientX - this.lastPosX
  27. this.canvas.viewportTransform[5] += opt.e.clientY - this.lastPosY
  28. this.canvas.requestRenderAll()
  29. this.lastPosX = opt.e.clientX
  30. this.lastPosY = opt.e.clientY
  31. }
  32. },
  33. // 监听鼠标松开事件
  34. onMouseUp(opt) {
  35. if (this.isDragging) {
  36. this.canvas.setViewportTransform(this.canvas.viewportTransform)
  37. this.isDragging = false
  38. }
  39. }
  40. }
  41. }
  42. </script>

2、以画布中心点为基准手动缩放

  1. <template>
  2. <el-tooltip content="放大" placement="bottom-start">
  3. <span class="iconfont icon-fangda" @click="onManualScale(-100)"></span>
  4. </el-tooltip>
  5. <el-tooltip content="缩小" placement="bottom-start">
  6. <span class="iconfont icon-suoxiao" @click="onManualScale(100)"></span>
  7. </el-tooltip>
  8. </template>
  9. <script>
  10. export default {
  11. // 中心点缩放画布
  12. onManualScale(delta) {
  13. let zoom = canvas.getZoom() // 获取画布当前缩放值
  14. zoom *= 0.999 ** delta
  15. zoom = zoom > 10 ? 10 : (zoom < 0.1 ? 0.1 : zoom) // 最大放大10倍,最小缩小至10%
  16. canvas.zoomToPoint({ // 以画布中心点为基准缩放
  17. x: this.canvasBoxWidth / 2, // canvasBoxWidth 画布宽度
  18. y: this.canvasBoxHeight / 2 // canvasBoxHeight 画布高度
  19. }, zoom)
  20. }
  21. }
  22. </script>

3、以鼠标指针位置为基准缩放

  1. this.canvas.on('mouse:wheel', this.onMouseWheel)
  2. // 监听鼠标放大缩小事件
  3. onMouseWheel(opt) {
  4. let delta = opt.e.deltaY // 滚轮,向上滚一下是 -100,向下滚一下是 100
  5. let zoom = this.canvas.getZoom() // 获取画布当前缩放值
  6. zoom *= 0.999 ** delta
  7. zoom = zoom > 10 ? 10 : (zoom < 0.1 ? 0.1 : zoom) // 最大放大10倍,最小缩小至10%
  8. this.canvas.zoomToPoint({ // 以鼠标指针位置为基准缩放
  9. x: opt.e.offsetX,
  10. y: opt.e.offsetY
  11. }, zoom)
  12. opt.e.preventDefault()
  13. opt.e.stopPropagation()
  14. }

十五、右键菜单删除

  1. <template>
  2. <div class="canvas-box" ref="canvasBox">
  3. <canvas id="canvas"></canvas>
  4. <div id="delMenu" ref="delMenu" v-show="isShowDelMenu" :style="delMenuStyle" @contextmenu.prevent="">
  5. <el-button type="iconButton" icon="h-icon-delete" @click="handleDeleteMenu">删除</el-button>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'PointerDetail',
  12. data () {
  13. return {
  14. canvas: null,
  15. activeEle: null, // 上次选中元素
  16. isShowDelMenu: false, // 是否显示删除弹窗
  17. delMenuStyle: '' // 删除弹窗定位样式
  18. }
  19. },
  20. mounted() {
  21. this.init()
  22. },
  23. methods: {
  24. // 初始化
  25. init() {
  26.     this.canvas = new fabric.Canvas('canvas', {
  27.     fireRightClick: true, // 启用右键,button的数字为3
  28.     stopContextMenu: true, // 禁止默认右键菜单
  29.     })
  30. this.canvas.on('mouse:down', this.onMouseDown)
  31. },
  32. // 监听鼠标按下事件
  33. onMouseDown(opt) {
  34. // 还原上次选中状态
  35. if (this.activeEle) {
  36. this.activeEle.set('fill', 'transparent')
  37. this.canvas.renderAll()
  38. }
  39. this.activeEle = opt.target || null
  40. // 按下鼠标右键
  41. if (opt.button === 3) {
  42. // 点击到非图片控件 显示删除弹窗和填充控件背景色
  43. if (opt.target && opt.target.type !== 'image') {
  44. this.activeEle.set('fill', 'rgba(100, 100, 255, 0.3)')
  45. this.canvas.renderAll()
  46. this.isShowDelMenu = true
  47. this.$nextTick(() => {
  48. this.delMenuStyle = this.getMenuStyle(this.$refs.delMenu, opt)
  49. })
  50. } else {
  51. // 否则隐藏删除弹窗
  52. this.hiddenMenu()
  53. }
  54. // 按下鼠标左键
  55. } else {
  56. this.hiddenMenu()
  57. }
  58. },
  59. // 获取弹窗坐标
  60. getMenuStyle(ele, opt) {
  61. let menuWidth = ele.offsetWidth
  62. let menuHeight = ele.offsetHeight
  63. let pointX = opt.pointer.x + 2
  64. let pointY = opt.pointer.y + 2
  65. if (this.$refs.canvasBox.offsetWidth - pointX <= menuWidth) {
  66. pointX -= menuWidth
  67. }
  68. if (this.$refs.canvasBox.offsetHeight - pointY <= menuHeight) {
  69. pointY -= menuHeight
  70. }
  71. return `left: ${pointX}px; top: ${pointY}px;`
  72. },
  73. // 隐藏菜单
  74. hiddenMenu() {
  75. this.activeEle = null
  76. this.isShowDelMenu = false
  77. },
  78. // 删除选中元素
  79. handleDeleteMenu() {
  80. this.canvas.remove(this.activeEle)
  81. this.canvas.requestRenderAll()
  82. this.hiddenMenu()
  83. }
  84. }
  85. }

十六、自由绘画

1、开启绘图模式

  1. let canvas = new fabric.Canvas('canvas', {
  2. isDrawingMode: true // 开启绘图模式
  3. })
  4. canvas.freeDrawingBrush.color = '#11999e' // 设置画笔颜色
  5. canvas.freeDrawingBrush.width = 10 // 设置画笔粗细
  6. canvas.freeDrawingBrush.shadow = new fabric.Shadow({ // 设置画笔投影
  7. blur: 10,
  8. offsetX: 10,
  9. offsetY: 10,
  10. affectStroke: true,
  11. color: '#30e3ca'
  12. })

2、关闭绘图模式

  1. canvas.isDrawingMode = false

十七、绘制背景图片

1、方式一:通过img元素添加

  1. <img src="@/assets/images/logo.png" id="logo">
  2. let img = document.getElementById('logo')
  3. img.onload = () => {
  4. let canvasImage = new fabric.Image(imgElement, {
  5. left: 100, // 距离画布左侧距离
  6. top: 100, // 距离画布顶部距离
  7. width: 200, // 图片宽度
  8. height: 200, // 图片高度
  9. angle: 50, // 旋转
  10. opacity: 1 // 透明度
  11. })
  12. canvas.add(canvasImage)
  13. }

2、方式二:通过图片路径添加

  1. let url = 'http://localhost:82/public/img/logo.png'
  2. fabric.Image.fromURL(url, img => {
  3. let canvasImage = img.set({
  4. scaleX: 0.5,
  5. scaleY: 0.5
  6. })
  7. canvas.add(canvasImage)
  8. })

十八、绘制文本

        Fabric也提供了文本的相关功能,Fabric文本允许以面向对象的方式处理文本,原生canvas方法,只允许在非常低的级别上填充或描边文本,通过实例化fabric.Text实例,我们就可以使用文本,就像我们将使用任何其他Fabric对象:移动它,缩放它,更改其属性等, 其次它提供比canvas给我们更丰富的功能,包括:

  1. Multiline support // 支持多行
  2. Text alignment // 文本对齐 Left、center、right
  3. Text background // 文本背景 背景也遵循文本对齐
  4. Text decoration // 文字装饰 下划线Underline、上划线overline、贯穿线strike-through
  5. Line height // 行高 使用多行文字时出错
  6. Char spacing // 字符间距 使文本更紧凑或间距更大
  7. Subranges // 子范围 将颜色和属性应用于文本对象的子范围
  8. Multibyte // 多字节 支持表情符号
  9. On canvas editing // 交互式画布编辑 可以直接在canvas上键入文本

1、基础用法

  1. let text = new fabric.Text('Hello World!', {
  2. left: 40,
  3. top: 10,
  4. fontFamily: 'Comic Sans', // 字体
  5. fontSize: 60, // 字号
  6. fontWeight: 600, // 字体重量(粗细),normal、bold 或 数字(100、200、400、600、800)
  7. fontStyle: 'normal', // 字体风格 正常 normal 或 斜体 italic
  8. charSpacing: 100, // 字距
  9. fill: 'red', // 字体颜色
  10. cornerColor: 'pink', // 角的颜色(被选中时)
  11. angle: 30, // 旋转
  12. backgroundColor: '#ffd460', // 背景色
  13. borderColor: 'yellowGreen', // 边框颜色(被选中时)
  14. borderScaleFactor: 4, // 边框粗细(被选中时)
  15. borderDashArray: [10, 4, 20], // 创建边框虚线
  16. stroke: '#3f72af', // 文字描边颜色(蓝色)
  17. strokeWidth: 2, // 文字描边粗细
  18. textAlign: 'left', // 对齐方式:left 左对齐; right 右对齐; center 居中
  19. opacity: 0.8, // 不透明度
  20. // text: '雷猴', // 文字内容,会覆盖之前设置的值
  21. selectable: true, // 能否被选中,默认true
  22. shadow: 'rgba(0, 0, 0, 0.5) 5px 5px 5px', // 投影
  23. })
  24. canvas.add(text)

2、文本修饰

  1. // 下划线
  2. let underlineText = new fabric.Text("I am an undrline text", {
  3. underline: true
  4. })
  5. canvas.add(underlineText)
  6. // 贯穿线
  7. let strokeThroughText = new fabric.Text("I am a stroke-through text", {
  8. linethrough: true,
  9. top: 40
  10. })
  11. canvas.add(strokeThroughText)
  12. // 上划线
  13. let overlineText = new fabric.Text("I am overline text", {
  14. overline:true,
  15. top: 80
  16. })
  17. canvas.add(overlineText)

3、可编辑文本

  1. let IText = new fabric.IText('雷猴啊,双击打几个字试下~', {
  2. fontFamily: 'Comic Sans'
  3. })
  4. canvas.add(IText)

十九、绘制线和路径

1、绘制直线

  1. let line = new fabric.Line([0, 100, 100, 100], {
  2. fill: 'green', // 填充色
  3. stroke: 'green', // 笔触颜色
  4. strokeWidth: 2, // 笔触宽度
  5. });
  6. canvas.add(line);

2、绘制虚线

        在绘制直线的基础上添加属性strokeDashArray[a,b],表示每隔a个像素空b个像素。

  1. let line = new fabric.Line([0, 100, 100, 100], {
  2. fill: 'green', // 填充色
  3. stroke: 'green', // 笔触颜色
  4. strokeWidth: 2, // 笔触宽度
  5. strokeDashArray:[3,1]
  6. });
  7. canvas.add(line);

3、绘制路径

  • Fabric.js使用 new fabric.Path 创建路径。
  • M:可以理解为新的起始点 x,y 坐标
  • L:每个折点的 x,y 坐标
  • z:自动闭合(自动把结束点和起始点连接起来)

  1. let path = new fabric.Path('M 0 0 L 200 100 L 170 200 z')
  2. path.set({
  3. top: 120, // 距离容器顶部距离 120px
  4. left: 120, // 距离容器左侧距离 120px
  5. fill: 'hotpink', // 填充 亮粉色
  6. opacity: 0.5, // 不透明度 50%
  7. stroke: 'black', // 描边颜色 黑色
  8. strokeWidth: 10 // 描边粗细 10px
  9. })

        上述代码第一行“M”代表“移动”命令,“M 0 0” 代表把画笔移动到(0, 0)点坐标。“L”代表“线”,“L 200 100 ”的意思是使用钢笔画一条线,从(0, 0)坐标画到(200, 100)坐标。“z” 代表让图形闭合路径。这样就画出了一个三角形。画好三角形后,我们可以用set( )方法对三角形的位置、颜色、角度、透明度等属性进行设置。

二十、自由绘制矩形

  1. <template>
  2. <div class="canvas-box" ref="canvasBox">
  3. <canvas id="canvas"></canvas>
  4. <el-button @click="handleActiveRect">绘制矩形</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'PointerDetail',
  10. data () {
  11. return {
  12. canvas: null,
  13. lastPoint: null, // 上次鼠标点位坐标
  14. strokeColor: 'transparent', // 轮廓填充颜色
  15. isActiveRect: false, // 是否激活绘制矩形
  16. rectList: [] // 绘制的矩形列表
  17. }
  18. },
  19. mounted() {
  20. this.init()
  21. },
  22. methods: {
  23. // 初始化
  24. init() {
  25. this.canvas = new fabric.Canvas('canvas', {
  26. width: this.$refs.canvasBox.offsetWidth,
  27. height: this.$refs.canvasBox.offsetHeight,
  28. backgroundColor: '#2E3136',
  29. selectionColor: 'transparent',
  30. selectionBorderColor: 'transparent',
  31. hoverCursor: 'default'
  32. })
  33. this.canvas.on('mouse:down', this.onMouseDown)
  34. this.canvas.on('mouse:up', this.onMouseUp)
  35. this.canvas.on('object:added', this.onObjectAdded)
  36. },
  37. // 监听鼠标按下事件
  38. onMouseDown(opt) {
  39. if (this.isActiveRect) {
  40. this.lastPoint = opt.absolutePointer || null
  41. this.strokeColor = '#00FF64'
  42. }
  43. },
  44. // 监听鼠标松开事件
  45. onMouseUp(opt) {
  46. if (this.isActiveRect) {
  47. this.drawRect(opt.absolutePointer)
  48. }
  49. },
  50. // 绘制完成元素事件
  51. onObjectAdded(opt) {
  52. let target = opt.target
  53. if (target.stroke === '#00FF64') {
  54. this.isActiveRect && this.rectList.push(target)
  55. }
  56. },
  57. // 绘制矩形
  58. drawRect(pointer) {
  59. if (!this.lastPoint || JSON.stringify(this.lastPoint) === JSON.stringify(pointer)) { // 点击事件,不生成矩形
  60. return
  61. }
  62. let top = Math.min(this.lastPoint.y, pointer.y)
  63. let left = Math.min(this.lastPoint.x, pointer.x)
  64. let width = Math.abs(this.lastPoint.x - pointer.x)
  65. let height = Math.abs(this.lastPoint.y - pointer.y)
  66. let rect = new fabric.Rect({
  67. top,
  68. left,
  69. width,
  70. height,
  71. fill: 'transparent',
  72. stroke: this.strokeColor,
  73. selectable: false
  74. })
  75. this.canvas.add(rect)
  76. this.lastPoint = null
  77. this.strokeColor = 'transparent'
  78. },
  79. // 激活绘制矩形
  80. handleActiveRect() {
  81. this.isActiveRect = !this.isActiveRect
  82. if(this.isActiveRect) {
  83. this.canvas.selectionBorderColor = '#00FF64'
  84. }
  85. }
  86. }
  87. }

二十一、自由绘制圆形

  1. <template>
  2. <div class="canvas-box" ref="canvasBox">
  3. <canvas id="canvas"></canvas>
  4. <el-button @click="handleActiveCircle">绘制圆形</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'PointerDetail',
  10. data () {
  11. return {
  12. canvas: null,
  13. canvasCircle: null,
  14. downPoint: null,
  15. strokeColor: 'transparent', // 轮廓填充颜色
  16. isActiveCircle: false, // 是否激活绘制圆形
  17. circleList: [] // 绘制的圆形列表
  18. }
  19. },
  20. mounted() {
  21. this.init()
  22. },
  23. methods: {
  24. // 初始化
  25. init() {
  26. this.canvas = new fabric.Canvas('canvas', {
  27. width: this.$refs.canvasBox.offsetWidth,
  28. height: this.$refs.canvasBox.offsetHeight,
  29. backgroundColor: '#2E3136',
  30. selectionColor: 'transparent',
  31. selectionBorderColor: 'transparent',
  32. hoverCursor: 'default'
  33. })
  34. this.canvas.on('mouse:down', this.onMouseDown)
  35. this.canvas.on('mouse:move', this.onMouseMove)
  36. this.canvas.on('mouse:up', this.onMouseUp)
  37. this.canvas.on('object:added', this.onObjectAdded)
  38. },
  39. // 监听鼠标按下事件
  40. onMouseDown(opt) {
  41. if (this.isActiveCircle) {
  42. this.downPoint = opt.absolutePointer
  43. this.strokeColor = '#00FF64'
  44. this.canvasCircle = new fabric.Circle({
  45. top: this.downPoint.y,
  46. left: this.downPoint.x,
  47. radius: 0,
  48. fill: 'transparent',
  49. stroke: this.strokeColor,
  50. strokeWidth: 2,
  51. selectable: false,
  52. })
  53. this.canvas.add(this.canvasCircle)
  54. }
  55. },
  56. // 监听鼠标移动事件
  57. onMouseMove(opt) {
  58. if (this.isActiveCircle && this.canvasCircle) {
  59. let radius = Math.min(Math.abs(this.downPoint.x - opt.absolutePointer.x), Math.abs(this.downPoint.y - opt.absolutePointer.y)) / 2
  60. let top = opt.absolutePointer.y > this.downPoint.y ? this.downPoint.y : this.downPoint.y - radius * 2
  61. let left = opt.absolutePointer.x > this.downPoint.x ? this.downPoint.x : this.downPoint.x - radius * 2
  62. this.canvasCircle.set('radius', radius)
  63. this.canvasCircle.set('top', top)
  64. this.canvasCircle.set('left', left)
  65. this.canvas.requestRenderAll()
  66. }
  67. },
  68. // 监听鼠标松开事件
  69. onMouseUp(opt) {
  70. if (this.isActiveCircle) {
  71. if (JSON.stringify(this.downPoint) === JSON.stringify(opt.absolutePointer)) {
  72. this.canvas.remove(this.canvasCircle)
  73. } else {
  74. if (this.canvasCircle){
  75. this.canvasCircle.set('stroke', this.strokeColor)
  76. }
  77. }
  78. this.canvasCircle = null
  79. }
  80. },
  81. // 绘制完成元素事件
  82. onObjectAdded(opt) {
  83. let target = opt.target
  84. if (target.stroke === '#00FF64') {
  85. this.isActiveCircle && this.circleList.push(target)
  86. }
  87. },
  88. // 激活绘制圆形
  89. handleActiveCircle() {
  90. this.isActiveCircle = !this.isActiveCircle
  91. if(this.isActiveCircle) {
  92. this.canvas.selectionBorderColor = '#00FF64'
  93. }
  94. }
  95. }
  96. }

二十二、自由绘制椭圆形

  1. <template>
  2. <div class="canvas-box" ref="canvasBox">
  3. <canvas id="canvas"></canvas>
  4. <el-button @click="handleActiveEllipse">绘制椭圆形</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'PointerDetail',
  10. data () {
  11. return {
  12. canvas: null,
  13. canvasEllipse: null,
  14. downPoint: null,
  15. strokeColor: 'transparent', // 轮廓填充颜色
  16. isActiveEllipse: false, // 是否激活绘制椭圆形
  17. ellipseList: [] // 绘制的椭圆形列表
  18. }
  19. },
  20. mounted() {
  21. this.init()
  22. },
  23. methods: {
  24. // 初始化
  25. init() {
  26. this.canvas = new fabric.Canvas('canvas', {
  27. width: this.$refs.canvasBox.offsetWidth,
  28. height: this.$refs.canvasBox.offsetHeight,
  29. backgroundColor: '#2E3136',
  30. selectionColor: 'transparent',
  31. selectionBorderColor: 'transparent',
  32. hoverCursor: 'default'
  33. })
  34. this.canvas.on('mouse:down', this.onMouseDown)
  35. this.canvas.on('mouse:move', this.onMouseMove)
  36. this.canvas.on('mouse:up', this.onMouseUp)
  37. this.canvas.on('object:added', this.onObjectAdded)
  38. },
  39. // 监听鼠标按下事件
  40. onMouseDown(opt) {
  41. if (this.isActiveEllipse) {
  42. this.downPoint = opt.absolutePointer
  43. this.strokeColor = '#00FF64'
  44. this.canvasEllipse = new fabric.Ellipse({
  45. top: this.downPoint.y,
  46. left: this.downPoint.x,
  47. rx: 0,
  48. ry: 0,
  49. fill: 'transparent',
  50. stroke: this.strokeColor,
  51. strokeWidth: 2,
  52. selectable: false,
  53. })
  54. this.canvas.add(this.canvasEllipse)
  55. }
  56. },
  57. // 监听鼠标移动事件
  58. onMouseMove(opt) {
  59. if (this.isActiveEllipse && this.canvasEllipse) {
  60. let rx = Math.abs(this.downPoint.x - opt.absolutePointer.x) / 2
  61. let ry = Math.abs(this.downPoint.y - opt.absolutePointer.y) / 2
  62. let top = opt.absolutePointer.y > this.downPoint.y ? this.downPoint.y : this.downPoint.y - ry * 2
  63. let left = opt.absolutePointer.x > this.downPoint.x ? this.downPoint.x : this.downPoint.x - rx * 2
  64. this.canvasEllipse.set('rx', rx)
  65. this.canvasEllipse.set('ry', ry)
  66. this.canvasEllipse.set('top', top)
  67. this.canvasEllipse.set('left', left)
  68. this.canvas.requestRenderAll()
  69. }
  70. },
  71. // 监听鼠标松开事件
  72. onMouseUp(opt) {
  73. if (this.isActiveEllipse) {
  74. if (JSON.stringify(this.downPoint) === JSON.stringify(opt.absolutePointer)) {
  75. this.canvas.remove(this.canvasEllipse)
  76. } else {
  77. if (this.canvasEllipse){
  78. this.canvasEllipse.set('stroke', this.strokeColor)
  79. }
  80. }
  81. this.canvasEllipse = null
  82. }
  83. },
  84. // 绘制完成元素事件
  85. onObjectAdded(opt) {
  86. let target = opt.target
  87. if (target.stroke === '#00FF64') {
  88. this.isActiveEllipse && this.ellipseList.push(target)
  89. }
  90. },
  91. // 激活绘制椭圆形
  92. handleActiveEllipse() {
  93. this.isActiveEllipse = !this.isActiveEllipse
  94. if(this.isActiveEllipse) {
  95. this.canvas.selectionBorderColor = '#00FF64'
  96. }
  97. }
  98. }
  99. }

二十三、自由绘制三角形

  1. <template>
  2. <div class="canvas-box" ref="canvasBox">
  3. <canvas id="canvas"></canvas>
  4. <el-button @click="handleActiveTriangle">绘制三角形</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'PointerDetail',
  10. data () {
  11. return {
  12. canvas: null,
  13. canvasTriangle: null,
  14. downPoint: null,
  15. strokeColor: 'transparent', // 轮廓填充颜色
  16. isActiveTriangle: false, // 是否激活绘制三角形
  17. triangleList: [] // 绘制的三角形列表
  18. }
  19. },
  20. mounted() {
  21. this.init()
  22. },
  23. methods: {
  24. // 初始化
  25. init() {
  26. this.canvas = new fabric.Canvas('canvas', {
  27. width: this.$refs.canvasBox.offsetWidth,
  28. height: this.$refs.canvasBox.offsetHeight,
  29. backgroundColor: '#2E3136',
  30. selectionColor: 'transparent',
  31. selectionBorderColor: 'transparent',
  32. hoverCursor: 'default'
  33. })
  34. this.canvas.on('mouse:down', this.onMouseDown)
  35. this.canvas.on('mouse:move', this.onMouseMove)
  36. this.canvas.on('mouse:up', this.onMouseUp)
  37. this.canvas.on('object:added', this.onObjectAdded)
  38. },
  39. // 监听鼠标按下事件
  40. onMouseDown(opt) {
  41. if (this.isActiveTriangle) {
  42. this.downPoint = opt.absolutePointer
  43. this.strokeColor = '#00FF64'
  44. this.canvasTriangle = new fabric.Triangle({
  45. top: this.downPoint.y,
  46. left: this.downPoint.x,
  47. width: 0,
  48. height: 0,
  49. fill: 'transparent',
  50. stroke: this.strokeColor,
  51. strokeWidth: 2,
  52. selectable: false,
  53. })
  54. this.canvas.add(this.canvasTriangle)
  55. }
  56. },
  57. // 监听鼠标移动事件
  58. onMouseMove(opt) {
  59. if (this.isActiveTriangle && this.canvasTriangle) {
  60. let width = Math.abs(this.downPoint.x - opt.absolutePointer.x)
  61. let height = Math.abs(this.downPoint.y - opt.absolutePointer.y)
  62. let top = opt.absolutePointer.y > this.downPoint.y ? this.downPoint.y : opt.absolutePointer.y
  63. let left = opt.absolutePointer.x > this.downPoint.x ? this.downPoint.x : opt.absolutePointer.x
  64. this.canvasTriangle.set('width', width)
  65. this.canvasTriangle.set('height', height)
  66. this.canvasTriangle.set('top', top)
  67. this.canvasTriangle.set('left', left)
  68. this.canvas.requestRenderAll()
  69. }
  70. },
  71. // 监听鼠标松开事件
  72. onMouseUp(opt) {
  73. if (this.isActiveTriangle) {
  74. if (JSON.stringify(this.downPoint) === JSON.stringify(opt.absolutePointer)) {
  75. this.canvas.remove(this.canvasTriangle)
  76. } else {
  77. if (this.canvasTriangle){
  78. this.canvasTriangle.set('stroke', this.strokeColor)
  79. }
  80. }
  81. this.canvasTriangle = null
  82. }
  83. },
  84. // 绘制完成元素事件
  85. onObjectAdded(opt) {
  86. let target = opt.target
  87. if (target.stroke === '#00FF64') {
  88. this.isActiveTriangle && this.triangleList.push(target)
  89. }
  90. },
  91. // 激活绘制矩形
  92. handleActiveTriangle() {
  93. this.isActiveTriangle = !this.isActiveTriangle
  94. if(this.isActiveTriangle) {
  95. this.canvas.selectionBorderColor = '#00FF64'
  96. }
  97. }
  98. }
  99. }

二十四、自由绘制多边形

  1. <template>
  2. <div class="canvas-box" ref="canvasBox">
  3. <canvas id="canvas"></canvas>
  4. <el-button @click="handleActivePolygon">绘制多边形</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'PointerDetail',
  10. data () {
  11. return {
  12. canvas: null,
  13. canvasPolygon: null,
  14. strokeColor: 'transparent', // 轮廓填充颜色
  15. isActivePolygon: false, // 是否激活绘制多边形
  16. polygonList: [] // 绘制的多边形列表
  17. }
  18. },
  19. mounted() {
  20. this.init()
  21. },
  22. methods: {
  23. // 初始化
  24. init() {
  25. this.canvas = new fabric.Canvas('canvas', {
  26. width: this.$refs.canvasBox.offsetWidth,
  27. height: this.$refs.canvasBox.offsetHeight,
  28. backgroundColor: '#2E3136',
  29. selectionColor: 'transparent',
  30. selectionBorderColor: 'transparent',
  31. hoverCursor: 'default'
  32. })
  33. this.canvas.on('mouse:down', this.onMouseDown)
  34. this.canvas.on('mouse:move', this.onMouseMove)
  35. this.canvas.on('mouse:dblclick', this.onDblclick)
  36. this.canvas.on('object:added', this.onObjectAdded)
  37. },
  38. // 监听鼠标按下事件
  39. onMouseDown(opt) {
  40. if (this.isActivePolygon) {
  41. this.strokeColor = '#00FF64'
  42. if (this.canvasPolygon === null) {
  43. this.createPolygon(opt)
  44. } else {
  45. this.changeCurrentPolygon(opt)
  46. }
  47. }
  48. },
  49. // 监听鼠标移动事件
  50. onMouseMove(opt) {
  51. if (this.isActivePolygon && this.canvasPolygon) {
  52. this.changePolygonBelt(opt)
  53. }
  54. },
  55. // 鼠标双击事件
  56. onDblclick(opt) {
  57. this.finishPolygon(opt)
  58. },
  59. // 绘制完成元素事件
  60. onObjectAdded(opt) {
  61. let target = opt.target
  62. if (target.stroke === '#00FF64') {
  63. this.isActivePolygon && this.polygonList.push(target)
  64. }
  65. },
  66. // 创建多边形
  67. createPolygon(opt) {
  68. this.canvasPolygon = new fabric.Polygon([
  69. { x: opt.absolutePointer.x, y: opt.absolutePointer.y },
  70. { x: opt.absolutePointer.x, y: opt.absolutePointer.y }
  71. ], {
  72. fill: 'transparent',
  73. stroke: this.strokeColor,
  74. objectCaching: false
  75. })
  76. this.canvas.add(this.canvasPolygon)
  77. },
  78. // 修改当前正在创建的多边形
  79. changeCurrentPolygon(opt) {
  80. let points = this.canvasPolygon.points
  81. points.push({
  82. x: opt.absolutePointer.x,
  83. y: opt.absolutePointer.y
  84. })
  85. this.canvas.requestRenderAll()
  86. },
  87. // 多边形橡皮带
  88. changePolygonBelt(opt) {
  89. let points = this.canvasPolygon.points
  90. points[points.length - 1].x = opt.absolutePointer.x
  91. points[points.length - 1].y = opt.absolutePointer.y
  92. this.canvas.requestRenderAll()
  93. },
  94. // 完成多边形绘制
  95. finishPolygon(opt) {
  96. let points = this.canvasPolygon.points
  97. points[points.length - 1].x = opt.absolutePointer.x
  98. points[points.length - 1].y = opt.absolutePointer.y
  99. points.pop()
  100. points.pop()
  101. this.canvas.remove(this.canvasPolygon)
  102. if (points.length > 2) {
  103. let polygon = new fabric.Polygon(points, {
  104. stroke: this.strokeColor,
  105. fill: 'transparent',
  106. selectable: false
  107. })
  108. this.canvas.add(polygon)
  109. } else {
  110. this.$message.warning('标记框小于最小标定像素!')
  111. }
  112. this.canvasPolygon = null
  113. this.canvas.requestRenderAll()
  114. this.strokeColor = 'transparent'
  115. },
  116. // 激活绘制多边形
  117. handleActivePolygon() {
  118. this.isActivePolygon = !this.isActivePolygon
  119. if(this.isActivePolygon) {
  120. this.canvas.selectionBorderColor = '#00FF64'
  121. }
  122. }
  123. }
  124. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/98110
推荐阅读
相关标签
  

闽ICP备14008679号