script/dom/canvas/2d/
canvasrenderingcontext2d.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use base::{Epoch, generic_channel};
6use canvas_traits::canvas::{Canvas2dMsg, CanvasId};
7use dom_struct::dom_struct;
8use euclid::default::Size2D;
9use pixels::Snapshot;
10use script_bindings::reflector::AssociatedMemory;
11use servo_url::ServoUrl;
12use webrender_api::ImageKey;
13
14use super::canvas_state::CanvasState;
15use crate::canvas_context::{CanvasContext, CanvasHelpers, HTMLCanvasElementOrOffscreenCanvas};
16use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
17    CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
18    CanvasRenderingContext2DMethods, CanvasTextAlign, CanvasTextBaseline,
19};
20use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
21use crate::dom::bindings::codegen::UnionTypes::{
22    HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
23    StringOrCanvasGradientOrCanvasPattern,
24};
25use crate::dom::bindings::error::{ErrorResult, Fallible};
26use crate::dom::bindings::num::Finite;
27use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
28use crate::dom::bindings::root::{Dom, DomRoot};
29use crate::dom::bindings::str::DOMString;
30use crate::dom::canvasgradient::CanvasGradient;
31use crate::dom::canvaspattern::CanvasPattern;
32use crate::dom::dommatrix::DOMMatrix;
33use crate::dom::globalscope::GlobalScope;
34use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
35use crate::dom::imagedata::ImageData;
36use crate::dom::path2d::Path2D;
37use crate::dom::textmetrics::TextMetrics;
38use crate::script_runtime::CanGc;
39
40// https://html.spec.whatwg.org/multipage/#canvasrenderingcontext2d
41#[dom_struct]
42pub(crate) struct CanvasRenderingContext2D {
43    reflector_: Reflector<AssociatedMemory>,
44    canvas: HTMLCanvasElementOrOffscreenCanvas,
45    canvas_state: CanvasState,
46}
47
48impl CanvasRenderingContext2D {
49    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
50    pub(crate) fn new_inherited(
51        global: &GlobalScope,
52        canvas: HTMLCanvasElementOrOffscreenCanvas,
53        size: Size2D<u32>,
54    ) -> Option<CanvasRenderingContext2D> {
55        let canvas_state =
56            CanvasState::new(global, Size2D::new(size.width as u64, size.height as u64))?;
57        Some(CanvasRenderingContext2D {
58            reflector_: Reflector::new(),
59            canvas,
60            canvas_state,
61        })
62    }
63
64    pub(crate) fn new(
65        global: &GlobalScope,
66        canvas: &HTMLCanvasElement,
67        size: Size2D<u32>,
68        can_gc: CanGc,
69    ) -> Option<DomRoot<CanvasRenderingContext2D>> {
70        CanvasRenderingContext2D::new_inherited(
71            global,
72            HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(Dom::from_ref(canvas)),
73            size,
74        )
75        .map(|context| reflect_dom_object(Box::new(context), global, can_gc))
76    }
77
78    pub(crate) fn take_missing_image_urls(&self) -> Vec<ServoUrl> {
79        std::mem::take(&mut self.canvas_state.get_missing_image_urls().borrow_mut())
80    }
81
82    pub(crate) fn get_canvas_id(&self) -> CanvasId {
83        self.canvas_state.get_canvas_id()
84    }
85
86    pub(crate) fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
87        self.canvas_state.send_canvas_2d_msg(msg)
88    }
89
90    pub(crate) fn set_image_key(&self, image_key: ImageKey) {
91        self.canvas_state.set_image_key(image_key);
92    }
93
94    pub(crate) fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
95        if !self.onscreen() {
96            return false;
97        }
98        self.canvas_state.update_rendering(Some(canvas_epoch))
99    }
100}
101
102impl CanvasContext for CanvasRenderingContext2D {
103    type ID = CanvasId;
104
105    fn context_id(&self) -> Self::ID {
106        self.canvas_state.get_canvas_id()
107    }
108
109    fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
110        Some(RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas))
111    }
112
113    fn resize(&self) {
114        self.reflector_
115            .update_memory_size(self, self.size().cast::<usize>().area());
116        self.canvas_state.set_bitmap_dimensions(self.size().cast());
117    }
118
119    fn reset_bitmap(&self) {
120        self.canvas_state.reset_bitmap()
121    }
122
123    fn get_image_data(&self) -> Option<Snapshot> {
124        if !self.canvas_state.is_paintable() {
125            return None;
126        }
127
128        let (sender, receiver) = generic_channel::channel().unwrap();
129        self.canvas_state
130            .send_canvas_2d_msg(Canvas2dMsg::GetImageData(None, sender));
131        Some(receiver.recv().unwrap().to_owned())
132    }
133
134    fn origin_is_clean(&self) -> bool {
135        self.canvas_state.origin_is_clean()
136    }
137
138    fn mark_as_dirty(&self) {
139        self.canvas.mark_as_dirty();
140    }
141}
142
143// We add a guard to each of methods by the spec:
144// http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas_CR/
145//
146// > Except where otherwise specified, for the 2D context interface,
147// > any method call with a numeric argument whose value is infinite or a NaN value must be ignored.
148//
149//  Restricted values are guarded in glue code. Therefore we need not add a guard.
150//
151// FIXME: this behavior should might be generated by some annotations to idl.
152impl CanvasRenderingContext2DMethods<crate::DomTypeHolder> for CanvasRenderingContext2D {
153    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-canvas>
154    fn Canvas(&self) -> DomRoot<HTMLCanvasElement> {
155        match &self.canvas {
156            HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => canvas.as_rooted(),
157            _ => panic!("Should not be called from offscreen canvas"),
158        }
159    }
160
161    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-save>
162    fn Save(&self) {
163        self.canvas_state.save()
164    }
165
166    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-restore>
167    fn Restore(&self) {
168        self.canvas_state.restore()
169    }
170
171    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
172    fn Reset(&self) {
173        self.canvas_state.reset()
174    }
175
176    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-scale>
177    fn Scale(&self, x: f64, y: f64) {
178        self.canvas_state.scale(x, y)
179    }
180
181    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate>
182    fn Rotate(&self, angle: f64) {
183        self.canvas_state.rotate(angle)
184    }
185
186    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-translate>
187    fn Translate(&self, x: f64, y: f64) {
188        self.canvas_state.translate(x, y)
189    }
190
191    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-transform>
192    fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
193        self.canvas_state.transform(a, b, c, d, e, f)
194    }
195
196    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform>
197    fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
198        self.canvas_state.get_transform(&self.global(), can_gc)
199    }
200
201    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
202    fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
203        self.canvas_state.set_transform(a, b, c, d, e, f);
204        Ok(())
205    }
206
207    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
208    fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
209        self.canvas_state.set_transform_(transform)
210    }
211
212    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform>
213    fn ResetTransform(&self) {
214        self.canvas_state.reset_transform()
215    }
216
217    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha>
218    fn GlobalAlpha(&self) -> f64 {
219        self.canvas_state.global_alpha()
220    }
221
222    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha>
223    fn SetGlobalAlpha(&self, alpha: f64) {
224        self.canvas_state.set_global_alpha(alpha)
225    }
226
227    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation>
228    fn GlobalCompositeOperation(&self) -> DOMString {
229        self.canvas_state.global_composite_operation()
230    }
231
232    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation>
233    fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
234        self.canvas_state.set_global_composite_operation(op_str)
235    }
236
237    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect>
238    fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
239        self.canvas_state.fill_rect(x, y, width, height);
240        self.mark_as_dirty();
241    }
242
243    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect>
244    fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
245        self.canvas_state.clear_rect(x, y, width, height);
246        self.mark_as_dirty();
247    }
248
249    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect>
250    fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
251        self.canvas_state.stroke_rect(x, y, width, height);
252        self.mark_as_dirty();
253    }
254
255    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath>
256    fn BeginPath(&self) {
257        self.canvas_state.begin_path()
258    }
259
260    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath>
261    fn ClosePath(&self) {
262        self.canvas_state.close_path()
263    }
264
265    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fill>
266    fn Fill(&self, fill_rule: CanvasFillRule) {
267        self.canvas_state.fill(fill_rule);
268        self.mark_as_dirty();
269    }
270
271    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fill>
272    fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
273        self.canvas_state.fill_(path.segments(), fill_rule);
274        self.mark_as_dirty();
275    }
276
277    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke>
278    fn Stroke(&self) {
279        self.canvas_state.stroke();
280        self.mark_as_dirty();
281    }
282
283    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke>
284    fn Stroke_(&self, path: &Path2D) {
285        self.canvas_state.stroke_(path.segments());
286        self.mark_as_dirty();
287    }
288
289    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clip>
290    fn Clip(&self, fill_rule: CanvasFillRule) {
291        self.canvas_state.clip(fill_rule)
292    }
293
294    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clip>
295    fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
296        self.canvas_state.clip_(path.segments(), fill_rule)
297    }
298
299    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath>
300    fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
301        self.canvas_state
302            .is_point_in_path(&self.global(), x, y, fill_rule)
303    }
304
305    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath>
306    fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
307        self.canvas_state
308            .is_point_in_path_(&self.global(), path.segments(), x, y, fill_rule)
309    }
310
311    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext>
312    fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
313        self.canvas_state.fill_text(
314            &self.global(),
315            self.canvas.canvas().as_deref(),
316            text,
317            x,
318            y,
319            max_width,
320        );
321        self.mark_as_dirty();
322    }
323
324    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroketext>
325    fn StrokeText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
326        self.canvas_state.stroke_text(
327            &self.global(),
328            self.canvas.canvas().as_deref(),
329            text,
330            x,
331            y,
332            max_width,
333        );
334        self.mark_as_dirty();
335    }
336
337    /// <https://html.spec.whatwg.org/multipage/#textmetrics>
338    fn MeasureText(&self, text: DOMString, can_gc: CanGc) -> DomRoot<TextMetrics> {
339        self.canvas_state.measure_text(
340            &self.global(),
341            self.canvas.canvas().as_deref(),
342            text,
343            can_gc,
344        )
345    }
346
347    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-font>
348    fn Font(&self) -> DOMString {
349        self.canvas_state.font()
350    }
351
352    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-font>
353    fn SetFont(&self, value: DOMString) {
354        self.canvas_state
355            .set_font(self.canvas.canvas().as_deref(), value)
356    }
357
358    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign>
359    fn TextAlign(&self) -> CanvasTextAlign {
360        self.canvas_state.text_align()
361    }
362
363    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign>
364    fn SetTextAlign(&self, value: CanvasTextAlign) {
365        self.canvas_state.set_text_align(value)
366    }
367
368    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline>
369    fn TextBaseline(&self) -> CanvasTextBaseline {
370        self.canvas_state.text_baseline()
371    }
372
373    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline>
374    fn SetTextBaseline(&self, value: CanvasTextBaseline) {
375        self.canvas_state.set_text_baseline(value)
376    }
377
378    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-direction>
379    fn Direction(&self) -> CanvasDirection {
380        self.canvas_state.direction()
381    }
382
383    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-direction>
384    fn SetDirection(&self, value: CanvasDirection) {
385        self.canvas_state.set_direction(value)
386    }
387
388    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
389    fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
390        self.canvas_state
391            .draw_image(self.canvas.canvas().as_deref(), image, dx, dy)
392    }
393
394    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
395    fn DrawImage_(
396        &self,
397        image: CanvasImageSource,
398        dx: f64,
399        dy: f64,
400        dw: f64,
401        dh: f64,
402    ) -> ErrorResult {
403        self.canvas_state
404            .draw_image_(self.canvas.canvas().as_deref(), image, dx, dy, dw, dh)
405    }
406
407    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
408    fn DrawImage__(
409        &self,
410        image: CanvasImageSource,
411        sx: f64,
412        sy: f64,
413        sw: f64,
414        sh: f64,
415        dx: f64,
416        dy: f64,
417        dw: f64,
418        dh: f64,
419    ) -> ErrorResult {
420        self.canvas_state.draw_image__(
421            self.canvas.canvas().as_deref(),
422            image,
423            sx,
424            sy,
425            sw,
426            sh,
427            dx,
428            dy,
429            dw,
430            dh,
431        )
432    }
433
434    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto>
435    fn MoveTo(&self, x: f64, y: f64) {
436        self.canvas_state.move_to(x, y)
437    }
438
439    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto>
440    fn LineTo(&self, x: f64, y: f64) {
441        self.canvas_state.line_to(x, y)
442    }
443
444    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rect>
445    fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
446        self.canvas_state.rect(x, y, width, height)
447    }
448
449    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto>
450    fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
451        self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
452    }
453
454    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto>
455    fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
456        self.canvas_state
457            .bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
458    }
459
460    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arc>
461    fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
462        self.canvas_state.arc(x, y, r, start, end, ccw)
463    }
464
465    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto>
466    fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
467        self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
468    }
469
470    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse>
471    fn Ellipse(
472        &self,
473        x: f64,
474        y: f64,
475        rx: f64,
476        ry: f64,
477        rotation: f64,
478        start: f64,
479        end: f64,
480        ccw: bool,
481    ) -> ErrorResult {
482        self.canvas_state
483            .ellipse(x, y, rx, ry, rotation, start, end, ccw)
484    }
485
486    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled>
487    fn ImageSmoothingEnabled(&self) -> bool {
488        self.canvas_state.image_smoothing_enabled()
489    }
490
491    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled>
492    fn SetImageSmoothingEnabled(&self, value: bool) {
493        self.canvas_state.set_image_smoothing_enabled(value)
494    }
495
496    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
497    fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
498        self.canvas_state.stroke_style()
499    }
500
501    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
502    fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
503        self.canvas_state
504            .set_stroke_style(self.canvas.canvas().as_deref(), value)
505    }
506
507    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
508    fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
509        self.canvas_state.fill_style()
510    }
511
512    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
513    fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
514        self.canvas_state
515            .set_fill_style(self.canvas.canvas().as_deref(), value)
516    }
517
518    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata>
519    fn CreateImageData(&self, sw: i32, sh: i32, can_gc: CanGc) -> Fallible<DomRoot<ImageData>> {
520        self.canvas_state
521            .create_image_data(&self.global(), sw, sh, can_gc)
522    }
523
524    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata>
525    fn CreateImageData_(
526        &self,
527        imagedata: &ImageData,
528        can_gc: CanGc,
529    ) -> Fallible<DomRoot<ImageData>> {
530        self.canvas_state
531            .create_image_data_(&self.global(), imagedata, can_gc)
532    }
533
534    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getimagedata>
535    fn GetImageData(
536        &self,
537        sx: i32,
538        sy: i32,
539        sw: i32,
540        sh: i32,
541        can_gc: CanGc,
542    ) -> Fallible<DomRoot<ImageData>> {
543        self.canvas_state
544            .get_image_data(self.canvas.size(), &self.global(), sx, sy, sw, sh, can_gc)
545    }
546
547    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata>
548    fn PutImageData(&self, imagedata: &ImageData, dx: i32, dy: i32) {
549        self.canvas_state
550            .put_image_data(self.canvas.size(), imagedata, dx, dy)
551    }
552
553    // https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
554    fn PutImageData_(
555        &self,
556        imagedata: &ImageData,
557        dx: i32,
558        dy: i32,
559        dirty_x: i32,
560        dirty_y: i32,
561        dirty_width: i32,
562        dirty_height: i32,
563    ) {
564        self.canvas_state.put_image_data_(
565            self.canvas.size(),
566            imagedata,
567            dx,
568            dy,
569            dirty_x,
570            dirty_y,
571            dirty_width,
572            dirty_height,
573        );
574        self.mark_as_dirty();
575    }
576
577    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient>
578    fn CreateLinearGradient(
579        &self,
580        x0: Finite<f64>,
581        y0: Finite<f64>,
582        x1: Finite<f64>,
583        y1: Finite<f64>,
584        can_gc: CanGc,
585    ) -> DomRoot<CanvasGradient> {
586        self.canvas_state
587            .create_linear_gradient(&self.global(), x0, y0, x1, y1, can_gc)
588    }
589
590    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient>
591    fn CreateRadialGradient(
592        &self,
593        x0: Finite<f64>,
594        y0: Finite<f64>,
595        r0: Finite<f64>,
596        x1: Finite<f64>,
597        y1: Finite<f64>,
598        r1: Finite<f64>,
599        can_gc: CanGc,
600    ) -> Fallible<DomRoot<CanvasGradient>> {
601        self.canvas_state
602            .create_radial_gradient(&self.global(), x0, y0, r0, x1, y1, r1, can_gc)
603    }
604
605    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern>
606    fn CreatePattern(
607        &self,
608        image: CanvasImageSource,
609        repetition: DOMString,
610        can_gc: CanGc,
611    ) -> Fallible<Option<DomRoot<CanvasPattern>>> {
612        self.canvas_state
613            .create_pattern(&self.global(), image, repetition, can_gc)
614    }
615
616    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth>
617    fn LineWidth(&self) -> f64 {
618        self.canvas_state.line_width()
619    }
620
621    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth>
622    fn SetLineWidth(&self, width: f64) {
623        self.canvas_state.set_line_width(width)
624    }
625
626    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap>
627    fn LineCap(&self) -> CanvasLineCap {
628        self.canvas_state.line_cap()
629    }
630
631    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap>
632    fn SetLineCap(&self, cap: CanvasLineCap) {
633        self.canvas_state.set_line_cap(cap)
634    }
635
636    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin>
637    fn LineJoin(&self) -> CanvasLineJoin {
638        self.canvas_state.line_join()
639    }
640
641    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin>
642    fn SetLineJoin(&self, join: CanvasLineJoin) {
643        self.canvas_state.set_line_join(join)
644    }
645
646    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit>
647    fn MiterLimit(&self) -> f64 {
648        self.canvas_state.miter_limit()
649    }
650
651    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit>
652    fn SetMiterLimit(&self, limit: f64) {
653        self.canvas_state.set_miter_limit(limit)
654    }
655
656    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
657    fn SetLineDash(&self, segments: Vec<f64>) {
658        self.canvas_state.set_line_dash(segments);
659    }
660
661    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
662    fn GetLineDash(&self) -> Vec<f64> {
663        self.canvas_state.line_dash()
664    }
665
666    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
667    fn LineDashOffset(&self) -> f64 {
668        self.canvas_state.line_dash_offset()
669    }
670
671    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
672    fn SetLineDashOffset(&self, offset: f64) {
673        self.canvas_state.set_line_dash_offset(offset);
674    }
675
676    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx>
677    fn ShadowOffsetX(&self) -> f64 {
678        self.canvas_state.shadow_offset_x()
679    }
680
681    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx>
682    fn SetShadowOffsetX(&self, value: f64) {
683        self.canvas_state.set_shadow_offset_x(value)
684    }
685
686    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety>
687    fn ShadowOffsetY(&self) -> f64 {
688        self.canvas_state.shadow_offset_y()
689    }
690
691    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety>
692    fn SetShadowOffsetY(&self, value: f64) {
693        self.canvas_state.set_shadow_offset_y(value)
694    }
695
696    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur>
697    fn ShadowBlur(&self) -> f64 {
698        self.canvas_state.shadow_blur()
699    }
700
701    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur>
702    fn SetShadowBlur(&self, value: f64) {
703        self.canvas_state.set_shadow_blur(value)
704    }
705
706    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor>
707    fn ShadowColor(&self) -> DOMString {
708        self.canvas_state.shadow_color()
709    }
710
711    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor>
712    fn SetShadowColor(&self, value: DOMString) {
713        self.canvas_state
714            .set_shadow_color(self.canvas.canvas().as_deref(), value)
715    }
716}