Browse Source

Added `Canvas#type` getter

v1.x
TJ Holowaychuk 13 years ago
parent
commit
03d1cacf9c
  1. 12
      src/Canvas.cc
  2. 1
      src/Canvas.h
  3. 11
      test/canvas.test.js

12
src/Canvas.cc

@ -41,6 +41,7 @@ Canvas::Initialize(Handle<Object> target) {
#ifdef HAVE_JPEG
NODE_SET_PROTOTYPE_METHOD(constructor, "streamJPEGSync", StreamJPEGSync);
#endif
proto->SetAccessor(String::NewSymbol("type"), GetType);
proto->SetAccessor(String::NewSymbol("width"), GetWidth, SetWidth);
proto->SetAccessor(String::NewSymbol("height"), GetHeight, SetHeight);
target->Set(String::NewSymbol("Canvas"), constructor->GetFunction());
@ -65,6 +66,17 @@ Canvas::New(const Arguments &args) {
return args.This();
}
/*
* Get type string.
*/
Handle<Value>
Canvas::GetType(Local<String> prop, const AccessorInfo &info) {
HandleScope scope;
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(info.This());
return scope.Close(String::New(canvas->isPDF() ? "pdf" : "image"));
}
/*
* Get width.
*/

1
src/Canvas.h

@ -48,6 +48,7 @@ class Canvas: public node::ObjectWrap {
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
static Handle<Value> ToBuffer(const Arguments &args);
static Handle<Value> GetType(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetWidth(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetHeight(Local<String> prop, const AccessorInfo &info);
static void SetWidth(Local<String> prop, Local<Value> val, const AccessorInfo &info);

11
test/canvas.test.js

@ -157,7 +157,16 @@ module.exports = {
ctx.fillStyle = 'rgba(0, 0, 0, 42.42)';
assert.equal('#000000', ctx.fillStyle);
},
'test Canvas#type': function(){
var canvas = new Canvas(10, 10);
assert('image' == canvas.type);
var canvas = new Canvas(10, 10, 'pdf');
assert('pdf' == canvas.type);
var canvas = new Canvas(10, 10, 'hey');
assert('image' == canvas.type);
},
'test Canvas#getContext("2d")': function(){
var canvas = new Canvas(200, 300)
, ctx = canvas.getContext('2d');

Loading…
Cancel
Save