• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  *
8  */
9 
10 #ifndef SkV8Example_JsContext_DEFINED
11 #define SkV8Example_JsContext_DEFINED
12 
13 #include <v8.h>
14 
15 #include "SkPaint.h"
16 #include "BaseContext.h"
17 
18 using namespace v8;
19 
20 class SkCanvas;
21 class Global;
22 
23 // Provides the canvas context implementation in JS, and the OnDraw() method in
24 // C++ that's used to bridge from C++ to JS. Should be used in JS as:
25 //
26 //  function onDraw(context) {
27 //    context.fillStyle="#FF0000";
28 //    context.fillRect(x, y, w, h);
29 //  }
30 class JsContext : public BaseContext {
31 public:
JsContext(Global * global)32     JsContext(Global* global)
33             : INHERITED(global)
34             , fCanvas(NULL)
35     {
36     }
~JsContext()37     virtual ~JsContext() {}
38 
39     // Parse the script.
40     bool initialize();
41 
42     // Call this with the SkCanvas you want onDraw to draw on.
43     void onDraw(SkCanvas* canvas);
44 
getCanvas()45     virtual SkCanvas* getCanvas() { return fCanvas; };
46 
47 private:
48 
49     // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
50     Handle<Object> wrap();
51 
52     // A handle to the onDraw function defined in the script.
53     Persistent<Function> fOnDraw;
54 
55     // The template for what a canvas context object looks like. The canvas
56     // context object is what's passed into the JS onDraw() function.
57     static Persistent<ObjectTemplate> gContextTemplate;
58 
59     // Only valid when inside OnDraw().
60     SkCanvas* fCanvas;
61 
62     typedef BaseContext INHERITED;
63 };
64 
65 #endif
66