Skip to content

Commit 725b40b

Browse files
committed
test: canvas paint test.
1 parent 31d56a9 commit 725b40b

File tree

3 files changed

+18
-68
lines changed

3 files changed

+18
-68
lines changed

webf/lib/src/html/canvas/canvas.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ class RenderCanvasPaint extends RenderCustomPaint {
2525
@override
2626
bool get isRepaintBoundary => true;
2727

28-
RenderCanvasPaint(
29-
{required CustomPainter painter, required Size preferredSize})
28+
@override
29+
CanvasPainter? get painter => super.painter as CanvasPainter;
30+
31+
RenderCanvasPaint({required CustomPainter painter, required Size preferredSize})
3032
: super(
31-
painter: painter,
32-
foregroundPainter: null, // Ignore foreground painter
33-
preferredSize: preferredSize,
34-
);
33+
painter: painter,
34+
foregroundPainter: null, // Ignore foreground painter
35+
preferredSize: preferredSize);
3536

3637
@override
3738
void paint(PaintingContext context, Offset offset) {
38-
context.pushClipRect(needsCompositing, offset,
39-
Rect.fromLTWH(0, 0, preferredSize.width, preferredSize.height),
39+
if (painter?.context == null) return;
40+
context.pushClipRect(true, offset, Rect.fromLTWH(0, 0, preferredSize.width, preferredSize.height),
4041
(context, offset) {
4142
super.paint(context, offset);
4243
});
@@ -71,18 +72,15 @@ class CanvasElement extends Element {
7172
@override
7273
void initializeMethods(Map<String, BindingObjectMethod> methods) {
7374
super.initializeMethods(methods);
74-
methods['getContext'] = BindingObjectMethodSync(
75-
call: (args) => getContext(castToType<String>(args[0])));
75+
methods['getContext'] = BindingObjectMethodSync(call: (args) => getContext(castToType<String>(args[0])));
7676
}
7777

7878
@override
7979
void initializeProperties(Map<String, BindingObjectProperty> properties) {
8080
super.initializeProperties(properties);
81-
properties['width'] = BindingObjectProperty(
82-
getter: () => width, setter: (value) => width = castToType<int>(value));
83-
properties['height'] = BindingObjectProperty(
84-
getter: () => height,
85-
setter: (value) => height = castToType<int>(value));
81+
properties['width'] = BindingObjectProperty(getter: () => width, setter: (value) => width = castToType<int>(value));
82+
properties['height'] =
83+
BindingObjectProperty(getter: () => height, setter: (value) => height = castToType<int>(value));
8684
}
8785

8886
@override
@@ -113,7 +111,8 @@ class CanvasElement extends Element {
113111
painter.dispose();
114112
}
115113

116-
context2d = CanvasRenderingContext2D(BindingContext(ownerView, ownerView.contextId, allocateNewBindingObject()), this);
114+
context2d =
115+
CanvasRenderingContext2D(BindingContext(ownerView, ownerView.contextId, allocateNewBindingObject()), this);
117116
painter.context = context2d;
118117

119118
return context2d!;

webf/lib/src/html/canvas/canvas_context_2d.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ class CanvasRenderingContext2D extends DynamicBindingObject {
382382
// HACK: We need record the current matrix state because flutter canvas not export resetTransform now.
383383
// https://github.com/flutter/engine/pull/25449
384384
Matrix4 _matrix = Matrix4.identity();
385-
Matrix4 _lastMatrix = Matrix4.identity();
386385

387386
int get actionCount => _actions.length;
388387

@@ -408,10 +407,6 @@ class CanvasRenderingContext2D extends DynamicBindingObject {
408407

409408
// Perform canvas drawing.
410409
List<CanvasAction> performActions(Canvas canvas, Size size) {
411-
// HACK: Must sync transform first because each paint will saveLayer and restore that make the transform not effect
412-
if (!_lastMatrix.isIdentity()) {
413-
canvas.transform(_lastMatrix.storage);
414-
}
415410
_pendingActions = _actions;
416411
_actions = [];
417412
for (int i = 0; i < _pendingActions.length; i++) {
@@ -422,9 +417,6 @@ class CanvasRenderingContext2D extends DynamicBindingObject {
422417

423418
// Clear the saved pending actions.
424419
void clearActions(List<CanvasAction> actions) {
425-
if (_lastMatrix != _matrix) {
426-
_lastMatrix = _matrix.clone();
427-
}
428420
actions.clear();
429421
}
430422

@@ -1276,7 +1268,6 @@ class CanvasRenderingContext2D extends DynamicBindingObject {
12761268
_actions = [];
12771269
_states.clear();
12781270
_matrix = Matrix4.identity();
1279-
_lastMatrix = Matrix4.identity();
12801271
_textAlign = TextAlign.start;
12811272
_textBaseline = CanvasTextBaseline.alphabetic;
12821273
_direction = TextDirection.ltr;

webf/lib/src/html/canvas/canvas_painter.dart

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class CanvasPainter extends CustomPainter {
1414

1515
CanvasRenderingContext2D? context;
1616

17-
final Paint _saveLayerPaint = Paint();
1817
final Paint _snapshotPaint = Paint();
1918

2019
// Cache the last paint image.
@@ -49,62 +48,23 @@ class CanvasPainter extends CustomPainter {
4948
}
5049

5150
@override
52-
void paint(Canvas canvas, Size size) async {
53-
if (_hasSnapshot && !_shouldPainting) {
54-
return canvas.drawImage(_snapshot!, Offset.zero, _snapshotPaint);
55-
}
56-
57-
final PictureRecorder pictureRecorder = PictureRecorder();
58-
final Canvas recordCanvas = Canvas(pictureRecorder);
59-
51+
void paint(Canvas canvas, Size size) {
6052
if (_scaleX != 1.0 || _scaleY != 1.0) {
61-
recordCanvas.scale(_scaleX, _scaleY);
62-
}
63-
64-
// This lets you create composite effects, for example making a group of drawing commands semi-transparent.
65-
// Without using saveLayer, each part of the group would be painted individually,
66-
// so where they overlap would be darker than where they do not. By using saveLayer to group them together,
67-
// they can be drawn with an opaque color at first,
68-
// and then the entire group can be made transparent using the saveLayer's paint.
69-
recordCanvas.saveLayer(null, _saveLayerPaint);
70-
71-
// Paint last content
72-
if (_hasSnapshot) {
73-
recordCanvas.drawImage(_snapshot!, Offset.zero, _snapshotPaint);
74-
_disposeSnapshot();
53+
canvas.scale(_scaleX, _scaleY);
7554
}
7655

7756
// Paint new actions
7857
List<CanvasAction>? actions;
7958
if (_shouldPainting) {
80-
actions = context!.performActions(recordCanvas, size);
59+
actions = context!.performActions(canvas, size);
8160
}
8261

83-
// Must pair each call to save()/saveLayer() with a later matching call to restore().
84-
recordCanvas.restore();
85-
86-
// After calling this function, both the picture recorder
87-
// and the canvas objects are invalid and cannot be used further.
88-
final Picture picture = pictureRecorder.endRecording();
89-
canvas.drawPicture(picture);
90-
91-
// Must flat picture to image, or raster will accept a growing command buffer.
92-
await _createSnapshot(picture, size);
93-
94-
// Dispose the used picture.
95-
picture.dispose();
9662
// Clear actions after snapshot was created, or next frame call may empty.
9763
if (actions != null) {
9864
context!.clearActions(actions);
9965
}
10066
}
10167

102-
Future<void> _createSnapshot(Picture picture, Size size) async {
103-
_updatingSnapshot = true;
104-
_snapshot = await picture.toImage(size.width.toInt(), size.height.toInt());
105-
_updatingSnapshot = false;
106-
}
107-
10868
@override
10969
bool shouldRepaint(CanvasPainter oldDelegate) {
11070
if (_shouldRepaint) {

0 commit comments

Comments
 (0)