Browse Source

drawImage() working

v1.x
Tj Holowaychuk 14 years ago
parent
commit
82a3d9cc36
  1. 32
      src/CanvasRenderingContext2d.cc

32
src/CanvasRenderingContext2d.cc

@ -406,10 +406,10 @@ Context2d::DrawImage(const Arguments &args) {
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This()); Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
cairo_t *ctx = context->context(); cairo_t *ctx = context->context();
int height = img->height int sx = 0
, width = img->width; , sy = 0
, sw = img->width
int sx, sy, sw, sh , sh = img->height
, dx, dy, dw, dh; , dx, dy, dw, dh;
// Arguments // Arguments
@ -436,8 +436,8 @@ Context2d::DrawImage(const Arguments &args) {
case 3: case 3:
dx = args[1]->NumberValue(); dx = args[1]->NumberValue();
dy = args[2]->NumberValue(); dy = args[2]->NumberValue();
dw = width; dw = img->width;
dh = height; dh = img->height;
break; break;
default: default:
// TODO: throw // TODO: throw
@ -446,16 +446,28 @@ Context2d::DrawImage(const Arguments &args) {
// Draw // Draw
cairo_save(ctx); cairo_save(ctx);
if (dw != width || dh != height) {
// Source surface
cairo_surface_t *src = cairo_surface_create_for_rectangle(
img->surface()
, sx
, sy
, sw
, sh);
// Scale src
if (dw != sw || dh != sh) {
cairo_scale(ctx cairo_scale(ctx
, (float) dw / width , (float) dw / sw
, (float) dh / height , (float) dh / sh
); );
} }
// TODO: globalAlpha // TODO: globalAlpha
cairo_set_source_surface(ctx, img->surface(), dx, dy); cairo_set_source_surface(ctx, src, dx, dy);
cairo_paint(ctx); cairo_paint(ctx);
cairo_restore(ctx); cairo_restore(ctx);
cairo_surface_destroy(src);
return Undefined(); return Undefined();
} }

Loading…
Cancel
Save