15 Star 176 Fork 64

德育处主任/Fabric.js学习资料(中文教程)

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
createCircle.html 3.11 KB
Copy Edit Raw Blame History
德育处主任 authored 2022-01-28 13:36 +08:00 . 自有绘制圆形 修改代码位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自由绘制圆形</title>
<style>
.toolbar {
margin-bottom: 10px;
}
#canvas {
border: 1px solid #ccc;
}
</style>
</head>
<body>
<!-- 工具栏 -->
<div class="toolbar">
<select onchange="typeChange(this.options[this.options.selectedIndex].value)">
<option value="default">默认(框选)</option>
<option value="circle">圆形</option>
</select>
</div>
<canvas id="canvas" width="800" height="800"></canvas>
<script src="../../script/fabric.js"></script>
<script>
let canvas = null
let currentType = 'default'
let downPoint = null
let upPoint = null
let currentCircle = null // 临时圆,创建圆的时候使用
// 初始化画板
function initCanvas() {
canvas = new fabric.Canvas('canvas')
canvas.on('mouse:down', canvasMouseDown) // 鼠标在画布上按下
canvas.on('mouse:move', canvasMouseMove) // 鼠标在画布上移动
canvas.on('mouse:up', canvasMouseUp) // 鼠标在画布上松开
}
// 画布操作类型切换
function typeChange(opt) {
currentType = opt
switch(opt) {
case 'default':
canvas.selection = true
canvas.selectionColor = 'rgba(100, 100, 255, 0.3)'
canvas.selectionBorderColor = 'rgba(255, 255, 255, 0.3)'
canvas.skipTargetFind = false // 允许选中
break
case 'circle':
canvas.selectionColor = 'transparent'
canvas.selectionBorderColor = 'transparent'
canvas.skipTargetFind = true // 禁止选中
break
}
}
// 鼠标在画布上按下
function canvasMouseDown(e) {
downPoint = e.absolutePointer
if (currentType === 'circle') {
currentCircle = new fabric.Circle({
top: downPoint.y,
left: downPoint.x,
radius: 0,
fill: 'transparent',
stroke: 'rgba(0, 0, 0, 0.2)'
})
canvas.add(currentCircle)
}
}
// 鼠标在画布上移动
function canvasMouseMove(e) {
if (currentType === 'circle' && currentCircle) {
const currentPoint = e.absolutePointer
let radius = Math.min(Math.abs(downPoint.x - currentPoint.x), Math.abs(downPoint.y - currentPoint.y)) / 2
let top = currentPoint.y > downPoint.y ? downPoint.y : downPoint.y - radius * 2
let left = currentPoint.x > downPoint.x ? downPoint.x : downPoint.x - radius * 2
currentCircle.set('radius', radius)
currentCircle.set('top', top)
currentCircle.set('left', left)
canvas.requestRenderAll()
}
}
// 鼠标在画布上松开
function canvasMouseUp(e) {
upPoint = e.absolutePointer
if (currentType === 'circle') {
if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) {
canvas.remove(currentCircle)
} else {
if (currentCircle){
currentCircle.set('stroke', '#000')
}
}
currentCircle = null
}
}
window.onload = function() {
initCanvas()
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/k21vin/fabricjs-demo.git
git@gitee.com:k21vin/fabricjs-demo.git
k21vin
fabricjs-demo
Fabric.js学习资料(中文教程)
master

Search