diff --git a/src/canvas.cc b/src/canvas.cc index 5cbcd9c..844ec23 100644 --- a/src/canvas.cc +++ b/src/canvas.cc @@ -43,15 +43,13 @@ Canvas::Initialize(Handle target) { Handle Canvas::New(const Arguments &args) { HandleScope scope; + int width = 0 + , height = 0; - if (!args[0]->IsNumber()) - return ThrowException(Exception::TypeError(String::New("width required"))); - if (!args[1]->IsNumber()) - return ThrowException(Exception::TypeError(String::New("height required"))); + if (args[0]->IsNumber()) width = args[0]->Uint32Value(); + if (args[1]->IsNumber()) height = args[1]->Uint32Value(); - Canvas *canvas = new Canvas(args[0]->Int32Value(), args[1]->Int32Value()); - args.This()->Set(String::NewSymbol("width"), args[0]); - args.This()->Set(String::NewSymbol("height"), args[1]); + Canvas *canvas = new Canvas(width, height); canvas->Wrap(args.This()); return args.This(); } @@ -104,8 +102,10 @@ Canvas::StreamPNGSync(const Arguments &args) { * Initialize cairo surface. */ -Canvas::Canvas(int width, int height): ObjectWrap() { - _surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); +Canvas::Canvas(int w, int h): ObjectWrap() { + width = w; + height = h; + _surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h); } /* @@ -116,6 +116,16 @@ Canvas::~Canvas() { cairo_surface_destroy(_surface); } +/* + * Re-alloc the surface, destroying the previous. + */ + +void +Canvas::resetSurface() { + cairo_surface_destroy(_surface); + _surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); +} + /* * Construct an Error from the given cairo status. */ diff --git a/src/canvas.h b/src/canvas.h index c8617bb..30dc890 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -48,8 +48,14 @@ using namespace v8; class Canvas: public node::ObjectWrap { public: + int width; + int height; static void Initialize(Handle target); static Handle New(const Arguments &args); + static Handle SetHeight(const Arguments &args); + static Handle GetHeight(const Arguments &args); + static Handle SetWidth(const Arguments &args); + static Handle GetWidth(const Arguments &args); static Handle SavePNG(const Arguments &args); static Handle StreamPNGSync(const Arguments &args); static Handle Error(cairo_status_t status); @@ -59,6 +65,7 @@ class Canvas: public node::ObjectWrap { private: ~Canvas(); + void resetSurface(); cairo_surface_t *_surface; };