haikuwebkit/LayoutTests/svg/as-image/img-with-svg-resource-in-do...

35 lines
1.2 KiB
HTML
Raw Permalink Normal View History

Drawing an SVG image into a <canvas> that is not in the DOM draws the wrong region https://bugs.webkit.org/show_bug.cgi?id=159276 Patch by Antoine Quint <graouts@apple.com> on 2016-06-30 Reviewed by Dean Jackson. Source/WebCore: In the event where the <img> element that we are passing to CanvasRenderingContext2D.drawImage() points to an SVG resource, we ensure that the container for the SVG image is sized to match the HTML element. The necessity for setting this container size, explained in webkit.org/b/148845, is that we must ensure a cached image does not have an outdated container size. Tests: svg/as-image/img-with-svg-resource-in-dom-and-drawImage.html svg/as-image/img-with-svg-resource-in-dom-no-size-and-drawImage.html svg/as-image/img-with-svg-resource-not-in-dom-and-drawImage.html svg/as-image/img-with-svg-resource-not-in-dom-no-size-and-drawImage.html * html/canvas/CanvasRenderingContext2D.cpp: (WebCore::CanvasRenderingContext2D::drawImage): LayoutTests: Adding a series of new tests to check we correctly respect mismatching source and destination rectangles with SVG images as sources, both with the source <img> element being present and absent from the DOM, and explicit sizes being set or not set. * svg/as-image/img-with-svg-resource-in-dom-and-drawImage-expected.html: Added. * svg/as-image/img-with-svg-resource-in-dom-and-drawImage.html: Added. * svg/as-image/img-with-svg-resource-in-dom-no-size-and-drawImage-expected.html: Added. * svg/as-image/img-with-svg-resource-in-dom-no-size-and-drawImage.html: Added. * svg/as-image/img-with-svg-resource-not-in-dom-and-drawImage-expected.html: Added. * svg/as-image/img-with-svg-resource-not-in-dom-and-drawImage.html: Added. * svg/as-image/img-with-svg-resource-not-in-dom-no-size-and-drawImage-expected.html: Added. * svg/as-image/img-with-svg-resource-not-in-dom-no-size-and-drawImage.html: Added. Canonical link: https://commits.webkit.org/177446@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@202712 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2016-06-30 22:18:12 +00:00
<body>
<p>This test shows a <code>&lt;canvas></code> element in which we draw the SVG image with the source rectangle set to fit the green rectangle from the SVG image and the destination rectangle as the bounds of the canvas element. In this example, the source <code>&lt;img></code> is in the DOM and is not explicitly sized.</p>
<script type="text/javascript">
if (window.testRunner)
testRunner.waitUntilDone();
var width = 200;
var height = 200;
var svgData = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><rect width="200" height="200" fill="red" /><rect x="20" y="20" width="40" height="40" fill="green" /></svg>';
var canvas = document.body.appendChild(document.createElement("canvas"));
canvas.width = width;
canvas.height = height;
var image = document.body.appendChild(new Image());
image.src = svgData;
image.style.width = `${width}px`;
image.style.height = `${height}px`;
image.style.visibility = "hidden";
if (image.complete)
draw();
else
image.addEventListener("load", draw);
function draw() {
canvas.getContext("2d").drawImage(image, 20, 20, 40, 40, 0, 0, width, height);
if (window.testRunner)
testRunner.notifyDone();
}
</script>
</body>