@@ -61,6 +61,13 @@ impl CanvasBackend {
6161 pub fn with_canvas_object ( canvas : HtmlCanvasElement ) -> Option < Self > {
6262 Self :: init_backend ( canvas)
6363 }
64+
65+ /// Sets the stroke style and line width in the underlying context.
66+ fn set_line_style ( & mut self , style : & impl BackendStyle ) {
67+ self . context
68+ . set_stroke_style ( & make_canvas_color ( style. as_color ( ) ) ) ;
69+ self . context . set_line_width ( style. stroke_width ( ) as f64 ) ;
70+ }
6471}
6572
6673fn make_canvas_color ( color : BackendColor ) -> JsValue {
@@ -110,9 +117,7 @@ impl DrawingBackend for CanvasBackend {
110117 return Ok ( ( ) ) ;
111118 }
112119
113- self . context
114- . set_stroke_style ( & make_canvas_color ( style. color ( ) ) ) ;
115- self . context . set_line_width ( style. stroke_width ( ) as f64 ) ;
120+ self . set_line_style ( style) ;
116121 self . context . begin_path ( ) ;
117122 self . context . move_to ( f64:: from ( from. 0 ) , f64:: from ( from. 1 ) ) ;
118123 self . context . line_to ( f64:: from ( to. 0 ) , f64:: from ( to. 1 ) ) ;
@@ -140,8 +145,7 @@ impl DrawingBackend for CanvasBackend {
140145 f64:: from ( bottom_right. 1 - upper_left. 1 ) ,
141146 ) ;
142147 } else {
143- self . context
144- . set_stroke_style ( & make_canvas_color ( style. color ( ) ) ) ;
148+ self . set_line_style ( style) ;
145149 self . context . stroke_rect (
146150 f64:: from ( upper_left. 0 ) ,
147151 f64:: from ( upper_left. 1 ) ,
@@ -163,8 +167,7 @@ impl DrawingBackend for CanvasBackend {
163167 let mut path = path. into_iter ( ) ;
164168 self . context . begin_path ( ) ;
165169 if let Some ( start) = path. next ( ) {
166- self . context
167- . set_stroke_style ( & make_canvas_color ( style. color ( ) ) ) ;
170+ self . set_line_style ( style) ;
168171 self . context . move_to ( f64:: from ( start. 0 ) , f64:: from ( start. 1 ) ) ;
169172 for next in path {
170173 self . context . line_to ( f64:: from ( next. 0 ) , f64:: from ( next. 1 ) ) ;
@@ -211,8 +214,7 @@ impl DrawingBackend for CanvasBackend {
211214 self . context
212215 . set_fill_style ( & make_canvas_color ( style. color ( ) ) ) ;
213216 } else {
214- self . context
215- . set_stroke_style ( & make_canvas_color ( style. color ( ) ) ) ;
217+ self . set_line_style ( style) ;
216218 }
217219 self . context . begin_path ( ) ;
218220 self . context
0 commit comments