当前位置:网站首页>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
边栏推荐
- SystemServer进程
- C语言高级用法--函数指针:回调函数;转换表
- 测试框架TestNG的使用(二):testNG xml的使用
- Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
- Launcher启动过程
- Multi rotor aircraft control using PID and LQR controllers
- 《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
- Fabric.js 自由绘制圆形
- 故事点 vs. 人天
- MQ教程 | Exchange(交换机)
猜你喜欢

Selenium, element operation and browser operation methods

Default slot, named slot, scope slot

MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)

Design and implementation of car query system based on php+mysql

QT new project_ MyNotepad++

Selenium element positioning method

Packet capturing tool Fiddler learning

What is erdma? Popular science cartoon illustration

万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单

Yyds dry goods inventory software encryption lock function
随机推荐
全屋Wi-Fi:一个谁也解决不好的痛点?
Design and implementation of car query system based on php+mysql
Some interview suggestions for Android programmers "suggestions collection"
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
kaggle如何使用utility script
How kaggle uses utility script
Daily learning 2
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
Quarkus学习四 - 项目开发到部署
自定义事件,全局事件总线,消息订阅与发布,$nextTick
无主灯设计:如何让智能照明更加「智能」?
什么是 eRDMA?丨科普漫画图解
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
Qt新建项目
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
Quick analysis: easy to share the Internet
NLA natural language analysis realizes zero threshold of data analysis
< 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