当前位置:网站首页>Fabric.js 自由绘制椭圆
Fabric.js 自由绘制椭圆
2022-07-02 11:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
本文讲解在 Fabric.js
中如何自由绘制椭圆形,如果你还不了解 Fabric.js
,可以查阅 《Fabric.js从入门到精通》。
效果如下图所示
<br>
<br>
思路
Fabric.js
在初始化画布之后,可以执行框选操作,但默认是使用矩形的方式来框选,如下图所示:
我希望使用鼠标创建椭圆的时候可以又一个椭圆的影子出来,这能让我更方便去观察当前要画的椭圆大概是什么样子。
所以我们可以先把框选时的边框和背景色设置成透明,然后再框选时监听鼠标点击、移动、松开的事件,从而绘制出一个椭圆。
<br>
详细思路步骤如下:
- 将框选时边框和背景设为透明
- 鼠标点击时创建椭圆
- 鼠标移动时修改椭圆尺寸
- 鼠标松开时生成正式的椭圆
<br>
我将整个绘制事件拆分成上面4步,但其实第3步还是有点难度的,我们要考虑几种情况:
- 点击时的坐标在移动时的左下方
- 点击时的坐标在移动时的左上方
- 点击时的坐标在移动时的右上方
- 点击时的坐标在移动时的右下方
<br>
这4种情况我在 《Fabric.js 自由绘制矩形》 里逐一分析过,可以去看看(记得点个赞哦~)
<br>
<br>
编码
在理清思路后,我们可以正式编码阶段。
我还是会按照1、2、3、4的步骤去编码,你在两个浏览器窗口中打开本文,一边是编码,一边是思路,对照着看可能会清晰点。
<canvas id="canvas" width="800" height="800"></canvas><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script><script> let canvas = null let currentType = 'default' let downPoint = null let upPoint = null let currentEllipse = null // 临时椭圆 // 初始化画板 function initCanvas() { canvas = new fabric.Canvas('canvas') canvas.on('mouse:down', canvasMouseDown) // 鼠标在画布上按下 canvas.on('mouse:move', canvasMouseMove) // 鼠标在画布上移动 canvas.on('mouse:up', canvasMouseUp) // 鼠标在画布上松开 } // 画布操作类型切换 function typeChange(opt) { currentType = opt switch(opt) { case 'default': // 默认框选模式 canvas.selection = true canvas.selectionColor = 'rgba(100, 100, 255, 0.3)' canvas.selectionBorderColor = 'rgba(255, 255, 255, 0.3)' canvas.skipTargetFind = false // 允许选中 break case 'ellipse': // 椭圆模式 // 将框选时,边框和背景设为透明 canvas.selectionColor = 'transparent' canvas.selectionBorderColor = 'transparent' canvas.skipTargetFind = true // 禁止选中 break } } // 鼠标在画布上按下 function canvasMouseDown(e) { downPoint = e.absolutePointer if (currentType === 'ellipse') { currentEllipse = new fabric.Ellipse({ top: downPoint.y, left: downPoint.x, rx: 0, ry: 0, fill: 'transparent', stroke: 'rgba(0, 0, 0, 0.2)' }) canvas.add(currentEllipse) } } // 鼠标在画布上移动 function canvasMouseMove(e) { if (currentType === 'ellipse' && currentEllipse) { const currentPoint = e.absolutePointer let rx = Math.abs(downPoint.x - currentPoint.x) / 2 let ry = Math.abs(downPoint.y - currentPoint.y) / 2 let top = currentPoint.y > downPoint.y ? downPoint.y : downPoint.y - ry * 2 let left = currentPoint.x > downPoint.x ? downPoint.x : downPoint.x - rx * 2 // 设置椭圆尺寸和所在位置 currentEllipse.set('rx', rx) currentEllipse.set('ry', ry) currentEllipse.set('top', top) currentEllipse.set('left', left) // 刷新一下画布 canvas.requestRenderAll() } } // 鼠标在画布上松开 function canvasMouseUp(e) { upPoint = e.absolutePointer if (currentType === 'ellipse') { // 需要判断点击时和松开时鼠标的坐标点是否相等,相等的话就不创建椭圆了 if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) { canvas.remove(currentEllipse) } else { // 设置椭圆样式 currentEllipse.set('stroke', '#000') } // 清除临时创建的椭圆 currentEllipse = null } } window.onload = function() { initCanvas() }</script>
从上面的代码可以看出,主要核心部分就是 “鼠标点击时,鼠标移动时,松开鼠标后” 这几个环节。鼠标移动时,要实时监听鼠标当前的坐标,松开鼠标后就不再监听。
<br>
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- 什么是 eRDMA?丨科普漫画图解
- STM32-DAC实验&高频DAC输出测试
- Selenium element positioning method
- Daily learning 2
- <口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
- 由粒子加速器产生的反中子形成的白洞
- QT new project_ MyNotepad++
- Golang quickly generates model and queryset of database tables
- C语言高级用法--函数指针:回调函数;转换表
- Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
猜你喜欢
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
瀏覽器驅動的下載
STM32-DAC实验&高频DAC输出测试
PyQt5_ Qscrollarea content is saved as a picture
抓包工具fiddler学习
c# 水晶报表打印
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
Qt新建项目
Launcher startup process
How kaggle uses utility script
随机推荐
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
Story points vs. human days
QT new project_ MyNotepad++
Chaos engineering platform chaosblade box new heavy release
C语言高级用法--函数指针:回调函数;转换表
Methods of software testing
Use of swagger
Solving the longest subsequence with linear DP -- three questions
Go operation redis
Yolov3 & yolov5 output result description
Selenium, element operation and browser operation methods
Codeforces Round #803 (Div. 2)(A~D)
Error: eacces: permission denied, access to "/usr/lib/node_modules"
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
Route (II)
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
docker mysql
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
软件测试的方法
Selenium installing selenium in pycharm