当前位置:网站首页>Fabric. JS free drawing ellipse
Fabric. JS free drawing ellipse
2022-07-02 14:26:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
This article is explained in Fabric.js
How to draw ellipses freely in , If you don't already know Fabric.js
, You can refer to 《Fabric.js From entry to mastery 》.
The effect is shown below
<br>
<br>
Ideas
Fabric.js
After initializing the canvas , You can perform box selection , But the default is to use a rectangle to frame , As shown in the figure below :
I hope that when I use the mouse to create an ellipse, I can see the shadow of another ellipse , This makes it easier for me to observe what the ellipse to be drawn looks like .
So we can select the box first Frame and Background color Set to transparent , Then monitor the mouse click when the box is selected 、 Move 、 Loose event , So as to draw an ellipse .
<br>
The detailed steps are as follows :
- When the box is selected Make the border and background transparent
- mouse Clicking time Create ellipse
- mouse When moving Modify the ellipse size
- mouse When loosening Generate a formal ellipse
<br>
I split the whole drawing event into the above 4 Step , But in fact 3 Step is still a little difficult , We need to consider several situations :
- The coordinates when clicking are at the bottom left when moving
- The coordinate when clicking is at the top left when moving
- The coordinate when clicking is at the top right when moving
- The coordinates when clicking are at the bottom right when moving
<br>
this 4 In this case, I'm 《Fabric.js Draw a rectangle freely 》 It has been analyzed one by one , You can check it out ( Remember to like ~)
<br>
<br>
code
After sorting out the ideas , We can formalize the coding phase .
I will still follow 1、2、3、4 Steps to code , You open this article in two browser windows , One side is coding , One side is thinking , It may be clearer by comparison .
<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 // Temporary ellipse // Initialize Sketchpad function initCanvas() { canvas = new fabric.Canvas('canvas') canvas.on('mouse:down', canvasMouseDown) // Press the mouse on the canvas canvas.on('mouse:move', canvasMouseMove) // Move the mouse over the canvas canvas.on('mouse:up', canvasMouseUp) // Release the mouse on the canvas } // Canvas operation type switching function typeChange(opt) { currentType = opt switch(opt) { case 'default': // Default box selection mode canvas.selection = true canvas.selectionColor = 'rgba(100, 100, 255, 0.3)' canvas.selectionBorderColor = 'rgba(255, 255, 255, 0.3)' canvas.skipTargetFind = false // Allow selection break case 'ellipse': // Elliptical mode // When the box is selected , Make the border and background transparent canvas.selectionColor = 'transparent' canvas.selectionBorderColor = 'transparent' canvas.skipTargetFind = true // Do not select break } } // Press the mouse on the canvas 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) } } // Move the mouse over the canvas 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 // Set the size and location of the ellipse currentEllipse.set('rx', rx) currentEllipse.set('ry', ry) currentEllipse.set('top', top) currentEllipse.set('left', left) // Refresh the canvas canvas.requestRenderAll() } } // Release the mouse on the canvas function canvasMouseUp(e) { upPoint = e.absolutePointer if (currentType === 'ellipse') { // You need to judge whether the coordinate points of the mouse are equal when clicking and releasing , If equal, no ellipse will be created if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) { canvas.remove(currentEllipse) } else { // Set ellipse style currentEllipse.set('stroke', '#000') } // Clear the temporarily created ellipse currentEllipse = null } } window.onload = function() { initCanvas() }</script>
As you can see from the code above , The main core part is “ On mouse click , When the mouse moves , Release the mouse ” These links . When the mouse moves , To monitor the current coordinates of the mouse in real time , Release the mouse and stop listening .
<br>
<br>
<br>
Code warehouse
Fabric.js Draw ellipses freely
<br>
<br>
Recommended reading
- 《Fabric.js Draw a rectangle freely 》
- 《Fabric.js Draw a rectangle freely 》
- 《Fabric.js Customize the right-click menu 》
- 《Fabric.js From entry to expansion 》
- 《Fabric.js 3 individual api Set canvas width and height 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- selenium 元素定位方法
- YOLOv3&YOLOv5输出结果说明
- 全屋Wi-Fi:一个谁也解决不好的痛点?
- [Hongke technology sharing] how to test DNS server: DNS performance and response time test
- OpenHarmony笔记-----------(四)
- Methods of software testing
- Fabric.js 元素被选中时保持原有层级
- Codeforces Round #803 (Div. 2)(A~D)
- docker mysql
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
猜你喜欢
SystemServer进程
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
Browser driven Download
QT - make a simple calculator - realize four operations
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
Selenium, element operation and browser operation methods
kaggle如何使用utility script
随机推荐
数据湖(十一):Iceberg表数据组织与查询
Characteristics of selenium
mongodb的认识
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
selenium 元素定位方法
Federated Search: all requirements in search
P1042 [noip2003 popularization group] Table Tennis
Start to write a small demo - three piece chess
故事點 vs. 人天
每日学习2
Quick analysis: easy to share the Internet
HMS core machine learning service helps zaful users to shop conveniently
docker mysql
BeanUtils -- shallow copy -- example / principle
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
Design and implementation of car query system based on php+mysql
Pychart connects to the remote server
QT new project
关于Flink框架窗口(window)函数最全解析
千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?