当前位置:网站首页>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
边栏推荐
- 【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
- Advanced usage of C language -- function pointer: callback function; Conversion table
- Systemserver process
- Design and implementation of car query system based on php+mysql
- freemarker的使用
- Selenium installing selenium in pycharm
- 数据湖(十一):Iceberg表数据组织与查询
- Essential elements of science fiction 3D scenes - City
- Adhere to the foundation of 20 minutes go every day II
- NLA natural language analysis realizes zero threshold of data analysis
猜你喜欢
C crystal report printing
Qt新建项目
Development skills of rxjs observable custom operator
In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
Yolov3 & yolov5 output result description
无主灯设计:如何让智能照明更加「智能」?
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
由粒子加速器产生的反中子形成的白洞
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
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
随机推荐
QT new project_ MyNotepad++
Qt新建项目
QT how to set fixed size
联合搜索:搜索中的所有需求
Browser driven Download
[deep learning] simple implementation of neural network forward propagation
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
Design and implementation of car query system based on php+mysql
QT new project
PHP linked list creation and traversal
uni-app中使用computed解决了tab切换中data()值显示的异常
Quarkus learning IV - project development to deployment
Advanced usage of C language -- function pointer: callback function; Conversion table
Fabric.js 手动加粗文本iText
Launcher启动过程
Talk about idempotent design
The most complete analysis of Flink frame window function
Use of swagger
Solving the longest subsequence with linear DP -- three questions
P1042 [NOIP2003 普及组] 乒乓球