aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/SkV8Example/DrawingMethods.cpp
diff options
context:
space:
mode:
authorGravatar jcgregorio <jcgregorio@google.com>2014-10-27 10:27:01 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-10-27 10:27:01 -0700
commit5e44b00392e791088b693a0b462b107b0b5a91ba (patch)
treea326a55590a4663d09ca4b8c42aa01956c9392fb /experimental/SkV8Example/DrawingMethods.cpp
parent13f701a14d2a303b153ea861a3482e25254a9c3c (diff)
Start moving to the new canvas structure.
Adds DrawingMethods with some of the methods it defines. Context is now an implementation of DrawingMethods. The sample.js file now shows how the context is used. Not much new code here, that's mostly in DrawingMethods::DrawPath, most everything else is a code move. BUG=skia: Review URL: https://codereview.chromium.org/676423002
Diffstat (limited to 'experimental/SkV8Example/DrawingMethods.cpp')
-rw-r--r--experimental/SkV8Example/DrawingMethods.cpp172
1 files changed, 172 insertions, 0 deletions
diff --git a/experimental/SkV8Example/DrawingMethods.cpp b/experimental/SkV8Example/DrawingMethods.cpp
new file mode 100644
index 0000000000..fdc8dc7d63
--- /dev/null
+++ b/experimental/SkV8Example/DrawingMethods.cpp
@@ -0,0 +1,172 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ */
+#include <v8.h>
+
+#include "Global.h"
+#include "DrawingMethods.h"
+#include "Path2D.h"
+#include "SkCanvas.h"
+#include "SkPaint.h"
+
+
+DrawingMethods* DrawingMethods::Unwrap(v8::Handle<v8::Object> obj) {
+ v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInternalField(0));
+ void* ptr = field->Value();
+ return static_cast<DrawingMethods*>(ptr);
+}
+
+
+void DrawingMethods::Save(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ DrawingMethods* drawingMethods = Unwrap(args.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ canvas->save();
+}
+
+void DrawingMethods::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ DrawingMethods* drawingMethods = Unwrap(args.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ canvas->restore();
+}
+
+void DrawingMethods::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ DrawingMethods* drawingMethods = Unwrap(args.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 1) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 1 arguments required."));
+ return;
+ }
+ double angle = args[0]->NumberValue();
+ canvas->rotate(SkRadiansToDegrees(angle));
+}
+
+void DrawingMethods::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ DrawingMethods* drawingMethods = Unwrap(args.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 2) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 2 arguments required."));
+ return;
+ }
+ double dx = args[0]->NumberValue();
+ double dy = args[1]->NumberValue();
+ canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
+}
+
+void DrawingMethods::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ DrawingMethods* drawingMethods = Unwrap(args.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ canvas->resetMatrix();
+}
+
+void DrawingMethods::DrawPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ DrawingMethods* drawingMethods = Unwrap(args.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 1) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 1 argument required."));
+ return;
+ }
+
+ v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
+ args[0]->ToObject()->GetInternalField(0));
+ void* ptr = field->Value();
+ Path2D* path = static_cast<Path2D*>(ptr);
+ if (NULL == path) {
+ return;
+ }
+ // TODO(jcgregorio) Add support for Paint2D parameter after Paint2D is
+ // implemented.
+ SkPaint fillStyle;
+ fillStyle.setColor(SK_ColorBLACK);
+ fillStyle.setAntiAlias(true);
+ fillStyle.setStyle(SkPaint::kFill_Style);
+ canvas->drawPath(path->getSkPath(), fillStyle);
+}
+
+
+void DrawingMethods::GetWidth(v8::Local<v8::String> name,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ DrawingMethods* drawingMethods = Unwrap(info.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ info.GetReturnValue().Set(
+ v8::Int32::New(
+ drawingMethods->fGlobal->getIsolate(), canvas->imageInfo().width()));
+}
+
+void DrawingMethods::GetHeight(v8::Local<v8::String> name,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ DrawingMethods* drawingMethods = Unwrap(info.This());
+ SkCanvas* canvas = drawingMethods->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ info.GetReturnValue().Set(
+ v8::Int32::New(
+ drawingMethods->fGlobal->getIsolate(), canvas->imageInfo().height()));
+}
+
+#define ADD_METHOD(name, fn) \
+ tmpl->Set(v8::String::NewFromUtf8( \
+ fGlobal->getIsolate(), name, \
+ v8::String::kInternalizedString), \
+ v8::FunctionTemplate::New(fGlobal->getIsolate(), fn))
+
+void DrawingMethods::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl) {
+ v8::HandleScope scope(fGlobal->getIsolate());
+
+ // Add accessors for each of the fields of the context object.
+ tmpl->SetAccessor(v8::String::NewFromUtf8(
+ fGlobal->getIsolate(), "width", v8::String::kInternalizedString),
+ GetWidth);
+ tmpl->SetAccessor(v8::String::NewFromUtf8(
+ fGlobal->getIsolate(), "height", v8::String::kInternalizedString),
+ GetHeight);
+
+ // Add methods.
+ ADD_METHOD("save", Save);
+ ADD_METHOD("restore", Restore);
+ ADD_METHOD("rotate", Rotate);
+ ADD_METHOD("translate", Translate);
+ ADD_METHOD("resetTransform", ResetTransform);
+
+ ADD_METHOD("drawPath", DrawPath);
+}