Skip to content

Commit

Permalink
Flip and mirror raster when drawing with negative width or height wit…
Browse files Browse the repository at this point in the history
…h the canvas graphics device (#370)

* Flip and mirror raster for negative width/height

* Update NEWS.md
  • Loading branch information
georgestagg authored Feb 28, 2024
1 parent 78c5c6a commit b1ee419
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

* Fixed spelling errors in some JavaScript errors thrown by webR.

* Rasters with negative width or height are now drawn mirrored or flipped when using the canvas graphics device (#350).

* Include `cex` parameters when calculating font size in canvas graphics device (#348).

# webR 0.2.2
Expand Down
15 changes: 13 additions & 2 deletions packages/webr/src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,17 @@ void canvasRaster(unsigned int *raster, int w, int h,
double rot,
Rboolean interpolate,
const pGEcontext gc, pDevDesc dd) {
int scale_x = 1, scale_y = -1;

y += height;
if (height < 0) {
y += height;
height = -height;
scale_y = 1;
}

if (width < 0) {
width = -width;
scale_x = -1;
}

EM_ASM({Module.canvasCtx.save();});
Expand All @@ -554,8 +562,11 @@ void canvasRaster(unsigned int *raster, int w, int h,
const img = new ImageData(new Uint8ClampedArray(raster), $1, $2);
const buffer = new OffscreenCanvas($1, $2);
buffer.getContext('2d').putImageData(img, 0, 0);
Module.canvasCtx.save();
Module.canvasCtx.scale($5, $6);
Module.canvasCtx.drawImage(buffer, 0, 0, $3, $4);
}, raster, w, h, 2*width, 2*height);
Module.canvasCtx.restore();
}, raster, w, h, 2*width, 2*height, scale_x, scale_y);

EM_ASM({Module.canvasCtx.restore();});
}
Expand Down

0 comments on commit b1ee419

Please sign in to comment.