Tj Holowaychuk
14 years ago
10 changed files with 251 additions and 45 deletions
@ -0,0 +1,29 @@ |
|||
|
|||
/** |
|||
* Module dependencies. |
|||
*/ |
|||
|
|||
var Canvas = require('../lib/canvas') |
|||
, canvas = new Canvas(320, 320) |
|||
, ctx = canvas.getContext('2d'); |
|||
|
|||
// Create gradients
|
|||
var lingrad = ctx.createLinearGradient(0,0,0,150); |
|||
lingrad.addColorStop(0, '#00ABEB'); |
|||
lingrad.addColorStop(0.5, '#fff'); |
|||
lingrad.addColorStop(0.5, '#26C000'); |
|||
lingrad.addColorStop(1, '#fff'); |
|||
|
|||
var lingrad2 = ctx.createLinearGradient(0,50,0,95); |
|||
lingrad2.addColorStop(0.5, '#000'); |
|||
lingrad2.addColorStop(1, 'rgba(0,0,0,0)'); |
|||
|
|||
// assign gradients to fill and stroke styles
|
|||
ctx.fillStyle = lingrad; |
|||
ctx.strokeStyle = lingrad2; |
|||
|
|||
// draw shapes
|
|||
ctx.fillRect(10,10,130,130); |
|||
ctx.strokeRect(50,50,50,50); |
|||
|
|||
canvas.savePNG(__dirname + '/gradients.png'); |
@ -0,0 +1,78 @@ |
|||
|
|||
//
|
|||
// gradient.cc
|
|||
//
|
|||
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|||
//
|
|||
|
|||
#include "canvas.h" |
|||
#include "gradient.h" |
|||
|
|||
void |
|||
Gradient::Initialize(Handle<Object> target) { |
|||
HandleScope scope; |
|||
Local<FunctionTemplate> t = FunctionTemplate::New(Gradient::New); |
|||
t->InstanceTemplate()->SetInternalFieldCount(1); |
|||
t->SetClassName(String::NewSymbol("CanvasGradient")); |
|||
|
|||
NODE_SET_PROTOTYPE_METHOD(t, "addColorStopRGBA", AddColorStopRGBA); |
|||
target->Set(String::NewSymbol("CanvasGradient"), t->GetFunction()); |
|||
} |
|||
|
|||
Handle<Value> |
|||
Gradient::New(const Arguments &args) { |
|||
HandleScope scope; |
|||
|
|||
// Linear
|
|||
if (4 == args.Length()) { |
|||
Gradient *grad = new Gradient( |
|||
args[0]->NumberValue() |
|||
, args[1]->NumberValue() |
|||
, args[2]->NumberValue() |
|||
, args[3]->NumberValue()); |
|||
grad->Wrap(args.This()); |
|||
return args.This(); |
|||
} |
|||
|
|||
// Radial
|
|||
if (6 == args.Length()) { |
|||
Gradient *grad = new Gradient( |
|||
args[0]->NumberValue() |
|||
, args[1]->NumberValue() |
|||
, args[2]->NumberValue() |
|||
, args[3]->NumberValue() |
|||
, args[4]->NumberValue() |
|||
, args[5]->NumberValue()); |
|||
grad->Wrap(args.This()); |
|||
return args.This(); |
|||
} |
|||
|
|||
return ThrowException(Exception::TypeError(String::New("invalid arguments"))); |
|||
} |
|||
|
|||
Handle<Value> |
|||
Gradient::AddColorStopRGBA(const Arguments &args) { |
|||
HandleScope scope; |
|||
if (!args[0]->IsNumber()) \ |
|||
return ThrowException(Exception::TypeError(String::New("offset required"))); \ |
|||
RGBA_ARGS(1); |
|||
Gradient *grad = ObjectWrap::Unwrap<Gradient>(args.This()); |
|||
cairo_pattern_add_color_stop_rgba( |
|||
grad->getPattern() |
|||
, args[0]->NumberValue() |
|||
, r / 255 * 1 |
|||
, g / 255 * 1 |
|||
, b / 255 * 1 |
|||
, a); |
|||
return Undefined(); |
|||
} |
|||
|
|||
Gradient::Gradient(double x0, double y0, double x1, double y1): |
|||
_x0(x0), _y0(y0), _x1(x1), _y1(y1) { |
|||
_pattern = cairo_pattern_create_linear(x0, y0, x1, y1); |
|||
} |
|||
|
|||
Gradient::Gradient(double x0, double y0, double r0, double x1, double y1, double r1): |
|||
_x0(x0), _y0(y0), _x1(x1), _y1(y1), _r0(r0), _r1(r1) { |
|||
_pattern = cairo_pattern_create_radial(x0, y0, r0, x1, y1, r1); |
|||
} |
@ -0,0 +1,29 @@ |
|||
|
|||
//
|
|||
// gradient.h
|
|||
//
|
|||
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|||
//
|
|||
|
|||
#ifndef __NODE_GRADIENT_H__ |
|||
#define __NODE_GRADIENT_H__ |
|||
|
|||
#include "canvas.h" |
|||
|
|||
using namespace v8; |
|||
|
|||
class Gradient: public node::ObjectWrap { |
|||
public: |
|||
static void Initialize(Handle<Object> target); |
|||
static Handle<Value> New(const Arguments &args); |
|||
static Handle<Value> AddColorStopRGBA(const Arguments &args); |
|||
Gradient(double x0, double y0, double x1, double y1); |
|||
Gradient(double x0, double y0, double r0, double x1, double y1, double r1); |
|||
inline cairo_pattern_t *getPattern(){ return _pattern; } |
|||
|
|||
private: |
|||
double _x0, _y0, _x1, _y1, _r0, _r1; |
|||
cairo_pattern_t *_pattern; |
|||
}; |
|||
|
|||
#endif |
Loading…
Reference in new issue