From 75a862d305bc4502e22bc5aa65fa271143b8cf6c Mon Sep 17 00:00:00 2001 From: Pushkar Joshi Date: Thu, 31 May 2012 12:18:43 -0700 Subject: move all the subpath functions to its prototype, and first version of the pen path runtime --- assets/canvas-runtime.js | 201 ++++++++++++++++++++++++++++++ js/lib/geom/sub-path.js | 311 ++++++++++++++++++++++++----------------------- 2 files changed, 357 insertions(+), 155 deletions(-) diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js index eeafaab6..ec5097bc 100644 --- a/assets/canvas-runtime.js +++ b/assets/canvas-runtime.js @@ -350,6 +350,10 @@ NinjaCvsRt.GLRuntime = Object.create(Object.prototype, { obj.importJSON( jObj ); break; + case 5: //subpath (created by pen tool) + obj = Object.create(NinjaCvsRt.RuntimeSubPath, {_materials: { value:[], writable:true}}); + obj.importJSON (jObj ); + break; default: throw new Error( "Attempting to load unrecognized object type: " + type ); break; @@ -1770,3 +1774,200 @@ NinjaCvsRt.RuntimePlasmaMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { }); + +// ************************************************************************** +// Runtime for the pen tool path +// ************************************************************************** +NinjaCvsRt.AnchorPoint = Object.create(Object.prototype, { + ///////////////////////////////////////// + // Instance variables + ///////////////////////////////////////// + _x: {value: 0.0, writable: true}, + _y: {value: 0.0, writable: true}, + _z: {value: 0.0, writable: true}, + + _prevX: {value: 0.0, writable: true}, + _prevY: {value: 0.0, writable: true}, + _prevZ: {value: 0.0, writable: true}, + + _nextX: {value: 0.0, writable: true}, + _nextY: {value: 0.0, writable: true}, + _nextZ: {value: 0.0, writable: true}, + + // *********** setters ************ + setPos: { + value: function(x,y,z){ + this._x = x; + this._y = y; + this._z = z; + } + }, + + setPrevPos: { + value: function (x, y, z) { + this._prevX = x; + this._prevY = y; + this._prevZ = z; + } + }, + + setNextPos: { + value: function (x, y, z) { + this._nextX = x; + this._nextY = y; + this._nextZ = z; + } + }, + + // *************** getters ****************** + // (add as needed) + getPosX: { + value: function () { + return this._x; + } + }, + + getPosY: { + value: function () { + return this._y; + } + }, + + getPosZ: { + value: function () { + return this._z; + } + }, + + getPrevX: { + value: function () { + return this._prevX; + } + }, + + getPrevY: { + value: function () { + return this._prevY; + } + }, + + getPrevZ: { + value: function () { + return this._prevZ; + } + }, + + getNextX: { + value: function () { + return this._nextX; + } + }, + + getNextY: { + value: function () { + return this._nextY; + } + }, + + getNextZ: { + value: function () { + return this._nextZ; + } + } +}); + +NinjaCvsRt.RuntimeSubPath = Object.create(NinjaCvsRt.RuntimeGeomObj, { + // array of anchor points + _Anchors: { value: null, writable: true }, + + //path properties + _isClosed: {value: false, writable: true}, + _strokeWidth: {value: 0, writable: true}, + _strokeColor: {value: null, writable: true}, + _fillColor: {value: null, writable: true}, + + importJSON: { + value: function(jo) { + if (this.geomType()!== jo.geomType){ + return; + } + //the geometry for this object + this._Anchors = []; + var i=0; + for (i=0;i1) { + ctx.beginPath(); + var prevAnchor = this._Anchors[0]; + ctx.moveTo(prevAnchor.getPosX(),prevAnchor.getPosY()); + for (var i = 1; i < numAnchors; i++) { + var currAnchor = this._Anchors[i]; + ctx.bezierCurveTo(prevAnchor.getNextX(),prevAnchor.getNextY(), currAnchor.getPrevX(), currAnchor.getPrevY(), currAnchor.getPosX(), currAnchor.getPosY()); + prevAnchor = currAnchor; + } + if (this._isClosed === true) { + var currAnchor = this._Anchors[0]; + ctx.bezierCurveTo(prevAnchor.getNextX(),prevAnchor.getNextY(), currAnchor.getPrevX(), currAnchor.getPrevY(), currAnchor.getPosX(), currAnchor.getPosY()); + prevAnchor = currAnchor; + ctx.fill(); + } + ctx.stroke(); + } + ctx.restore(); + } + } +});// ************************************************************************** +// END runtime for the pen tool path +// ************************************************************************** + + diff --git a/js/lib/geom/sub-path.js b/js/lib/geom/sub-path.js index a9034def..db115655 100755 --- a/js/lib/geom/sub-path.js +++ b/js/lib/geom/sub-path.js @@ -68,178 +68,179 @@ var GLSubpath = function GLSubpath() { this._selectedAnchorIndex = -1; this._SAMPLING_EPSILON = 0.5; //epsilon used for sampling the curve +}; //function GLSubpath ...class definition - // (current GeomObj complains if buildBuffers/render is added to GLSubpath prototype) - //buildBuffers - // Build the stroke vertices, normals, textures and colors - // Add that array data to the GPU using OpenGL data binding - this.buildBuffers = function () { - // return; //no need to do anything for now - }; - - //render - // specify how to render the subpath in Canvas2D - this.render = function () { - // get the world - var world = this.getWorld(); - if (!world) throw( "null world in subpath render" ); - if (!this._canvas){ - //set the canvas by querying the world - this._canvas = this.getWorld().getCanvas(); - } - // get the context - var ctx = world.get2DContext(); - if (!ctx) throw ("null context in subpath render"); - - var numAnchors = this.getNumAnchors(); - if (numAnchors === 0) { - return; //nothing to do for empty paths - } - this.createSamples(false); //dirty bit checked in this function...will generate a polyline representation +GLSubpath.prototype = Object.create(GeomObj, {}); - var numPoints = this._Samples.length; - if (numPoints === 0){ - return; //nothing to do for empty paths - } - - //figure the size of the area we will draw into - var bboxWidth=0, bboxHeight=0; - bboxWidth = this._BBoxMax[0] - this._BBoxMin[0]; - bboxHeight = this._BBoxMax[1] - this._BBoxMin[1]; - - ctx.save(); - ctx.clearRect(0, 0, bboxWidth, bboxHeight); - - ctx.lineWidth = this._strokeWidth; - ctx.strokeStyle = "black"; - if (this._strokeColor) { - //ctx.strokeStyle = MathUtils.colorToHex( this._strokeColor ); - var strokeColorStr = "rgba("+parseInt(255*this._strokeColor[0])+","+parseInt(255*this._strokeColor[1])+","+parseInt(255*this._strokeColor[2])+","+this._strokeColor[3]+")"; - ctx.strokeStyle = strokeColorStr; - } +//buildBuffers +GLSubpath.prototype.buildBuffers = function () { + //no need to do anything for now (no WebGL) +}; - ctx.fillStyle = "white"; - if (this._fillColor){ - //ctx.fillStyle = MathUtils.colorToHex( this._fillColor ); - var fillColorStr = "rgba("+parseInt(255*this._fillColor[0])+","+parseInt(255*this._fillColor[1])+","+parseInt(255*this._fillColor[2])+","+this._fillColor[3]+")"; - ctx.fillStyle = fillColorStr; - } - var lineCap = ['butt','round','square']; - ctx.lineCap = lineCap[1]; - var lineJoin = ['round','bevel','miter']; - ctx.lineJoin = lineJoin[0]; - - /* - commenting this out for now because of Chrome bug where coincident endpoints of bezier curve cause the curve to not be rendered - ctx.beginPath(); - var prevAnchor = this.getAnchor(0); - ctx.moveTo(prevAnchor.getPosX()-bboxMin[0],prevAnchor.getPosY()-bboxMin[1]); - for (var i = 1; i < numAnchors; i++) { - var currAnchor = this.getAnchor(i); - ctx.bezierCurveTo(prevAnchor.getNextX()-bboxMin[0],prevAnchor.getNextY()-bboxMin[1], currAnchor.getPrevX()-bboxMin[0], currAnchor.getPrevY()-bboxMin[1], currAnchor.getPosX()-bboxMin[0], currAnchor.getPosY()-bboxMin[1]); - prevAnchor = currAnchor; - } - if (this._isClosed === true) { - var currAnchor = this.getAnchor(0); - ctx.bezierCurveTo(prevAnchor.getNextX()-bboxMin[0],prevAnchor.getNextY()-bboxMin[1], currAnchor.getPrevX()-bboxMin[0], currAnchor.getPrevY()-bboxMin[1], currAnchor.getPosX()-bboxMin[0], currAnchor.getPosY()-bboxMin[1]); - prevAnchor = currAnchor; - ctx.fill(); - } - */ +//render +// specify how to render the subpath in Canvas2D +GLSubpath.prototype.render = function () { + // get the world + var world = this.getWorld(); + if (!world) throw( "null world in subpath render" ); + if (!this._canvas){ + //set the canvas by querying the world + this._canvas = this.getWorld().getCanvas(); + } + // get the context + var ctx = world.get2DContext(); + if (!ctx) throw ("null context in subpath render"); + var numAnchors = this.getNumAnchors(); + if (numAnchors === 0) { + return; //nothing to do for empty paths + } + this.createSamples(false); //dirty bit checked in this function...will generate a polyline representation - ctx.beginPath(); - ctx.moveTo(this._Samples[0][0],this._Samples[0][1]); - for (var i=0;i