Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Unified Diff: experimental/SkV8Example/SkV8Example.cpp

Issue 108813004: Added canvas.fillStyle with support for just the "#RRGGBB" style of colors. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix more 80 cols Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: experimental/SkV8Example/SkV8Example.cpp
diff --git a/experimental/SkV8Example/SkV8Example.cpp b/experimental/SkV8Example/SkV8Example.cpp
index 3c7a5f36425dc7a69f71dcfe703fc91a98a6de92..4134e2435fddc67a7b91120b889df905adf5528b 100644
--- a/experimental/SkV8Example/SkV8Example.cpp
+++ b/experimental/SkV8Example/SkV8Example.cpp
@@ -19,6 +19,7 @@ using namespace v8;
#include "SkDraw.h"
#include "SkGpuDevice.h"
#include "SkGraphics.h"
+#include "SkScalar.h"
void application_init() {
@@ -79,7 +80,6 @@ SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas)
: INHERITED(hwnd)
, fJsCanvas(canvas)
{
- fRotationAngle = SkIntToScalar(0);
this->setConfig(SkBitmap::kARGB_8888_Config);
this->setVisibleP(true);
this->setClipToBounds(false);
@@ -95,21 +95,62 @@ void JsCanvas::inval(const v8::FunctionCallbackInfo<Value>& args) {
unwrap(args.This())->fWindow->inval(NULL);
}
-void JsCanvas::drawRect(const v8::FunctionCallbackInfo<Value>& args) {
- SkCanvas* canvas = unwrap(args.This())->fCanvas;
+void JsCanvas::fillRect(const v8::FunctionCallbackInfo<Value>& args) {
+ 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.
robertphillips 2013/12/11 16:45:46 We shouldn't worry about fixed point.
jcgregorio 2013/12/11 17:23:47 Done.
+ // Also conversion could depend on SkScalar being float or fixed-point.
+ // If fixed-point, check for overflow.
+ 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 = {
- SkIntToScalar(10), SkIntToScalar(10),
- SkIntToScalar(128), SkIntToScalar(128)
+ SkDoubleToScalar(x),
+ 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,
robertphillips 2013/12/11 16:45:46 line this up with preceding line?
jcgregorio 2013/12/11 17:23:47 Done.
+ const PropertyCallbackInfo<Value>& info) {
+ JsCanvas* jsCanvas = unwrap(info.This());
+ SkColor color = jsCanvas->fFillStyle.getColor();
+ char buf[8];
robertphillips 2013/12/11 16:45:46 _snprintf?
jcgregorio 2013/12/11 17:23:47 Not done per our conversation.
+ sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
+ SkColorGetB(color));
+
+ info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
+}
+
robertphillips 2013/12/11 16:45:46 rm space in "( L")?
jcgregorio 2013/12/11 17:23:47 Done.
+void JsCanvas::setFillStyle( Local<String> name, Local<Value> value,
robertphillips 2013/12/11 16:45:46 line this up with preceding line?
jcgregorio 2013/12/11 17:23:47 Done.
+ const PropertyCallbackInfo<void>& info) {
+ JsCanvas* jsCanvas = unwrap(info.This());
+ Local<String> s = value->ToString();
+ if (s->Length() != 7) {
+ info.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
robertphillips 2013/12/11 16:45:46 "Invalid fill style format"?
jcgregorio 2013/12/11 17:23:47 Done.
+ info.GetIsolate(), "Invalid style format."));
+ return;
+ }
+ char buf[8];
+ s->WriteUtf8(buf, sizeof(buf));
robertphillips 2013/12/11 16:45:46 Test if buf[0] == '#'?
jcgregorio 2013/12/11 17:23:47 Done.
+ long color = strtol(buf+1, NULL, 16);
+ jsCanvas->fFillStyle.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE));
}
+
Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate;
Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() {
@@ -121,10 +162,15 @@ Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() {
result->SetInternalFieldCount(1);
// 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(
String::NewFromUtf8(
- fIsolate, "drawRect", String::kInternalizedString),
- FunctionTemplate::New(drawRect));
+ fIsolate, "fillRect", String::kInternalizedString),
+ FunctionTemplate::New(fillRect));
result->Set(
String::NewFromUtf8(
fIsolate, "inval", String::kInternalizedString),
@@ -214,13 +260,7 @@ void JsCanvas::onDraw(SkCanvas* canvas, SkOSWindow* window) {
void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
canvas->save();
-
- fRotationAngle += SkDoubleToScalar(0.2);
- if (fRotationAngle > SkDoubleToScalar(360.0)) {
- fRotationAngle -= SkDoubleToScalar(360.0);
- }
-
- canvas->rotate(fRotationAngle);
+ canvas->drawColor(SK_ColorWHITE);
// Now jump into JS and call the onDraw(canvas) method defined there.
fJsCanvas->onDraw(canvas, this);
@@ -310,6 +350,7 @@ bool JsCanvas::initialize(const char script[]) {
Handle<Value> fn_val = context->Global()->Get(fn_name);
if (!fn_val->IsFunction()) {
+ printf("Not a function.\n");
return false;
}
@@ -332,10 +373,17 @@ SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
Isolate* isolate = Isolate::GetCurrent();
JsCanvas* jsCanvas = new JsCanvas(isolate);
- const char* script = "function onDraw(canvas){"
- "canvas.drawRect();"
- "canvas.inval();"
- "};";
+ const char* script =
robertphillips 2013/12/11 16:45:46 move var right a bit to line up with }?
jcgregorio 2013/12/11 17:23:47 Reformatted. Eventually would like to add a comma
+"var onDraw = function(){ \n"
+" 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)) {
printf("Failed to initialize.\n");
exit(1);
« experimental/SkV8Example/SkV8Example.h ('K') | « experimental/SkV8Example/SkV8Example.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698