Skip to content
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
29 changes: 21 additions & 8 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,8 @@ NAN_METHOD(Context2d::DrawImage) {
, dy = 0
, dw = 0
, dh = 0
, fw = 0
, fh = 0;
, source_w = 0
, source_h = 0;

cairo_surface_t *surface;

Expand All @@ -1125,15 +1125,15 @@ NAN_METHOD(Context2d::DrawImage) {
if (!img->isComplete()) {
return Nan::ThrowError("Image given has not completed loading");
}
fw = sw = img->width;
fh = sh = img->height;
source_w = sw = img->width;
source_h = sh = img->height;
surface = img->surface();

// Canvas
} else if (Nan::New(Canvas::constructor)->HasInstance(obj)) {
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(obj);
fw = sw = canvas->getWidth();
fh = sh = canvas->getHeight();
source_w = sw = canvas->getWidth();
source_h = sh = canvas->getHeight();
surface = canvas->surface();

// Invalid
Expand Down Expand Up @@ -1183,17 +1183,30 @@ NAN_METHOD(Context2d::DrawImage) {
float fx = (float) dw / sw;
float fy = (float) dh / sh;
bool needScale = dw != sw || dh != sh;
bool needCut = sw != fw || sh != fh;
bool needCut = sw != source_w || sh != source_h || sx < 0 || sy < 0;
bool needCairoClip = sx < 0 || sy < 0 || sw > source_w || sh > source_h;

bool sameCanvas = surface == context->canvas()->surface();
bool needsExtraSurface = sameCanvas || needCut || needScale;
bool needsExtraSurface = sameCanvas || needCut || needScale || needCairoClip;
cairo_surface_t *surfTemp = NULL;
cairo_t *ctxTemp = NULL;

if (needsExtraSurface) {
surfTemp = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, dw, dh);
ctxTemp = cairo_create(surfTemp);
cairo_scale(ctxTemp, fx, fy);
if (needCairoClip) {
float clip_w = (std::min)(sw, source_w);
float clip_h = (std::min)(sh, source_h);
if (sx > 0) {
clip_w -= sx;
}
if (sy > 0) {
clip_h -= sy;
}
cairo_rectangle(ctxTemp, -sx , -sy , clip_w, clip_h);
cairo_clip(ctxTemp);
}
cairo_set_source_surface(ctxTemp, surface, -sx, -sy);
cairo_pattern_set_filter(cairo_get_source(ctxTemp), context->state->imageSmoothingEnabled ? context->state->patternQuality : CAIRO_FILTER_NEAREST);
cairo_pattern_set_extend(cairo_get_source(ctxTemp), CAIRO_EXTEND_REFLECT);
Expand Down
37 changes: 37 additions & 0 deletions test/public/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ gco.forEach(op => {
var img2 = new Image()
img1.onload = function () {
img2.onload = function () {
ctx.globalAlpha = 0.7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will surface better the properties of compositing when images overlap.

ctx.drawImage(img1, 0, 0)
ctx.globalCompositeOperation = op
ctx.drawImage(img2, 0, 0)
Expand All @@ -1210,6 +1211,42 @@ gco.forEach(op => {
}
})

gco.forEach(op => {
tests['9 args, transform, globalCompositeOperator ' + op] = function (ctx, done) {
var img1 = new Image()
var img2 = new Image()
img1.onload = function () {
img2.onload = function () {
ctx.globalAlpha = 0.7
ctx.drawImage(img1, 0, 0)
ctx.globalCompositeOperation = op
ctx.rotate(0.1)
ctx.scale(0.8, 1.2)
ctx.translate(5, -5)
ctx.drawImage(img2, -80, -50, 400, 400, 10, 10, 180, 180)
done()
}
img2.src = imageSrc('newcontent.png')
}
img1.src = imageSrc('existing.png')
}
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy of the test where the color effect of the global composite operation is less visible, but the complication of using the drawImage with free 9 args in a transformed context are better visible.


tests['drawImage issue #1249'] = function (ctx, done) {
var img1 = new Image()
var img2 = new Image()
img1.onload = function () {
img2.onload = function () {
ctx.drawImage(img1, 0, 0, 200, 200)
ctx.drawImage(img2, -8, -8, 18, 18, 0, 0, 200, 200)
ctx.restore()
done()
}
img2.src = imageSrc('checkers.png')
}
img1.src = imageSrc('chrome.jpg')
}

tests['known bug #416'] = function (ctx, done) {
var img1 = new Image()
var img2 = new Image()
Expand Down