Browse Source

Merge branch 'fix-canvas-resize'

v1.x
Tj Holowaychuk 14 years ago
parent
commit
72b97f15f3
  1. 17
      src/Canvas.cc
  2. 2
      src/Canvas.h
  3. 4
      src/CanvasRenderingContext2d.h

17
src/Canvas.cc

@ -6,6 +6,7 @@
// //
#include "Canvas.h" #include "Canvas.h"
#include "CanvasRenderingContext2d.h"
#include "closure.h" #include "closure.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
@ -81,7 +82,7 @@ Canvas::SetWidth(Local<String> prop, Local<Value> val, const AccessorInfo &info)
if (val->IsNumber()) { if (val->IsNumber()) {
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(info.This()); Canvas *canvas = ObjectWrap::Unwrap<Canvas>(info.This());
canvas->width = val->Uint32Value(); canvas->width = val->Uint32Value();
canvas->resurface(); canvas->resurface(info.This());
} }
} }
@ -104,7 +105,7 @@ Canvas::SetHeight(Local<String> prop, Local<Value> val, const AccessorInfo &info
if (val->IsNumber()) { if (val->IsNumber()) {
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(info.This()); Canvas *canvas = ObjectWrap::Unwrap<Canvas>(info.This());
canvas->height = val->Uint32Value(); canvas->height = val->Uint32Value();
canvas->resurface(); canvas->resurface(info.This());
} }
} }
@ -286,9 +287,19 @@ Canvas::~Canvas() {
*/ */
void void
Canvas::resurface() { Canvas::resurface(Handle<Object> canvas) {
HandleScope scope;
// Re-surface
cairo_surface_destroy(_surface); cairo_surface_destroy(_surface);
_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); _surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
// Reset context
Local<Value> context = canvas->Get(String::New("context"));
Context2d *context2d = ObjectWrap::Unwrap<Context2d>(context->ToObject());
cairo_t *prev = context2d->context();
context2d->setContext(cairo_create(surface()));
cairo_destroy(prev);
} }
/* /*

2
src/Canvas.h

@ -67,7 +67,7 @@ class Canvas: public node::ObjectWrap {
inline uint8_t *data(){ return cairo_image_surface_get_data(_surface); } inline uint8_t *data(){ return cairo_image_surface_get_data(_surface); }
inline int stride(){ return cairo_image_surface_get_stride(_surface); } inline int stride(){ return cairo_image_surface_get_stride(_surface); }
Canvas(int width, int height); Canvas(int width, int height);
void resurface(); void resurface(Handle<Object> canvas);
private: private:
~Canvas(); ~Canvas();

4
src/CanvasRenderingContext2d.h

@ -45,6 +45,7 @@ class Context2d: public node::ObjectWrap {
short stateno; short stateno;
canvas_state_t *states[CANVAS_MAX_STATES]; canvas_state_t *states[CANVAS_MAX_STATES];
canvas_state_t *state; canvas_state_t *state;
Context2d(Canvas *canvas);
static Persistent<FunctionTemplate> constructor; static Persistent<FunctionTemplate> constructor;
static void Initialize(Handle<Object> target); static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args); static Handle<Value> New(const Arguments &args);
@ -122,9 +123,6 @@ class Context2d: public node::ObjectWrap {
void save(); void save();
void restore(); void restore();
protected:
Context2d(Canvas *canvas);
private: private:
~Context2d(); ~Context2d();
Canvas *_canvas; Canvas *_canvas;

Loading…
Cancel
Save