Skip to main content

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