当前位置:网站首页>Fabric. JS round brush
Fabric. JS round brush
2022-07-02 05:18:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
In this paper, Fabric.js
Circular brush function of .
The circular brush is used to “ Freehand drawing ” On the brush of . You can tell by the name , This brush will fill the painted path with circles .
It will be more intuitive to look at the pictures
Fabric.js
Will use different frequencies 、 size 、 Color depth to draw the above effect .
<br>
This article USES the
Fabric.js 5.2.1
<br>
<br>
Common configuration
To make the effect of the above figure , First, you need to set the canvas to Painting mode .
<br>
Initialize canvas
First, you need to initialize the canvas , After that, each attribute and method will be explained , Will be based on this code .
<canvas id="c" width="600" height="400" style="border: 1px solid #ccc;"></canvas><!-- introduce Fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script><script> // How to write it 1 const canvas = new fabric.Canvas('c', { isDrawingMode: true // Turn on painting mode }) // How to write it 2 // const canvas = new fabric.Canvas('c') // canvas.isDrawingMode = true</script>
To set the canvas to Painting mode , Need to put isDrawingMode
Set to true
.
Choose one of the above two ways to write .
<br>
Turn on the round brush
Setting the brush to round also has 2 Species writing .
<br>
How to write it 1
// Omit the initialization code canvas.freeDrawingBrush = new fabric.CircleBrush(canvas)
<br>
How to write it 2
// Omit the initialization code let circleBrush = new fabric.CircleBrush()circleBrush.initialize(canvas)canvas.freeDrawingBrush = circleBrush
<br>
Either of the above two ways can be used , But it will affect the amount of subsequent code .
If you want to modify the brush properties frequently in the future , I suggest using How to write it 2 .
<br>
Set the brush width
If you use the above step How to write it 1 , To set the brush width, you need to write this
// Omit the initialization code canvas.freeDrawingBrush = new fabric.CircleBrush(canvas) // Create a circular brush canvas.freeDrawingBrush.width = 6 // brush width , Default 10
<br>
If you use How to write it 2 , The set code is as follows
// Omit the initialization code // Create a circular brush let circleBrush = new fabric.CircleBrush()circleBrush.initialize(canvas)canvas.freeDrawingBrush = circleBrushcircleBrush.width = 30
<br>
Set brush color
// Omit the initialization code circleBrush.color = 'pink'
I set the brush to pink , Except for keyword color , And support rgb
Etc
circleBrush.color = '#c123a8'circleBrush.color = 'rgb(10, 230, 120)'
<br>
Set shadow
// Omit the initialization code circleBrush.shadow = new fabric.Shadow({ blur: 10, // The degree of eclosion offsetX: 20, // x Shaft offset offsetY: 20, // y Shaft offset color: '#30e3ca' // Projection color })
and Basic brush The method of setting shadows is the same .
<br>
<br>
Common methods
There are several common methods , But there are some things that need attention , To be effective, use it in combination .
<br>
The mouse click
// Omit the initialization code circleBrush.onMouseDown = function (pointer, e) { console.log(pointer) console.log(e)}
Use onMouseDown
You can set the event to be triggered when the mouse is pressed . The event has 2 Parameters .
<br>
Release the mouse
// Omit the initialization code circleBrush.onMouseUp = function (pointer) { console.log(pointer)}
and “ The mouse click ” It's as simple as . Use onMouseUp
You can set the event of mouse release .
<br>
When the mouse moves
// Omit the initialization code circleBrush.onMouseMove = function (pointer, e) { console.log(pointer) console.log(e) circleBrush.drawDot(pointer)}
Use onMouseMove
You can set the event when the mouse moves . However, in this event, it is necessary to perform drawDot
event , And pass in the current mouse position to draw correctly .
<br>
More methods can be added to mouse movement events , For example, on the basis of drawing , Draw another line nearby
// Omit the initialization code circleBrush.onMouseMove = function (pointer, e) { console.log(pointer) console.log(e) circleBrush.drawDot(pointer) circleBrush.addPoint({x: pointer.x + 50, y: pointer.y + 50}) // There is one more line nearby }
That's all Fabric
Common playing methods of round brushes ~
<br>
<br>
Code warehouse
<br>
<br>
Recommended reading
- 《Fabric.js How to use the brush ?》
- 《Fabric.js Draw ellipses freely 》
- 《Fabric.js How to use an eraser ( Including recovery function )》
- 《Fabric.js Customize the right-click menu 》
- 《Fabric.js From entry to expansion 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- 黑馬筆記---Set系列集合
- Go implements leetcode rotation array
- Mathematical knowledge -- understanding and examples of fast power
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
- Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
- CubeMx DMA笔记
- Thread pool batch processing data
- 视差特效的原理和实现方法
- Disable access to external entities in XML parsing
- 线程池批量处理数据
猜你喜欢
Dark horse notes -- Set Series Collection
摆正元素(带过渡动画)
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Fabric.js 激活输入框
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
函数栈帧的创建和销毁
黑马笔记---Map集合体系
LS1046nfs挂载文件系统
How to configure PostgreSQL 12.9 to allow remote connections
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]
随机推荐
[opencv] image binarization
Operator details
Implementation of leetcode two number addition go
数据库问题汇总
Simple and practical accounting software, so that accounts can be checked
Dark horse notes -- map set system
Implementation of go language for deleting duplicate items in sorting array
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]
Mathematical knowledge (Euler function)
Differential identities (help find mean, variance, and other moments)
运维工作的“本手、妙手、俗手”
Essence and physical meaning of convolution (deep and brief understanding)
操作符详解
ERP management system development and design existing source code
Leetcode basic programming: array
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
Gee series: unit 8 time series analysis in Google Earth engine [time series]
Gee series: Unit 1 Introduction to Google Earth engine
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]