From 230611991cf21a844e461178ddce133fa39f098f Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Thu, 30 Sep 2010 11:02:15 -0700 Subject: [PATCH 1/4] Added globalAlpha getter/setter --- src/context2d.cc | 22 ++++++++++++++++++++++ src/context2d.h | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/context2d.cc b/src/context2d.cc index 52ec7b4..f82e9b6 100644 --- a/src/context2d.cc +++ b/src/context2d.cc @@ -100,6 +100,7 @@ Context2d::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "arc", Arc); NODE_SET_PROTOTYPE_METHOD(t, "setFillRGBA", SetFillRGBA); NODE_SET_PROTOTYPE_METHOD(t, "setStrokeRGBA", SetStrokeRGBA); + proto->SetAccessor(String::NewSymbol("globalAlpha"), GetGlobalAlpha, SetGlobalAlpha); proto->SetAccessor(String::NewSymbol("miterLimit"), GetMiterLimit, SetMiterLimit); proto->SetAccessor(String::NewSymbol("lineWidth"), GetLineWidth, SetLineWidth); proto->SetAccessor(String::NewSymbol("lineCap"), GetLineCap, SetLineCap); @@ -128,6 +129,7 @@ Context2d::Context2d(Canvas *canvas): ObjectWrap() { _canvas = canvas; _context = cairo_create(canvas->getSurface()); cairo_set_line_width(_context, 1); + globalAlpha = NULL; RGBA(fill,0,0,0,1); RGBA(stroke,0,0,0,1); } @@ -140,6 +142,26 @@ Context2d::~Context2d() { cairo_destroy(_context); } +/* + * Get global alpha. + */ + +Handle +Context2d::GetGlobalAlpha(Local prop, const AccessorInfo &info) { + Context2d *context = ObjectWrap::Unwrap(info.This()); + return Number::New(context->globalAlpha); +} + +/* + * Set global alpha. + */ + +void +Context2d::SetGlobalAlpha(Local prop, Local val, const AccessorInfo &info) { + Context2d *context = ObjectWrap::Unwrap(info.This()); + context->globalAlpha = val->NumberValue(); +} + /* * Get miter limit. */ diff --git a/src/context2d.h b/src/context2d.h index 7249a8f..b9c3695 100644 --- a/src/context2d.h +++ b/src/context2d.h @@ -18,6 +18,7 @@ class Context2d: public node::ObjectWrap { public: rgba_t fill; rgba_t stroke; + float globalAlpha; static void Initialize(Handle target); static Handle New(const Arguments &args); static Handle Save(const Arguments &args); @@ -40,10 +41,12 @@ class Context2d: public node::ObjectWrap { static Handle ClearRect(const Arguments &args); static Handle Rect(const Arguments &args); static Handle Arc(const Arguments &args); + static Handle GetGlobalAlpha(Local prop, const AccessorInfo &info); static Handle GetMiterLimit(Local prop, const AccessorInfo &info); static Handle GetLineCap(Local prop, const AccessorInfo &info); static Handle GetLineJoin(Local prop, const AccessorInfo &info); static Handle GetLineWidth(Local prop, const AccessorInfo &info); + static void SetGlobalAlpha(Local prop, Local val, const AccessorInfo &info); static void SetMiterLimit(Local prop, Local val, const AccessorInfo &info); static void SetLineCap(Local prop, Local val, const AccessorInfo &info); static void SetLineJoin(Local prop, Local val, const AccessorInfo &info); From e0bed743abe142e4b610f1476e3dfe46c1dfd903 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Thu, 30 Sep 2010 11:05:06 -0700 Subject: [PATCH 2/4] Implemented globalAlpha --- src/context2d.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/context2d.cc b/src/context2d.cc index f82e9b6..e2a2704 100644 --- a/src/context2d.cc +++ b/src/context2d.cc @@ -32,7 +32,7 @@ using namespace node; , C.r \ , C.g \ , C.b \ - , C.a); + , context->globalAlpha == -1 ? C.a : context->globalAlpha); /* * Rectangle arg assertions. @@ -129,7 +129,7 @@ Context2d::Context2d(Canvas *canvas): ObjectWrap() { _canvas = canvas; _context = cairo_create(canvas->getSurface()); cairo_set_line_width(_context, 1); - globalAlpha = NULL; + globalAlpha = -1; RGBA(fill,0,0,0,1); RGBA(stroke,0,0,0,1); } From 489700330f87d96136d20c6d755544078d2e0799 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Thu, 30 Sep 2010 11:06:16 -0700 Subject: [PATCH 3/4] Added globalAlpha test --- test/canvas.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/canvas.test.js b/test/canvas.test.js index a203a00..52266d4 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -339,5 +339,25 @@ module.exports = { , path , 'fc8bbf2cf6ae2d85fcf526103200e844' , 'Context2d#save() / resetore() failed'); + }, + + 'test Context2d#globalAlpha=': function(assert){ + var canvas = new Canvas(200, 200) + , ctx = canvas.getContext('2d') + , path = __dirname + '/images/globalAlpha.png'; + + ctx.fillRect(0,0,50,50); + ctx.translate(15,15); + ctx.globalAlpha = 0.6; + ctx.fillStyle = 'red'; + ctx.fillRect(0,0,20,20); + ctx.fillStyle = 'yellow'; + ctx.fillRect(0,0,10,10); + + assertChecksum( + canvas + , path + , 'd8365233f2beb420ba18ff78dc6d7405' + , 'Context2d#globalAlpha= failed'); } } \ No newline at end of file From b8528640be4180a35bdf8d3003039a41548f43a6 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Thu, 30 Sep 2010 11:08:23 -0700 Subject: [PATCH 4/4] Added globalAlpha getter assertion --- test/canvas.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/canvas.test.js b/test/canvas.test.js index 52266d4..00cf731 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -349,6 +349,7 @@ module.exports = { ctx.fillRect(0,0,50,50); ctx.translate(15,15); ctx.globalAlpha = 0.6; + assert.equal(0.6, ctx.globalAlpha.toFixed(1)); ctx.fillStyle = 'red'; ctx.fillRect(0,0,20,20); ctx.fillStyle = 'yellow';