Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Change cloneImageElement() to return clone every time #37811

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/web_ui/lib/src/engine/html_image_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class HtmlImage implements ui.Image {
}

final DomHTMLImageElement imgElement;
bool _requiresClone = false;
bool _didClone = false;

bool _disposed = false;
@override
Expand Down Expand Up @@ -204,16 +204,12 @@ class HtmlImage implements ui.Image {
}
}

// Returns absolutely positioned actual image element on first call and
// clones on subsequent calls.
DomHTMLImageElement cloneImageElement() {
if (_requiresClone) {
return imgElement.cloneNode(true) as DomHTMLImageElement;
} else {
_requiresClone = true;
if (!_didClone) {
_didClone = true;
imgElement.style.position = 'absolute';
return imgElement;
}
return imgElement.cloneNode(true) as DomHTMLImageElement;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ Future<void> testMain() async {
rc.restore();
await canvasScreenshot(rc, 'canvas_image_blend_and_text');
});

test('Does not re-use styles with same image src', () async {
final RecordingCanvas rc = RecordingCanvas(
const Rect.fromLTRB(0, 0, 400, 400));
final HtmlImage flutterImage = createFlutterLogoTestImage();
rc.save();
rc.drawRect(const Rect.fromLTWH(0, 50, 200, 50), makePaint()
..color = white);
rc.drawImage(flutterImage, const Offset(0, 50),
makePaint()
..colorFilter = const EngineColorFilter.mode(red, BlendMode.modulate));

// Expect that the colorFilter is only applied to the first image, since the
// colorFilter is applied to a clone of the flutterImage and not the original
rc.drawImage(flutterImage, const Offset(0, 100), makePaint());

rc.restore();
await canvasScreenshot(rc, 'canvas_image_same_src');
});
}

Paragraph createTestParagraph() {
Expand Down