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