1
- import { deg2rad , Point , Vector } from '../../api/canvas.ts' ;
1
+ import { deg2rad , PI2 , Point , Vector } from '../../api/canvas.ts' ;
2
2
3
3
interface IMemo {
4
4
when : number ;
@@ -8,8 +8,10 @@ interface IMemo {
8
8
9
9
let raf = 0 ;
10
10
let ctx : CanvasRenderingContext2D ;
11
- const R = 40 ;
11
+ const R = 20 ;
12
12
const D = 2 * R ;
13
+ const LINE_WIDTH = 4 ;
14
+ const SHADOW_WIDTH = 4 ;
13
15
const pRotationAxis = new Point ( R , R ) ;
14
16
const WHITE = 'rgb(100% 100% 100%)' ;
15
17
const BLACK = 'rgb(0% 0% 0%)' ;
@@ -47,8 +49,8 @@ function initContext(_ctx: CanvasRenderingContext2D) {
47
49
ctx . canvas . width = D ;
48
50
ctx . canvas . height = D ;
49
51
ctx . lineCap = 'round' ;
50
- ctx . lineWidth = 4 ;
51
- ctx . shadowBlur = 4 ;
52
+ ctx . lineWidth = LINE_WIDTH ;
53
+ ctx . shadowBlur = SHADOW_WIDTH ;
52
54
53
55
onColourSchemeChange ( ( scheme ) => {
54
56
primaryColour = scheme === 'dark' ? WHITE : BLACK ;
@@ -62,24 +64,37 @@ function draw() {
62
64
const drawTime = Date . now ( ) ;
63
65
ctx . clearRect ( 0 , 0 , D , D ) ;
64
66
65
- for ( let n = 0 , N = memory . length ; n < N ; n ++ ) {
66
- const memo = memory [ n ] ;
67
- memo . age = drawTime - memo . when ;
68
- drawLine ( memo ) ;
69
- }
67
+ if ( memory . length ) {
68
+ for ( let n = 0 , N = memory . length ; n < N ; n ++ ) {
69
+ const memo = memory [ n ] ;
70
+ memo . age = drawTime - memo . when ;
71
+ drawLine ( memo ) ;
72
+ }
70
73
71
- let n = memory . length ;
72
- while ( n -- ) {
73
- if ( memory [ n ] . age >= ANIMATION_DURATION ) {
74
- memory . pop ( ) ;
75
- } else {
76
- break ;
74
+ let n = memory . length ;
75
+ while ( n -- ) {
76
+ if ( memory [ n ] . age >= ANIMATION_DURATION ) {
77
+ memory . pop ( ) ;
78
+ } else {
79
+ break ;
80
+ }
77
81
}
82
+ } else {
83
+ drawCenter ( ) ;
78
84
}
79
85
80
86
raf = requestAnimationFrame ( draw ) ;
81
87
}
82
88
89
+ function drawCenter ( ) {
90
+ ctx . save ( ) ;
91
+ ctx . beginPath ( ) ;
92
+ ctx . arc ( pRotationAxis . x , pRotationAxis . y , 0.5 , 0 , PI2 ) ;
93
+ ctx . stroke ( ) ;
94
+ ctx . closePath ( ) ;
95
+ ctx . restore ( ) ;
96
+ }
97
+
83
98
function drawLine ( memo : IMemo ) {
84
99
const length = R - memo . age * R / ANIMATION_DURATION ;
85
100
const p = memo . vector . setLength ( length ) . toPoint ( pRotationAxis ) ;
@@ -95,9 +110,6 @@ function drawLine(memo: IMemo) {
95
110
96
111
type TColourScheme = 'light' | 'dark' ;
97
112
98
- /**
99
- * NOTE: if OS is dark but devtools is default - then scheme is dark
100
- */
101
113
export function onColourSchemeChange (
102
114
callback : ( scheme : TColourScheme ) => void ,
103
115
) {
0 commit comments