Arrow Element
Arrow element used to add start/end arrowheads to lines. It provides 12 built-in arrow styles and supports custom definitions.
Install Plugin
You need to install the arrow plugin to use it. Click here to visit the Github repository.
npm install @leafer-in/arrowpnpm add @leafer-in/arrowyarn add @leafer-in/arrowbun add @leafer-in/arrowOr include via script tag and access plugin features using the global variable LeaferIN.arrow.
<script src="https://unpkg.com/@leafer-in/arrow@2.1.0/dist/arrow.min.js"></script>
<script>
const { Arrow } = LeaferIN.arrow
</script><script src="https://unpkg.com/@leafer-in/arrow@2.1.0/dist/arrow.js"></script>
<script>
const { Arrow } = LeaferIN.arrow
</script>Key Properties
startArrow: IArrowType
Start arrow. Default is none.
Supports custom arrow styles. Line / Path elements also support this property (requires this plugin).
endArrow: IArrowType
End arrow. Default is angle.
Supports custom arrow styles. Line / Path elements also support this property (requires this plugin).
type IArrowType =
| 'none'
| 'angle' // angle arrow (high performance)
| 'angle-side' // single-side angle arrow
| 'arrow' // standard arrow
| 'triangle' // triangle arrow
| 'triangle-flip' // flipped triangle arrow
| 'circle' // circle arrow
| 'circle-line' // circle line arrow
| 'square' // square arrow
| 'square-line' // square line arrow
| 'diamond' // diamond arrow
| 'diamond-line' // diamond line arrow
| 'mark' // mark arrow
| IArrowTypeData // arrow object
| string // custom arrow
interface IArrowTypeData {
type: IArrowType // arrow type
scale?: number // scale factor, avoid overly large values (may cause artifacts)
rotation?: number // additional rotation angle
}Points Mode
Use points to define a polyline.
points: number[]
Draw polyline using coordinate array [x1, y1, x2, y2, ...].
curve: boolean | number
Whether to convert to a smooth path. Default is false.
Can be set from 0 ~ 1 to control curvature. Default is 0.5.
Path Mode
Path priority mode
Corner Radius
cornerRadius: number
Corner radius for smoothing polyline corners.
Static Method
registerArrow ( name: string, data: IPathDataArrow )
Register a custom arrow style. See example.
Designed based on line width = 1. Imagine the arrow placed from left to right, with the endpoint at (0,0). Scaling and rotation are handled automatically.
interface IPathDataArrow {
connect?: IPathDataArrowOffset // connection point with line
offset?: IPathDataArrowOffset // offset to align with endpoint
path: IPathCommandData // supports M, L, C, Q, O commands only
fill?: boolean // since v2.1.0, supports filled arrows (stroke must not be transparent; use opacity instead)
}
interface IPathDataArrowOffset {
x?: number // x-axis offset
bevelJoin?: number // offset when strokeJoin is bevel
roundJoin?: number // offset when strokeJoin is round
}Inheritance
Arrow > Line > UI
Examples
Angle arrow
Make arrow larger
Smooth strokeJoin
Smooth strokeCap
Smooth strokeCap / strokeJoin
Single-side angle arrow
Standard arrow
Smooth strokeCap / strokeJoin
// #箭头样式 [标准箭头 - 箭头都变得平滑]
import { Leafer } from 'leafer-ui'
import { Arrow } from '@leafer-in/arrow' // 导入箭头插件
const leafer = new Leafer({ view: window })
const arrow = new Arrow({
y: 50,
endArrow: 'arrow',
strokeCap: 'round',
strokeJoin: 'round',
strokeWidth: 5,
stroke: '#32cd79'
})
leafer.add(arrow)Triangle arrow
Flipped triangle arrow
Circle arrow
Circle line arrow
Square arrow
Square line arrow
Diamond arrow
Diamond line arrow
Mark arrow
Dashed arrow animation
// #动画样式 [虚线箭头动画]
import { Leafer } from 'leafer-ui'
import { Arrow } from '@leafer-in/arrow' // 导入箭头插件
import '@leafer-in/animate' // 导入动画插件
const leafer = new Leafer({ view: window })
const arrow = new Arrow({
x: 100,
y: 100,
stroke: '#32cd79',
strokeWidth: 5,
dashPattern: [10, 10], // 绘制虚线
dashOffset: 0,
animation: { // 虚线动画
style: { dashOffset: -20 },
easing: 'linear',
duration: 0.5,
loop: true,
}
})
leafer.add(arrow)Arrow scale x2
Arrow rotation +30 degrees
Register custom arrow style
The path() method creates a PathCreator instance, similar to Canvas 2D API, for quickly drawing paths.
// #箭头样式 [自定义箭头样式]
import { Leafer, path } from 'leafer-ui'
import { Arrow } from '@leafer-in/arrow' // 导入箭头插件
// 注册自定义箭头样式
Arrow.registerArrow('custom-arrow', {
// 按照线宽为 1 自定义,箭头末端为(0,0),内部会自动处理缩放、旋转角度
connect: { x: -0.5 }, // 箭头与线条的连接点位置
offset: {
x: -0.71, // 偏移距离(x轴)
bevelJoin: 0.36, // strokeJoin 为 bevel 时增加的偏移距离(x轴)
roundJoin: 0.22 // strokeJoin 为 round 时增加的偏移距离(x轴)
},
// 路径数据,用线条绘制、拼凑出箭头形状(没有fill),只用关心箭头从左往右的水平样式,仅支持 M、L、C、Q、O 绘图命令
path: path().moveTo(-5, -5).lineTo(0, 0).lineTo(-5, 3).path, // = [1, -5, -3, 2, 0, 0, 2, -5, 3]
// fill: true // v2.0.7 开始支持启用fill
})
const leafer = new Leafer({ view: window })
const arrow = new Arrow({
y: 50,
strokeWidth: 5,
stroke: '#32cd79',
endArrow: 'custom-arrow' // 使用自定义箭头
})
leafer.add(arrow)