当前位置:网站首页>Fabric.js 自由绘制圆形
Fabric.js 自由绘制圆形
2022-07-02 11:08:00 【德育处主任】
本文简介
这次要讲的是 自由绘制圆形 。
在 《Fabric.js 自由绘制矩形》 里讲到的思路,放在圆形里不太适用。
这次要做到的效果如下图所示。

<br>
<br>
思路
Fabric.js 默认的框选操作是矩形,如果需要做到上图的效果,需要做以下3步:
- 点击画布时
canvas.on('mouse:down', fn),创建一个圆形。 - 鼠标移动时
canvas.on('mouse:move', fn),圆形的大小跟随鼠标所在的位置进行缩放。 - 松开鼠标时
canvas.on('mouse:up', fn),确定圆形大小。
<br>
交互操作方面,我按照 PhotoShop 椭圆工具的操作逻辑。
圆形的直径是矩形的短边。

<br>
如果 “移动鼠标的坐标点” 在 点击时的坐标点 左侧或者上方,需要将圆形的左上角移到 “移动鼠标的坐标点” 。
<br>
<br>
动手实现
我在这里贴出用 原生方式 实现的代码和注释。
如果你想知道在 Vue3 环境下如何实现 Fabric.js 自由绘制矩形,可以在 代码仓库 里查找。
<!-- 工具栏 --><div class="toolbar"> <select onchange="typeChange(this.options[this.options.selectedIndex].value)"> <option value="default">默认(框选)</option> <option value="circle">圆形</option> </select></div><!-- 画布 --><canvas id="canvas" width="800" height="800"></canvas><!-- 引入fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.js"></script><script>let canvas = null // 画布对象let currentType = 'default' // 当前操作模式(默认 || 创建圆形)let downPoint = null // 按下鼠标时的坐标let upPoint = null // 松开鼠标时的坐标let currentCircle = 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 'circle': // 创建矩形模式 canvas.selectionColor = 'transparent' // 选框填充色:透明 canvas.selectionBorderColor = 'transparent' // 选框边框颜色:透明度很低的黑色(看上去是灰色) canvas.skipTargetFind = true // 禁止选中 break }}// 鼠标在画布上按下function canvasMouseDown(e) { downPoint = e.absolutePointer if (currentType === 'circle') { // 使用 Fabric.js 提供的api创建圆形,此时圆形的半径是0 currentCircle = new fabric.Circle({ top: downPoint.y, left: downPoint.x, radius: 0, fill: 'transparent', stroke: 'rgba(0, 0, 0, 0.2)' }) canvas.add(currentCircle) }}// 鼠标在画布上移动function canvasMouseMove(e) { if (currentType === 'circle' && currentCircle) { const currentPoint = e.absolutePointer // 半径:用短边来计算圆形的直径,最后除以2,得到圆形的半径 let radius = Math.min(Math.abs(downPoint.x - currentPoint.x), Math.abs(downPoint.y - currentPoint.y)) / 2 // 计算圆形的top和left坐标位置 let top = currentPoint.y > downPoint.y ? downPoint.y : downPoint.y - radius * 2 let left = currentPoint.x > downPoint.x ? downPoint.x : downPoint.x - radius * 2 // 分别设置圆形的半径、top和left currentCircle.set('radius', radius) currentCircle.set('top', top) currentCircle.set('left', left) canvas.requestRenderAll() }}// 鼠标在画布上松开function canvasMouseUp(e) { upPoint = e.absolutePointer if (currentType === 'circle') { // 如果鼠标点击和松开是在同一个坐标,那就不会创建圆形(其实是把刚创建半径为0的圆形删掉) if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) { canvas.remove(currentCircle) } else { if (currentCircle) { // 创建圆形(其实是把圆形边框的颜色改成 #000 currentCircle.set('stroke', '#000') } } // 完成以上操作后,临时的圆形清空掉。 currentCircle = null }}// 页面加载的生命周期,在此执行 初始化画布 的操作window.onload = function() { initCanvas()}</script><br>
<br>
代码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- < schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
- P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
- Yyds dry goods inventory software encryption lock function
- 默认插槽,具名插槽,作用域插槽
- How kaggle uses utility script
- Analysis of CPU surge in production environment service
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
- C crystal report printing
- Daily learning 3
- HMS core machine learning service helps zaful users to shop conveniently
猜你喜欢

你知道Oracle的数据文件大小有上限么?

《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持

QT - make a simple calculator - realize four operations

uniapp自动化测试学习

C crystal report printing

Custom events, global event bus, message subscription and publishing, $nexttick

千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?

<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持

<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持

Mysql5.7 installation super easy tutorial
随机推荐
Just 1000 fans, record it
Some interview suggestions for Android programmers "suggestions collection"
QT - make a simple calculator - realize four operations
BeanUtils -- shallow copy -- example / principle
freemarker的使用
Getting started with QT - making a simple calculator
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
联合搜索:搜索中的所有需求
Generally speaking, if the error of inconsistent tab and space occurs frequently
Yolov3 & yolov5 output result description
QT new project_ MyNotepad++
The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
Design and implementation of car query system based on php+mysql
Codeforces Round #803 (Div. 2)(A~D)
mongodb的认识
Adhere to the foundation of 20 minutes go every day II
How kaggle uses utility script
[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
Error: eacces: permission denied, access to "/usr/lib/node_modules"