@@ -3,19 +3,32 @@ import { nodes, isLeaf, Node } from "./quadtree";
3
3
export function initializeCanvas ( ) {
4
4
const canvas = document . getElementById ( "canvas" ) as HTMLCanvasElement ;
5
5
const scale = window . devicePixelRatio ;
6
- canvas . width = window . innerWidth * scale ;
7
- canvas . height = window . innerHeight * scale ;
6
+ canvas . width = Math . floor ( window . innerWidth * scale ) ;
7
+ canvas . height = Math . floor ( window . innerHeight * scale ) ;
8
8
const context = canvas . getContext ( "2d" ) as CanvasRenderingContext2D ;
9
9
context . scale ( scale , scale ) ;
10
+
10
11
return {
11
- canvas,
12
- context,
12
+ draw : ( tree : Node ) => {
13
+ clear ( context , canvas . width , canvas . height ) ;
14
+ for ( let node of nodes ( tree ) ) {
15
+ drawNode ( context , node ) ;
16
+ drawPoints ( context , node ) ;
17
+ }
18
+ } ,
19
+ width : canvas . width / scale ,
20
+ height : canvas . height / scale ,
21
+ element : canvas ,
13
22
} ;
14
23
}
15
24
16
- export function clearCanvas ( context : CanvasRenderingContext2D ) {
25
+ function clear (
26
+ context : CanvasRenderingContext2D ,
27
+ width : number ,
28
+ height : number
29
+ ) {
17
30
context . fillStyle = "#eee" ;
18
- context . fillRect ( 0 , 0 , window . innerWidth , window . innerHeight ) ;
31
+ context . fillRect ( 0 , 0 , width , height ) ;
19
32
}
20
33
21
34
function drawNode ( context : CanvasRenderingContext2D , node : Node ) {
@@ -35,11 +48,3 @@ function drawPoints(context: CanvasRenderingContext2D, node: Node) {
35
48
}
36
49
}
37
50
}
38
-
39
- export function draw ( tree : Node , context : CanvasRenderingContext2D ) {
40
- clearCanvas ( context ) ;
41
- for ( let node of nodes ( tree ) ) {
42
- drawNode ( context , node ) ;
43
- drawPoints ( context , node ) ;
44
- }
45
- }
0 commit comments