Browse Source

Added Context2d#antialias=

v1.x
Tj Holowaychuk 14 years ago
parent
commit
12e26814bb
  1. 2
      Makefile
  2. 9
      Readme.md
  3. 44
      src/CanvasRenderingContext2d.cc
  4. 2
      src/CanvasRenderingContext2d.h
  5. 17
      test/canvas.test.js
  6. 2
      test/public/tests.js

2
Makefile

@ -3,7 +3,7 @@
build/default/canvas.node: src/*.cc
node-waf configure build
test:
test: build/default/canvas.node
@./support/expresso/bin/expresso \
-I lib \
test/*.test.js

9
Readme.md

@ -103,6 +103,15 @@ or specify the mime type:
- hsl-color
- hsl-luminosity
## Anti-Aliasing
Set anti-aliasing mode:
- default
- none
- gray
- subpixel
## Benchmarks
Although node-canvas is extremely new, and we have not even begun optimization yet it is already quite fast. For benchmarks vs other node canvas implementations view this [gist](https://gist.github.com/664922), or update the submodules and run `$ make benchmark` yourself.

44
src/CanvasRenderingContext2d.cc

@ -108,6 +108,7 @@ Context2d::Initialize(Handle<Object> target) {
proto->SetAccessor(String::NewSymbol("shadowOffsetX"), GetShadowOffsetX, SetShadowOffsetX);
proto->SetAccessor(String::NewSymbol("shadowOffsetY"), GetShadowOffsetY, SetShadowOffsetY);
proto->SetAccessor(String::NewSymbol("shadowBlur"), GetShadowBlur, SetShadowBlur);
proto->SetAccessor(String::NewSymbol("antialias"), GetAntiAlias, SetAntiAlias);
target->Set(String::NewSymbol("CanvasRenderingContext2d"), t->GetFunction());
}
@ -573,6 +574,49 @@ Context2d::SetShadowBlur(Local<String> prop, Local<Value> val, const AccessorInf
}
}
/*
* Get current antialiasing setting.
*/
Handle<Value>
Context2d::GetAntiAlias(Local<String> prop, const AccessorInfo &info) {
Context2d *context = ObjectWrap::Unwrap<Context2d>(info.This());
switch (cairo_get_antialias(context->getContext())) {
case CAIRO_ANTIALIAS_NONE:
return String::NewSymbol("none");
case CAIRO_ANTIALIAS_GRAY:
return String::NewSymbol("gray");
case CAIRO_ANTIALIAS_SUBPIXEL:
return String::NewSymbol("subpixel");
default:
return String::NewSymbol("default");
}
}
/*
* Set antialiasing.
*/
void
Context2d::SetAntiAlias(Local<String> prop, Local<Value> val, const AccessorInfo &info) {
String::AsciiValue str(val->ToString());
Context2d *context = ObjectWrap::Unwrap<Context2d>(info.This());
cairo_t *ctx = context->getContext();
cairo_antialias_t a;
if (0 == strcmp("none", *str)) {
a = CAIRO_ANTIALIAS_NONE;
} else if (0 == strcmp("default", *str)) {
a = CAIRO_ANTIALIAS_DEFAULT;
} else if (0 == strcmp("gray", *str)) {
a = CAIRO_ANTIALIAS_GRAY;
} else if (0 == strcmp("subpixel", *str)) {
a = CAIRO_ANTIALIAS_SUBPIXEL;
} else {
a = cairo_get_antialias(ctx);
}
cairo_set_antialias(ctx, a);
}
/*
* Get miter limit.
*/

2
src/CanvasRenderingContext2d.h

@ -89,6 +89,7 @@ class Context2d: public node::ObjectWrap {
static Handle<Value> GetShadowOffsetX(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetShadowOffsetY(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetShadowBlur(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetAntiAlias(Local<String> prop, const AccessorInfo &info);
static void SetGlobalCompositeOperation(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetGlobalAlpha(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetMiterLimit(Local<String> prop, Local<Value> val, const AccessorInfo &info);
@ -98,6 +99,7 @@ class Context2d: public node::ObjectWrap {
static void SetShadowOffsetX(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetShadowOffsetY(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetShadowBlur(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetAntiAlias(Local<String> prop, Local<Value> val, const AccessorInfo &info);
inline cairo_t *getContext(){ return _context; }
inline Canvas *getCanvas(){ return _canvas; }
inline bool hasShadow();

17
test/canvas.test.js

@ -174,6 +174,23 @@ module.exports = {
assert.equal(10, ctx.lineWidth);
},
'test Context2d#antiAlias=': function(assert){
var canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d');
assert.equal('default', ctx.antialias);
ctx.antialias = 'none';
assert.equal('none', ctx.antialias);
ctx.antialias = 'gray';
assert.equal('gray', ctx.antialias);
ctx.antialias = 'subpixel';
assert.equal('subpixel', ctx.antialias);
ctx.antialias = 'invalid';
assert.equal('subpixel', ctx.antialias);
ctx.antialias = 1;
assert.equal('subpixel', ctx.antialias);
},
'test Context2d#lineCap=': function(assert){
var canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d');

2
test/public/tests.js

@ -1357,4 +1357,4 @@ tests['shadow integration'] = function(ctx){
ctx.arc(50, 151, 50, 0, Math.PI*2, false);
ctx.fillStyle = "gold";
ctx.fill();
};
};
Loading…
Cancel
Save