|
| 1 | +import { |
| 2 | + computed, |
| 3 | + initial, |
| 4 | + signal, |
| 5 | + vector2Signal, |
| 6 | +} from '@motion-canvas/2d/lib/decorators'; |
| 7 | +import {Vector2, Vector2Signal} from '@motion-canvas/core/lib/types'; |
| 8 | +import {VectorBase, VectorBaseProps} from './VectorBase'; |
| 9 | +import {SimpleSignal} from '@motion-canvas/core/lib/signals'; |
| 10 | + |
| 11 | +export interface ArcVectorProps extends VectorBaseProps { |
| 12 | + counter?: boolean; |
| 13 | + x?: number; |
| 14 | + y?: number; |
| 15 | + position?: Vector2; |
| 16 | + from?: number; |
| 17 | + to?: number; |
| 18 | + radius?: number; |
| 19 | +} |
| 20 | + |
| 21 | +export class ArcVector extends VectorBase { |
| 22 | + @initial(false) |
| 23 | + @signal() |
| 24 | + public declare readonly counter: SimpleSignal<boolean, this>; |
| 25 | + |
| 26 | + @initial(0) |
| 27 | + @signal() |
| 28 | + public declare readonly from: SimpleSignal<number, this>; |
| 29 | + |
| 30 | + @initial(90) |
| 31 | + @signal() |
| 32 | + public declare readonly to: SimpleSignal<number, this>; |
| 33 | + |
| 34 | + @initial(100) |
| 35 | + @signal() |
| 36 | + public declare readonly radius: SimpleSignal<number, this>; |
| 37 | + |
| 38 | + @vector2Signal() |
| 39 | + public declare readonly position: Vector2Signal<this>; |
| 40 | + |
| 41 | + @computed() |
| 42 | + public length(): number { |
| 43 | + const to = this.to(); |
| 44 | + const from = this.from(); |
| 45 | + |
| 46 | + let angle = to - from; |
| 47 | + angle %= 360; |
| 48 | + if (angle < 0) { |
| 49 | + angle += 360; |
| 50 | + } |
| 51 | + |
| 52 | + const radius = this.radius(); |
| 53 | + |
| 54 | + return (Math.PI * radius * radius * angle) / 360; |
| 55 | + } |
| 56 | + |
| 57 | + public constructor(props: ArcVectorProps) { |
| 58 | + super(props); |
| 59 | + } |
| 60 | + |
| 61 | + @computed() |
| 62 | + public center(): Vector2 { |
| 63 | + const angle = (this.to() - this.from()) / 2; |
| 64 | + return Vector2.fromRadians((angle / 180) * Math.PI).scale(this.radius()); |
| 65 | + } |
| 66 | + |
| 67 | + public override localToParent(): DOMMatrix { |
| 68 | + return new DOMMatrix([1, 0, 0, 1, this.position.x(), this.position.y()]); |
| 69 | + } |
| 70 | + |
| 71 | + protected override draw(context: CanvasRenderingContext2D) { |
| 72 | + context.save(); |
| 73 | + this.applyStyle(context); |
| 74 | + const startArrow = this.startArrow(); |
| 75 | + const endArrow = this.endArrow(); |
| 76 | + const startOffset = this.startOffset(); |
| 77 | + const endOffset = this.endOffset(); |
| 78 | + let start = this.start(); |
| 79 | + let end = this.end(); |
| 80 | + |
| 81 | + if (end < start) { |
| 82 | + [end, start] = [start, end]; |
| 83 | + } |
| 84 | + |
| 85 | + const counter = this.counter(); |
| 86 | + const radius = this.radius(); |
| 87 | + let from = (this.from() / 180) * Math.PI; |
| 88 | + let to = (this.to() / 180) * Math.PI; |
| 89 | + |
| 90 | + from += counter ? startOffset / -radius : startOffset / radius; |
| 91 | + to -= counter ? endOffset / -radius : endOffset / radius; |
| 92 | + |
| 93 | + let angle = to - from; |
| 94 | + to = from + angle * end; |
| 95 | + from = from + angle * start; |
| 96 | + angle = to - from; |
| 97 | + |
| 98 | + if (angle !== 0) { |
| 99 | + const arrowSize = Math.min( |
| 100 | + this.arrowSize(), |
| 101 | + (Math.abs(angle) * radius) / 2, |
| 102 | + ); |
| 103 | + let arrowDistance = arrowSize / radius; |
| 104 | + if (counter) { |
| 105 | + arrowDistance *= -1; |
| 106 | + } |
| 107 | + |
| 108 | + context.beginPath(); |
| 109 | + context.arc( |
| 110 | + 0, |
| 111 | + 0, |
| 112 | + radius, |
| 113 | + from + (startArrow ? arrowDistance : 0), |
| 114 | + to - (endArrow ? arrowDistance : 0), |
| 115 | + counter, |
| 116 | + ); |
| 117 | + context.stroke(); |
| 118 | + |
| 119 | + if (startArrow) { |
| 120 | + const direction = Vector2.fromRadians(from).scale(radius); |
| 121 | + const arrow = Vector2.fromRadians( |
| 122 | + from + arrowDistance / 2, |
| 123 | + ).perpendicular.scale(counter ? arrowSize : -arrowSize); |
| 124 | + |
| 125 | + this.drawArrow(context, direction, arrow); |
| 126 | + } |
| 127 | + |
| 128 | + if (endArrow) { |
| 129 | + const direction = Vector2.fromRadians(to).scale(radius); |
| 130 | + const arrow = Vector2.fromRadians( |
| 131 | + to - arrowDistance / 2, |
| 132 | + ).perpendicular.scale(counter ? -arrowSize : arrowSize); |
| 133 | + |
| 134 | + this.drawArrow(context, direction, arrow); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + context.restore(); |
| 139 | + super.draw(context); |
| 140 | + } |
| 141 | +} |
0 commit comments