Added canvas.fillStyle with support for just the "#RRGGBB" style of colors.
Also renamed drawRect to fillRect, which now take x,y,w,h parameters. BUG= R=robertphillips@google.com Author: jcgregorio@google.com Review URL: https://codereview.chromium.org/108813004 git-svn-id: http://skia.googlecode.com/svn/trunk@12621 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
6de4a7836f
commit
3a6143d91f
@ -19,6 +19,7 @@ using namespace v8;
|
|||||||
#include "SkDraw.h"
|
#include "SkDraw.h"
|
||||||
#include "SkGpuDevice.h"
|
#include "SkGpuDevice.h"
|
||||||
#include "SkGraphics.h"
|
#include "SkGraphics.h"
|
||||||
|
#include "SkScalar.h"
|
||||||
|
|
||||||
|
|
||||||
void application_init() {
|
void application_init() {
|
||||||
@ -79,37 +80,83 @@ SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas)
|
|||||||
: INHERITED(hwnd)
|
: INHERITED(hwnd)
|
||||||
, fJsCanvas(canvas)
|
, fJsCanvas(canvas)
|
||||||
{
|
{
|
||||||
fRotationAngle = SkIntToScalar(0);
|
|
||||||
this->setConfig(SkBitmap::kARGB_8888_Config);
|
this->setConfig(SkBitmap::kARGB_8888_Config);
|
||||||
this->setVisibleP(true);
|
this->setVisibleP(true);
|
||||||
this->setClipToBounds(false);
|
this->setClipToBounds(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsCanvas* JsCanvas::unwrap(Handle<Object> obj) {
|
JsCanvas* JsCanvas::Unwrap(Handle<Object> obj) {
|
||||||
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
|
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
|
||||||
void* ptr = field->Value();
|
void* ptr = field->Value();
|
||||||
return static_cast<JsCanvas*>(ptr);
|
return static_cast<JsCanvas*>(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsCanvas::inval(const v8::FunctionCallbackInfo<Value>& args) {
|
void JsCanvas::Inval(const v8::FunctionCallbackInfo<Value>& args) {
|
||||||
unwrap(args.This())->fWindow->inval(NULL);
|
Unwrap(args.This())->fWindow->inval(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsCanvas::drawRect(const v8::FunctionCallbackInfo<Value>& args) {
|
void JsCanvas::FillRect(const v8::FunctionCallbackInfo<Value>& args) {
|
||||||
SkCanvas* canvas = unwrap(args.This())->fCanvas;
|
JsCanvas* jsCanvas = Unwrap(args.This());
|
||||||
|
SkCanvas* canvas = jsCanvas->fCanvas;
|
||||||
|
|
||||||
canvas->drawColor(SK_ColorWHITE);
|
if (args.Length() != 4) {
|
||||||
|
args.GetIsolate()->ThrowException(
|
||||||
|
v8::String::NewFromUtf8(
|
||||||
|
args.GetIsolate(), "Error: 4 arguments required."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO(jcgregorio) Really figure out the conversion from JS numbers to
|
||||||
|
// SkScalars. Maybe test if int first? Not sure of the performance impact.
|
||||||
|
double x = args[0]->NumberValue();
|
||||||
|
double y = args[1]->NumberValue();
|
||||||
|
double w = args[2]->NumberValue();
|
||||||
|
double h = args[3]->NumberValue();
|
||||||
|
|
||||||
// Draw a rectangle with red paint.
|
|
||||||
SkPaint paint;
|
|
||||||
paint.setColor(SK_ColorRED);
|
|
||||||
SkRect rect = {
|
SkRect rect = {
|
||||||
SkIntToScalar(10), SkIntToScalar(10),
|
SkDoubleToScalar(x),
|
||||||
SkIntToScalar(128), SkIntToScalar(128)
|
SkDoubleToScalar(y),
|
||||||
|
SkDoubleToScalar(x) + SkDoubleToScalar(w),
|
||||||
|
SkDoubleToScalar(y) + SkDoubleToScalar(h)
|
||||||
};
|
};
|
||||||
canvas->drawRect(rect, paint);
|
canvas->drawRect(rect, jsCanvas->fFillStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JsCanvas::GetFillStyle(Local<String> name,
|
||||||
|
const PropertyCallbackInfo<Value>& info) {
|
||||||
|
JsCanvas* jsCanvas = Unwrap(info.This());
|
||||||
|
SkColor color = jsCanvas->fFillStyle.getColor();
|
||||||
|
char buf[8];
|
||||||
|
sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
|
||||||
|
SkColorGetB(color));
|
||||||
|
|
||||||
|
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsCanvas::SetFillStyle(Local<String> name, Local<Value> value,
|
||||||
|
const PropertyCallbackInfo<void>& info) {
|
||||||
|
JsCanvas* jsCanvas = Unwrap(info.This());
|
||||||
|
Local<String> s = value->ToString();
|
||||||
|
if (s->Length() != 7) {
|
||||||
|
info.GetIsolate()->ThrowException(
|
||||||
|
v8::String::NewFromUtf8(
|
||||||
|
info.GetIsolate(), "Invalid fill style format."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
char buf[8];
|
||||||
|
s->WriteUtf8(buf, sizeof(buf));
|
||||||
|
|
||||||
|
if (buf[0] != '#') {
|
||||||
|
info.GetIsolate()->ThrowException(
|
||||||
|
v8::String::NewFromUtf8(
|
||||||
|
info.GetIsolate(), "Invalid fill style format."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long color = strtol(buf+1, NULL, 16);
|
||||||
|
jsCanvas->fFillStyle.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate;
|
Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate;
|
||||||
|
|
||||||
Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() {
|
Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() {
|
||||||
@ -121,14 +168,19 @@ Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() {
|
|||||||
result->SetInternalFieldCount(1);
|
result->SetInternalFieldCount(1);
|
||||||
|
|
||||||
// Add accessors for each of the fields of the canvas object.
|
// Add accessors for each of the fields of the canvas object.
|
||||||
|
result->SetAccessor(
|
||||||
|
String::NewFromUtf8(fIsolate, "fillStyle", String::kInternalizedString),
|
||||||
|
GetFillStyle, SetFillStyle);
|
||||||
|
|
||||||
|
// Add methods.
|
||||||
result->Set(
|
result->Set(
|
||||||
String::NewFromUtf8(
|
String::NewFromUtf8(
|
||||||
fIsolate, "drawRect", String::kInternalizedString),
|
fIsolate, "fillRect", String::kInternalizedString),
|
||||||
FunctionTemplate::New(drawRect));
|
FunctionTemplate::New(FillRect));
|
||||||
result->Set(
|
result->Set(
|
||||||
String::NewFromUtf8(
|
String::NewFromUtf8(
|
||||||
fIsolate, "inval", String::kInternalizedString),
|
fIsolate, "inval", String::kInternalizedString),
|
||||||
FunctionTemplate::New(inval));
|
FunctionTemplate::New(Inval));
|
||||||
|
|
||||||
// Return the result through the current handle scope.
|
// Return the result through the current handle scope.
|
||||||
return handleScope.Escape(result);
|
return handleScope.Escape(result);
|
||||||
@ -214,13 +266,7 @@ void JsCanvas::onDraw(SkCanvas* canvas, SkOSWindow* window) {
|
|||||||
void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
|
void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
|
||||||
|
|
||||||
canvas->save();
|
canvas->save();
|
||||||
|
canvas->drawColor(SK_ColorWHITE);
|
||||||
fRotationAngle += SkDoubleToScalar(0.2);
|
|
||||||
if (fRotationAngle > SkDoubleToScalar(360.0)) {
|
|
||||||
fRotationAngle -= SkDoubleToScalar(360.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
canvas->rotate(fRotationAngle);
|
|
||||||
|
|
||||||
// Now jump into JS and call the onDraw(canvas) method defined there.
|
// Now jump into JS and call the onDraw(canvas) method defined there.
|
||||||
fJsCanvas->onDraw(canvas, this);
|
fJsCanvas->onDraw(canvas, this);
|
||||||
@ -310,6 +356,7 @@ bool JsCanvas::initialize(const char script[]) {
|
|||||||
Handle<Value> fn_val = context->Global()->Get(fn_name);
|
Handle<Value> fn_val = context->Global()->Get(fn_name);
|
||||||
|
|
||||||
if (!fn_val->IsFunction()) {
|
if (!fn_val->IsFunction()) {
|
||||||
|
printf("Not a function.\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,10 +379,17 @@ SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
|
|||||||
Isolate* isolate = Isolate::GetCurrent();
|
Isolate* isolate = Isolate::GetCurrent();
|
||||||
|
|
||||||
JsCanvas* jsCanvas = new JsCanvas(isolate);
|
JsCanvas* jsCanvas = new JsCanvas(isolate);
|
||||||
const char* script = "function onDraw(canvas){"
|
const char* script =
|
||||||
"canvas.drawRect();"
|
"var onDraw = function(){ \n"
|
||||||
"canvas.inval();"
|
" var tick = 0; \n"
|
||||||
"};";
|
" function f(canvas) { \n"
|
||||||
|
" tick += 0.001; \n"
|
||||||
|
" canvas.fillStyle = '#00FF00'; \n"
|
||||||
|
" canvas.fillRect(20, 20, 100, Math.abs(Math.cos(tick)*100)); \n"
|
||||||
|
" canvas.inval(); \n"
|
||||||
|
" }; \n"
|
||||||
|
" return f; \n"
|
||||||
|
"}(); \n";
|
||||||
if (!jsCanvas->initialize(script)) {
|
if (!jsCanvas->initialize(script)) {
|
||||||
printf("Failed to initialize.\n");
|
printf("Failed to initialize.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include <v8.h>
|
#include <v8.h>
|
||||||
|
|
||||||
#include "SkWindow.h"
|
#include "SkWindow.h"
|
||||||
|
#include "SkPaint.h"
|
||||||
|
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
@ -32,7 +33,6 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
typedef SkOSWindow INHERITED;
|
typedef SkOSWindow INHERITED;
|
||||||
SkScalar fRotationAngle;
|
|
||||||
JsCanvas* fJsCanvas;
|
JsCanvas* fJsCanvas;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -41,17 +41,19 @@ private:
|
|||||||
// that's used to bridge from C++ to JS. Should be used in JS as:
|
// that's used to bridge from C++ to JS. Should be used in JS as:
|
||||||
//
|
//
|
||||||
// function onDraw(canvas) {
|
// function onDraw(canvas) {
|
||||||
// canvas.drawRect();
|
// canvas.fillStyle="#FF0000";
|
||||||
// canvas.inval();
|
// canvas.fillRect(x, y, w, h);
|
||||||
|
// canvas.inval();
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
class JsCanvas {
|
class JsCanvas {
|
||||||
public:
|
public:
|
||||||
JsCanvas(Isolate* isolate)
|
JsCanvas(Isolate* isolate)
|
||||||
: fIsolate(isolate)
|
: fIsolate(isolate)
|
||||||
, fCanvas(NULL)
|
, fCanvas(NULL)
|
||||||
, fWindow(NULL)
|
, fWindow(NULL)
|
||||||
{}
|
{
|
||||||
|
fFillStyle.setColor(SK_ColorRED);
|
||||||
|
}
|
||||||
~JsCanvas();
|
~JsCanvas();
|
||||||
|
|
||||||
// Parse the script.
|
// Parse the script.
|
||||||
@ -61,14 +63,20 @@ public:
|
|||||||
void onDraw(SkCanvas* canvas, SkOSWindow* window);
|
void onDraw(SkCanvas* canvas, SkOSWindow* window);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Implementation of the canvas.drawRect() JS function.
|
// Implementation of the canvas.fillStyle field.
|
||||||
static void drawRect(const v8::FunctionCallbackInfo<Value>& args);
|
static void GetFillStyle(Local<String> name,
|
||||||
|
const PropertyCallbackInfo<Value>& info);
|
||||||
|
static void SetFillStyle(Local<String> name, Local<Value> value,
|
||||||
|
const PropertyCallbackInfo<void>& info);
|
||||||
|
|
||||||
|
// Implementation of the canvas.fillRect() JS function.
|
||||||
|
static void FillRect(const v8::FunctionCallbackInfo<Value>& args);
|
||||||
|
|
||||||
// Implementation of the canvas.inval() JS function.
|
// Implementation of the canvas.inval() JS function.
|
||||||
static void inval(const v8::FunctionCallbackInfo<Value>& args);
|
static void Inval(const v8::FunctionCallbackInfo<Value>& args);
|
||||||
|
|
||||||
// Get the pointer out of obj.
|
// Get the pointer out of obj.
|
||||||
static JsCanvas* unwrap(Handle<Object> obj);
|
static JsCanvas* Unwrap(Handle<Object> obj);
|
||||||
|
|
||||||
// Create a template for JS object associated with JsCanvas, called lazily
|
// Create a template for JS object associated with JsCanvas, called lazily
|
||||||
// by Wrap() and the results are stored in fCanvasTemplate;
|
// by Wrap() and the results are stored in fCanvasTemplate;
|
||||||
@ -82,6 +90,8 @@ private:
|
|||||||
// Only valid when inside OnDraw().
|
// Only valid when inside OnDraw().
|
||||||
SkCanvas* fCanvas;
|
SkCanvas* fCanvas;
|
||||||
|
|
||||||
|
SkPaint fFillStyle;
|
||||||
|
|
||||||
// Only valid when inside OnDraw().
|
// Only valid when inside OnDraw().
|
||||||
SkOSWindow* fWindow;
|
SkOSWindow* fWindow;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user