2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- import 'dart:html' as html;
6
5
import 'dart:math' as math;
7
6
import 'dart:typed_data' ;
8
7
@@ -46,17 +45,17 @@ import 'window.dart';
46
45
/// can be reused, [CanvasPool] will move canvas(s) from pool to reusablePool
47
46
/// to prevent reallocation.
48
47
class CanvasPool extends _SaveStackTracking {
49
- html. CanvasRenderingContext2D ? _context;
48
+ DomCanvasRenderingContext2D ? _context;
50
49
ContextStateHandle ? _contextHandle;
51
50
final int _widthInBitmapPixels, _heightInBitmapPixels;
52
51
// List of canvases that have been allocated and used in this paint cycle.
53
- List <html. CanvasElement >? _activeCanvasList;
52
+ List <DomCanvasElement >? _activeCanvasList;
54
53
// List of canvases available to reuse from prior paint cycle.
55
- List <html. CanvasElement >? _reusablePool;
54
+ List <DomCanvasElement >? _reusablePool;
56
55
// Current canvas element or null if marked for lazy allocation.
57
- html. CanvasElement ? _canvas;
56
+ DomCanvasElement ? _canvas;
58
57
59
- html. HtmlElement ? _rootElement;
58
+ DomHTMLElement ? _rootElement;
60
59
int _saveContextCount = 0 ;
61
60
final double _density;
62
61
@@ -65,7 +64,7 @@ class CanvasPool extends _SaveStackTracking {
65
64
this ._density);
66
65
67
66
/// Initializes canvas pool to be hosted on a surface.
68
- void mount (html. HtmlElement rootElement) {
67
+ void mount (DomHTMLElement rootElement) {
69
68
_rootElement = rootElement;
70
69
}
71
70
@@ -78,8 +77,8 @@ class CanvasPool extends _SaveStackTracking {
78
77
}
79
78
80
79
/// Returns [CanvasRenderingContext2D] api to draw into this canvas.
81
- html. CanvasRenderingContext2D get context {
82
- html. CanvasRenderingContext2D ? ctx = _context;
80
+ DomCanvasRenderingContext2D get context {
81
+ DomCanvasRenderingContext2D ? ctx = _context;
83
82
if (ctx == null ) {
84
83
_createCanvas ();
85
84
ctx = _context! ;
@@ -128,7 +127,7 @@ class CanvasPool extends _SaveStackTracking {
128
127
if (_canvas != null ) {
129
128
_restoreContextSave ();
130
129
_contextHandle! .reset ();
131
- _activeCanvasList ?? = < html. CanvasElement > [];
130
+ _activeCanvasList ?? = < DomCanvasElement > [];
132
131
_activeCanvasList! .add (_canvas! );
133
132
_canvas = null ;
134
133
_context = null ;
@@ -139,7 +138,7 @@ class CanvasPool extends _SaveStackTracking {
139
138
void _createCanvas () {
140
139
bool requiresClearRect = false ;
141
140
bool reused = false ;
142
- html. CanvasElement ? canvas;
141
+ DomCanvasElement ? canvas;
143
142
if (_canvas != null ) {
144
143
_canvas! .width = 0 ;
145
144
_canvas! .height = 0 ;
@@ -214,13 +213,13 @@ class CanvasPool extends _SaveStackTracking {
214
213
_replayClipStack ();
215
214
}
216
215
217
- html. CanvasElement ? _allocCanvas (int width, int height) {
216
+ DomCanvasElement ? _allocCanvas (int width, int height) {
218
217
// The dartdocs for `tryCreateCanvasElement` on why we don't use the
219
- // `html.CanvasElement ` constructor.
218
+ // `DomCanvasElement ` constructor.
220
219
return tryCreateCanvasElement (
221
220
(width * _density).ceil (),
222
221
(height * _density).ceil (),
223
- );
222
+ ) as DomCanvasElement ? ;
224
223
}
225
224
226
225
@override
@@ -229,7 +228,7 @@ class CanvasPool extends _SaveStackTracking {
229
228
230
229
if (_canvas != null ) {
231
230
// Restore to the state where we have only applied the scaling.
232
- final html. CanvasRenderingContext2D ? ctx = _context;
231
+ final DomCanvasRenderingContext2D ? ctx = _context;
233
232
if (ctx != null ) {
234
233
try {
235
234
ctx.font = '' ;
@@ -247,7 +246,7 @@ class CanvasPool extends _SaveStackTracking {
247
246
248
247
int _replaySingleSaveEntry (int clipDepth, Matrix4 prevTransform,
249
248
Matrix4 transform, List <SaveClipEntry >? clipStack) {
250
- final html. CanvasRenderingContext2D ctx = context;
249
+ final DomCanvasRenderingContext2D ctx = context;
251
250
if (clipStack != null ) {
252
251
for (final int clipCount = clipStack.length;
253
252
clipDepth < clipCount;
@@ -306,7 +305,7 @@ class CanvasPool extends _SaveStackTracking {
306
305
307
306
void _replayClipStack () {
308
307
// Replay save/clip stack on this canvas now.
309
- final html. CanvasRenderingContext2D ctx = context;
308
+ final DomCanvasRenderingContext2D ctx = context;
310
309
int clipDepth = 0 ;
311
310
Matrix4 prevTransform = Matrix4 .identity ();
312
311
final int len = _saveStack.length;
@@ -327,7 +326,7 @@ class CanvasPool extends _SaveStackTracking {
327
326
if (_canvas != null ) {
328
327
_restoreContextSave ();
329
328
_contextHandle! .reset ();
330
- _activeCanvasList ?? = < html. CanvasElement > [];
329
+ _activeCanvasList ?? = < DomCanvasElement > [];
331
330
_activeCanvasList! .add (_canvas! );
332
331
_context = null ;
333
332
_contextHandle = null ;
@@ -344,7 +343,7 @@ class CanvasPool extends _SaveStackTracking {
344
343
/// that are reused from last instance can be cleanup.
345
344
void endOfPaint () {
346
345
if (_reusablePool != null ) {
347
- for (final html. CanvasElement e in _reusablePool! ) {
346
+ for (final DomCanvasElement e in _reusablePool! ) {
348
347
if (browserEngine == BrowserEngine .webkit) {
349
348
e.width = e.height = 0 ;
350
349
}
@@ -366,7 +365,7 @@ class CanvasPool extends _SaveStackTracking {
366
365
/// coordinate system, and the pixel ratio is applied such that CSS pixels are
367
366
/// translated to bitmap pixels.
368
367
void _initializeViewport (bool clearCanvas) {
369
- final html. CanvasRenderingContext2D ctx = context;
368
+ final DomCanvasRenderingContext2D ctx = context;
370
369
// Save the canvas state with top-level transforms so we can undo
371
370
// any clips later when we reuse the canvas.
372
371
ctx.save ();
@@ -390,7 +389,7 @@ class CanvasPool extends _SaveStackTracking {
390
389
EnginePlatformDispatcher .browserDevicePixelRatio * _density;
391
390
392
391
void _resetTransform () {
393
- final html. CanvasElement ? canvas = _canvas;
392
+ final DomCanvasElement ? canvas = _canvas;
394
393
if (canvas != null ) {
395
394
canvas.style.transformOrigin = '' ;
396
395
canvas.style.transform = '' ;
@@ -399,7 +398,7 @@ class CanvasPool extends _SaveStackTracking {
399
398
400
399
/// Returns a "data://" URI containing a representation of the image in this
401
400
/// canvas in PNG format.
402
- String toDataUrl () => _canvas? .toDataUrl ( ) ?? '' ;
401
+ String toDataUrl () => _canvas? .toDataURL ( 'image/png' ) ?? '' ;
403
402
404
403
@override
405
404
void save () {
@@ -498,7 +497,7 @@ class CanvasPool extends _SaveStackTracking {
498
497
}
499
498
}
500
499
501
- void _clipRect (html. CanvasRenderingContext2D ctx, ui.Rect rect) {
500
+ void _clipRect (DomCanvasRenderingContext2D ctx, ui.Rect rect) {
502
501
ctx.beginPath ();
503
502
ctx.rect (rect.left, rect.top, rect.width, rect.height);
504
503
ctx.clip ();
@@ -512,7 +511,7 @@ class CanvasPool extends _SaveStackTracking {
512
511
}
513
512
}
514
513
515
- void _clipRRect (html. CanvasRenderingContext2D ctx, ui.RRect rrect) {
514
+ void _clipRRect (DomCanvasRenderingContext2D ctx, ui.RRect rrect) {
516
515
final ui.Path path = ui.Path ()..addRRect (rrect);
517
516
_runPath (ctx, path as SurfacePath );
518
517
ctx.clip ();
@@ -522,7 +521,7 @@ class CanvasPool extends _SaveStackTracking {
522
521
void clipPath (ui.Path path) {
523
522
super .clipPath (path);
524
523
if (_canvas != null ) {
525
- final html. CanvasRenderingContext2D ctx = context;
524
+ final DomCanvasRenderingContext2D ctx = context;
526
525
_runPath (ctx, path as SurfacePath );
527
526
if (path.fillType == ui.PathFillType .nonZero) {
528
527
ctx.clip ();
@@ -534,7 +533,7 @@ class CanvasPool extends _SaveStackTracking {
534
533
535
534
/// Fill a virtually infinite rect with a color and optional blendMode.
536
535
void drawColor (ui.Color color, ui.BlendMode blendMode) {
537
- final html. CanvasRenderingContext2D ctx = context;
536
+ final DomCanvasRenderingContext2D ctx = context;
538
537
contextHandle.blendMode = blendMode;
539
538
contextHandle.fillStyle = colorToCssString (color);
540
539
contextHandle.strokeStyle = '' ;
@@ -548,7 +547,7 @@ class CanvasPool extends _SaveStackTracking {
548
547
549
548
/// Fill a virtually infinite rect with the color.
550
549
void fill () {
551
- final html. CanvasRenderingContext2D ctx = context;
550
+ final DomCanvasRenderingContext2D ctx = context;
552
551
ctx.beginPath ();
553
552
// We can't use (0, 0, width, height) because the current transform can
554
553
// cause it to not fill the entire clip.
@@ -557,7 +556,7 @@ class CanvasPool extends _SaveStackTracking {
557
556
558
557
/// Draws a line from [p1] to [p2] .
559
558
void strokeLine (ui.Offset p1, ui.Offset p2) {
560
- final html. CanvasRenderingContext2D ctx = context;
559
+ final DomCanvasRenderingContext2D ctx = context;
561
560
ctx.beginPath ();
562
561
final ui.Rect ? shaderBounds = contextHandle._shaderBounds;
563
562
if (shaderBounds == null ) {
@@ -573,7 +572,7 @@ class CanvasPool extends _SaveStackTracking {
573
572
/// Draws a set of points with given radius, lines between points or
574
573
/// a polygon.
575
574
void drawPoints (ui.PointMode pointMode, Float32List points, double radius) {
576
- final html. CanvasRenderingContext2D ctx = context;
575
+ final DomCanvasRenderingContext2D ctx = context;
577
576
final int len = points.length;
578
577
final ui.Rect ? shaderBounds = contextHandle._shaderBounds;
579
578
final double offsetX = shaderBounds == null ? 0 : - shaderBounds.left;
@@ -611,7 +610,7 @@ class CanvasPool extends _SaveStackTracking {
611
610
static Float32List _runBuffer = Float32List (PathRefIterator .kMaxBufferSize);
612
611
613
612
/// 'Runs' the given [path] by applying all of its commands to the canvas.
614
- void _runPath (html. CanvasRenderingContext2D ctx, SurfacePath path) {
613
+ void _runPath (DomCanvasRenderingContext2D ctx, SurfacePath path) {
615
614
ctx.beginPath ();
616
615
final Float32List p = _runBuffer;
617
616
final PathRefIterator iter = PathRefIterator (path.pathRef);
@@ -668,7 +667,7 @@ class CanvasPool extends _SaveStackTracking {
668
667
/// Applies path to drawing context, preparing for fill and other operations.
669
668
///
670
669
/// WARNING: Don't refactor _runPath/_runPathWithOffset. Latency sensitive
671
- void _runPathWithOffset (html. CanvasRenderingContext2D ctx, SurfacePath path,
670
+ void _runPathWithOffset (DomCanvasRenderingContext2D ctx, SurfacePath path,
672
671
double offsetX, double offsetY) {
673
672
ctx.beginPath ();
674
673
final Float32List p = _runBuffer;
@@ -857,7 +856,7 @@ class CanvasPool extends _SaveStackTracking {
857
856
858
857
void _clearActiveCanvasList () {
859
858
if (_activeCanvasList != null ) {
860
- for (final html. CanvasElement c in _activeCanvasList! ) {
859
+ for (final DomCanvasElement c in _activeCanvasList! ) {
861
860
if (browserEngine == BrowserEngine .webkit) {
862
861
c.width = c.height = 0 ;
863
862
}
@@ -874,7 +873,7 @@ class CanvasPool extends _SaveStackTracking {
874
873
/// to initialize current values.
875
874
class ContextStateHandle {
876
875
/// Associated canvas element context tracked by this context state.
877
- final html. CanvasRenderingContext2D context;
876
+ final DomCanvasRenderingContext2D context;
878
877
final CanvasPool _canvasPool;
879
878
/// Dpi of context.
880
879
final double density;
@@ -890,7 +889,7 @@ class ContextStateHandle {
890
889
Object ? _currentStrokeStyle;
891
890
double _currentLineWidth = 1.0 ;
892
891
893
- /// See [html.CanvasRenderingContext2D ] .
892
+ /// See [DomCanvasRenderingContext2D ] .
894
893
set blendMode (ui.BlendMode ? blendMode) {
895
894
if (blendMode != _currentBlendMode) {
896
895
_currentBlendMode = blendMode;
@@ -899,7 +898,7 @@ class ContextStateHandle {
899
898
}
900
899
}
901
900
902
- /// See [html.CanvasRenderingContext2D ] .
901
+ /// See [DomCanvasRenderingContext2D ] .
903
902
set strokeCap (ui.StrokeCap ? strokeCap) {
904
903
strokeCap ?? = ui.StrokeCap .butt;
905
904
if (strokeCap != _currentStrokeCap) {
@@ -908,15 +907,15 @@ class ContextStateHandle {
908
907
}
909
908
}
910
909
911
- /// See [html.CanvasRenderingContext2D ] .
910
+ /// See [DomCanvasRenderingContext2D ] .
912
911
set lineWidth (double lineWidth) {
913
912
if (lineWidth != _currentLineWidth) {
914
913
_currentLineWidth = lineWidth;
915
914
context.lineWidth = lineWidth;
916
915
}
917
916
}
918
917
919
- /// See [html.CanvasRenderingContext2D ] .
918
+ /// See [DomCanvasRenderingContext2D ] .
920
919
set strokeJoin (ui.StrokeJoin ? strokeJoin) {
921
920
strokeJoin ?? = ui.StrokeJoin .miter;
922
921
if (strokeJoin != _currentStrokeJoin) {
@@ -925,15 +924,15 @@ class ContextStateHandle {
925
924
}
926
925
}
927
926
928
- /// See [html.CanvasRenderingContext2D ] .
927
+ /// See [DomCanvasRenderingContext2D ] .
929
928
set fillStyle (Object ? colorOrGradient) {
930
929
if (! identical (colorOrGradient, _currentFillStyle)) {
931
930
_currentFillStyle = colorOrGradient;
932
931
context.fillStyle = colorOrGradient;
933
932
}
934
933
}
935
934
936
- /// See [html.CanvasRenderingContext2D ] .
935
+ /// See [DomCanvasRenderingContext2D ] .
937
936
set strokeStyle (Object ? colorOrGradient) {
938
937
if (! identical (colorOrGradient, _currentStrokeStyle)) {
939
938
_currentStrokeStyle = colorOrGradient;
@@ -984,9 +983,7 @@ class ContextStateHandle {
984
983
if (paint.shader is EngineGradient ) {
985
984
final EngineGradient engineShader = paint.shader! as EngineGradient ;
986
985
final Object paintStyle =
987
- engineShader.createPaintStyle (_canvasPool.context as
988
- DomCanvasRenderingContext2D , shaderBounds,
989
- density);
986
+ engineShader.createPaintStyle (_canvasPool.context, shaderBounds, density);
990
987
fillStyle = paintStyle;
991
988
strokeStyle = paintStyle;
992
989
_shaderBounds = shaderBounds;
@@ -995,9 +992,7 @@ class ContextStateHandle {
995
992
} else if (paint.shader is EngineImageShader ) {
996
993
final EngineImageShader imageShader = paint.shader! as EngineImageShader ;
997
994
final Object paintStyle =
998
- imageShader.createPaintStyle (_canvasPool.context as
999
- DomCanvasRenderingContext2D , shaderBounds,
1000
- density);
995
+ imageShader.createPaintStyle (_canvasPool.context, shaderBounds, density);
1001
996
fillStyle = paintStyle;
1002
997
strokeStyle = paintStyle;
1003
998
if (imageShader.requiresTileOffset) {
@@ -1117,7 +1112,7 @@ class ContextStateHandle {
1117
1112
}
1118
1113
1119
1114
/// Resets drawing context state to defaults for
1120
- /// [html.CanvasRenderingContext2D ] .
1115
+ /// [DomCanvasRenderingContext2D ] .
1121
1116
void reset () {
1122
1117
context.fillStyle = '' ;
1123
1118
// Read back fillStyle/strokeStyle values from context so that input such
0 commit comments