From cd110186256cee6acb0d503ab4af4b7ea7dc48a1 Mon Sep 17 00:00:00 2001 From: "commit-bot@chromium.org" Date: Fri, 10 Jan 2014 21:33:01 +0000 Subject: Just use one version of the scripts in both the browser and in SkV8 by using feature detection to determine if we are running in a browser, and if the platform supports the Path() object. Also add oval, console.log, and the snow example code. BUG= R=robertphillips@google.com Author: jcgregorio@google.com Review URL: https://codereview.chromium.org/132413002 git-svn-id: http://skia.googlecode.com/svn/trunk@13031 2bbb7eff-a529-9590-31e7-b0007b416f81 --- experimental/SkV8Example/Path.cpp | 29 +++++++- experimental/SkV8Example/Path.h | 1 + experimental/SkV8Example/SkV8Example.cpp | 27 ++++++-- experimental/SkV8Example/compare/gears.html | 4 +- experimental/SkV8Example/compare/gears.js | 93 -------------------------- experimental/SkV8Example/compare/snow.html | 11 +++ experimental/SkV8Example/gears.js | 100 +++++++++++++++++++++------- experimental/SkV8Example/snow.js | 92 +++++++++++++++++++++++++ 8 files changed, 231 insertions(+), 126 deletions(-) delete mode 100644 experimental/SkV8Example/compare/gears.js create mode 100644 experimental/SkV8Example/compare/snow.html create mode 100644 experimental/SkV8Example/snow.js (limited to 'experimental/SkV8Example') diff --git a/experimental/SkV8Example/Path.cpp b/experimental/SkV8Example/Path.cpp index 2b53a0f8e1..90574f40f0 100644 --- a/experimental/SkV8Example/Path.cpp +++ b/experimental/SkV8Example/Path.cpp @@ -43,13 +43,14 @@ void Path::AddToGlobal(Global* global) { gGlobal->getIsolate(), Path::ConstructPath); constructor->InstanceTemplate()->SetInternalFieldCount(1); - ADD_METHOD("close", ClosePath); + ADD_METHOD("closePath", ClosePath); ADD_METHOD("moveTo", MoveTo); ADD_METHOD("lineTo", LineTo); ADD_METHOD("quadraticCurveTo", QuadraticCurveTo); ADD_METHOD("bezierCurveTo", BezierCurveTo); ADD_METHOD("arc", Arc); ADD_METHOD("rect", Rect); + ADD_METHOD("oval", Oval); context->Global()->Set(String::NewFromUtf8( gGlobal->getIsolate(), "Path"), constructor->GetFunction()); @@ -191,3 +192,29 @@ void Path::Rect(const v8::FunctionCallbackInfo& args) { Path* path = Unwrap(args); path->fSkPath.addRect(rect); } + +void Path::Oval(const v8::FunctionCallbackInfo& args) { + if (args.Length() != 4 && args.Length() != 5) { + args.GetIsolate()->ThrowException( + v8::String::NewFromUtf8( + args.GetIsolate(), "Error: 4 or 5 args required.")); + return; + } + double x = args[0]->NumberValue(); + double y = args[1]->NumberValue(); + double radiusX = args[2]->NumberValue(); + double radiusY = args[3]->NumberValue(); + SkPath::Direction dir = SkPath::kCW_Direction; + if (args.Length() == 5 && !args[4]->BooleanValue()) { + dir = SkPath::kCCW_Direction; + } + Path* path = Unwrap(args); + SkRect rect = { + SkDoubleToScalar(x-radiusX), + SkDoubleToScalar(y-radiusX), + SkDoubleToScalar(x+radiusY), + SkDoubleToScalar(y+radiusY) + }; + + path->fSkPath.addOval(rect, dir); +} diff --git a/experimental/SkV8Example/Path.h b/experimental/SkV8Example/Path.h index 0c697700cd..3c21c9c0f4 100644 --- a/experimental/SkV8Example/Path.h +++ b/experimental/SkV8Example/Path.h @@ -39,6 +39,7 @@ public: static void BezierCurveTo(const v8::FunctionCallbackInfo& args); static void Arc(const v8::FunctionCallbackInfo& args); static void Rect(const v8::FunctionCallbackInfo& args); + static void Oval(const v8::FunctionCallbackInfo& args); private: SkPath fSkPath; diff --git a/experimental/SkV8Example/SkV8Example.cpp b/experimental/SkV8Example/SkV8Example.cpp index 6eb8d89e04..eb0e713376 100644 --- a/experimental/SkV8Example/SkV8Example.cpp +++ b/experimental/SkV8Example/SkV8Example.cpp @@ -156,12 +156,28 @@ SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { Isolate* isolate = Isolate::GetCurrent(); Global* global = new Global(isolate); + + // Set up things to look like a browser by creating + // a console object that invokes our print function. + const char* startupScript = + "function Console() {}; \n" + "Console.prototype.log = function() { \n" + " var args = Array.prototype.slice.call(arguments).join(' '); \n" + " print(args); \n" + "}; \n" + "console = new Console(); \n"; + + if (!global->parseScript(startupScript)) { + printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]); + exit(1); + } + const char* script = -"function onDraw(canvas) { \n" -" canvas.fillStyle = '#00FF00'; \n" -" canvas.fillRect(20, 20, 100, 100); \n" -" canvas.inval(); \n" -"} \n"; + "function onDraw(canvas) { \n" + " canvas.fillStyle = '#00FF00'; \n" + " canvas.fillRect(20, 20, 100, 100); \n" + " canvas.inval(); \n" + "} \n"; SkAutoTUnref data; if (FLAGS_infile.count()) { @@ -179,6 +195,7 @@ SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { exit(1); } + JsContext* jsContext = new JsContext(global); if (!jsContext->initialize()) { diff --git a/experimental/SkV8Example/compare/gears.html b/experimental/SkV8Example/compare/gears.html index b59664a8a1..a53789c9dc 100644 --- a/experimental/SkV8Example/compare/gears.html +++ b/experimental/SkV8Example/compare/gears.html @@ -5,7 +5,7 @@ - - + + diff --git a/experimental/SkV8Example/compare/gears.js b/experimental/SkV8Example/compare/gears.js deleted file mode 100644 index cca8213d98..0000000000 --- a/experimental/SkV8Example/compare/gears.js +++ /dev/null @@ -1,93 +0,0 @@ -var NumTeeth = 24; -var Delta = Math.PI/90; -var NumGears = 60; -var FaceColors = ["#000099", "#006600", "#990000", "#EEEE00"]; -var SideColors = ["#0000FF", "#009900", "#FF0000", "#CCCC00"]; - - -function drawGear(ctx, r) { - var dT = Math.PI*2/NumTeeth; - var dTq = dT/4; - var outer = r; - var inner = 0.7 * r; - ctx.beginPath(); - for (var i=0; i= Math.PI*2) { - rotation = 0; - } - - for (var i=0; i < gears.length; i++) { - draw3DGearAt(ctx, gears[i].x, gears[i].y, gears[i].r, rotation, - gears[i].faceColor, gears[i].sideColor); } - - ticks++; - requestAnimationFrame(draw); - } - - requestAnimationFrame(draw); - - - function fps() { - console.log(ticks); - ticks = 0; - } - - setInterval(fps, 1000); -}(); - - diff --git a/experimental/SkV8Example/compare/snow.html b/experimental/SkV8Example/compare/snow.html new file mode 100644 index 0000000000..cc5ad0cb75 --- /dev/null +++ b/experimental/SkV8Example/compare/snow.html @@ -0,0 +1,11 @@ + + + + Snow + + + + + + + diff --git a/experimental/SkV8Example/gears.js b/experimental/SkV8Example/gears.js index 89547a278d..7eb4c5b198 100644 --- a/experimental/SkV8Example/gears.js +++ b/experimental/SkV8Example/gears.js @@ -1,45 +1,78 @@ +var IS_SKV8 = typeof document == "undefined"; +var HAS_PATH = typeof Path != "undefined"; + var NumTeeth = 24; var NumGears = 60; var DeltaTheta = Math.PI/90; var FaceColors = ["#000099", "#006600", "#990000", "#EEEE00"]; var SideColors = ["#0000FF", "#009900", "#FF0000", "#CCCC00"]; -function gearPath(r) { - var outer = r; - var inner = 0.7 * r; +function makeGear(pathLike, r) { var dT = Math.PI*2/NumTeeth; var dTq = dT/4; - p = new Path(); - p.moveTo(Math.sin(-2*dTq)*outer, Math.cos(-2*dTq)*outer); + var outer = r; + var inner = 0.7 * r; + pathLike.moveTo(Math.sin(-2*dTq)*outer, Math.cos(-2*dTq)*outer); for (var i=0; i W) { + p.x-=W; + } + if (p.x < 0) { + p.x += W; + } + if(p.y>(H+1)){ + p.y = 0; + } + if (IS_SKV8) { + ctx.save(); + ctx.translate(p.x, p.y); + ctx.fill(p.path); + ctx.restore(); + } else { + ctx.beginPath(); + ctx.moveTo(p.x, p.y); + ctx.arc(p.x, p.y, p.r, 0, 2*Math.PI, true); + ctx.closePath(); + ctx.fill(); + } + }; + + ticks++; + if (IS_SKV8) { + inval(); + } + } + + function fps() { + console.log(ticks); + ticks = 0; + setTimeout(fps, 1000); + } + + setTimeout(fps, 1000); + + return draw; +}(); + +if (!IS_SKV8) { + window.onload = function(){ + var canvas = document.getElementById("snow"); + var ctx = canvas.getContext("2d"); + function drawCallback() { + onDraw(ctx); + setTimeout(drawCallback, 1); + } + setTimeout(drawCallback, 1); + } +} -- cgit v1.2.3