@@ -30,59 +30,72 @@ impl core::hash::Hash for OverlayContext {
30
30
impl OverlayContext {
31
31
pub fn quad ( & mut self , quad : Quad ) {
32
32
self . render_context . begin_path ( ) ;
33
- self . render_context . move_to ( quad. 0 [ 3 ] . x . round ( ) , quad. 0 [ 3 ] . y . round ( ) ) ;
33
+ self . render_context . move_to ( quad. 0 [ 3 ] . x . round ( ) - 0.5 , quad. 0 [ 3 ] . y . round ( ) - 0.5 ) ;
34
34
for i in 0 ..4 {
35
- self . render_context . line_to ( quad. 0 [ i] . x . round ( ) , quad. 0 [ i] . y . round ( ) ) ;
35
+ self . render_context . line_to ( quad. 0 [ i] . x . round ( ) - 0.5 , quad. 0 [ i] . y . round ( ) - 0.5 ) ;
36
36
}
37
37
self . render_context . set_stroke_style ( & wasm_bindgen:: JsValue :: from_str ( COLOR_OVERLAY_BLUE ) ) ;
38
38
self . render_context . stroke ( ) ;
39
39
}
40
40
41
41
pub fn line ( & mut self , start : DVec2 , end : DVec2 , color : Option < & str > ) {
42
+ let start = start. round ( ) - DVec2 :: splat ( 0.5 ) ;
43
+ let end = end. round ( ) - DVec2 :: splat ( 0.5 ) ;
44
+
42
45
self . render_context . begin_path ( ) ;
43
46
self . render_context . move_to ( start. x , start. y ) ;
44
47
self . render_context . line_to ( end. x , end. y ) ;
45
48
self . render_context . set_stroke_style ( & wasm_bindgen:: JsValue :: from_str ( color. unwrap_or ( COLOR_OVERLAY_BLUE ) ) ) ;
46
49
self . render_context . stroke ( ) ;
47
50
}
48
51
49
- pub fn handle ( & mut self , position : DVec2 , selected : bool ) {
52
+ pub fn manipulator_handle ( & mut self , position : DVec2 , selected : bool ) {
53
+ let position = position. round ( ) - DVec2 :: splat ( 0.5 ) ;
54
+
50
55
self . render_context . begin_path ( ) ;
51
- let position = position. round ( ) ;
52
- self . render_context
53
- . arc ( position. x + 0.5 , position. y + 0.5 , MANIPULATOR_GROUP_MARKER_SIZE / 2. , 0. , PI * 2. )
54
- . expect ( "draw circle" ) ;
56
+ self . render_context . arc ( position. x , position. y , MANIPULATOR_GROUP_MARKER_SIZE / 2. , 0. , PI * 2. ) . expect ( "draw circle" ) ;
55
57
56
58
let fill = if selected { COLOR_OVERLAY_BLUE } else { COLOR_OVERLAY_WHITE } ;
57
59
self . render_context . set_fill_style ( & wasm_bindgen:: JsValue :: from_str ( fill) ) ;
58
- self . render_context . fill ( ) ;
59
60
self . render_context . set_stroke_style ( & wasm_bindgen:: JsValue :: from_str ( COLOR_OVERLAY_BLUE ) ) ;
61
+ self . render_context . fill ( ) ;
60
62
self . render_context . stroke ( ) ;
61
63
}
62
64
63
- pub fn square ( & mut self , position : DVec2 , selected : bool , color_selected : Option < & str > ) {
64
- let color_selected = color_selected. unwrap_or ( COLOR_OVERLAY_BLUE ) ;
65
+ pub fn manipulator_anchor ( & mut self , position : DVec2 , selected : bool , color : Option < & str > ) {
66
+ let color_stroke = color. unwrap_or ( COLOR_OVERLAY_BLUE ) ;
67
+ let color_fill = if selected { color_stroke } else { COLOR_OVERLAY_WHITE } ;
68
+ self . square ( position, None , Some ( color_fill) , Some ( color_stroke) ) ;
69
+ }
70
+
71
+ pub fn square ( & mut self , position : DVec2 , size : Option < f64 > , color_fill : Option < & str > , color_stroke : Option < & str > ) {
72
+ let size = size. unwrap_or ( MANIPULATOR_GROUP_MARKER_SIZE ) ;
73
+ let color_fill = color_fill. unwrap_or ( COLOR_OVERLAY_WHITE ) ;
74
+ let color_stroke = color_stroke. unwrap_or ( COLOR_OVERLAY_BLUE ) ;
75
+
76
+ let position = position. round ( ) - DVec2 :: splat ( 0.5 ) ;
77
+ let corner = position - DVec2 :: splat ( size) / 2. ;
65
78
66
79
self . render_context . begin_path ( ) ;
67
- let corner = position - DVec2 :: splat ( MANIPULATOR_GROUP_MARKER_SIZE ) / 2. ;
68
- self . render_context
69
- . rect ( corner. x . round ( ) , corner. y . round ( ) , MANIPULATOR_GROUP_MARKER_SIZE , MANIPULATOR_GROUP_MARKER_SIZE ) ;
70
- let fill = if selected { color_selected } else { COLOR_OVERLAY_WHITE } ;
71
- self . render_context . set_fill_style ( & wasm_bindgen:: JsValue :: from_str ( fill) ) ;
80
+ self . render_context . rect ( corner. x , corner. y , size, size) ;
81
+ self . render_context . set_fill_style ( & wasm_bindgen:: JsValue :: from_str ( color_fill) ) ;
82
+ self . render_context . set_stroke_style ( & wasm_bindgen:: JsValue :: from_str ( color_stroke) ) ;
72
83
self . render_context . fill ( ) ;
73
- self . render_context . set_stroke_style ( & wasm_bindgen:: JsValue :: from_str ( color_selected) ) ;
74
84
self . render_context . stroke ( ) ;
75
85
}
76
86
77
- pub fn pivot ( & mut self , pivot : DVec2 ) {
78
- let x = pivot. x . round ( ) ;
79
- let y = pivot. y . round ( ) ;
87
+ pub fn pivot ( & mut self , position : DVec2 ) {
88
+ let ( x, y) = ( position. round ( ) - DVec2 :: splat ( 0.5 ) ) . into ( ) ;
89
+
90
+ // Circle
80
91
81
92
self . render_context . begin_path ( ) ;
82
93
self . render_context . arc ( x, y, PIVOT_DIAMETER / 2. , 0. , PI * 2. ) . expect ( "draw circle" ) ;
83
94
self . render_context . set_fill_style ( & wasm_bindgen:: JsValue :: from_str ( COLOR_OVERLAY_YELLOW ) ) ;
84
95
self . render_context . fill ( ) ;
85
96
97
+ // Crosshair
98
+
86
99
// Round line caps add half the stroke width to the length on each end, so we subtract that here before halving to get the radius
87
100
let crosshair_radius = ( PIVOT_CROSSHAIR_LENGTH - PIVOT_CROSSHAIR_THICKNESS ) / 2. ;
88
101
@@ -101,31 +114,44 @@ impl OverlayContext {
101
114
}
102
115
103
116
pub fn outline < ' a > ( & mut self , subpaths : impl Iterator < Item = & ' a Subpath < ManipulatorGroupId > > , transform : DAffine2 ) {
104
- let transform = |point| transform. transform_point2 ( point) ;
105
117
self . render_context . begin_path ( ) ;
106
118
for subpath in subpaths {
107
119
let mut curves = subpath. iter ( ) . peekable ( ) ;
120
+
108
121
let Some ( first) = curves. peek ( ) else {
109
122
continue ;
110
123
} ;
111
- self . render_context . move_to ( transform ( first. start ( ) ) . x , transform ( first. start ( ) ) . y ) ;
124
+
125
+ self . render_context . move_to ( transform. transform_point2 ( first. start ( ) ) . x , transform. transform_point2 ( first. start ( ) ) . y ) ;
112
126
for curve in curves {
113
127
match curve. handles {
114
- bezier_rs:: BezierHandles :: Linear => self . render_context . line_to ( transform ( curve. end ( ) ) . x , transform ( curve. end ( ) ) . y ) ,
128
+ bezier_rs:: BezierHandles :: Linear => {
129
+ let a = transform. transform_point2 ( curve. end ( ) ) ;
130
+ let a = a. round ( ) - DVec2 :: splat ( 0.5 ) ;
131
+
132
+ self . render_context . line_to ( a. x , a. y )
133
+ }
115
134
bezier_rs:: BezierHandles :: Quadratic { handle } => {
116
- self . render_context
117
- . quadratic_curve_to ( transform ( handle) . x , transform ( handle) . y , transform ( curve. end ( ) ) . x , transform ( curve. end ( ) ) . y )
135
+ let a = transform. transform_point2 ( handle) ;
136
+ let b = transform. transform_point2 ( curve. end ( ) ) ;
137
+ let a = a. round ( ) - DVec2 :: splat ( 0.5 ) ;
138
+ let b = b. round ( ) - DVec2 :: splat ( 0.5 ) ;
139
+
140
+ self . render_context . quadratic_curve_to ( a. x , a. y , b. x , b. y )
141
+ }
142
+ bezier_rs:: BezierHandles :: Cubic { handle_start, handle_end } => {
143
+ let a = transform. transform_point2 ( handle_start) ;
144
+ let b = transform. transform_point2 ( handle_end) ;
145
+ let c = transform. transform_point2 ( curve. end ( ) ) ;
146
+ let a = a. round ( ) - DVec2 :: splat ( 0.5 ) ;
147
+ let b = b. round ( ) - DVec2 :: splat ( 0.5 ) ;
148
+ let c = c. round ( ) - DVec2 :: splat ( 0.5 ) ;
149
+
150
+ self . render_context . bezier_curve_to ( a. x , a. y , b. x , b. y , c. x , c. y )
118
151
}
119
- bezier_rs:: BezierHandles :: Cubic { handle_start, handle_end } => self . render_context . bezier_curve_to (
120
- transform ( handle_start) . x ,
121
- transform ( handle_start) . y ,
122
- transform ( handle_end) . x ,
123
- transform ( handle_end) . y ,
124
- transform ( curve. end ( ) ) . x ,
125
- transform ( curve. end ( ) ) . y ,
126
- ) ,
127
152
}
128
153
}
154
+
129
155
if subpath. closed ( ) {
130
156
self . render_context . close_path ( ) ;
131
157
}
0 commit comments