-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 新增细尾箭头绘制元素(thinTailArrow) (#382)
* feat: 新增细尾箭头绘制元素(thinTailArrow) * fix: 细尾箭头样式调整
- Loading branch information
1 parent
2a3f9a4
commit bc2cc82
Showing
4 changed files
with
120 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* @Author: wuchenguang1998 | ||
* @Date: 2024-05-10 10:46:10 | ||
* @LastEditors: wuchenguang1998 | ||
* @LastEditTime: 2024-05-10 22:08:51 | ||
* @Description: 细尾箭头,支持控制条拖拽不变形 | ||
*/ | ||
import { fabric } from 'fabric'; | ||
|
||
fabric.ThinTailArrow = fabric.util.createClass(fabric.Line, { | ||
type: 'thinTailArrow', | ||
superType: 'drawing', | ||
initialize(points, options) { | ||
if (!points) { | ||
const { x1, x2, y1, y2 } = options; | ||
points = [x1, y1, x2, y2]; | ||
} | ||
options = options || {}; | ||
this.callSuper('initialize', points, options); | ||
}, | ||
_render(ctx) { | ||
ctx.save(); | ||
// 乘或除对应的scaleX(Y),抵消元素放缩造成的影响,使箭头不会变形 | ||
ctx.scale(1 / this.scaleX, 1 / this.scaleY); | ||
const xDiff = (this.x2 - this.x1) * this.scaleX; | ||
const yDiff = (this.y2 - this.y1) * this.scaleY; | ||
ctx.translate(-xDiff / 2, -yDiff / 2); | ||
// 箭头方位角 | ||
const angle = Math.atan2(yDiff, xDiff); | ||
ctx.rotate(angle); | ||
// 箭头总长(最小长度是20) | ||
let length = Math.hypot(xDiff, yDiff); | ||
length = length < 20 ? 20 : length; | ||
// 绘制箭头 | ||
ctx.beginPath(); | ||
ctx.moveTo(0, 0); | ||
ctx.lineTo(length - 18, -5); | ||
ctx.lineTo(length - 20, -12); | ||
ctx.lineTo(length, 0); | ||
ctx.lineTo(length - 20, 12); | ||
ctx.lineTo(length - 18, 5); | ||
ctx.lineTo(0, 0); | ||
ctx.lineWidth = this.strokeWidth; | ||
ctx.strokeStyle = this.stroke; | ||
ctx.fillStyle = this.fill; | ||
ctx.stroke(); | ||
ctx.fill(); | ||
ctx.restore(); | ||
}, | ||
}); | ||
|
||
fabric.ThinTailArrow.fromObject = (options, callback) => { | ||
const { x1, x2, y1, y2 } = options; | ||
return callback(new fabric.ThinTailArrow([x1, y1, x2, y2], options)); | ||
}; | ||
|
||
export default fabric.ThinTailArrow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters