当前位置:网站首页>Fabric. JS free draw circle
Fabric. JS free draw circle
2022-07-02 14:27:00 【Director of Moral Education Department】
Brief introduction
This time we're going to talk about Draw circles freely .
stay 《Fabric.js Draw a rectangle freely 》 The ideas mentioned in , It's not very suitable to put it in a circle .
The effect to be achieved this time is shown in the figure below .
<br>
<br>
Ideas
Fabric.js
The default box selection operation is rectangle , If you need to achieve the effect shown in the figure above , We need to do the following 3 Step :
- When clicking on the canvas
canvas.on('mouse:down', fn)
, Create a circle . - When the mouse moves
canvas.on('mouse:move', fn)
, The size of the circle is scaled according to the position of the mouse . - When you release the mouse
canvas.on('mouse:up', fn)
, Determine the size of the circle .
<br>
In terms of interaction , I follow PhotoShop
Operation logic of Ellipse Tool .
The diameter of a circle is the short side of a rectangle .
<br>
If “ Move the coordinate point of the mouse ” stay Coordinate point when clicking Left or above , You need to move the upper left corner of the circle to “ Move the coordinate point of the mouse ” .
<br>
<br>
Do it
I post it here for The original way Implementation code and comments .
If you want to know Vue3
How to achieve Fabric.js Draw a rectangle freely
, Can be in Code warehouse Search in .
<!-- The toolbar --><div class="toolbar"> <select onchange="typeChange(this.options[this.options.selectedIndex].value)"> <option value="default"> Default ( Frame selection )</option> <option value="circle"> circular </option> </select></div><!-- canvas --><canvas id="canvas" width="800" height="800"></canvas><!-- introduce fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.js"></script><script>let canvas = null // Canvas object let currentType = 'default' // Current operating mode ( Default || Create a circle )let downPoint = null // Coordinates when pressing the mouse let upPoint = null // Coordinates when releasing the mouse let currentCircle = null // Temporary circle , Use... When creating circles // 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 // Allow box selection canvas.selectionColor = 'rgba(100, 100, 255, 0.3)' // Check box fill color : Translucent blue canvas.selectionBorderColor = 'rgba(255, 255, 255, 0.3)' // Check box border color : Translucent gray canvas.skipTargetFind = false // Allow selection break case 'circle': // Create a rectangular pattern canvas.selectionColor = 'transparent' // Check box fill color : transparent canvas.selectionBorderColor = 'transparent' // Check box border color : Black with low transparency ( It looks gray ) canvas.skipTargetFind = true // Do not select break }}// Press the mouse on the canvas function canvasMouseDown(e) { downPoint = e.absolutePointer if (currentType === 'circle') { // Use Fabric.js Provided api Create a circle , Now the radius of the circle is 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) }}// Move the mouse over the canvas function canvasMouseMove(e) { if (currentType === 'circle' && currentCircle) { const currentPoint = e.absolutePointer // radius : Use the short side to calculate the diameter of the circle , Finally divide by 2, Get the radius of the circle let radius = Math.min(Math.abs(downPoint.x - currentPoint.x), Math.abs(downPoint.y - currentPoint.y)) / 2 // Calculate the of a circle top and left coordinates let top = currentPoint.y > downPoint.y ? downPoint.y : downPoint.y - radius * 2 let left = currentPoint.x > downPoint.x ? downPoint.x : downPoint.x - radius * 2 // Set the radius of the circle separately 、top and left currentCircle.set('radius', radius) currentCircle.set('top', top) currentCircle.set('left', left) canvas.requestRenderAll() }}// Release the mouse on the canvas function canvasMouseUp(e) { upPoint = e.absolutePointer if (currentType === 'circle') { // If the mouse click and release are in the same coordinate , Then you won't create a circle ( In fact, the radius just created is 0 Delete the circle ) if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) { canvas.remove(currentCircle) } else { if (currentCircle) { // Create a circle ( It's actually changing the color of the circular border to #000 currentCircle.set('stroke', '#000') } } // After finishing the above operation , Empty the temporary circle . currentCircle = null }}// The life cycle of page loading , Execute here Initialize canvas The operation of window.onload = function() { initCanvas()}</script>
<br>
<br>
Code warehouse
<br>
<br>
Recommended reading
- 《Fabric.js Draw a rectangle freely 》
- 《Fabric.js Customize the right-click menu 》
- 《Fabric.js From entry to expansion 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- 数据湖(十一):Iceberg表数据组织与查询
- Daily learning 3
- Fabric.js 元素被选中时保持原有层级
- HMS core machine learning service helps zaful users to shop conveniently
- 全屋Wi-Fi:一个谁也解决不好的痛点?
- Multi rotor aircraft control using PID and LQR controllers
- Quick analysis: easy to share the Internet
- Design of non main lamp: how to make intelligent lighting more "intelligent"?
- Use of UIC in QT
- P1042 [noip2003 popularization group] Table Tennis
猜你喜欢
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
QT new project
Federated Search: all requirements in search
由粒子加速器产生的反中子形成的白洞
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
Fabric.js 上划线、中划线(删除线)、下划线
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
Use of swagger
In 2021, the global TCB adapter revenue was about $93 million, and it is expected to reach $315.5 million in 2028
c# 水晶报表打印
随机推荐
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
Methods of software testing
The most complete analysis of Flink frame window function
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
Design of non main lamp: how to make intelligent lighting more "intelligent"?
P1042 [NOIP2003 普及组] 乒乓球
c# 水晶报表打印
Route (II)
故事點 vs. 人天
Yyds dry goods inventory software encryption lock function
P1908 逆序对
无主灯设计:如何让智能照明更加「智能」?
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
字符串匹配问题
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
Thymeleaf dependency
Getting started with QT - making a simple calculator
PyQt5_ Qscrollarea content is saved as a picture
Multi rotor aircraft control using PID and LQR controllers
Daily learning 2