From 653c851433c955ddb6e9d37e21b9c2eef24b750e Mon Sep 17 00:00:00 2001 From: kenperlin Date: Wed, 5 Nov 2014 08:53:11 -0500 Subject: [PATCH 01/30] Added plane geometry to four.js. Added sketches/coord.js to help demonstrate result of matrix transformations. --- four.js | 14 ++++++++++--- sketches/coord.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ sketches/tactonic.js | 36 ++++++++++++++++++++++++--------- 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 sketches/coord.js diff --git a/four.js b/four.js index 9ca1424..cfa8545 100644 --- a/four.js +++ b/four.js @@ -49,12 +49,13 @@ this.matrixWorldNeedsUpdate = true; } + function cubeGeometry() { return new THREE.BoxGeometry(2, 2, 2); } function cylinderGeometry(n) { return new THREE.CylinderGeometry(1, 1, 2, n, 1, false); } - function openCylinderGeometry(n) { return new THREE.CylinderGeometry(1, 1, 2, n, 1, true); } + function globeGeometry(m,n, p0,p1, t0,t1) { return new THREE.SphereGeometry(1, m,n, p0,p1, t0,t1); } function latheGeometry(points, n) { return new THREE.LatheGeometry(points, n); } + function openCylinderGeometry(n) { return new THREE.CylinderGeometry(1, 1, 2, n, 1, true); } + function planeGeometry(n) { return new THREE.PlaneGeometry(2,2,n,n); } function torusGeometry(r, m, n) { return new THREE.TorusGeometry(1, r, m, n); } - function cubeGeometry() { return new THREE.BoxGeometry(2, 2, 2); } - function globeGeometry(m,n, p0,p1, t0,t1) { return new THREE.SphereGeometry(1, m,n, p0,p1, t0,t1); } THREE.Object3D.prototype.addTorus = function(r, m, n) { var geometry = torusGeometry(r, m, n); @@ -96,6 +97,13 @@ return mesh; } + THREE.Object3D.prototype.addPlane = function() { + var geometry = planeGeometry(); + var mesh = new THREE.Mesh(geometry, bgMaterial()); + this.add(mesh); + return mesh; + } + THREE.Object3D.prototype.addGlobe = function(m,n, p0,p1, t0,t1) { if (m === undefined) m = 32; if (n === undefined) n = floor(m / 2); diff --git a/sketches/coord.js b/sketches/coord.js new file mode 100644 index 0000000..b8f808f --- /dev/null +++ b/sketches/coord.js @@ -0,0 +1,47 @@ +function Coord() { + this.labels = "coord".split(' '); + this.is3D = true; + this.radius = 0.3; + this.edges = [[0,1],[2,3],[4,5],[6,7],[0,2],[1,3],[4,6],[5,7],[0,4],[1,5],[2,6],[3,7]]; + this.render = function() { + var r = this.radius; + + mLine([-1,0], [1,0]); + mLine([0,-1], [0,1]); + + this.duringSketch(function() { + mClosedCurve([ [-r,-r], [r,-r], [r,r], [-r,r] ]); + }); + + this.afterSketch(function() { + mLine([0,0,-1], [0,0,1]); + + textHeight(12); + mText("x", [1.2,0,0], .5,.5); + mText("y", [0,1.2,0], .5,.5); + mText("z", [0,0,1.2], .5,.5); + + var C = []; + for (var k = -r ; k <= r ; k += 2 * r) + for (var j = -r ; j <= r ; j += 2 * r) + for (var i = -r ; i <= r ; i += 2 * r) + C.push([i,j,k]); + + if (isDef(this.inValue[0]) && this.inValue[0].length == 16) { + var M = this.inValue[0]; + for (var n = 0 ; n < C.length ; n++) { + var x = C[n][0], y = C[n][1], z = C[n][2]; + C[n] = [ max(-1,min(1, [ x * M[0] + y * M[4] + z * M[ 8] + M[12] ])), + max(-1,min(1, [ x * M[1] + y * M[5] + z * M[ 9] + M[13] ])), + max(-1,min(1, [ x * M[2] + y * M[6] + z * M[10] + M[14] ])) ]; + } + } + + lineWidth(1); + for (var n = 0 ; n < this.edges.length ; n++) + mLine(C[this.edges[n][0]], C[this.edges[n][1]]); + }); + } +} +Coord.prototype = new Sketch; +addSketchType("Coord"); diff --git a/sketches/tactonic.js b/sketches/tactonic.js index 97ff8e0..a3cb7b5 100644 --- a/sketches/tactonic.js +++ b/sketches/tactonic.js @@ -1,12 +1,15 @@ function Tactonic() { this.labels = "tactonic".split(' '); + this.nc = 48; + this.nr = 72; + this.S = []; - var s = 1 / 36; - for (var c = 0 ; c < 48 ; c++) { - var x = (c - 24) * s; - for (var r = 0 ; r < 72 ; r++) { - var y = (r - 36) * s; + var s = 1 / (this.nr/2); + for (var c = 0 ; c < this.nc ; c++) { + var x = (c - this.nc/2) * s; + for (var r = 0 ; r < this.nr ; r++) { + var y = (r - this.nr/2) * s; this.S.push([[x,y],[x+s*1.1,y],[x+s*1.1,y+s*1.1],[x,y+s*1.1]]); } } @@ -15,17 +18,32 @@ function Tactonic() { for (var i = 0 ; i < 255 ; i++) this.C.push('rgb(' + i + ',' + i + ',' + i + ')'); + this.ttMesh = null; + this.render = function() { this.duringSketch(function() { mLine([-1,1],[1,1]); mLine([0,1],[0,-1]); }); this.afterSketch(function() { +/* + if (this.ttMesh == null) { + this.ttMesh = root.addPlane(); + this.ttMesh.setMaterial(whiteMaterial); + } + var xx = this.xyz[2] * this.tx() + this.xyz[0]; + var yy = this.xyz[2] * this.ty() + this.xyz[1]; + var tx = xx / pixelsPerUnit; + var ty = -yy / pixelsPerUnit; + var sc = m._m()[0] / pixelsPerUnit * this.xyz[2]; + this.ttMesh.getMatrix().identity().translate(tx, ty, 0).scale(sc); + console.log(this.xyz); +*/ var n = 0; - for (var c = 0 ; c < 48 ; c++) { - var x = (c - 24) * s; - for (var r = 0 ; r < 72 ; r++) { - var y = (r - 36) * s; + for (var c = 0 ; c < this.nc ; c++) { + var x = (c - this.nc/2) * s; + for (var r = 0 ; r < this.nr ; r++) { + var y = (r - this.nr/2) * s; var f = .5 + .5 * sin(5 * x - time) * sin(5 * y); color(this.C[floor(255 * max(0, min(1, f)))]); mFillCurve(this.S[n]); From a23bee6269fd4f517960aa5bf0755effc1ae3919 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Sat, 8 Nov 2014 16:43:17 -0500 Subject: [PATCH 02/30] Moved over to new 3D rendering approach, in which the rendered object is part of the original sketch. --- four.js | 42 ++++++++------ g.js | 12 ++-- index.html | 3 - october-sketch-pages.js | 4 +- sketch.js | 106 +++++++++++++++++++++++++++++++++- sketchPage.js | 3 - sketches/abacus.js | 116 ++++++++++++++++++++++++++++++++++++++ sketches/c2s.js | 16 ++++-- sketches/house.js | 12 ++++ sketches/s2c.js | 16 ++++-- sketches/tactonic.js | 58 +++++-------------- sui.js | 122 ---------------------------------------- widgets.js | 9 ++- 13 files changed, 310 insertions(+), 209 deletions(-) create mode 100644 sketches/abacus.js create mode 100644 sketches/house.js diff --git a/four.js b/four.js index cfa8545..fd4fb44 100644 --- a/four.js +++ b/four.js @@ -462,11 +462,12 @@ function gl() { return renderer.context; } function isValidVertexShader (string) { return isValidShader(gl().VERTEX_SHADER , string); } function isValidFragmentShader(string) { return isValidShader(gl().FRAGMENT_SHADER, string); } function isValidShader(type, string) { - string = "precision highp float;\n" + string; var shader = gl().createShader(type); gl().shaderSource(shader, string); gl().compileShader(shader); - return gl().getShaderParameter(shader, gl().COMPILE_STATUS); + var status = gl().getShaderParameter(shader, gl().COMPILE_STATUS); + console.log(status); + return status; }; function addUniforms(material, string) { @@ -506,7 +507,7 @@ function shaderMaterial(vertexShader, fragmentShaderString) { vertexShader: vertexShader, }); - var u = "alpha mx my pixelSize selectedIndex time x y z".split(' '); + var u = "alpha mx my mz pixelSize selectedIndex time x y z".split(' '); for (var i = 0 ; i < u.length ; i++) material.uniforms[u[i]] = { type: "f", value: (u[i]=="alpha" ? 1 : 0) }; @@ -518,23 +519,31 @@ function shaderMaterial(vertexShader, fragmentShaderString) { // THIS VERTEX SHADER WILL SUFFICE FOR MOST SHADER PLANES: -var defaultVertexShader = ["\ - varying float dx;\ - varying float dy;\ - varying vec3 vPosition;\ - varying vec3 vNormal;\ - void main() {\ - dx = 2. * uv.x - 1.;\ - dy = 2. * uv.y - 1.;\ - vNormal = normalize((modelViewMatrix * vec4(normal, 0.)).xyz);\ - vPosition = position*.03;\ - gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);\ - }\ -"].join("\n"); +var defaultVertexShader = [ + 'varying float dx;' + ,'varying float dy;' + ,'varying vec3 vPosition;' + ,'varying vec3 vNormal;' + ,'void main() {' + ,' dx = 2. * uv.x - 1.;' + ,' dy = 2. * uv.y - 1.;' + ,' vNormal = normalize((modelViewMatrix * vec4(normal, 0.)).xyz);' + ,' vPosition = position*.03;' + ,' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);' + ,'}' +].join("\n"); + +var defaultFragmentShader = [ + 'void main() {' + ,' float c = .1 + .9 * max(0., dot(vNormal, vec3(.7,.7,.7)));' + ,' gl_FragColor = vec4(pow(vec3(c,c,c), vec3(.45,.45,.45)), alpha);' + ,'}' +].join("\n"); // DEFINES FRAGMENT SHADER FUNCTIONS noise() and turbulence() AND VARS x, y, time and alpha. var fragmentShaderHeader = ["\ + precision mediump float;\ vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\ vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\ vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }\ @@ -591,6 +600,7 @@ var fragmentShaderHeader = ["\ varying float dy;\ uniform float mx;\ uniform float my;\ + uniform float mz;\ uniform float pixelSize;\ uniform float selectedIndex;\ uniform float time;\ diff --git a/g.js b/g.js index b4e2407..65cd035 100644 --- a/g.js +++ b/g.js @@ -2858,6 +2858,9 @@ console.log(lo + " " + hi); function deleteSketchOnly(sketch) { + if (sketch.mesh !== undefined) + root.remove(sketch.mesh); + if (this.visibleEdgesMesh !== undefined) sketchPage.scene.remove(this.visibleEdgesMesh); @@ -3183,7 +3186,6 @@ console.log(lo + " " + hi); this.meshAlpha !== undefined ? this.meshAlpha : sketchPage.fadeAway; this.mesh.setOpacity(sCurve(this.alpha)); - console.log(sCurve(this.alpha)); if (this.glyphSketch != null && this.glyphSketch.fadeAway == 0) this.glyphSketch = null; @@ -3306,10 +3308,6 @@ console.log(lo + " " + hi); */ } - this.setUniform = function(name, val) { - if (isDef(this.mesh.material.uniforms[name])) - this.mesh.material.uniforms[name].value = val; - } this.mesh = null; } GeometrySketch.prototype = new SimpleSketch; @@ -3465,7 +3463,9 @@ console.log(lo + " " + hi); // TELL THE MATERIAL ABOUT ALPHA AND THE FADEAWAY BEFORE THE SKETCH IS DELETED. - S.setUniform('alpha', (S.fadeAway == 0 ? 1 : S.fadeAway) * (isDef(S.alpha) ? S.alpha : 1)); + var alpha = (S.fadeAway == 0 ? 1 : S.fadeAway) * (isDef(S.alpha) ? S.alpha : 1); + mesh.material.transparent = alpha < 1; + S.setUniform('alpha', alpha); // TELL THE MATERIAL WHICH INDEX IS SELECTED IN THE SKETCH'S CODE TEXT BUBBLE. diff --git a/index.html b/index.html index 701de2f..56b117e 100644 --- a/index.html +++ b/index.html @@ -27,15 +27,12 @@ - - - diff --git a/october-sketch-pages.js b/october-sketch-pages.js index cfb118c..d33a203 100644 --- a/october-sketch-pages.js +++ b/october-sketch-pages.js @@ -1,6 +1,6 @@ var sketchTypes = ( - "Abacus Ball Teleg " -+ "Lens Grid Hammer Lattice Motion Example1 " + "Ball Teleg " ++ "Lens Grid Hammer Lattice Motion " + "Noises MothAndCandle OldCamera Radio " + "Scroll Spike Typewriter Tablet Telegraph Television Book1" ).split(' '); diff --git a/sketch.js b/sketch.js index e59c775..46cfbe8 100644 --- a/sketch.js +++ b/sketch.js @@ -78,6 +78,24 @@ this.colorId = i; this.color = palette[i]; } + this.setRenderMatrix = function(mat) { + var D = norm(vecDiff(m.transform([0,0,0]), m.transform([1,0,0]))) * this.xyz[2]; + var s = .24 * width(); + var p = this.toPixel([0,0,0]); + + mat.identity(); + + mat.translate((p[0] - width()/2) / s, (height()/2 - p[1]) / s, 0); + + mat.perspective(0, 0, -width()/2); + + var yy = min(1, 4 * this.rY * this.rY); + mat.rotateX(PI * -this.rY); + mat.rotateY(PI * this.rX * (1 - yy)); + mat.rotateZ(PI * this.rX * yy); + + mat.scale(D / s); + } this.transformX2D = function(x, y) { var angle = 2 * this.rX; return this.x2D + this.scale() * (cos(angle)*x + sin(angle)*y); @@ -114,10 +132,12 @@ _g.restore(); } } - this.extendBounds = function(ax, ay, bx, by) { + this.extendBounds = function(a, b) { this.afterSketch(function() { + var saveStrokeStyle = _g.strokeStyle; color('rgba(0,0,0,.01)'); - mLine([ax, ay], [bx, by]); + mLine(a, b); + _g.strokeStyle = saveStrokeStyle; }); } this.clearPorts = function() { @@ -420,6 +440,10 @@ this.glyphTransition = 0; this.groupPath = []; this.groupPathLen = 1; + this.hasBounds = function() { + return this.xlo !== undefined && this.xhi !== undefined && this.xlo <= this.xhi && + this.ylo !== undefined && this.yhi !== undefined && this.ylo <= this.yhi ; + } this.hasMotionPath = function() { return this.motionPath.length > 0 && this.motionPath[0].length > 1; } @@ -594,6 +618,8 @@ _g.save(); m.save(); this.render(elapsed); + if (! isMakingGlyph && this.createMesh !== undefined) + this.updateMesh(); m.restore(); _g.restore(); } @@ -708,6 +734,10 @@ _g.restore(); } + this.setUniform = function(name, val) { + if (isDef(this.mesh.material.uniforms[name])) + this.mesh.material.uniforms[name].value = val; + } this.sketchLength = 1; this.cursorTransition = 0; this.sketchProgress = 0; @@ -742,6 +772,9 @@ this.textStrs = []; this.textX = 0; this.textY = 0; + this.toPixel = function(point) { + return this.adjustXY(m.transform(point)); + } this.toTrace = function() { var src = this.sp; var dst = []; @@ -827,7 +860,76 @@ this.fragmentShader = codeTextArea.value); } } + this.updateMesh = function() { + if (this.createMesh !== undefined && this.mesh === undefined) { + + if (this.vertexShader === undefined) + this.vertexShader = defaultVertexShader; + + if (this.fragmentShader === undefined) + this.fragmentShader = defaultFragmentShader; + + this.shaderMaterial = function() { + return shaderMaterial(this.vertexShader, this.fragmentShader); + } + + this.updateFragmentShader = function() { + if (this.fragmentShader != codeTextArea.value + && isValidFragmentShader(formFragmentShader(codeTextArea.value))) { + this.fragmentShader = codeTextArea.value; + this.mesh.material = this.shaderMaterial(); + } + } + + if (this.code == null) + this.code = []; + this.code.push(["fragmentShader", this.fragmentShader, this.updateFragmentShader]); + + this.mesh = this.createMesh(); + root.add(this.mesh); + this.is3D = true; + } + if (this.mesh !== undefined) { + + // SET MESH MATRIX TO MATCH SKETCH'S POSITION/ROTATION/SCALE. + + this.setRenderMatrix(this.mesh.getMatrix()); + + // SET OPACITY. + + var alpha = max(0, 2 * this.glyphTransition - 1) * + (this.fadeAway == 0 ? 1 : this.fadeAway) * + (isDef(this.alpha) ? this.alpha : 1); + this.mesh.material.transparent = alpha < 1; + + // SET VARIOUS UNIFORMS IN THE FRAGMENT SHADER. + + if (this.mesh.material.uniforms !== undefined) { + + // SET TIME. + + this.setUniform('time', time); + // SET MOUSE CURSOR. + + var a = this.toPixel([0,0]); + var b = this.toPixel([1,1]); + + this.setUniform('mx', (sketchPage.mx - a[0]) / (b[0] - a[0])); + this.setUniform('my', (sketchPage.my - a[1]) / (b[1] - a[1])); + this.setUniform('mz', sketchPage.isPressed ? 1 : 0); + + this.setUniform('alpha', alpha); + } + else { + this.mesh.setOpacity(alpha); + } + + // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. + + this.extendBounds([-1,-1],[1,1]); + } + } this.value = null; this.x = 0; this.xyz = []; diff --git a/sketchPage.js b/sketchPage.js index 2230d8e..ef23304 100644 --- a/sketchPage.js +++ b/sketchPage.js @@ -1375,9 +1375,6 @@ var sketchToDelete = null; case 'e': toggleCodeWidget(); break; - case 'f': - console.log(tree_obj.vertices.length + " " + tree_obj.faces.length); - break; case 'g': this.toggleGroup(); break; diff --git a/sketches/abacus.js b/sketches/abacus.js new file mode 100644 index 0000000..d60118a --- /dev/null +++ b/sketches/abacus.js @@ -0,0 +1,116 @@ +function Abacus() { + this.labels = "abacus".split(' '); + + this.digits = [0,0,0]; + this.digitsIndex = 0; + this.stones = null; + + this.mouseDown = function(x, y) { + var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); + var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); + this.digitsIndex = xx < -.4 ? 0 : xx < .4 ? 1 : 2; + this.xx = xx; + this.yy = yy; + } + this.mouseDrag = function(x, y) { + var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); + var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); + var index = this.digitsIndex; + var value = this.digits[index] - (yy - this.yy) / 20; + this.digits[index] = max(0, min(9.99, value)); + } + this.mouseUp = function(x, y) { + var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); + var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); + if (abs(xx - this.xx) > abs(yy - this.yy)) + this.animateAbacus = this.animateAbacus === undefined ? true : undefined; + } + + this.render = function(elapsed) { + this.duringSketch(function() { + mCurve( [[-1,1],[1,1],[1,-1],[-1,-1],[-1,1]] ); + mCurve( [[ -1,.5],[ 1,.5]] ); + mCurve( [[-.5, 1],[-.5,-1]] ); + mCurve( [[ 0, 1],[ 0,-1]] ); + mCurve( [[ .5, 1],[ .5,-1]] ); + }); + this.afterSketch(function() { + + if (this.animateAbacus !== undefined) + this.digits[2] += 16 * elapsed; + + for (var index = 2 ; index >= 0 ; index--) + if (this.digits[index] >= 10) { + this.digits[index] -= 10; + if (index > 0) + this.digits[index-1]++; + } + else if (this.digits[index] < 0) { + this.digits[index] += 10; + if (this.digits[index] > 0) + this.digits[index-1]--; + } + + if (isCodeWidget && this == codeSketch) { + this.oldDigits = this.newDigits; + this.newDigits = floor(this.digits[0]) + "" + + floor(this.digits[1]) + "" + + floor(this.digits[2]) ; + if (this.newDigits != this.oldDigits) { + this.code = [["", this.newDigits]]; + toggleCodeWidget(); + toggleCodeWidget(); + } + } + + for (var i = 0 ; i < this.stones.children.length ; i++) { + var n = i % 5; + var d = this.digits[floor(i/5)]; + this.stones.children[i].getMatrix().identity(); + if (n==0 ? d % 5 >= 4 : + n==1 ? d % 5 >= 3 : + n==2 ? d % 5 >= 2 : + n==3 ? d % 5 >= 1 : d >= 5) + this.stones.children[i].getMatrix().translate(0, .1, 0); + } + }); + } + + this.createMesh = function() { + var abacus = new THREE.Mesh(); + + abacus.addCylinder(16).getMatrix().translate( 0, 1,0).rotateZ(PI/2).scale(.05,1 ,.1); + abacus.addCylinder(16).getMatrix().translate( 0,.45,0).rotateZ(PI/2).scale(.10,1 ,.1); + abacus.addCylinder(16).getMatrix().translate( 0, -1,0).rotateZ(PI/2).scale(.05,1 ,.1); + abacus.addCylinder(16).getMatrix().translate(-1, 0,0) .scale(.05,1 ,.1); + abacus.addCylinder(16).getMatrix().translate( 1, 0,0) .scale(.05,1 ,.1); + + abacus.addGlobe(16, 8).getMatrix().translate(-1, 1,0) .scale(.05,.05,.1); + abacus.addGlobe(16, 8).getMatrix().translate( 1, 1,0) .scale(.05,.05,.1); + abacus.addGlobe(16, 8).getMatrix().translate(-1,-1,0) .scale(.05,.05,.1); + abacus.addGlobe(16, 8).getMatrix().translate( 1,-1,0) .scale(.05,.05,.1); + + abacus.addCylinder(16).getMatrix().translate(-.6,0,0).scale(.03,1,.03); + abacus.addCylinder(16).getMatrix().translate( 0,0,0).scale(.03,1,.03); + abacus.addCylinder(16).getMatrix().translate( .6,0,0).scale(.03,1,.03); + + this.stones = abacus.addNode(); + for (var i = 0 ; i < 3 ; i++) { + var x = -.6 + .6 * i; + for (var j = 0 ; j < 6 ; j++) + if (j != 4) { + var y = (j == 5 ? 1.5 : j * .3) - .8; + this.stones.addNode().addGlobe(16,8).getMatrix().translate(x,y,0).scale(.2,.155,.2); + } + } + + abacus.setMaterial(new phongMaterial().setAmbient(.2,.1,.05) + .setDiffuse(.2,.1,.05) + .setSpecular(.2,.2,.2,20)); + return abacus; + } +} +Abacus.prototype = new Sketch; +addSketchType("Abacus"); + + diff --git a/sketches/c2s.js b/sketches/c2s.js index 8fe5faa..5208b49 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -1,7 +1,15 @@ +function C2S() { + this.labels = "c2s".split(' '); - function C2S() { - this.initSketchTo3D("c2s", [ makeOval(-1,-1,2,2,20,-PI/2,3*PI/2) ], function() { return root.addGlobe(); }); + this.render = function() { + this.duringSketch(function() { + mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); + }); } - C2S.prototype = new SketchTo3D; - addSketchType("C2S"); + this.createMesh = function() { + return new THREE.Mesh(globeGeometry(32,16), this.shaderMaterial()); + } +} +C2S.prototype = new SketchTo3D; +addSketchType("C2S"); diff --git a/sketches/house.js b/sketches/house.js new file mode 100644 index 0000000..e87080e --- /dev/null +++ b/sketches/house.js @@ -0,0 +1,12 @@ +function House() { + this.labels = "house".split(' '); + this.render = function(elapsed) { + + // A SIMPLE OUTLINE OF A HOUSE WITH A POINTED ROOF. + + mClosedCurve([ [-1,.8], [-1,-1], [1,-1], [1,.8], [0,1.7] ]); + } +} +House.prototype = new Sketch; +addSketchType("House"); + diff --git a/sketches/s2c.js b/sketches/s2c.js index 2a419be..06e4d4b 100644 --- a/sketches/s2c.js +++ b/sketches/s2c.js @@ -1,7 +1,15 @@ +function S2C() { + this.labels = "s2c".split(' '); - function S2C() { - this.initSketchTo3D("s2c", [ [[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1] ] ], function() { return root.addCube(); }); + this.render = function() { + this.duringSketch(function() { + mCurve([ [-1,-1],[1,-1],[1,1],[-1,1],[-1,-1] ]); + }); } - S2C.prototype = new SketchTo3D; - addSketchType("S2C"); + this.createMesh = function() { + return new THREE.Mesh(cubeGeometry(), this.shaderMaterial()); + } +} +S2C.prototype = new SketchTo3D; +addSketchType("S2C"); diff --git a/sketches/tactonic.js b/sketches/tactonic.js index a3cb7b5..738a705 100644 --- a/sketches/tactonic.js +++ b/sketches/tactonic.js @@ -1,57 +1,25 @@ function Tactonic() { this.labels = "tactonic".split(' '); - this.nc = 48; - this.nr = 72; - - this.S = []; - var s = 1 / (this.nr/2); - for (var c = 0 ; c < this.nc ; c++) { - var x = (c - this.nc/2) * s; - for (var r = 0 ; r < this.nr ; r++) { - var y = (r - this.nr/2) * s; - this.S.push([[x,y],[x+s*1.1,y],[x+s*1.1,y+s*1.1],[x,y+s*1.1]]); - } - } - - this.C = []; - for (var i = 0 ; i < 255 ; i++) - this.C.push('rgb(' + i + ',' + i + ',' + i + ')'); - - this.ttMesh = null; - this.render = function() { this.duringSketch(function() { mLine([-1,1],[1,1]); mLine([0,1],[0,-1]); }); - this.afterSketch(function() { -/* - if (this.ttMesh == null) { - this.ttMesh = root.addPlane(); - this.ttMesh.setMaterial(whiteMaterial); - } - var xx = this.xyz[2] * this.tx() + this.xyz[0]; - var yy = this.xyz[2] * this.ty() + this.xyz[1]; - var tx = xx / pixelsPerUnit; - var ty = -yy / pixelsPerUnit; - var sc = m._m()[0] / pixelsPerUnit * this.xyz[2]; - this.ttMesh.getMatrix().identity().translate(tx, ty, 0).scale(sc); - console.log(this.xyz); -*/ - var n = 0; - for (var c = 0 ; c < this.nc ; c++) { - var x = (c - this.nc/2) * s; - for (var r = 0 ; r < this.nr ; r++) { - var y = (r - this.nr/2) * s; - var f = .5 + .5 * sin(5 * x - time) * sin(5 * y); - color(this.C[floor(255 * max(0, min(1, f)))]); - mFillCurve(this.S[n]); - n++; - } - } - }); + } + + this.fragmentShader = [ + 'void main(void) {' + ,' float c = ( (dx-mx < 0.) == (dy-my < 0.) ) == (mz == 1.) ? .5 : 1.;' + ,' gl_FragColor = vec4(pow(c,.45), pow(c,.45), pow(c,.45), alpha);' + ,'}' + ].join("\n"); + + this.createMesh = function() { + return new THREE.Mesh(planeGeometry(), this.shaderMaterial()); } } Tactonic.prototype = new Sketch; addSketchType("Tactonic"); + + diff --git a/sui.js b/sui.js index a3e0194..d78b01e 100644 --- a/sui.js +++ b/sui.js @@ -1,126 +1,4 @@ - function Abacus() { - this.initSketchTo3D( - "abacus", - [ - [[-1,1],[1,1],[1,-1],[-1,-1],[-1,1]], - [[ -1,.5],[ 1,.5]], - [[-.5, 1],[-.5,-1]], - [[ 0, 1],[ 0,-1]], - [[ .5, 1],[ .5,-1]], - ], - function() { - var abacus = root.addNode(); - abacus.addCylinder(16).getMatrix().translate( 0, 1,0).rotateZ(PI/2).scale(.05,1 ,.1); - abacus.addCylinder(16).getMatrix().translate( 0,.45,0).rotateZ(PI/2).scale(.10,1 ,.1); - abacus.addCylinder(16).getMatrix().translate( 0, -1,0).rotateZ(PI/2).scale(.05,1 ,.1); - abacus.addCylinder(16).getMatrix().translate(-1, 0,0) .scale(.05,1 ,.1); - abacus.addCylinder(16).getMatrix().translate( 1, 0,0) .scale(.05,1 ,.1); - - abacus.addGlobe(16, 8).getMatrix().translate(-1, 1,0) .scale(.05,.05,.1); - abacus.addGlobe(16, 8).getMatrix().translate( 1, 1,0) .scale(.05,.05,.1); - abacus.addGlobe(16, 8).getMatrix().translate(-1,-1,0) .scale(.05,.05,.1); - abacus.addGlobe(16, 8).getMatrix().translate( 1,-1,0) .scale(.05,.05,.1); - - abacus.addCylinder(16).getMatrix().translate(-.6,0,0).scale(.03,1,.03); - abacus.addCylinder(16).getMatrix().translate( 0,0,0).scale(.03,1,.03); - abacus.addCylinder(16).getMatrix().translate( .6,0,0).scale(.03,1,.03); - - abacus.stones = abacus.addNode(); - for (var i = 0 ; i < 3 ; i++) { - var x = -.6 + .6 * i; - for (var j = 0 ; j < 6 ; j++) - if (j != 4) { - var y = (j == 5 ? 1.5 : j * .3) - .8; - abacus.stones.addNode().addGlobe(16,8).getMatrix().translate(x,y,0).scale(.2,.155,.2); - } - } - - abacus.setMaterial(new phongMaterial().setAmbient(.2,.1,.05) - .setDiffuse(.2,.1,.05) - .setSpecular(.2,.2,.2,20)); - - return abacus; - } - ); - this.render = function(elapsed) { - Abacus.prototype.render.call(this, elapsed); - var sketch = this.shapeSketch; - if (sketch !== undefined) { - if (sketch.digits === undefined) { - sketch.digits = [0,0,0]; - sketch.digitsIndex = 0; - } - sketch.mouseDown = function(x, y) { - var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); - var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); - this.digitsIndex = xx < -.4 ? 0 : xx < .4 ? 1 : 2; - this.xx = xx; - this.yy = yy; - } - sketch.mouseDrag = function(x, y) { - var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); - var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); - var index = this.digitsIndex; - value = this.digits[index] - (yy - this.yy) / 20; - this.digits[index] = max(0, min(9.99, value)); - } - sketch.mouseUp = function(x, y) { - var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); - var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); - if (abs(xx - this.xx) > abs(yy - this.yy)) - this.animateAbacus = this.animateAbacus === undefined ? true : undefined; - } - sketch.mesh.update = function(elapsed) { - var sketch = this.sketch; - var stones = this.stones; - - if (sketch.code == null) - sketch.code = [["","000"]]; - - if (sketch.animateAbacus !== undefined) - sketch.digits[2] += 16 * elapsed; - - for (var index = 2 ; index >= 0 ; index--) - if (sketch.digits[index] >= 10) { - sketch.digits[index] -= 10; - if (index > 0) - sketch.digits[index-1]++; - } - else if (sketch.digits[index] < 0) { - sketch.digits[index] += 10; - if (sketch.digits[index] > 0) - sketch.digits[index-1]--; - } - - if (isCodeWidget && sketch == codeSketch) { - sketch.oldDigits = sketch.newDigits; - sketch.newDigits = floor(sketch.digits[0]) + "" + - floor(sketch.digits[1]) + "" + - floor(sketch.digits[2]) ; - if (sketch.newDigits != sketch.oldDigits) { - sketch.code = [["", sketch.newDigits]]; - toggleCodeWidget(); - toggleCodeWidget(); - } - } - - for (var i = 0 ; i < stones.children.length ; i++) { - var n = i % 5; - var d = sketch.digits[floor(i/5)]; - stones.children[i].getMatrix().identity(); - if (n==0 ? d % 5 >= 4 : - n==1 ? d % 5 >= 3 : - n==2 ? d % 5 >= 2 : - n==3 ? d % 5 >= 1 : d >= 5) - stones.children[i].getMatrix().translate(0, .1, 0); - } - } - } - } - } - Abacus.prototype = new SketchTo3D; - function Ball() { this.initSketchTo3D( "ball", diff --git a/widgets.js b/widgets.js index d2de0a6..f20190e 100644 --- a/widgets.js +++ b/widgets.js @@ -678,8 +678,13 @@ FOR WHEN WE HAVE DRAW_PATH SHORTCUT: if (isCodeScript()) { } else if (code() != null) { - code()[codeSelector.selectedIndex][1] = codeTextArea.value; - codeSketch.selectedIndex = codeSelector.selectedIndex; + var index = codeSelector.selectedIndex; + code()[index][1] = codeTextArea.value; + codeSketch.selectedIndex = index; + if (code()[index][2] !== undefined) { + codeSketch.temporaryFunction = code()[index][2]; + codeSketch.temporaryFunction(); + } } }; From e07a39b60f23cacdd7336209e98cee3be9405511 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Sat, 8 Nov 2014 20:35:41 -0500 Subject: [PATCH 03/30] Added ambient, diffuse and specular to defaultFragmentShader. --- four.js | 21 ++++++++++++++++----- sketch.js | 15 ++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/four.js b/four.js index fd4fb44..b5b8626 100644 --- a/four.js +++ b/four.js @@ -474,7 +474,10 @@ function addUniforms(material, string) { // PARSE THE FRAGMENT SHADER CODE TO FIND CUSTOM UNIFORMS: - var typeInfo = "float f 0 vec3 v2 [0,0] vec3 v3 [0,0,0]".split(' '); + var v2_zero = new THREE.Vector2(0, 0); + var v3_zero = new THREE.Vector3(0, 0, 0); + var v4_zero = new THREE.Vector4(0, 0, 0, 0); + var typeInfo = "float f 0 vec2 v2 v2_zero vec3 v3 v3_zero vec4 v4 v4_zero".split(' '); var declarations = string.substring(0, string.indexOf("void main")).split(";"); for (var i = 0 ; i < declarations.length ; i++) { var declaration = declarations[i].trim(); @@ -484,11 +487,12 @@ function addUniforms(material, string) { if (words[0] == 'uniform') { var type = words[1]; var name = words[2]; - for (var n = 0 ; n < typeInfo.length ; n += 3) + for (var n = 0 ; n < typeInfo.length ; n += 3) { if (type == typeInfo[n]) { material.uniforms[name] = { type: typeInfo[n+1], value: typeInfo[n+2] }; break; } + } } } } @@ -534,9 +538,16 @@ var defaultVertexShader = [ ].join("\n"); var defaultFragmentShader = [ - 'void main() {' - ,' float c = .1 + .9 * max(0., dot(vNormal, vec3(.7,.7,.7)));' - ,' gl_FragColor = vec4(pow(vec3(c,c,c), vec3(.45,.45,.45)), alpha);' + 'uniform vec3 ambient;' + ,'uniform vec3 diffuse;' + ,'uniform vec4 specular;' + ,'void main() {' + ,' vec3 Ldir = normalize(vec3(1.,1.,1.));' + ,' vec3 W = vec3(0.,0.,-1.);' + ,' vec3 R = W - 2. * vNormal * dot(vNormal, W);' + ,' vec3 color = ambient + diffuse * max(0., dot(vNormal, Ldir));' + ,' color += specular.rgb * pow(max(0., dot(R, Ldir)), specular.a);' + ,' gl_FragColor = vec4(pow(color, vec3(.45,.45,.45)), alpha);' ,'}' ].join("\n"); diff --git a/sketch.js b/sketch.js index 46cfbe8..cde6d63 100644 --- a/sketch.js +++ b/sketch.js @@ -735,8 +735,9 @@ _g.restore(); } this.setUniform = function(name, val) { - if (isDef(this.mesh.material.uniforms[name])) + if (isDef(this.mesh.material.uniforms[name])) { this.mesh.material.uniforms[name].value = val; + } } this.sketchLength = 1; this.cursorTransition = 0; @@ -870,7 +871,8 @@ this.fragmentShader = defaultFragmentShader; this.shaderMaterial = function() { - return shaderMaterial(this.vertexShader, this.fragmentShader); + var material = shaderMaterial(this.vertexShader, this.fragmentShader); + return material; } this.updateFragmentShader = function() { @@ -888,6 +890,12 @@ this.mesh = this.createMesh(); root.add(this.mesh); this.is3D = true; + + if (this.fragmentShader == defaultFragmentShader) { + this.setUniform('ambient', new THREE.Vector3(.1,.1,.1)); + this.setUniform('diffuse', new THREE.Vector3(.5,.5,.5)); + this.setUniform('specular', new THREE.Vector4(.5,.5,.5,10)); + } } if (this.mesh !== undefined) { @@ -921,9 +929,6 @@ this.setUniform('alpha', alpha); } - else { - this.mesh.setOpacity(alpha); - } // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. From 7654c660d20b68f3e1bd491ffdd1283b6b389977 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Sun, 9 Nov 2014 01:43:53 -0500 Subject: [PATCH 04/30] Added method setUniform to Material in four.js. Fixed logic for setting ambient, diffuse and specular defaults when editing a fragment shader. --- four.js | 22 ++++++++++++++++++---- sketch.js | 24 +++++++++++------------- sketches/c2s.js | 2 ++ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/four.js b/four.js index b5b8626..7c12552 100644 --- a/four.js +++ b/four.js @@ -7,6 +7,19 @@ renderer.camera.updateProjectionMatrix(); }); + THREE.Material.prototype.setUniform = function(name, val) { + if (this.uniforms[name] !== undefined) { + if (Array.isArray(val)) { + switch (val.length) { + case 2: val = new THREE.Vector2(val[0],val[1]); break; + case 3: val = new THREE.Vector3(val[0],val[1],val[2]); break; + case 4: val = new THREE.Vector4(val[0],val[1],val[2],val[3]); break; + } + } + this.uniforms[name].value = val; + } + } + THREE.Object3D.prototype.setMaterial = function(material) { if (isShowingMeshEdges) material = bgMaterial(); @@ -542,11 +555,12 @@ var defaultFragmentShader = [ ,'uniform vec3 diffuse;' ,'uniform vec4 specular;' ,'void main() {' - ,' vec3 Ldir = normalize(vec3(1.,1.,1.));' + ,' vec3 N = normalize(vNormal);' + ,' vec3 L = normalize(vec3(1.,1.,1.));' ,' vec3 W = vec3(0.,0.,-1.);' - ,' vec3 R = W - 2. * vNormal * dot(vNormal, W);' - ,' vec3 color = ambient + diffuse * max(0., dot(vNormal, Ldir));' - ,' color += specular.rgb * pow(max(0., dot(R, Ldir)), specular.a);' + ,' vec3 R = W - 2. * N * dot(N, W);' + ,' vec3 color = ambient + diffuse * max(0., dot(N, L));' + ,' color += specular.rgb * pow(max(0., dot(R, L)), specular.a);' ,' gl_FragColor = vec4(pow(color, vec3(.45,.45,.45)), alpha);' ,'}' ].join("\n"); diff --git a/sketch.js b/sketch.js index cde6d63..283f3b3 100644 --- a/sketch.js +++ b/sketch.js @@ -735,9 +735,8 @@ _g.restore(); } this.setUniform = function(name, val) { - if (isDef(this.mesh.material.uniforms[name])) { - this.mesh.material.uniforms[name].value = val; - } + if (this.mesh !== undefined) + this.mesh.material.setUniform(name, val); } this.sketchLength = 1; this.cursorTransition = 0; @@ -872,14 +871,19 @@ this.shaderMaterial = function() { var material = shaderMaterial(this.vertexShader, this.fragmentShader); + material.setUniform('ambient' , [.1,.1,.1]); + material.setUniform('diffuse' , [.3,.3,.3]); + material.setUniform('specular', [.5,.5,.5,20]); return material; } this.updateFragmentShader = function() { - if (this.fragmentShader != codeTextArea.value - && isValidFragmentShader(formFragmentShader(codeTextArea.value))) { - this.fragmentShader = codeTextArea.value; - this.mesh.material = this.shaderMaterial(); + if (this.fragmentShader != codeTextArea.value) { + var isValid = isValidFragmentShader(formFragmentShader(codeTextArea.value)); + if (isValid) { + this.fragmentShader = codeTextArea.value; + this.mesh.material = this.shaderMaterial(); + } } } @@ -890,12 +894,6 @@ this.mesh = this.createMesh(); root.add(this.mesh); this.is3D = true; - - if (this.fragmentShader == defaultFragmentShader) { - this.setUniform('ambient', new THREE.Vector3(.1,.1,.1)); - this.setUniform('diffuse', new THREE.Vector3(.5,.5,.5)); - this.setUniform('specular', new THREE.Vector4(.5,.5,.5,10)); - } } if (this.mesh !== undefined) { diff --git a/sketches/c2s.js b/sketches/c2s.js index 5208b49..8c203b1 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -5,6 +5,8 @@ function C2S() { this.duringSketch(function() { mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); }); + //this.setUniform('ambient', [.2,0,0]); + //this.setUniform('diffuse', [.5,0,0]); } this.createMesh = function() { return new THREE.Mesh(globeGeometry(32,16), this.shaderMaterial()); From de3a8fe75b407b2d050af97ba7c056a286e72065 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Sun, 9 Nov 2014 09:39:36 -0500 Subject: [PATCH 05/30] Added multiple lights to defaultFragmentShader. --- four.js | 64 ++++++++++++++++++++++++++++++++++++++----------------- sketch.js | 8 ++++--- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/four.js b/four.js index 7c12552..30bc761 100644 --- a/four.js +++ b/four.js @@ -7,14 +7,25 @@ renderer.camera.updateProjectionMatrix(); }); - THREE.Material.prototype.setUniform = function(name, val) { + function toVec(src) { + switch (src.length) { + default: return new THREE.Vector2(src[0],src[1]); + case 3 : return new THREE.Vector3(src[0],src[1],src[2]); + case 4 : return new THREE.Vector4(src[0],src[1],src[2],src[3]); + } + } + + THREE.Material.prototype.setUniform = function(name, src) { if (this.uniforms[name] !== undefined) { - if (Array.isArray(val)) { - switch (val.length) { - case 2: val = new THREE.Vector2(val[0],val[1]); break; - case 3: val = new THREE.Vector3(val[0],val[1],val[2]); break; - case 4: val = new THREE.Vector4(val[0],val[1],val[2],val[3]); break; - } + var val = src; + if (Array.isArray(src)) { + if (! Array.isArray(src[0])) + val = toVec(src); + else { + val = []; + for (var i = 0 ; i < src.length ; i++) + val.push(toVec(src[i])); + } } this.uniforms[name].value = val; } @@ -487,22 +498,27 @@ function addUniforms(material, string) { // PARSE THE FRAGMENT SHADER CODE TO FIND CUSTOM UNIFORMS: - var v2_zero = new THREE.Vector2(0, 0); - var v3_zero = new THREE.Vector3(0, 0, 0); - var v4_zero = new THREE.Vector4(0, 0, 0, 0); - var typeInfo = "float f 0 vec2 v2 v2_zero vec3 v3 v3_zero vec4 v4 v4_zero".split(' '); + var typeInfo = "float f 0 vec2 v2 0 vec3 v3 0 vec4 v4 0".split(' '); var declarations = string.substring(0, string.indexOf("void main")).split(";"); for (var i = 0 ; i < declarations.length ; i++) { var declaration = declarations[i].trim(); if (declaration.length > 0) { - var words = declaration.split(" "); if (words[0] == 'uniform') { var type = words[1]; var name = words[2]; + var j = name.indexOf('['); + if (j >= 0) + name = name.substring(0, j); for (var n = 0 ; n < typeInfo.length ; n += 3) { if (type == typeInfo[n]) { - material.uniforms[name] = { type: typeInfo[n+1], value: typeInfo[n+2] }; + var key = typeInfo[n+1]; + var val = typeInfo[n+2]; + if (j >= 0) { + key += 'v'; + val = '[]'; + } + material.uniforms[name] = { type: key, value: val }; break; } } @@ -554,14 +570,22 @@ var defaultFragmentShader = [ 'uniform vec3 ambient;' ,'uniform vec3 diffuse;' ,'uniform vec4 specular;' + ,'uniform vec3 L_rgb[3];' + ,'uniform vec3 L_dir[3];' ,'void main() {' - ,' vec3 N = normalize(vNormal);' - ,' vec3 L = normalize(vec3(1.,1.,1.));' - ,' vec3 W = vec3(0.,0.,-1.);' - ,' vec3 R = W - 2. * N * dot(N, W);' - ,' vec3 color = ambient + diffuse * max(0., dot(N, L));' - ,' color += specular.rgb * pow(max(0., dot(R, L)), specular.a);' - ,' gl_FragColor = vec4(pow(color, vec3(.45,.45,.45)), alpha);' + ,' vec3 color = ambient;' + ,' vec3 W = vec3(0.,0.,-1.);' + ,' vec3 N = normalize(vNormal);' + ,' vec3 R = W - 2. * N * dot(N, W);' + ,' for (int i = 0 ; i < 3 ; i++) {' + ,' vec3 Lrgb = L_rgb[i];' + ,' vec3 Ldir = normalize(L_dir[i]);' + ,' float D = dot(N, Ldir);' + ,' float S = dot(R, Ldir);' + ,' color += Lrgb * ( diffuse * mix(max(0.,D),max(0.,.5+.5*D),.5) +' + ,' specular.rgb * pow(max(0., S), specular.a) );' + ,' }' + ,' gl_FragColor = vec4(sqrt(color), alpha);' ,'}' ].join("\n"); diff --git a/sketch.js b/sketch.js index 283f3b3..c095ebd 100644 --- a/sketch.js +++ b/sketch.js @@ -871,9 +871,11 @@ this.shaderMaterial = function() { var material = shaderMaterial(this.vertexShader, this.fragmentShader); - material.setUniform('ambient' , [.1,.1,.1]); - material.setUniform('diffuse' , [.3,.3,.3]); - material.setUniform('specular', [.5,.5,.5,20]); + material.setUniform('ambient' , [.025,.025,.025]); + material.setUniform('diffuse' , [.2,.2,.2]); + material.setUniform('specular', [.5,.5,.5,10]); + material.setUniform('L_dir', [[ 1.0, 1.0, 0.5], [-1.0,-0.5,-1.0], [ 0.0,-1.0,-1.2]]); + material.setUniform('L_rgb', [[ 1.0, 1.0, 1.0], [ 0.2, 0.2, 0.2], [ 0.2, 0.2, 0.2]]); return material; } From 4eedbf25289509cc5179bebf8498fcf433da1ec9 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Sun, 9 Nov 2014 13:57:38 -0500 Subject: [PATCH 06/30] Got copySketch to work with sketch.createMesh, via small change in g.js. --- chroma-key.js | 1 - g.js | 2 ++ sketches/c2s.js | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/chroma-key.js b/chroma-key.js index 8f41d9c..0a1a873 100644 --- a/chroma-key.js +++ b/chroma-key.js @@ -210,7 +210,6 @@ function ChromaKeyedVideo() gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.video); } - gl.useProgram(shaderProgram); // recalc geometry stuff diff --git a/g.js b/g.js index 65cd035..566156e 100644 --- a/g.js +++ b/g.js @@ -2975,6 +2975,8 @@ console.log(lo + " " + hi); } addSketch(s.clone()); + if (sk().createMesh !== undefined) + sk().mesh = undefined; sk().sketchProgress = 1; sk().sketchState = 'finished'; diff --git a/sketches/c2s.js b/sketches/c2s.js index 8c203b1..b5d7d92 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -9,7 +9,7 @@ function C2S() { //this.setUniform('diffuse', [.5,0,0]); } this.createMesh = function() { - return new THREE.Mesh(globeGeometry(32,16), this.shaderMaterial()); + return new THREE.Mesh(globeGeometry(40,20), this.shaderMaterial()); } } C2S.prototype = new SketchTo3D; From e1b2e689a65e2d039d0f090f5ae35ce6955e5619 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Sun, 9 Nov 2014 17:33:23 -0500 Subject: [PATCH 07/30] Got line-drawing fade-out working properly for sketches with a createMesh method. Renamed sketches/abacus.js to sketches/zabacus.js to work around a timing bug on load. Made variable isMakingGlyph a member of Sketch, rather than a global variable. --- four.js | 16 +++--- g.js | 13 ++--- sketch.js | 17 ++++--- sketchPage.js | 10 +++- sketches/c2s.js | 2 +- sketches/diagram.js | 2 +- sketches/s2c.js | 2 +- sketches/zabacus.js | 121 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 155 insertions(+), 28 deletions(-) create mode 100644 sketches/zabacus.js diff --git a/four.js b/four.js index 30bc761..9ca94eb 100644 --- a/four.js +++ b/four.js @@ -77,6 +77,7 @@ function cylinderGeometry(n) { return new THREE.CylinderGeometry(1, 1, 2, n, 1, false); } function globeGeometry(m,n, p0,p1, t0,t1) { return new THREE.SphereGeometry(1, m,n, p0,p1, t0,t1); } function latheGeometry(points, n) { return new THREE.LatheGeometry(points, n); } + function nullGeometry() { return new THREE.Geometry(); } function openCylinderGeometry(n) { return new THREE.CylinderGeometry(1, 1, 2, n, 1, true); } function planeGeometry(n) { return new THREE.PlaneGeometry(2,2,n,n); } function torusGeometry(r, m, n) { return new THREE.TorusGeometry(1, r, m, n); } @@ -570,20 +571,19 @@ var defaultFragmentShader = [ 'uniform vec3 ambient;' ,'uniform vec3 diffuse;' ,'uniform vec4 specular;' - ,'uniform vec3 L_rgb[3];' - ,'uniform vec3 L_dir[3];' + ,'uniform vec3 Lrgb[3];' + ,'uniform vec3 Ldir[3];' ,'void main() {' ,' vec3 color = ambient;' ,' vec3 W = vec3(0.,0.,-1.);' ,' vec3 N = normalize(vNormal);' ,' vec3 R = W - 2. * N * dot(N, W);' ,' for (int i = 0 ; i < 3 ; i++) {' - ,' vec3 Lrgb = L_rgb[i];' - ,' vec3 Ldir = normalize(L_dir[i]);' - ,' float D = dot(N, Ldir);' - ,' float S = dot(R, Ldir);' - ,' color += Lrgb * ( diffuse * mix(max(0.,D),max(0.,.5+.5*D),.5) +' - ,' specular.rgb * pow(max(0., S), specular.a) );' + ,' vec3 L = normalize(Ldir[i]);' + ,' float D = dot(N, L);' + ,' float S = dot(R, L);' + ,' color += Lrgb[i] * ( diffuse * mix(max(0.,D),max(0.,.5+.5*D),.5) +' + ,' specular.rgb * pow(max(0., S), specular.a) );' ,' }' ,' gl_FragColor = vec4(sqrt(color), alpha);' ,'}' diff --git a/g.js b/g.js index 566156e..ab72867 100644 --- a/g.js +++ b/g.js @@ -13,7 +13,6 @@ var isAltPressed = false; var isBottomGesture = false; var isExpertMode = true; - var isMakingGlyph = false; var isMouseOverBackground = true; var isShowing2DMeshEdges = false; var isShowingMeshEdges = false; @@ -38,8 +37,8 @@ // SOMETIMES WE NEED TO SET A CUSTOM HEIGHT TO MAKE THINGS WORK WITH A PARTICULAR PROJECTOR. //function height() { return 640; } - //function height() { return 720; } - function height() { return 800; } + function height() { return 720; } + //function height() { return 800; } // TRANSPARENT INK IN THE DEFAULT PEN COLOR. @@ -408,7 +407,7 @@ if (! isk()) return; - if (isMakingGlyph) { + if (sk().isMakingGlyph) { if (! (sk() instanceof Sketch2D)) y = -y; buildTrace(glyphTrace, x, y, isLine); @@ -956,9 +955,9 @@ // CREATE GLYPH SHAPE INFO. glyphTrace = []; - isMakingGlyph = true; + sk().isMakingGlyph = true; sk().renderWrapper(0.02); - isMakingGlyph = false; + sk().isMakingGlyph = undefined; // REGISTER THE GLYPH. @@ -3309,8 +3308,6 @@ console.log(lo + " " + hi); } */ } - - this.mesh = null; } GeometrySketch.prototype = new SimpleSketch; diff --git a/sketch.js b/sketch.js index c095ebd..227c221 100644 --- a/sketch.js +++ b/sketch.js @@ -80,7 +80,7 @@ } this.setRenderMatrix = function(mat) { var D = norm(vecDiff(m.transform([0,0,0]), m.transform([1,0,0]))) * this.xyz[2]; - var s = .24 * width(); + var s = .381872 * height(); var p = this.toPixel([0,0,0]); mat.identity(); @@ -111,9 +111,9 @@ return (y - this.y2D) / this.scale(); } this.duringSketch = function(callbackFunction) { - if (this.sketchProgress < 1) { + if (this.createMesh !== null ? this.glyphTransition < 0.5 : this.sketchProgress < 1) { _g.save(); - _g.globalAlpha = 1 - this.styleTransition; + _g.globalAlpha = 1 - this.styleTransition; this.duringSketchCallbackFunction = callbackFunction; this.duringSketchCallbackFunction(); _g.restore(); @@ -618,10 +618,11 @@ _g.save(); m.save(); this.render(elapsed); - if (! isMakingGlyph && this.createMesh !== undefined) - this.updateMesh(); m.restore(); _g.restore(); + if (this.isMakingGlyph === undefined && this.createMesh !== undefined) { + this.updateMesh(); + } } this.sc = 1; this.scale = function(value) { @@ -874,8 +875,8 @@ material.setUniform('ambient' , [.025,.025,.025]); material.setUniform('diffuse' , [.2,.2,.2]); material.setUniform('specular', [.5,.5,.5,10]); - material.setUniform('L_dir', [[ 1.0, 1.0, 0.5], [-1.0,-0.5,-1.0], [ 0.0,-1.0,-1.2]]); - material.setUniform('L_rgb', [[ 1.0, 1.0, 1.0], [ 0.2, 0.2, 0.2], [ 0.2, 0.2, 0.2]]); + material.setUniform('Ldir', [[ 1.0, 1.0, 0.5], [-1.0,-0.5,-1.0], [ 0.0,-1.0,-1.2]]); + material.setUniform('Lrgb', [[ 1.0, 1.0, 1.0], [ 0.1, 0.1, 0.1], [ 0.1, 0.1, 0.1]]); return material; } @@ -905,7 +906,7 @@ // SET OPACITY. - var alpha = max(0, 2 * this.glyphTransition - 1) * + var alpha = max(0, this.glyphTransition) * (this.fadeAway == 0 ? 1 : this.fadeAway) * (isDef(this.alpha) ? this.alpha : 1); this.mesh.material.transparent = alpha < 1; diff --git a/sketchPage.js b/sketchPage.js index ef23304..0adf2a9 100644 --- a/sketchPage.js +++ b/sketchPage.js @@ -1718,7 +1718,15 @@ var sketchToDelete = null; } if (sk().sketchTrace != null && sk().sketchState != 'finished') { - morphSketchToGlyphSketch(); + if (sk().createMesh !== undefined) { + var alpha = 1 - sk().glyphTransition; + if (alpha > 0) { + _g.globalAlpha = alpha * alpha; + morphSketchToGlyphSketch(); + } + } + else + morphSketchToGlyphSketch(); var rate = sk().glyphTransition < 0.5 ? 1 : 1.5; sk().glyphTransition = min(1, sk().glyphTransition + rate * elapsed); diff --git a/sketches/c2s.js b/sketches/c2s.js index b5d7d92..39c1a5d 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -12,6 +12,6 @@ function C2S() { return new THREE.Mesh(globeGeometry(40,20), this.shaderMaterial()); } } -C2S.prototype = new SketchTo3D; +C2S.prototype = new Sketch; addSketchType("C2S"); diff --git a/sketches/diagram.js b/sketches/diagram.js index a1baaf3..63ba9a0 100644 --- a/sketches/diagram.js +++ b/sketches/diagram.js @@ -66,7 +66,7 @@ var w = this.width; var h = this.height; - if (isMakingGlyph) h = w; + if (this.isMakingGlyph !== undefined) h = w; var sel = (this.selection + 1000 * this.labels.length) % this.labels.length; diff --git a/sketches/s2c.js b/sketches/s2c.js index 06e4d4b..3e339ef 100644 --- a/sketches/s2c.js +++ b/sketches/s2c.js @@ -10,6 +10,6 @@ function S2C() { return new THREE.Mesh(cubeGeometry(), this.shaderMaterial()); } } -S2C.prototype = new SketchTo3D; +S2C.prototype = new Sketch; addSketchType("S2C"); diff --git a/sketches/zabacus.js b/sketches/zabacus.js new file mode 100644 index 0000000..75de70b --- /dev/null +++ b/sketches/zabacus.js @@ -0,0 +1,121 @@ +function Abacus() { + this.labels = "abacus".split(' '); + + this.digits = [0,0,0]; + this.digitsIndex = 0; + this.stones = null; + + this.mouseDown = function(x, y) { + var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); + var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); + this.digitsIndex = xx < -.4 ? 0 : xx < .4 ? 1 : 2; + this.xx = xx; + this.yy = yy; + } + this.mouseDrag = function(x, y) { + var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); + var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); + var index = this.digitsIndex; + var value = this.digits[index] - (yy - this.yy) / 20; + this.digits[index] = max(0, min(9.99, value)); + } + this.mouseUp = function(x, y) { + var xx = (x - this.xlo + x - this.xhi) / (this.xhi - this.xlo); + var yy = (y - this.ylo + y - this.yhi) / (this.yhi - this.ylo); + if (abs(xx - this.xx) > abs(yy - this.yy)) + this.animateAbacus = this.animateAbacus === undefined ? true : undefined; + } + + this.render = function(elapsed) { + this.duringSketch(function() { + mCurve( [[-1,1],[1,1],[1,-1],[-1,-1],[-1,1]] ); + mCurve( [[ -1,.5],[ 1,.5]] ); + mCurve( [[-.6, 1],[-.6,-1]] ); + mCurve( [[ 0, 1],[ 0,-1]] ); + mCurve( [[ .6, 1],[ .6,-1]] ); + }); + + this.afterSketch(function() { + + if (this.animateAbacus !== undefined) + this.digits[2] += 16 * elapsed; + + for (var index = 2 ; index >= 0 ; index--) + if (this.digits[index] >= 10) { + this.digits[index] -= 10; + if (index > 0) + this.digits[index-1]++; + } + else if (this.digits[index] < 0) { + this.digits[index] += 10; + if (this.digits[index] > 0) + this.digits[index-1]--; + } + + if (isCodeWidget && this == codeSketch) { + this.oldDigits = this.newDigits; + this.newDigits = floor(this.digits[0]) + "" + + floor(this.digits[1]) + "" + + floor(this.digits[2]) ; + if (this.newDigits != this.oldDigits) { + this.code = [["", this.newDigits]]; + toggleCodeWidget(); + toggleCodeWidget(); + } + } + + if (this.mesh !== undefined) + for (var i = 0 ; i < this.stones.children.length ; i++) { + var n = i % 5; + var d = this.digits[floor(i/5)]; + this.stones.children[i].getMatrix().identity(); + if (n==0 ? d % 5 >= 4 : + n==1 ? d % 5 >= 3 : + n==2 ? d % 5 >= 2 : + n==3 ? d % 5 >= 1 : d >= 5) + this.stones.children[i].getMatrix().translate(0, .1, 0); + } + }); + } + + this.createMesh = function() { + var abacus = new THREE.Mesh(); + + abacus.addCylinder(16).getMatrix().translate( 0, 1,0).rotateZ(PI/2).scale(.05,1 ,.1); + abacus.addCylinder(16).getMatrix().translate( 0,.45,0).rotateZ(PI/2).scale(.10,1 ,.1); + abacus.addCylinder(16).getMatrix().translate( 0, -1,0).rotateZ(PI/2).scale(.05,1 ,.1); + abacus.addCylinder(16).getMatrix().translate(-1, 0,0) .scale(.05,1 ,.1); + abacus.addCylinder(16).getMatrix().translate( 1, 0,0) .scale(.05,1 ,.1); + + abacus.addGlobe(16, 8).getMatrix().translate(-1, 1,0) .scale(.05,.05,.1); + abacus.addGlobe(16, 8).getMatrix().translate( 1, 1,0) .scale(.05,.05,.1); + abacus.addGlobe(16, 8).getMatrix().translate(-1,-1,0) .scale(.05,.05,.1); + abacus.addGlobe(16, 8).getMatrix().translate( 1,-1,0) .scale(.05,.05,.1); + + abacus.addCylinder(16).getMatrix().translate(-.6,0,0).scale(.03,1,.03); + abacus.addCylinder(16).getMatrix().translate( 0,0,0).scale(.03,1,.03); + abacus.addCylinder(16).getMatrix().translate( .6,0,0).scale(.03,1,.03); + + this.stones = abacus.addNode(); + for (var i = 0 ; i < 3 ; i++) { + var x = -.6 + .6 * i; + for (var j = 0 ; j < 6 ; j++) + if (j != 4) { + var y = (j == 5 ? 1.5 : j * .3) - .8; + this.stones.addNode().addGlobe(24,12).getMatrix().translate(x,y,0).scale(.2,.155,.2); + } + } + + abacus.setMaterial(this.shaderMaterial()); +/* + abacus.setMaterial(new phongMaterial().setAmbient(.2,.1,.05) + .setDiffuse(.2,.1,.05) + .setSpecular(.2,.2,.2,20)); +*/ + return abacus; + } +} +Abacus.prototype = new Sketch; +addSketchType("Abacus"); + + From 638c1caf6045a9afc24e4153435241edcd6c5be8 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Mon, 10 Nov 2014 11:32:10 -0500 Subject: [PATCH 08/30] Proper checking for .js extension when loading files from sketches/*.js. Added ignoredSketches array, to we can temporarily turn off some sketches. --- four.js | 1 - g.js | 32 +++++++++++++++++++++++--------- utility.js | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/four.js b/four.js index 9ca94eb..ef919b9 100644 --- a/four.js +++ b/four.js @@ -491,7 +491,6 @@ function isValidShader(type, string) { gl().shaderSource(shader, string); gl().compileShader(shader); var status = gl().getShaderParameter(shader, gl().COMPILE_STATUS); - console.log(status); return status; }; diff --git a/g.js b/g.js index ab72867..914a4ab 100644 --- a/g.js +++ b/g.js @@ -1,4 +1,7 @@ + // Do not load any of the following sketches. + var ignoredSketches = 'reflect'.split(' '); + // GLOBAL VARIABLES. var PMA = 8; // PIE MENU NUMBER OF ANGLES @@ -600,8 +603,26 @@ lsRequest.onloadend = function () { if (lsRequest.responseText != "") { var ls = lsRequest.responseText.trim().split("\n"); - for (var i = 0; i < ls.length; i++) - importSketch(ls[i]); + for (var n = 0; n < ls.length; n++) { + var filename = ls[n]; + + // Ignore files with no extension. + var iDot = filename.indexOf('.'); + if (iDot < 0) + continue; + + // Ignore files that do not have the .js extension. + var extension = filename.substring(iDot, filename.length); + if (extension !== '.js') + continue; + + // Ignore the ignoredSketches. + var name = filename.substring(0, iDot); + if (getIndex(ignoredSketches, name) >= 0) + continue; + + importSketch(filename); + } } } lsRequest.send(); @@ -611,13 +632,6 @@ var sketchScript = {}; function importSketch(filename) { - - // IF A FILE IS CURRENTLY BEING EDITED, DON'T TRY TO LOAD ITS SWAP FILE. - - var len = filename.length; - if (filename.substring(len-3, len) == "swp") - return; - var sketchRequest = new XMLHttpRequest(); sketchRequest.open("GET", "sketches/" + filename); sketchRequest.filename = filename; diff --git a/utility.js b/utility.js index affaf0c..1e8ac7c 100644 --- a/utility.js +++ b/utility.js @@ -544,7 +544,7 @@ function getIndex(arr, obj) { var i = arr.length; - while (--i >= 0 && arr[i] != obj) ; + while (--i >= 0 && arr[i] !== obj) ; return i; } From 5983b2e9a52de069ecf1e18745fcc709a4a81210 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Mon, 10 Nov 2014 22:10:20 -0500 Subject: [PATCH 09/30] Corrected ratio between panning on canvas and translation of 3D render. --- g.js | 8 ++++---- utility.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/g.js b/g.js index 914a4ab..141c332 100644 --- a/g.js +++ b/g.js @@ -40,8 +40,8 @@ // SOMETIMES WE NEED TO SET A CUSTOM HEIGHT TO MAKE THINGS WORK WITH A PARTICULAR PROJECTOR. //function height() { return 640; } - function height() { return 720; } - //function height() { return 800; } + //function height() { return 720; } + function height() { return 800; } // TRANSPARENT INK IN THE DEFAULT PEN COLOR. @@ -2123,8 +2123,8 @@ console.log(lo + " " + hi); // PAN 3D OBJECTS TOO - root.position.x = _g.panX / (0.391 * height()); - root.position.y = -_g.panY / (0.391 * height()); + root.position.x = _g.panX / (0.3819 * height()); + root.position.y = -_g.panY / (0.3819 * height()); if (sketchPage.isWhiteboard) { color(backgroundColor); diff --git a/utility.js b/utility.js index 1e8ac7c..dd51b3c 100644 --- a/utility.js +++ b/utility.js @@ -3,7 +3,7 @@ // CHECKING FOR SYNTAX ERRORS IN JAVASCRIPT CODE. - function findSyntaxError( code ) { + function findSyntaxError( code ) { var error = []; var save_onerror = onerror; onerror = function(errorMsg, url, lineNumber) { From b117f325db50b343d497d031c1bc035befe12b44 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Mon, 10 Nov 2014 23:40:09 -0500 Subject: [PATCH 10/30] Allow editing of sketch scripts. --- g.js | 8 ++++++++ widgets.js | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/g.js b/g.js index 141c332..7fcd476 100644 --- a/g.js +++ b/g.js @@ -664,6 +664,14 @@ function gStart() { +/* +var harry = { fred: 10 }; +console.log(harry.fred); +harry._foobar = function() { this.fred = 20; } +eval('harry._foobar()'); +console.log(harry.fred); +*/ + preLoadObjs(); // PREVENT DOUBLE CLICK FROM SELECTING THE CANVAS: diff --git a/widgets.js b/widgets.js index f20190e..23ee74c 100644 --- a/widgets.js +++ b/widgets.js @@ -665,6 +665,9 @@ FOR WHEN WE HAVE DRAW_PATH SHORTCUT: ////////////////////////////// CODE TEXT EDITOR ////////////////////////////////// + function innerCode(src) { + } + var codeElement, codeSelector, codeTextArea, @@ -674,17 +677,23 @@ FOR WHEN WE HAVE DRAW_PATH SHORTCUT: updateF(); }, updateF = function() { - codeSketch.evalCode(codeTextArea.value); + var text = codeTextArea.value; if (isCodeScript()) { + var i = text.indexOf('{'); + var j = text.lastIndexOf('}'); + codeSketch._temporaryFunction = new Function(text.substring(i + 1, j)); + codeSketch._temporaryFunction(); } else if (code() != null) { var index = codeSelector.selectedIndex; - code()[index][1] = codeTextArea.value; + code()[index][1] = text; codeSketch.selectedIndex = index; if (code()[index][2] !== undefined) { - codeSketch.temporaryFunction = code()[index][2]; - codeSketch.temporaryFunction(); + codeSketch._temporaryFunction = code()[index][2]; + codeSketch._temporaryFunction(); } + else + codeSketch.evalCode(text); } }; From 6d68b0b5383a9687aae84020cb4f62cc433f2d4e Mon Sep 17 00:00:00 2001 From: kenperlin Date: Wed, 12 Nov 2014 07:54:07 -0500 Subject: [PATCH 11/30] Added capability for procedural vertex displacement. Capability for dropping color swatch onto 3D object in new version of shaders. --- four.js | 231 ++++++++++++++++++++++++++---------------------- sketch.js | 16 +++- sketchPage.js | 3 +- sketches/c2s.js | 2 +- widgets.js | 4 + 5 files changed, 145 insertions(+), 111 deletions(-) diff --git a/four.js b/four.js index ef919b9..76357e0 100644 --- a/four.js +++ b/four.js @@ -19,13 +19,13 @@ if (this.uniforms[name] !== undefined) { var val = src; if (Array.isArray(src)) { - if (! Array.isArray(src[0])) - val = toVec(src); + if (! Array.isArray(src[0])) + val = toVec(src); else { - val = []; - for (var i = 0 ; i < src.length ; i++) - val.push(toVec(src[i])); - } + val = []; + for (var i = 0 ; i < src.length ; i++) + val.push(toVec(src[i])); + } } this.uniforms[name].value = val; } @@ -169,7 +169,7 @@ THREE.Object3D.prototype.findBoundsWorld = function(bb) { if (bb === undefined) { this.updateMatrixWorld(); - bb = [10000,10000,10000,-10000,-10000,-10000]; + bb = [10000,10000,10000,-10000,-10000,-10000]; } this.geometry.matrixWorld = this.matrixWorld; this.geometry.expandBoundsWorld(bb); @@ -285,16 +285,16 @@ var n0 = this.faces[edge[0]].viewNormal; var n1 = this.faces[edge[1]].viewNormal; - if ((n0.z >= -0.0001 && n1.z <= 0.0001) || - (n0.z <= 0.0001 && n1.z >= -0.0001) || - (n0.dot(n1) < 0.5)) - { - // console.log("v dot = " + (n0.dot(n1).toFixed(6)) + ", " + n0.z.toFixed(6) + ", " + n1.z.toFixed(6)); - visibleEdges.push(edge[2]); - } - else { - // console.log("nv dot = " + (n0.dot(n1).toFixed(6)) + ", " + n0.z.toFixed(6) + ", " + n1.z.toFixed(6)); - } + if ((n0.z >= -0.0001 && n1.z <= 0.0001) || + (n0.z <= 0.0001 && n1.z >= -0.0001) || + (n0.dot(n1) < 0.5)) + { + // console.log("v dot = " + (n0.dot(n1).toFixed(6)) + ", " + n0.z.toFixed(6) + ", " + n1.z.toFixed(6)); + visibleEdges.push(edge[2]); + } + else { + // console.log("nv dot = " + (n0.dot(n1).toFixed(6)) + ", " + n0.z.toFixed(6) + ", " + n1.z.toFixed(6)); + } } // console.log("fve: hidden point count = " + ___hiddenpoint_count.toFixed(0)); @@ -329,10 +329,10 @@ THREE.Geometry.prototype.isHiddenPoint = function(p) { for (var n = 0 ; n < this.faces.length ; n++) - if (this.isPointHiddenByFace(p, n)) { - ___hiddenpoint_count++; - return true; - } + if (this.isPointHiddenByFace(p, n)) { + ___hiddenpoint_count++; + return true; + } return false; } @@ -340,8 +340,8 @@ THREE.Geometry.prototype.isPointHiddenByFace = function(p, n) { var face = this.faces[n]; return isPointHiddenByTriangle(p, this.vertexWorld(face.a), - this.vertexWorld(face.b), - this.vertexWorld(face.c)); + this.vertexWorld(face.b), + this.vertexWorld(face.c)); } // Find out whether point p is hidden by triangle a,b,c. @@ -507,17 +507,17 @@ function addUniforms(material, string) { if (words[0] == 'uniform') { var type = words[1]; var name = words[2]; - var j = name.indexOf('['); - if (j >= 0) - name = name.substring(0, j); + var j = name.indexOf('['); + if (j >= 0) + name = name.substring(0, j); for (var n = 0 ; n < typeInfo.length ; n += 3) { if (type == typeInfo[n]) { - var key = typeInfo[n+1]; - var val = typeInfo[n+2]; - if (j >= 0) { - key += 'v'; - val = '[]'; - } + var key = typeInfo[n+1]; + var val = typeInfo[n+2]; + if (j >= 0) { + key += 'v'; + val = '[]'; + } material.uniforms[name] = { type: key, value: val }; break; } @@ -527,17 +527,20 @@ function addUniforms(material, string) { } } -function formFragmentShader(string) { - - // PREPEND THE HEADER OF PREDEFINED THINGS: +// PREPEND THE HEADER OF PREDEFINED THINGS TO A VERTEX SHADER: +function formVertexShader(string) { + return vertexShaderHeader.concat(string); +} +// PREPEND THE HEADER OF PREDEFINED THINGS TO A FRAGMENT SHADER: +function formFragmentShader(string) { return fragmentShaderHeader.concat(string); } function shaderMaterial(vertexShader, fragmentShaderString) { var material = new THREE.ShaderMaterial({ uniforms: {}, - vertexShader: vertexShader, + vertexShader: formVertexShader(vertexShader), }); var u = "alpha mx my mz pixelSize selectedIndex time x y z".split(' '); @@ -557,12 +560,18 @@ var defaultVertexShader = [ ,'varying float dy;' ,'varying vec3 vPosition;' ,'varying vec3 vNormal;' + ,'float displace(vec3 p) { return 0. /* 0.1 * noise(3. * p + vec3(0.,-time,0.)) */; }' ,'void main() {' ,' dx = 2. * uv.x - 1.;' ,' dy = 2. * uv.y - 1.;' ,' vNormal = normalize((modelViewMatrix * vec4(normal, 0.)).xyz);' ,' vPosition = position*.03;' - ,' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);' + ,' float _p0 = displace(position);' + ,' float _px = displace(position + vec3(epsilon, 0.0, 0.0));' + ,' float _py = displace(position + vec3(0.0, epsilon, 0.0));' + ,' float _pz = displace(position + vec3(0.0, 0.0, epsilon));' + ,' vNormal = normalize(vNormal + vec3(_px - _p0, _py - _p0, _pz - _p0) / epsilon);' + ,' gl_Position = projectionMatrix * modelViewMatrix * vec4(position * (1. - _p0), 1.);' ,'}' ].join("\n"); @@ -590,74 +599,84 @@ var defaultFragmentShader = [ // DEFINES FRAGMENT SHADER FUNCTIONS noise() and turbulence() AND VARS x, y, time and alpha. -var fragmentShaderHeader = ["\ - precision mediump float;\ - vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\ - vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\ - vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }\ - vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }\ - vec3 fade(vec3 t) { return t*t*t*(t*(t*6.0-15.0)+10.0); }\ - float noise(vec3 P) {\ - vec3 i0 = mod289(floor(P)), i1 = mod289(i0 + vec3(1.0));\ - vec3 f0 = fract(P), f1 = f0 - vec3(1.0), f = fade(f0);\ - vec4 ix = vec4(i0.x, i1.x, i0.x, i1.x), iy = vec4(i0.yy, i1.yy);\ - vec4 iz0 = i0.zzzz, iz1 = i1.zzzz;\ - vec4 ixy = permute(permute(ix) + iy), ixy0 = permute(ixy + iz0), ixy1 = permute(ixy + iz1);\ - vec4 gx0 = ixy0 * (1.0 / 7.0), gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;\ - vec4 gx1 = ixy1 * (1.0 / 7.0), gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;\ - gx0 = fract(gx0); gx1 = fract(gx1);\ - vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0), sz0 = step(gz0, vec4(0.0));\ - vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1), sz1 = step(gz1, vec4(0.0));\ - gx0 -= sz0 * (step(0.0, gx0) - 0.5); gy0 -= sz0 * (step(0.0, gy0) - 0.5);\ - gx1 -= sz1 * (step(0.0, gx1) - 0.5); gy1 -= sz1 * (step(0.0, gy1) - 0.5);\ - vec3 g0 = vec3(gx0.x,gy0.x,gz0.x), g1 = vec3(gx0.y,gy0.y,gz0.y),\ - g2 = vec3(gx0.z,gy0.z,gz0.z), g3 = vec3(gx0.w,gy0.w,gz0.w),\ - g4 = vec3(gx1.x,gy1.x,gz1.x), g5 = vec3(gx1.y,gy1.y,gz1.y),\ - g6 = vec3(gx1.z,gy1.z,gz1.z), g7 = vec3(gx1.w,gy1.w,gz1.w);\ - vec4 norm0 = taylorInvSqrt(vec4(dot(g0,g0), dot(g2,g2), dot(g1,g1), dot(g3,g3)));\ - vec4 norm1 = taylorInvSqrt(vec4(dot(g4,g4), dot(g6,g6), dot(g5,g5), dot(g7,g7)));\ - g0 *= norm0.x; g2 *= norm0.y; g1 *= norm0.z; g3 *= norm0.w;\ - g4 *= norm1.x; g6 *= norm1.y; g5 *= norm1.z; g7 *= norm1.w;\ - vec4 nz = mix(vec4(dot(g0, vec3(f0.x, f0.y, f0.z)), dot(g1, vec3(f1.x, f0.y, f0.z)),\ - dot(g2, vec3(f0.x, f1.y, f0.z)), dot(g3, vec3(f1.x, f1.y, f0.z))),\ - vec4(dot(g4, vec3(f0.x, f0.y, f1.z)), dot(g5, vec3(f1.x, f0.y, f1.z)),\ - dot(g6, vec3(f0.x, f1.y, f1.z)), dot(g7, vec3(f1.x, f1.y, f1.z))), f.z);\ - return 2.2 * mix(mix(nz.x,nz.z,f.y), mix(nz.y,nz.w,f.y), f.x);\ - }\ - float noise(vec2 P) { return noise(vec3(P, 0.0)); }\ - float fractal(vec3 P) {\ - float f = 0., s = 1.;\ - for (int i = 0 ; i < 9 ; i++) {\ - f += noise(s * P) / s;\ - s *= 2.;\ - P = vec3(.866 * P.x + .5 * P.z, P.y + 100., -.5 * P.x + .866 * P.z);\ - }\ - return f;\ - }\ - float turbulence(vec3 P) {\ - float f = 0., s = 1.;\ - for (int i = 0 ; i < 9 ; i++) {\ - f += abs(noise(s * P)) / s;\ - s *= 2.;\ - P = vec3(.866 * P.x + .5 * P.z, P.y + 100., -.5 * P.x + .866 * P.z);\ - }\ - return f;\ - }\ - uniform float alpha;\ - varying float dx;\ - varying float dy;\ - uniform float mx;\ - uniform float my;\ - uniform float mz;\ - uniform float pixelSize;\ - uniform float selectedIndex;\ - uniform float time;\ - varying vec3 vNormal;\ - varying vec3 vPosition;\ - uniform float x;\ - uniform float y;\ - uniform float z;\ -"].join("\n"); +var sharedHeader = [ + 'precision highp float;' +,'float epsilon = .001;' +,'vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }' +,'vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }' +,'vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }' +,'vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }' +,'vec3 fade(vec3 t) { return t*t*t*(t*(t*6.0-15.0)+10.0); }' +,'float noise(vec3 P) {' +,' vec3 i0 = mod289(floor(P)), i1 = mod289(i0 + vec3(1.0));' +,' vec3 f0 = fract(P), f1 = f0 - vec3(1.0), f = fade(f0);' +,' vec4 ix = vec4(i0.x, i1.x, i0.x, i1.x), iy = vec4(i0.yy, i1.yy);' +,' vec4 iz0 = i0.zzzz, iz1 = i1.zzzz;' +,' vec4 ixy = permute(permute(ix) + iy), ixy0 = permute(ixy + iz0), ixy1 = permute(ixy + iz1);' +,' vec4 gx0 = ixy0 * (1.0 / 7.0), gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;' +,' vec4 gx1 = ixy1 * (1.0 / 7.0), gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;' +,' gx0 = fract(gx0); gx1 = fract(gx1);' +,' vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0), sz0 = step(gz0, vec4(0.0));' +,' vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1), sz1 = step(gz1, vec4(0.0));' +,' gx0 -= sz0 * (step(0.0, gx0) - 0.5); gy0 -= sz0 * (step(0.0, gy0) - 0.5);' +,' gx1 -= sz1 * (step(0.0, gx1) - 0.5); gy1 -= sz1 * (step(0.0, gy1) - 0.5);' +,' vec3 g0 = vec3(gx0.x,gy0.x,gz0.x), g1 = vec3(gx0.y,gy0.y,gz0.y),' +,' g2 = vec3(gx0.z,gy0.z,gz0.z), g3 = vec3(gx0.w,gy0.w,gz0.w),' +,' g4 = vec3(gx1.x,gy1.x,gz1.x), g5 = vec3(gx1.y,gy1.y,gz1.y),' +,' g6 = vec3(gx1.z,gy1.z,gz1.z), g7 = vec3(gx1.w,gy1.w,gz1.w);' +,' vec4 norm0 = taylorInvSqrt(vec4(dot(g0,g0), dot(g2,g2), dot(g1,g1), dot(g3,g3)));' +,' vec4 norm1 = taylorInvSqrt(vec4(dot(g4,g4), dot(g6,g6), dot(g5,g5), dot(g7,g7)));' +,' g0 *= norm0.x; g2 *= norm0.y; g1 *= norm0.z; g3 *= norm0.w;' +,' g4 *= norm1.x; g6 *= norm1.y; g5 *= norm1.z; g7 *= norm1.w;' +,' vec4 nz = mix(vec4(dot(g0, vec3(f0.x, f0.y, f0.z)), dot(g1, vec3(f1.x, f0.y, f0.z)),' +,' dot(g2, vec3(f0.x, f1.y, f0.z)), dot(g3, vec3(f1.x, f1.y, f0.z))),' +,' vec4(dot(g4, vec3(f0.x, f0.y, f1.z)), dot(g5, vec3(f1.x, f0.y, f1.z)),' +,' dot(g6, vec3(f0.x, f1.y, f1.z)), dot(g7, vec3(f1.x, f1.y, f1.z))), f.z);' +,' return 2.2 * mix(mix(nz.x,nz.z,f.y), mix(nz.y,nz.w,f.y), f.x);' +,'}' +,'float noise(vec2 P) { return noise(vec3(P, 0.0)); }' +,'float fractal(vec3 P) {' +,' float f = 0., s = 1.;' +,' for (int i = 0 ; i < 9 ; i++) {' +,' f += noise(s * P) / s;' +,' s *= 2.;' +,' P = vec3(.866 * P.x + .5 * P.z, P.y + 100., -.5 * P.x + .866 * P.z);' +,' }' +,' return f;' +,'}' +,'float turbulence(vec3 P) {' +,' float f = 0., s = 1.;' +,' for (int i = 0 ; i < 9 ; i++) {' +,' f += abs(noise(s * P)) / s;' +,' s *= 2.;' +,' P = vec3(.866 * P.x + .5 * P.z, P.y + 100., -.5 * P.x + .866 * P.z);' +,' }' +,' return f;' +,'}' +].join('\n'); + +var vertexShaderHeader = [ + sharedHeader +,'uniform float time;' +].join('\n'); + +var fragmentShaderHeader = [ + sharedHeader +,'uniform float alpha;' +,'varying float dx;' +,'varying float dy;' +,'uniform float mx;' +,'uniform float my;' +,'uniform float mz;' +,'uniform float pixelSize;' +,'uniform float selectedIndex;' +,'uniform float time;' +,'varying vec3 vNormal;' +,'varying vec3 vPosition;' +,'uniform float x;' +,'uniform float y;' +,'uniform float z;' +].join('\n'); function createVisibleEdgesMesh(veds) { var material = new THREE.LineBasicMaterial(); @@ -671,7 +690,7 @@ var fragmentShaderHeader = ["\ for (var n = 0 ; n < edges.length ; n++) mesh.geometry.addLine(.007, geom.vertexWorld(edges[n][0]), - geom.vertexWorld(edges[n][1])); + geom.vertexWorld(edges[n][1])); } return mesh; diff --git a/sketch.js b/sketch.js index 227c221..1a47e27 100644 --- a/sketch.js +++ b/sketch.js @@ -863,7 +863,6 @@ } this.updateMesh = function() { if (this.createMesh !== undefined && this.mesh === undefined) { - if (this.vertexShader === undefined) this.vertexShader = defaultVertexShader; @@ -872,8 +871,6 @@ this.shaderMaterial = function() { var material = shaderMaterial(this.vertexShader, this.fragmentShader); - material.setUniform('ambient' , [.025,.025,.025]); - material.setUniform('diffuse' , [.2,.2,.2]); material.setUniform('specular', [.5,.5,.5,10]); material.setUniform('Ldir', [[ 1.0, 1.0, 0.5], [-1.0,-0.5,-1.0], [ 0.0,-1.0,-1.2]]); material.setUniform('Lrgb', [[ 1.0, 1.0, 1.0], [ 0.1, 0.1, 0.1], [ 0.1, 0.1, 0.1]]); @@ -900,6 +897,19 @@ } if (this.mesh !== undefined) { + // UPDATE MESH COLOR IF NEEDED. + + if (this.meshColorId !== this.colorId) { + var rgb = paletteRGB[this.colorId]; + var R = rgb[0] / 255; + var G = rgb[1] / 255; + var B = rgb[2] / 255; + this.mesh.material.setUniform('ambient' , [0.025 * R, 0.025 * G, 0.025 * B]); + this.mesh.material.setUniform('diffuse' , [0.2 * R, 0.2 * G, 0.2 * B]); + + this.meshColorId = this.colorId; + } + // SET MESH MATRIX TO MATCH SKETCH'S POSITION/ROTATION/SCALE. this.setRenderMatrix(this.mesh.getMatrix()); diff --git a/sketchPage.js b/sketchPage.js index 0adf2a9..99568aa 100644 --- a/sketchPage.js +++ b/sketchPage.js @@ -541,8 +541,9 @@ var sketchToDelete = null; bgActionEnd(x, y); bgClickCount = 0; } - else + else { bgActionUp(x, y); + } return; } diff --git a/sketches/c2s.js b/sketches/c2s.js index 39c1a5d..687a8fa 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -9,7 +9,7 @@ function C2S() { //this.setUniform('diffuse', [.5,0,0]); } this.createMesh = function() { - return new THREE.Mesh(globeGeometry(40,20), this.shaderMaterial()); + return new THREE.Mesh(globeGeometry(80,40), this.shaderMaterial()); } } C2S.prototype = new Sketch; diff --git a/widgets.js b/widgets.js index 23ee74c..428598f 100644 --- a/widgets.js +++ b/widgets.js @@ -679,6 +679,10 @@ FOR WHEN WE HAVE DRAW_PATH SHORTCUT: updateF = function() { var text = codeTextArea.value; if (isCodeScript()) { + + // EVAL THE PART OF SKETCH SCRIPT WITHIN { ... }, INSIDE CONTEXT OF codeSketch. + // THIS WILL REDEFINE THE SKETCH METHODS ONLY FOR THIS ONE INSTANCE. + var i = text.indexOf('{'); var j = text.lastIndexOf('}'); codeSketch._temporaryFunction = new Function(text.substring(i + 1, j)); From c83adbf5ae9e09abf370c765050e78da366c7689 Mon Sep 17 00:00:00 2001 From: kenperlin Date: Wed, 12 Nov 2014 08:37:31 -0500 Subject: [PATCH 12/30] Added sketches/torus.js. --- sketch.js | 3 ++- sketches/torus.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 sketches/torus.js diff --git a/sketch.js b/sketch.js index 1a47e27..8de6021 100644 --- a/sketch.js +++ b/sketch.js @@ -891,6 +891,7 @@ this.code = []; this.code.push(["fragmentShader", this.fragmentShader, this.updateFragmentShader]); + this.meshBounds = [[-1,-1],[1,1]]; this.mesh = this.createMesh(); root.add(this.mesh); this.is3D = true; @@ -943,7 +944,7 @@ // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. - this.extendBounds([-1,-1],[1,1]); + this.extendBounds(this.meshBounds[0], this.meshBounds[1]); } } this.value = null; diff --git a/sketches/torus.js b/sketches/torus.js new file mode 100644 index 0000000..687a8fa --- /dev/null +++ b/sketches/torus.js @@ -0,0 +1,17 @@ +function C2S() { + this.labels = "c2s".split(' '); + + this.render = function() { + this.duringSketch(function() { + mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); + }); + //this.setUniform('ambient', [.2,0,0]); + //this.setUniform('diffuse', [.5,0,0]); + } + this.createMesh = function() { + return new THREE.Mesh(globeGeometry(80,40), this.shaderMaterial()); + } +} +C2S.prototype = new Sketch; +addSketchType("C2S"); + From ced95d5b89ee310600a77fa2e3efaefeb9d47c7c Mon Sep 17 00:00:00 2001 From: kenperlin Date: Wed, 12 Nov 2014 08:57:23 -0500 Subject: [PATCH 13/30] Fixed an error in file naming of sketches/{c2s,torus}.js. --- sketches/c2s.js | 2 -- sketches/torus.js | 19 +++++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sketches/c2s.js b/sketches/c2s.js index 687a8fa..a97199f 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -5,8 +5,6 @@ function C2S() { this.duringSketch(function() { mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); }); - //this.setUniform('ambient', [.2,0,0]); - //this.setUniform('diffuse', [.5,0,0]); } this.createMesh = function() { return new THREE.Mesh(globeGeometry(80,40), this.shaderMaterial()); diff --git a/sketches/torus.js b/sketches/torus.js index 687a8fa..4c25297 100644 --- a/sketches/torus.js +++ b/sketches/torus.js @@ -1,17 +1,20 @@ -function C2S() { - this.labels = "c2s".split(' '); +function Torus() { + this.labels = "torus".split(' '); + this.r = .4; this.render = function() { this.duringSketch(function() { - mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); + var r = this.r; + mCurve(makeOval(-1-r, -1-r, 2+2*r, 2+2*r, 20, -PI/2, 3*PI/2)); + mCurve(makeOval(-1+r, -1+r, 2-2*r, 2-2*r, 20, -PI/2, 3*PI/2)); }); - //this.setUniform('ambient', [.2,0,0]); - //this.setUniform('diffuse', [.5,0,0]); } this.createMesh = function() { - return new THREE.Mesh(globeGeometry(80,40), this.shaderMaterial()); + var r = this.r; + this.meshBounds = [[-1-r,-1-r], [1+r,1+r]]; + return new THREE.Mesh(torusGeometry(r, 40,40), this.shaderMaterial()); } } -C2S.prototype = new Sketch; -addSketchType("C2S"); +Torus.prototype = new Sketch; +addSketchType("Torus"); From 1a1e2b4c321ac8bf96fe08d6704446fd036a8a5d Mon Sep 17 00:00:00 2001 From: kenperlin Date: Wed, 12 Nov 2014 09:15:08 -0500 Subject: [PATCH 14/30] In sketches.js, made it easier to define good meshBounds. Defined better meshBounds for sketches/torus.js. --- sketch.js | 9 +++++---- sketches/torus.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/sketch.js b/sketch.js index 8de6021..ebe0097 100644 --- a/sketch.js +++ b/sketch.js @@ -132,11 +132,11 @@ _g.restore(); } } - this.extendBounds = function(a, b) { + this.extendBounds = function(points) { this.afterSketch(function() { var saveStrokeStyle = _g.strokeStyle; color('rgba(0,0,0,.01)'); - mLine(a, b); + mCurve(points); _g.strokeStyle = saveStrokeStyle; }); } @@ -891,7 +891,8 @@ this.code = []; this.code.push(["fragmentShader", this.fragmentShader, this.updateFragmentShader]); - this.meshBounds = [[-1,-1],[1,1]]; + if (this.meshBounds == undefined) + this.meshBounds = [ [-1, -1] , [1, 1] ]; this.mesh = this.createMesh(); root.add(this.mesh); this.is3D = true; @@ -944,7 +945,7 @@ // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. - this.extendBounds(this.meshBounds[0], this.meshBounds[1]); + this.extendBounds(this.meshBounds); } } this.value = null; diff --git a/sketches/torus.js b/sketches/torus.js index 4c25297..aedb0cc 100644 --- a/sketches/torus.js +++ b/sketches/torus.js @@ -2,6 +2,13 @@ function Torus() { this.labels = "torus".split(' '); this.r = .4; + var R = 1 + this.r; + this.meshBounds = []; + for (var theta = 0 ; theta < TAU ; theta += TAU / 8) { + this.meshBounds.push([R * cos(theta), R * sin(theta), -this.r]); + this.meshBounds.push([R * cos(theta), R * sin(theta), this.r]); + } + this.render = function() { this.duringSketch(function() { var r = this.r; @@ -10,9 +17,7 @@ function Torus() { }); } this.createMesh = function() { - var r = this.r; - this.meshBounds = [[-1-r,-1-r], [1+r,1+r]]; - return new THREE.Mesh(torusGeometry(r, 40,40), this.shaderMaterial()); + return new THREE.Mesh(torusGeometry(this.r, 40, 40), this.shaderMaterial()); } } Torus.prototype = new Sketch; From a88ead14145dec4c58cf9654ae17670bc64aece0 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 10:52:06 -0500 Subject: [PATCH 15/30] Cleaned up format of sketches/ngon.js. --- sketches/ngon.js | 184 +++++++++++++++++++++++------------------------ 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/sketches/ngon.js b/sketches/ngon.js index e7d7f5e..e680ef5 100644 --- a/sketches/ngon.js +++ b/sketches/ngon.js @@ -1,108 +1,108 @@ +function NGon() { + this.labels = "triangle diamond pentagon hexagon".split(' '); - function NGon() { - var jNext = function(j, P) { return (j + 1) % P.length; } - var jPrev = function(j, P) { return (j - 1 + P.length) % P.length; } + var jNext = function(j, P) { return (j + 1) % P.length; } + var jPrev = function(j, P) { return (j - 1 + P.length) % P.length; } - this.fillMode = 0; - this.P = null; - this.mF = new M4(); - this.mI = new M4(); - this.labels = "triangle diamond pentagon hexagon".split(' '); - this.onSwipe = function(dx, dy) { - if (this.jP != -1) - return; - switch (pieMenuIndex(dx, dy)) { - case 0: - this.fillMode = (this.fillMode + 2) % 3; - break; - case 1: - break; - case 2: - this.fillMode = (this.fillMode + 1) % 3; - break; - case 3: - break; - } + this.fillMode = 0; + this.P = null; + this.mF = new M4(); + this.mI = new M4(); + this.onSwipe = function(dx, dy) { + if (this.jP != -1) + return; + switch (pieMenuIndex(dx, dy)) { + case 0: + this.fillMode = (this.fillMode + 2) % 3; + break; + case 1: + break; + case 2: + this.fillMode = (this.fillMode + 1) % 3; + break; + case 3: + break; } - this.mouseDown = function(x, y) { + } + this.mouseDown = function(x, y) { - this.standardView(this.mF); - this.standardViewInverse(this.mI); + this.standardView(this.mF); + this.standardViewInverse(this.mI); - // GRAB ANY VERTEX THAT IS WITHIN CLICK-DISTANCE FROM THE CURSOR. + // GRAB ANY VERTEX THAT IS WITHIN CLICK-DISTANCE FROM THE CURSOR. - this.jP = -1; - for (var i = 0 ; i < this.P.length ; i++) { - var p = this.mF.transform(this.P[i]); - if (len(p[0] - x, p[1] - y) <= clickSize) { - this.jP = i; - break; - } - } + this.jP = -1; + for (var i = 0 ; i < this.P.length ; i++) { + var p = this.mF.transform(this.P[i]); + if (len(p[0] - x, p[1] - y) <= clickSize) { + this.jP = i; + break; + } + } - // INSERT NEW VERTEX IF WITHIN CLICK-DISTANCE OF AN EDGE MIDPOINT. + // INSERT NEW VERTEX IF WITHIN CLICK-DISTANCE OF AN EDGE MIDPOINT. - if (this.jP == -1) - for (var i = 0 ; i < this.P.length ; i++) { - var j = (i + 1) % this.P.length; - var p = this.mF.transform(this.P[i]); - var q = this.mF.transform(this.P[j]); - var a = [ (p[0] + q[0]) / 2, (p[1] + q[1]) / 2 ]; - if (len(a[0] - x, a[1] - y) <= clickSize) { - this.P.splice(j, 0, this.mI.transform([ x, y ])); - this.jP = j; - break; - } - } - } - this.mouseDrag = function(x, y) { - if (this.jP >= 0) - this.P[this.jP] = this.mI.transform([ x, y ]); - } - this.mouseUp = function(x, y) { + if (this.jP == -1) + for (var i = 0 ; i < this.P.length ; i++) { + var j = (i + 1) % this.P.length; + var p = this.mF.transform(this.P[i]); + var q = this.mF.transform(this.P[j]); + var a = [ (p[0] + q[0]) / 2, (p[1] + q[1]) / 2 ]; + if (len(a[0] - x, a[1] - y) <= clickSize) { + this.P.splice(j, 0, this.mI.transform([ x, y ])); + this.jP = j; + break; + } + } + } + this.mouseDrag = function(x, y) { + if (this.jP >= 0) + this.P[this.jP] = this.mI.transform([ x, y ]); + } + this.mouseUp = function(x, y) { - // DELETE VERTEX IF IT HAS MOVED TO WITHIN CLICK-DISTANCE OF EDGE CONNECTING ITS NEIGHBORS. + // DELETE VERTEX IF IT HAS MOVED TO WITHIN CLICK-DISTANCE OF EDGE CONNECTING ITS NEIGHBORS. - if (this.jP >= 0) { - var j = this.jP; - var i = jPrev(j, this.P); - var k = jNext(j, this.P); + if (this.jP >= 0) { + var j = this.jP; + var i = jPrev(j, this.P); + var k = jNext(j, this.P); - var p = this.mF.transform(this.P[i]); - var q = this.mF.transform(this.P[j]); - var r = this.mF.transform(this.P[k]); - var a = [ (p[0] + r[0]) / 2, (p[1] + r[1]) / 2 ]; - if (len(a[0] - q[0], a[1] - q[1]) <= clickSize / 2) - this.P.splice(j, 1); - } + var p = this.mF.transform(this.P[i]); + var q = this.mF.transform(this.P[j]); + var r = this.mF.transform(this.P[k]); + var a = [ (p[0] + r[0]) / 2, (p[1] + r[1]) / 2 ]; + if (len(a[0] - q[0], a[1] - q[1]) <= clickSize / 2) + this.P.splice(j, 1); } - this.render = function() { - function makeNgon(n) { return makeOval(-1,-1,2,2, n, TAU/4, TAU/4 + TAU * (n-1) / n); } - if (! isNumeric(this.xlo)) { - switch (this.labels[this.selection]) { - case 'triangle': this.P = makeNgon(3); break; - case 'diamond' : this.P = makeNgon(4); break; - case 'pentagon': this.P = makeNgon(5); break; - case 'hexagon' : this.P = makeNgon(6); break; - } - } - switch (this.fillMode) { - case 0: - mClosedCurve(this.P); - break; - case 1: - var c = _g.strokeStyle; - color(scrimColor(0.25)); - mFillCurve(this.P); - color(c); - mClosedCurve(this.P); - break; - case 2: - mFillCurve(this.P); - break; + } + this.render = function() { + function makeNgon(n) { return makeOval(-1,-1,2,2, n, TAU/4, TAU/4 + TAU * (n-1) / n); } + if (! isNumeric(this.xlo)) { + switch (this.labels[this.selection]) { + case 'triangle': this.P = makeNgon(3); break; + case 'diamond' : this.P = makeNgon(4); break; + case 'pentagon': this.P = makeNgon(5); break; + case 'hexagon' : this.P = makeNgon(6); break; } } + switch (this.fillMode) { + case 0: + mClosedCurve(this.P); + break; + case 1: + var c = _g.strokeStyle; + color(scrimColor(0.25)); + mFillCurve(this.P); + color(c); + mClosedCurve(this.P); + break; + case 2: + mFillCurve(this.P); + break; + } } - NGon.prototype = new Sketch; - addSketchType("NGon"); +} +NGon.prototype = new Sketch; +addSketchType("NGon"); From ad179aaad94d44479903d541fe108d13602d6685 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 12:26:38 -0500 Subject: [PATCH 16/30] Fixed implementation of sketch.setRenderMatrix() so z rotation goes the correct way. --- sketch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sketch.js b/sketch.js index ebe0097..61d3646 100644 --- a/sketch.js +++ b/sketch.js @@ -92,7 +92,7 @@ var yy = min(1, 4 * this.rY * this.rY); mat.rotateX(PI * -this.rY); mat.rotateY(PI * this.rX * (1 - yy)); - mat.rotateZ(PI * this.rX * yy); + mat.rotateZ(PI * -this.rX * yy); mat.scale(D / s); } From 36d9963f31977b328f2d10cb3881d8781e8ec651 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 12:46:59 -0500 Subject: [PATCH 17/30] Fixed a bug whereby sketch.onClick did not override opening up fragment shader. --- sketchPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sketchPage.js b/sketchPage.js index 99568aa..c5e183e 100644 --- a/sketchPage.js +++ b/sketchPage.js @@ -741,7 +741,7 @@ var sketchToDelete = null; // CLICK ON A CODE SKETCH TO BRING UP ITS CODE. - if (bgClickCount == 0 && sk().code != null) { + if (bgClickCount == 0 && sk().isClick === undefined && sk().code != null) { if (isCodeWidget && codeSketch != sk()) toggleCodeWidget(); codeSketch = sk(); From ea33ebdf64e2ac38e32472600f5414600060fc18 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 13:21:15 -0500 Subject: [PATCH 18/30] Call sketch.update inside sketch.updateMesh(). --- sketch.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sketch.js b/sketch.js index 61d3646..0ab75ad 100644 --- a/sketch.js +++ b/sketch.js @@ -943,6 +943,9 @@ this.setUniform('alpha', alpha); } + if (this.update !== undefined) + this.update(); + // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. this.extendBounds(this.meshBounds); From 70e1a89722185635fc0070658a92264a33ace1cb Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 13:50:20 -0500 Subject: [PATCH 19/30] Renamed sketch.updateMesh() to sketch._updateMesh(). Added optional user callback sketch.updateMesh(). --- sketch.js | 8 ++++---- sketches/c2s.js | 2 ++ sketches/torus.js | 9 ++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/sketch.js b/sketch.js index 0ab75ad..3fedefa 100644 --- a/sketch.js +++ b/sketch.js @@ -621,7 +621,7 @@ m.restore(); _g.restore(); if (this.isMakingGlyph === undefined && this.createMesh !== undefined) { - this.updateMesh(); + this._updateMesh(); } } this.sc = 1; @@ -861,7 +861,7 @@ this.fragmentShader = codeTextArea.value); } } - this.updateMesh = function() { + this._updateMesh = function() { if (this.createMesh !== undefined && this.mesh === undefined) { if (this.vertexShader === undefined) this.vertexShader = defaultVertexShader; @@ -943,8 +943,8 @@ this.setUniform('alpha', alpha); } - if (this.update !== undefined) - this.update(); + if (this.updateMesh !== undefined) + this.updateMesh(); // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. diff --git a/sketches/c2s.js b/sketches/c2s.js index a97199f..a242404 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -1,4 +1,5 @@ function C2S() { + this.labels = "c2s".split(' '); this.render = function() { @@ -6,6 +7,7 @@ function C2S() { mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); }); } + this.createMesh = function() { return new THREE.Mesh(globeGeometry(80,40), this.shaderMaterial()); } diff --git a/sketches/torus.js b/sketches/torus.js index aedb0cc..8c832e6 100644 --- a/sketches/torus.js +++ b/sketches/torus.js @@ -2,11 +2,13 @@ function Torus() { this.labels = "torus".split(' '); this.r = .4; - var R = 1 + this.r; this.meshBounds = []; + var r = this.r; for (var theta = 0 ; theta < TAU ; theta += TAU / 8) { - this.meshBounds.push([R * cos(theta), R * sin(theta), -this.r]); - this.meshBounds.push([R * cos(theta), R * sin(theta), this.r]); + var x = (1 + r) * cos(theta); + var y = (1 + r) * sin(theta); + this.meshBounds.push([x, y, -r]); + this.meshBounds.push([x, y, r]); } this.render = function() { @@ -16,6 +18,7 @@ function Torus() { mCurve(makeOval(-1+r, -1+r, 2-2*r, 2-2*r, 20, -PI/2, 3*PI/2)); }); } + this.createMesh = function() { return new THREE.Mesh(torusGeometry(this.r, 40, 40), this.shaderMaterial()); } From 12193daab299d1a5e0ec411f97ac41d641bd2241 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 14:32:13 -0500 Subject: [PATCH 20/30] Added sketch.setMaterialRGB(rgb[]) for setting the color of a mesh material. --- sketch.js | 9 ++++++--- sketches/c2s.js | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sketch.js b/sketch.js index 3fedefa..c0328c4 100644 --- a/sketch.js +++ b/sketch.js @@ -861,6 +861,10 @@ this.fragmentShader = codeTextArea.value); } } + this.setMaterialRGB = function(rgb) { + this.mesh.material.setUniform('ambient' , [0.025 * rgb[0], 0.025 * rgb[1], 0.025 * rgb[2]]); + this.mesh.material.setUniform('diffuse' , [0.2 * rgb[0], 0.2 * rgb[1], 0.2 * rgb[2]]); + } this._updateMesh = function() { if (this.createMesh !== undefined && this.mesh === undefined) { if (this.vertexShader === undefined) @@ -906,10 +910,9 @@ var R = rgb[0] / 255; var G = rgb[1] / 255; var B = rgb[2] / 255; - this.mesh.material.setUniform('ambient' , [0.025 * R, 0.025 * G, 0.025 * B]); - this.mesh.material.setUniform('diffuse' , [0.2 * R, 0.2 * G, 0.2 * B]); - + this.setMaterialRGB([R,G,B]); this.meshColorId = this.colorId; + } // SET MESH MATRIX TO MATCH SKETCH'S POSITION/ROTATION/SCALE. diff --git a/sketches/c2s.js b/sketches/c2s.js index a242404..117ef6f 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -6,6 +6,8 @@ function C2S() { this.duringSketch(function() { mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); }); + if (this.mesh !== undefined) + this.setMaterialRGB([.1,.2,.3]); } this.createMesh = function() { From 83c7acb9347bde8e0ead8ed356c5138e97eaa79c Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 16:16:11 -0500 Subject: [PATCH 21/30] Allow application programmer to specify sketch/{ambient,diffuse,specular}. --- sketch.js | 29 ++++++++++++++++++++--------- sketches/c2s.js | 2 -- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/sketch.js b/sketch.js index c0328c4..75df145 100644 --- a/sketch.js +++ b/sketch.js @@ -861,10 +861,6 @@ this.fragmentShader = codeTextArea.value); } } - this.setMaterialRGB = function(rgb) { - this.mesh.material.setUniform('ambient' , [0.025 * rgb[0], 0.025 * rgb[1], 0.025 * rgb[2]]); - this.mesh.material.setUniform('diffuse' , [0.2 * rgb[0], 0.2 * rgb[1], 0.2 * rgb[2]]); - } this._updateMesh = function() { if (this.createMesh !== undefined && this.mesh === undefined) { if (this.vertexShader === undefined) @@ -900,21 +896,36 @@ this.mesh = this.createMesh(); root.add(this.mesh); this.is3D = true; + + // DEFAULT VALUES FOR PHONG COEFFICIENTS. + + this._ambient = [.025,.025,.025]; + this._diffuse = [.200,.200,.200]; + this._specular = [.5,.5,.5,10]; } if (this.mesh !== undefined) { // UPDATE MESH COLOR IF NEEDED. + if (this.ambient === undefined) this.ambient = this._ambient; + if (this.diffuse === undefined) this.diffuse = this._diffuse; + if (this.specular === undefined) this.specular = this._specular; + if (this.meshColorId !== this.colorId) { var rgb = paletteRGB[this.colorId]; - var R = rgb[0] / 255; - var G = rgb[1] / 255; - var B = rgb[2] / 255; - this.setMaterialRGB([R,G,B]); + this.ambient = [0.025 * rgb[0] / 255, 0.025 * rgb[1] / 255, 0.025 * rgb[2] / 255]; + this.diffuse = [0.2 * rgb[0] / 255, 0.2 * rgb[1] / 255, 0.2 * rgb[2] / 255]; this.meshColorId = this.colorId; - } + if (this.ambient != this._ambient ) this.mesh.material.setUniform('ambient' , this.ambient); + if (this.diffuse != this._diffuse ) this.mesh.material.setUniform('diffuse' , this.diffuse); + if (this.specular != this._specular) this.mesh.material.setUniform('specular', this.specular); + + this._ambient = this.ambient; + this._diffuse = this.diffuse; + this._specular = this.specular; + // SET MESH MATRIX TO MATCH SKETCH'S POSITION/ROTATION/SCALE. this.setRenderMatrix(this.mesh.getMatrix()); diff --git a/sketches/c2s.js b/sketches/c2s.js index 117ef6f..a242404 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -6,8 +6,6 @@ function C2S() { this.duringSketch(function() { mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); }); - if (this.mesh !== undefined) - this.setMaterialRGB([.1,.2,.3]); } this.createMesh = function() { From 04294b29d7ab2eddfe38e2250321ee6fe938fc6d Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Wed, 12 Nov 2014 16:24:40 -0500 Subject: [PATCH 22/30] Correct scale factor for perspective operation in sketch.setRenderMatrix(). --- sketch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sketch.js b/sketch.js index 75df145..8e99f77 100644 --- a/sketch.js +++ b/sketch.js @@ -87,7 +87,7 @@ mat.translate((p[0] - width()/2) / s, (height()/2 - p[1]) / s, 0); - mat.perspective(0, 0, -width()/2); + mat.perspective(0, 0, -7 * height() / s); var yy = min(1, 4 * this.rY * this.rY); mat.rotateX(PI * -this.rY); From 4668a86e245641ae2eb989ab90577d0734c821c1 Mon Sep 17 00:00:00 2001 From: Gal Sasson Date: Wed, 12 Nov 2014 20:50:17 -0500 Subject: [PATCH 23/30] add draw rect functions --- mat.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mat.js b/mat.js index 9396640..efdce36 100644 --- a/mat.js +++ b/mat.js @@ -165,9 +165,9 @@ var x = this._x(a), y = this._y(a), z = this._z(a), w = this._w(a); var X = this._x(t), Y = this._y(t), Z = this._z(t), W = this._w(t); - return [this._d(x, X), this._d(x, Y), this._d(x, Z), this._d(x, W), - this._d(y, X), this._d(y, Y), this._d(y, Z), this._d(y, W), - this._d(z, X), this._d(z, Y), this._d(z, Z), this._d(z, W), + return [this._d(x, X), this._d(x, Y), this._d(x, Z), this._d(x, W), + this._d(y, X), this._d(y, Y), this._d(y, Z), this._d(y, W), + this._d(z, X), this._d(z, Y), this._d(z, Z), this._d(z, W), this._d(w, X), this._d(w, Y), this._d(w, Z), this._d(w, W)]; }; this._mv = function(m,v) { @@ -354,6 +354,13 @@ var P = m.transform(p); text(str,P[0],P[1],ax,ay); }; + function mDrawRect(c1, c2) { + mClosedCurve([c1, [c2[0], c1[1]], c2, [c1[0], c2[1]]]); + }; + function mFillRect(c1, c2) { + mFillCurve([c1, [c2[0], c1[1]], c2, [c1[0], c2[1]], c1]); + }; + var m = new M4(); From f457c5b39e731e5b3f64393a0b3df4a6f2e9924a Mon Sep 17 00:00:00 2001 From: Gal Sasson Date: Wed, 12 Nov 2014 20:50:41 -0500 Subject: [PATCH 24/30] add gal sketch for tactonic (name to be changed) --- sketches/gal.js | 237 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 sketches/gal.js diff --git a/sketches/gal.js b/sketches/gal.js new file mode 100644 index 0000000..d6f9b9c --- /dev/null +++ b/sketches/gal.js @@ -0,0 +1,237 @@ +function Gal() +{ + this.labels = "gal".split(' '); + this.myText = "Tactonic"; + + this.recordBtn = new Button([-0.9, -0.9], [-0.7, -0.7], function() { + this.myText = "record clicked"; + }); + + this.playBtn = new Button([-0.6, -0.9], [-0.4, -0.7], function () { + this.myText = "play clicked"; + }); + + this.onClick = function(x, y) {}; + + this.mouseDown = function(x, y) + { + var p = m.transform([x, y]); + + this.playBtn.test(p[0], p[1]); + this.recordBtn.test(p[0], p[1]); + }; + + this.mouseDrag = function() + { + + }; + + this.mouseUp = function() + { + this.playBtn.release(); + this.recordBtn.release(); + }; + + + this.render = function() + { + this.duringSketch(function() { + mLine([-1, 1], [1, 1]); + mLine([1, 1], [-1, -1]); + mLine([-1, -1], [1, -1]); + }); + + this.afterSketch(function() { + // main rectangle + mDrawRect([-1, -1], [1, 1]); + + // buttons + this.playBtn.draw(); + this.recordBtn.draw(); + + mText(this.myText, [-0.9, 0.9], 0, 0); + + this.table.applyHeights(ttForce); + + this.sendValues(); + }); + }; + + // the visualization surface + this.createMesh = function() { + this.table = new InformTable(32, 32); + this.table.init(); + this.table.rotateOnAxis(new THREE.Vector3(1, 0, 0), -Math.PI/8); + + this.mesh = new THREE.Mesh(); + this.mesh.setMaterial(this.shaderMaterial()); + this.mesh.add(this.table); + + return this.mesh; + }; + + this.sendValues = function() + { + var max = 0; + for (var i=0; i max) { + max = ttForce[i]; + } + } + this.outValue[0] = max; + } +} +Gal.prototype = new Sketch; +addSketchType("Gal"); + + +function Button(c1, c2, onclick) +{ + this.rect = [c1, c2]; + this.onclick = onclick; + this.pressed = false; + + this.draw = function() { + if (this.pressed) { + mFillRect(this.rect[0], this.rect[1]); + } + else { + mDrawRect(this.rect[0], this.rect[1]); + } + } + + this.test = function(x, y) { + if (x < this.rect[0][0] || + x > this.rect[1][0] || + y < this.rect[0][1] || + y > this.rect[1][1]) { + + return false; + } + + this.pressed = true; + this.onclick(); + return true; + } + + this.release = function() { + this.pressed = false; + } + +}; + + + + + +var MAX_HEIGHT = 0.5; + +InformTable = function(rows, cols) +{ + THREE.Object3D.call(this); + + this.squareSize = 1.0 / Math.max(rows, cols); + this.spacing = this.squareSize / 4; + this.rows = rows; + this.cols = cols; + + console.log("square size = " + this.squareSize); + + this.cubes = []; + this.table = {}; + this.dampSpeed = 0.3; + + this.showClipping = false; + + this.whiteMaterial = new THREE.MeshLambertMaterial( { color: 0xffffff, ambient: 0xffffff } ); + this.greyMaterial = new THREE.MeshLambertMaterial( { color: 0x444444, ambient: 0x222222 } ); + this.blackMaterial = new THREE.MeshLambertMaterial( { color: 0x111111, ambient: 0x111111 } ); +}; + +InformTable.prototype = Object.create(THREE.Object3D.prototype); + +InformTable.prototype.init = function() +{ + // create the big table + var geo = new THREE.CubeGeometry(this.cols*(this.squareSize + this.spacing) + this.spacing*2, + this.rows*(this.squareSize + this.spacing) + this.spacing*2, MAX_HEIGHT-0.1); + this.table = new THREE.Mesh(geo, this.greyMaterial); + this.add(this.table); + + // create the pixels + var topLeft = {}; + topLeft.x = -(this.cols*(this.squareSize + this.spacing))/2 + this.spacing*2; + topLeft.y = -(this.rows*(this.squareSize + this.spacing))/2 + this.spacing*2; + + geo = new THREE.CubeGeometry(this.squareSize, this.squareSize, MAX_HEIGHT); + var index=0; + for (var y=0; y 4) { + cube.position.z = 4; + } + } + } +}; + + + From 3e9cc595e9d27072f36a6ad50e453e8f1fa15929 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Thu, 13 Nov 2014 12:26:10 -0500 Subject: [PATCH 25/30] Took out perspective for now, in setRenderMatrix, until we can get it working properly with shading. --- four.js | 4 +++- sketch.js | 33 +++++++++++++++++++-------------- sketchPage.js | 2 +- sketches/tactonic.js | 4 ++++ sketches/torus.js | 2 +- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/four.js b/four.js index 76357e0..0fd40cc 100644 --- a/four.js +++ b/four.js @@ -560,7 +560,7 @@ var defaultVertexShader = [ ,'varying float dy;' ,'varying vec3 vPosition;' ,'varying vec3 vNormal;' - ,'float displace(vec3 p) { return 0. /* 0.1 * noise(3. * p + vec3(0.,-time,0.)) */; }' + ,'float displace(vec3 p) { return 0.0 /* * noise(3.0*p + time*vec3(0.0,-1.0,0.0)) */; }' ,'void main() {' ,' dx = 2. * uv.x - 1.;' ,' dy = 2. * uv.y - 1.;' @@ -658,6 +658,7 @@ var sharedHeader = [ var vertexShaderHeader = [ sharedHeader ,'uniform float time;' +,'' ].join('\n'); var fragmentShaderHeader = [ @@ -676,6 +677,7 @@ var fragmentShaderHeader = [ ,'uniform float x;' ,'uniform float y;' ,'uniform float z;' +,'' ].join('\n'); function createVisibleEdgesMesh(veds) { diff --git a/sketch.js b/sketch.js index 8e99f77..e410bc0 100644 --- a/sketch.js +++ b/sketch.js @@ -87,7 +87,7 @@ mat.translate((p[0] - width()/2) / s, (height()/2 - p[1]) / s, 0); - mat.perspective(0, 0, -7 * height() / s); + //mat.perspective(0, 0, -7 * height() / s); var yy = min(1, 4 * this.rY * this.rY); mat.rotateX(PI * -this.rY); @@ -877,6 +877,16 @@ return material; } + this.updateVertexShader = function() { + if (this.vertexShader != codeTextArea.value) { + var isValid = isValidVertexShader(formVertexShader(codeTextArea.value)); + /*if (isValid)*/ { + this.vertexShader = codeTextArea.value; + this.mesh.material = this.shaderMaterial(); + } + } + } + this.updateFragmentShader = function() { if (this.fragmentShader != codeTextArea.value) { var isValid = isValidFragmentShader(formFragmentShader(codeTextArea.value)); @@ -889,6 +899,7 @@ if (this.code == null) this.code = []; + this.code.push(["vertexShader", this.vertexShader, this.updateVertexShader]); this.code.push(["fragmentShader", this.fragmentShader, this.updateFragmentShader]); if (this.meshBounds == undefined) @@ -899,17 +910,15 @@ // DEFAULT VALUES FOR PHONG COEFFICIENTS. - this._ambient = [.025,.025,.025]; - this._diffuse = [.200,.200,.200]; - this._specular = [.5,.5,.5,10]; + this.meshColorId = this.colorId; } if (this.mesh !== undefined) { // UPDATE MESH COLOR IF NEEDED. - if (this.ambient === undefined) this.ambient = this._ambient; - if (this.diffuse === undefined) this.diffuse = this._diffuse; - if (this.specular === undefined) this.specular = this._specular; + if (this.ambient === undefined) this.ambient = [.025,.025,.025]; + if (this.diffuse === undefined) this.diffuse = [.2,.2,.2]; + if (this.specular === undefined) this.specular = [.5,.5,.5,10]; if (this.meshColorId !== this.colorId) { var rgb = paletteRGB[this.colorId]; @@ -918,13 +927,9 @@ this.meshColorId = this.colorId; } - if (this.ambient != this._ambient ) this.mesh.material.setUniform('ambient' , this.ambient); - if (this.diffuse != this._diffuse ) this.mesh.material.setUniform('diffuse' , this.diffuse); - if (this.specular != this._specular) this.mesh.material.setUniform('specular', this.specular); - - this._ambient = this.ambient; - this._diffuse = this.diffuse; - this._specular = this.specular; + this.mesh.material.setUniform('ambient' , this.ambient); + this.mesh.material.setUniform('diffuse' , this.diffuse); + this.mesh.material.setUniform('specular', this.specular); // SET MESH MATRIX TO MATCH SKETCH'S POSITION/ROTATION/SCALE. diff --git a/sketchPage.js b/sketchPage.js index c5e183e..82c355a 100644 --- a/sketchPage.js +++ b/sketchPage.js @@ -741,7 +741,7 @@ var sketchToDelete = null; // CLICK ON A CODE SKETCH TO BRING UP ITS CODE. - if (bgClickCount == 0 && sk().isClick === undefined && sk().code != null) { + if (bgClickCount == 0 && sk().onClick === undefined && sk().code != null) { if (isCodeWidget && codeSketch != sk()) toggleCodeWidget(); codeSketch = sk(); diff --git a/sketches/tactonic.js b/sketches/tactonic.js index 738a705..3350505 100644 --- a/sketches/tactonic.js +++ b/sketches/tactonic.js @@ -6,6 +6,10 @@ function Tactonic() { mLine([-1,1],[1,1]); mLine([0,1],[0,-1]); }); + + this.afterSketch(function() { + mClosedCurve([ [-1.1,-1.1], [1.1,-1.1], [1.1,1.1], [-1.1,1.1] ]); + }); } this.fragmentShader = [ diff --git a/sketches/torus.js b/sketches/torus.js index 8c832e6..e8e7a9b 100644 --- a/sketches/torus.js +++ b/sketches/torus.js @@ -20,7 +20,7 @@ function Torus() { } this.createMesh = function() { - return new THREE.Mesh(torusGeometry(this.r, 40, 40), this.shaderMaterial()); + return new THREE.Mesh(torusGeometry(this.r, 80, 80), this.shaderMaterial()); } } Torus.prototype = new Sketch; From 7176083a9718a39517d5a403353ed0cc575918a3 Mon Sep 17 00:00:00 2001 From: Max Dumas Date: Thu, 13 Nov 2014 12:40:45 -0500 Subject: [PATCH 26/30] Removed bad sketch treeSlice.js --- sketches/treeSlice.js | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 sketches/treeSlice.js diff --git a/sketches/treeSlice.js b/sketches/treeSlice.js deleted file mode 100644 index 58d010a..0000000 --- a/sketches/treeSlice.js +++ /dev/null @@ -1,35 +0,0 @@ - -function TreeSlice() { - this.labels = "treeSlice".split(' '); - - this.vertices = tree_obj.vertices; - this.faces = tree_obj.faces; - - this.render = function(elapsed) { - var z = isDef(this.in[0]) ? this.inValue[0] : 0; - - for (var f = 0 ; f < this.faces.length ; f++) { - var face = this.faces[f]; - var P = [ this.vertices[face[0]-1], - this.vertices[face[1]-1], - this.vertices[face[2]-1] ]; - var C = []; - for (var i = 0 ; i < 3 ; i++) { - var j = (i + 1) % 3; - if (P[i] === undefined || P[j] === undefined) - continue; - if ((P[i][2] > z) != (P[j][2] > z)) { - var t = (z - P[i][2]) / (P[j][2] - P[i][2]); - var x = lerp(t, P[i][0], P[j][0]); - var y = lerp(t, P[i][1], P[j][1]); - C.push([x,y,z]); - } - } - if (C.length > 1) - mCurve(C); - } - } -} -TreeSlice.prototype = new Sketch; -addSketchType("TreeSlice"); - From 5a43523ba8fd161bfbd898a580090cf7a3cdfb47 Mon Sep 17 00:00:00 2001 From: Ken Perlin Date: Thu, 13 Nov 2014 15:31:53 -0500 Subject: [PATCH 27/30] Figured out how to do syntax checking for a vertex shader. --- four.js | 28 +++++++++++++++++++++++----- sketch.js | 4 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/four.js b/four.js index 0fd40cc..1d6d4f6 100644 --- a/four.js +++ b/four.js @@ -484,13 +484,18 @@ } function gl() { return renderer.context; } -function isValidVertexShader (string) { return isValidShader(gl().VERTEX_SHADER , string); } +function isValidVertexShader (string) { + return isValidShader(gl().VERTEX_SHADER, + string); +} function isValidFragmentShader(string) { return isValidShader(gl().FRAGMENT_SHADER, string); } function isValidShader(type, string) { var shader = gl().createShader(type); gl().shaderSource(shader, string); gl().compileShader(shader); var status = gl().getShaderParameter(shader, gl().COMPILE_STATUS); + if (! status) + console.log(gl().getShaderInfoLog(shader)); return status; }; @@ -527,6 +532,11 @@ function addUniforms(material, string) { } } +// PREPEND THE HEADER OF PREDEFINED THINGS TO SYNTAX CHECK A VERTEX SHADER: +function formSyntaxCheckVertexShader(string) { + return syntaxCheckVertexShaderHeader.concat(string); +} + // PREPEND THE HEADER OF PREDEFINED THINGS TO A VERTEX SHADER: function formVertexShader(string) { return vertexShaderHeader.concat(string); @@ -556,14 +566,10 @@ function shaderMaterial(vertexShader, fragmentShaderString) { // THIS VERTEX SHADER WILL SUFFICE FOR MOST SHADER PLANES: var defaultVertexShader = [ - 'varying float dx;' - ,'varying float dy;' ,'varying vec3 vPosition;' ,'varying vec3 vNormal;' ,'float displace(vec3 p) { return 0.0 /* * noise(3.0*p + time*vec3(0.0,-1.0,0.0)) */; }' ,'void main() {' - ,' dx = 2. * uv.x - 1.;' - ,' dy = 2. * uv.y - 1.;' ,' vNormal = normalize((modelViewMatrix * vec4(normal, 0.)).xyz);' ,' vPosition = position*.03;' ,' float _p0 = displace(position);' @@ -655,6 +661,18 @@ var sharedHeader = [ ,'}' ].join('\n'); +// THE SYNTAX CHECKING VERSION ALSO INCLUDES VARS THAT WILL BE DECLARED BY THREE.JS. + +var syntaxCheckVertexShaderHeader = [ + sharedHeader +,'uniform mat4 modelViewMatrix;' +,'uniform mat4 projectionMatrix;' +,'varying vec3 normal;' +,'varying vec3 position;' +,'uniform float time;' +,'' +].join('\n'); + var vertexShaderHeader = [ sharedHeader ,'uniform float time;' diff --git a/sketch.js b/sketch.js index e410bc0..ead8b3d 100644 --- a/sketch.js +++ b/sketch.js @@ -879,8 +879,8 @@ this.updateVertexShader = function() { if (this.vertexShader != codeTextArea.value) { - var isValid = isValidVertexShader(formVertexShader(codeTextArea.value)); - /*if (isValid)*/ { + var isValid = isValidVertexShader(formSyntaxCheckVertexShader(codeTextArea.value)); + if (isValid) { this.vertexShader = codeTextArea.value; this.mesh.material = this.shaderMaterial(); } From b621bf994e1a7a9f4d78f5739a929997ace97165 Mon Sep 17 00:00:00 2001 From: Jason Sigal Date: Fri, 21 Nov 2014 15:47:21 -0500 Subject: [PATCH 28/30] ITP template: grunt build and reorganizing --- ITP/create-template/README.md | 26 +- ITP/create-template/appdirectory-build/app.js | 15 - .../appdirectory-build/app/main.js | 18 - .../appdirectory-build/index.html | 9 - .../appdirectory-build/lib/DLtest_01.js | 12 - .../appdirectory-build/lib/app.build.js | 10 - .../appdirectory-build/lib/core.js | 11 - .../appdirectory-build/lib/testSubObject.js | 38 - ITP/create-template/build/chalktalk.js | 93 + ITP/create-template/build/chalktalk.min.js | 1 + ITP/create-template/{tools => deps}/build.js | 0 ITP/create-template/{tools => deps}/r.js | 0 .../lib => deps}/require.js | 0 ITP/create-template/gruntfile.js | 56 + .../{www-built => html/loadSRC}/index.html | 2 +- .../{www => html/withRequire}/index.html | 2 +- ITP/create-template/package.json | 7 + .../{www/lib => src}/DLtest_01.js | 4 +- .../{www/app/main.js => src/app.js} | 10 +- .../lib => src}/basicMath.js | 0 ITP/create-template/src/core.js | 13 + .../app => src}/messages.js | 0 .../lib => src/objects}/testObject.js | 2 +- .../{www/lib => src/objects}/testSubObject.js | 2 +- .../{appdirectory-build/lib => src}/print.js | 0 .../lib => src}/utility/mat.js | 0 ITP/create-template/www-built/app.js | 1 - ITP/create-template/www-built/app/main.js | 1 - ITP/create-template/www-built/app/messages.js | 1 - ITP/create-template/www-built/build.txt | 6 - .../www-built/lib/DLtest_01.js | 1 - ITP/create-template/www-built/lib/core.js | 1 - ITP/create-template/www-built/lib/print.js | 1 - ITP/create-template/www-built/lib/require.js | 7 - .../www-built/lib/utility/mat.js | 1 - ITP/create-template/www/app.js | 15 - ITP/create-template/www/app/messages.js | 7 - ITP/create-template/www/lib/app.build.js | 10 - ITP/create-template/www/lib/basicMath.js | 20 - ITP/create-template/www/lib/core.js | 11 - ITP/create-template/www/lib/print.js | 5 - ITP/create-template/www/lib/require.js | 2076 ------------- ITP/create-template/www/lib/testObject.js | 34 - ITP/create-template/www/lib/utility/mat.js | 373 --- node.log | 1 + utility.js | 2574 +++++++++-------- 46 files changed, 1483 insertions(+), 3994 deletions(-) delete mode 100644 ITP/create-template/appdirectory-build/app.js delete mode 100644 ITP/create-template/appdirectory-build/app/main.js delete mode 100644 ITP/create-template/appdirectory-build/index.html delete mode 100644 ITP/create-template/appdirectory-build/lib/DLtest_01.js delete mode 100644 ITP/create-template/appdirectory-build/lib/app.build.js delete mode 100644 ITP/create-template/appdirectory-build/lib/core.js delete mode 100644 ITP/create-template/appdirectory-build/lib/testSubObject.js create mode 100644 ITP/create-template/build/chalktalk.js create mode 100644 ITP/create-template/build/chalktalk.min.js rename ITP/create-template/{tools => deps}/build.js (100%) rename ITP/create-template/{tools => deps}/r.js (100%) rename ITP/create-template/{appdirectory-build/lib => deps}/require.js (100%) create mode 100644 ITP/create-template/gruntfile.js rename ITP/create-template/{www-built => html/loadSRC}/index.html (62%) rename ITP/create-template/{www => html/withRequire}/index.html (56%) rename ITP/create-template/{www/lib => src}/DLtest_01.js (74%) rename ITP/create-template/{www/app/main.js => src/app.js} (72%) rename ITP/create-template/{appdirectory-build/lib => src}/basicMath.js (100%) create mode 100644 ITP/create-template/src/core.js rename ITP/create-template/{appdirectory-build/app => src}/messages.js (100%) rename ITP/create-template/{appdirectory-build/lib => src/objects}/testObject.js (93%) rename ITP/create-template/{www/lib => src/objects}/testSubObject.js (92%) rename ITP/create-template/{appdirectory-build/lib => src}/print.js (100%) rename ITP/create-template/{appdirectory-build/lib => src}/utility/mat.js (100%) delete mode 100644 ITP/create-template/www-built/app.js delete mode 100644 ITP/create-template/www-built/app/main.js delete mode 100644 ITP/create-template/www-built/app/messages.js delete mode 100644 ITP/create-template/www-built/build.txt delete mode 100644 ITP/create-template/www-built/lib/DLtest_01.js delete mode 100644 ITP/create-template/www-built/lib/core.js delete mode 100644 ITP/create-template/www-built/lib/print.js delete mode 100644 ITP/create-template/www-built/lib/require.js delete mode 100644 ITP/create-template/www-built/lib/utility/mat.js delete mode 100644 ITP/create-template/www/app.js delete mode 100644 ITP/create-template/www/app/messages.js delete mode 100644 ITP/create-template/www/lib/app.build.js delete mode 100644 ITP/create-template/www/lib/basicMath.js delete mode 100644 ITP/create-template/www/lib/core.js delete mode 100644 ITP/create-template/www/lib/print.js delete mode 100644 ITP/create-template/www/lib/require.js delete mode 100644 ITP/create-template/www/lib/testObject.js delete mode 100644 ITP/create-template/www/lib/utility/mat.js diff --git a/ITP/create-template/README.md b/ITP/create-template/README.md index 36165e7..78afc58 100644 --- a/ITP/create-template/README.md +++ b/ITP/create-template/README.md @@ -1,22 +1,16 @@ This web project has the following setup: -* www/ - the web assets for the project - * index.html - the entry point into the app. - * app.js - the top-level config script used by index.html - * app/ - the directory to store project-specific scripts. - * lib/ - the directory to hold third party scripts. -* tools/ - the build tools to optimize the project. +* build/ - contains output files generated with grunt chalktalk.js and chalktalk.min.js -To optimize, run: +* deps/ - the directory to hold third party scripts. - node tools/r.js -o tools/build.js +* html/ - example and test index.html files. One loads with require.js, and another loads without require. -That build command creates an optimized version of the project in a -**www-built** directory. The app.js file will be optimized to include -all of its dependencies. +* src/ - our source files for the project + * app.js - the top-level config script, used by require.js -For more information on the optimizer: -http://requirejs.org/docs/optimization.html - -For more information on using requirejs: -http://requirejs.org/docs/api.html +* gruntfile.js - defines grunt routines + * first, from the command line, type ```npm install```. This generated a node_modules folder that includes all the grunt modules. + * If you have not already installed the Grunt Command Line Interface (i.e. ```which grunt``` does not give a path to grunt), then install it with ```npm install -g grunt-cli```. + * ```grunt requirejs``` --> builds chalktalk.js and chalktalk.min.js + * ```grunt watch``` --> builds every time you change a file \ No newline at end of file diff --git a/ITP/create-template/appdirectory-build/app.js b/ITP/create-template/appdirectory-build/app.js deleted file mode 100644 index 6150425..0000000 --- a/ITP/create-template/appdirectory-build/app.js +++ /dev/null @@ -1,15 +0,0 @@ -// For any third party dependencies, like jQuery, place them in the lib folder. - -// Configure loading modules from the lib directory, -// except for 'app' ones, which are in a sibling -// directory. -requirejs.config({ - baseUrl: 'lib', - paths: { - app: '../app' - } -}); - -// Start loading the main app file. Put all of -// your application logic in there. -requirejs(['app/main']); diff --git a/ITP/create-template/appdirectory-build/app/main.js b/ITP/create-template/appdirectory-build/app/main.js deleted file mode 100644 index b5cd146..0000000 --- a/ITP/create-template/appdirectory-build/app/main.js +++ /dev/null @@ -1,18 +0,0 @@ -define(function (require) { - - // Load any app-specific modules - // with a relative require call, - // like: - // - var CT = require('core'); - require('DLtest_01'); - require('testObject'); - require('testSubObject'); - require('basicMath'); - - window.CT = CT; - - - return CT; - -}); diff --git a/ITP/create-template/appdirectory-build/index.html b/ITP/create-template/appdirectory-build/index.html deleted file mode 100644 index cb240d1..0000000 --- a/ITP/create-template/appdirectory-build/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - -

Hello World

- - diff --git a/ITP/create-template/appdirectory-build/lib/DLtest_01.js b/ITP/create-template/appdirectory-build/lib/DLtest_01.js deleted file mode 100644 index 6ba3085..0000000 --- a/ITP/create-template/appdirectory-build/lib/DLtest_01.js +++ /dev/null @@ -1,12 +0,0 @@ -define(function(){ - - var CT = require('core'); - - CT.prototype.thing = function(b){ - var a = b*b; - document.write(a); - }; - - return CT; - -}); diff --git a/ITP/create-template/appdirectory-build/lib/app.build.js b/ITP/create-template/appdirectory-build/lib/app.build.js deleted file mode 100644 index db7331c..0000000 --- a/ITP/create-template/appdirectory-build/lib/app.build.js +++ /dev/null @@ -1,10 +0,0 @@ -({ - appDir: "../", - baseUrl: "lib", - dir: "../../appdirectory-build", - modules: [ - { - name: "main" - } - ] -}) diff --git a/ITP/create-template/appdirectory-build/lib/core.js b/ITP/create-template/appdirectory-build/lib/core.js deleted file mode 100644 index 2c28d19..0000000 --- a/ITP/create-template/appdirectory-build/lib/core.js +++ /dev/null @@ -1,11 +0,0 @@ -define(function () { - - var CT = function(){ - - this._version = 'v.001'; - - } - - return CT; - -}); diff --git a/ITP/create-template/appdirectory-build/lib/testSubObject.js b/ITP/create-template/appdirectory-build/lib/testSubObject.js deleted file mode 100644 index 1cc7958..0000000 --- a/ITP/create-template/appdirectory-build/lib/testSubObject.js +++ /dev/null @@ -1,38 +0,0 @@ -define(function () { - - // var CT = require('core'); - - var CT = require('testObject'); - - CT.subObj = function () { - - CT.obj.call(this); - - // private variables can go here - - var _subVar = 36; - - // set getters and setters here - - Object.defineProperty(this, 'sub', { - get: function () { - return _subVar - }, - set: function (y) { - if (y > 12) _subVar = y; - } - }); - - }; - - CT.subObj.prototype = Object.create(CT.obj.prototype); - - CT.subObj.prototype.sayHi = function (s) { - - console.log(s); - - }; - - return CT; - -}); \ No newline at end of file diff --git a/ITP/create-template/build/chalktalk.js b/ITP/create-template/build/chalktalk.js new file mode 100644 index 0000000..55115c5 --- /dev/null +++ b/ITP/create-template/build/chalktalk.js @@ -0,0 +1,93 @@ +;(function() { +var core, DLtest_01, objects_testObject, objects_testSubObject, basicMath, app; +core = function (require) { + var CT = function () { + this._version = 'v.001'; + this._hello = 'hello'; + this._whatisup = 'whatisup'; + this._isThing = 'thing'; + }; + return CT; +}({}); +DLtest_01 = function (require) { + + var CT = core; + CT.prototype.thing = function (b) { + var a = b * b; + document.write(a); + }; + return CT; +}({}); +objects_testObject = function (require) { + var CT = core; + CT.obj = function () { + //public variables go here under 'this' + this.publicVar = Math.PI; + }; + CT.obj.prototype = { + _privateVar: 36, + constructor: CT.obj, + get privateVar() { + return this._privateVar; + }, + set privateVar(a) { + if (typeof a == 'number') + this._privateVar = a; + else + console.log('NaN'); + } + }; + return CT; +}({}); +objects_testSubObject = function () { + // var CT = require('core'); + var CT = objects_testObject; + CT.subObj = function () { + CT.obj.call(this); + // private variables can go here + var _subVar = 36; + // set getters and setters here + Object.defineProperty(this, 'sub', { + get: function () { + return _subVar; + }, + set: function (y) { + if (y > 12) + _subVar = y; + } + }); + }; + CT.subObj.prototype = Object.create(CT.obj.prototype); + CT.subObj.prototype.sayHi = function (s) { + console.log(s); + }; + return CT; +}(); +basicMath = function () { + var CT = core; + CT.prototype.Math = { + square: function (b) { + var a = b * b; + return a; + }, + multiply: function (a, b) { + return a * b; + } + }; + return CT.Math; +}(); +app = function (require) { + + // Load any app-specific modules + // with a relative require call, + // like: + // + var CT = core; + DLtest_01; + objects_testObject; + objects_testSubObject; + basicMath; + window.CT = CT; + return CT; +}({}); +}()); \ No newline at end of file diff --git a/ITP/create-template/build/chalktalk.min.js b/ITP/create-template/build/chalktalk.min.js new file mode 100644 index 0000000..b80b93d --- /dev/null +++ b/ITP/create-template/build/chalktalk.min.js @@ -0,0 +1 @@ +!function(){var t,n,o,r,e,i;t=function(){var t=function(){this._version="v.001",this._hello="hello",this._whatisup="whatisup",this._isThing="thing"};return t}({}),n=function(){var n=t;return n.prototype.thing=function(t){var n=t*t;document.write(n)},n}({}),o=function(){var n=t;return n.obj=function(){this.publicVar=Math.PI},n.obj.prototype={_privateVar:36,constructor:n.obj,get privateVar(){return this._privateVar},set privateVar(t){"number"==typeof t?this._privateVar=t:console.log("NaN")}},n}({}),r=function(){var t=o;return t.subObj=function(){t.obj.call(this);var n=36;Object.defineProperty(this,"sub",{get:function(){return n},set:function(t){t>12&&(n=t)}})},t.subObj.prototype=Object.create(t.obj.prototype),t.subObj.prototype.sayHi=function(t){console.log(t)},t}(),e=function(){var n=t;return n.prototype.Math={square:function(t){var n=t*t;return n},multiply:function(t,n){return t*n}},n.Math}(),i=function(){var n=t;return window.CT=n,n}({})}(); \ No newline at end of file diff --git a/ITP/create-template/tools/build.js b/ITP/create-template/deps/build.js similarity index 100% rename from ITP/create-template/tools/build.js rename to ITP/create-template/deps/build.js diff --git a/ITP/create-template/tools/r.js b/ITP/create-template/deps/r.js similarity index 100% rename from ITP/create-template/tools/r.js rename to ITP/create-template/deps/r.js diff --git a/ITP/create-template/appdirectory-build/lib/require.js b/ITP/create-template/deps/require.js similarity index 100% rename from ITP/create-template/appdirectory-build/lib/require.js rename to ITP/create-template/deps/require.js diff --git a/ITP/create-template/gruntfile.js b/ITP/create-template/gruntfile.js new file mode 100644 index 0000000..072073d --- /dev/null +++ b/ITP/create-template/gruntfile.js @@ -0,0 +1,56 @@ +module.exports = function(grunt) { + + grunt.initConfig({ + + requirejs: { + unmin: { + options: { + baseUrl: './src', + include: ['app'], + out: 'build/chalktalk.js', + optimize: 'none', + findNestedDependencies: true, + onModuleBundleComplete: function( data ) { + var fs = require('fs'); + var amdclean = require('amdclean'); + outputFile = data.path; + fs.writeFileSync(outputFile, amdclean.clean({ + 'filePath': outputFile + })); + } + } + }, + min: { + options: { + baseUrl: './src', + include: ['app'], + out: 'build/chalktalk.min.js', + optimize: 'uglify2', + paths: '<%= requirejs.unmin.options.paths %>', + onModuleBundleComplete: function( data ) { + var fs = require('fs'); + var amdclean = require('amdclean'); + outputFile = data.path; + fs.writeFileSync(outputFile, amdclean.clean({ + 'filePath': outputFile + })); + } + } + } + }, + + watch: { + // + main: { + files: ['**/*.js'], + tasks: ['requirejs'], + options: { livereload: true } + }, + } + + }); + + grunt.loadNpmTasks('grunt-contrib-requirejs'); + grunt.loadNpmTasks('grunt-contrib-watch'); + +}; \ No newline at end of file diff --git a/ITP/create-template/www-built/index.html b/ITP/create-template/html/loadSRC/index.html similarity index 62% rename from ITP/create-template/www-built/index.html rename to ITP/create-template/html/loadSRC/index.html index cb240d1..3a764b8 100644 --- a/ITP/create-template/www-built/index.html +++ b/ITP/create-template/html/loadSRC/index.html @@ -1,7 +1,7 @@ - +

Hello World

diff --git a/ITP/create-template/www/index.html b/ITP/create-template/html/withRequire/index.html similarity index 56% rename from ITP/create-template/www/index.html rename to ITP/create-template/html/withRequire/index.html index cb240d1..9e919c7 100644 --- a/ITP/create-template/www/index.html +++ b/ITP/create-template/html/withRequire/index.html @@ -1,7 +1,7 @@ - +

Hello World

diff --git a/ITP/create-template/package.json b/ITP/create-template/package.json index f7667d9..c228d5e 100644 --- a/ITP/create-template/package.json +++ b/ITP/create-template/package.json @@ -2,5 +2,12 @@ "amd": {}, "volo": { "baseUrl": "www/lib" + }, + "devDependencies": { + "grunt": "~0.4.5", + "grunt-contrib-requirejs": "~0.4.4", + "grunt-contrib-concat": "~0.5.0", + "amdclean": "~2.3.0", + "grunt-contrib-watch": "~0.6.1" } } diff --git a/ITP/create-template/www/lib/DLtest_01.js b/ITP/create-template/src/DLtest_01.js similarity index 74% rename from ITP/create-template/www/lib/DLtest_01.js rename to ITP/create-template/src/DLtest_01.js index 6ba3085..ef5151e 100644 --- a/ITP/create-template/www/lib/DLtest_01.js +++ b/ITP/create-template/src/DLtest_01.js @@ -1,4 +1,6 @@ -define(function(){ +define(function(require){ + + 'use strict'; var CT = require('core'); diff --git a/ITP/create-template/www/app/main.js b/ITP/create-template/src/app.js similarity index 72% rename from ITP/create-template/www/app/main.js rename to ITP/create-template/src/app.js index b5cd146..418b46d 100644 --- a/ITP/create-template/www/app/main.js +++ b/ITP/create-template/src/app.js @@ -1,18 +1,20 @@ define(function (require) { + 'use strict'; + // Load any app-specific modules // with a relative require call, // like: // + var CT = require('core'); require('DLtest_01'); - require('testObject'); - require('testSubObject'); + require('objects/testObject'); + require('objects/testSubObject'); require('basicMath'); window.CT = CT; - return CT; -}); +}); \ No newline at end of file diff --git a/ITP/create-template/appdirectory-build/lib/basicMath.js b/ITP/create-template/src/basicMath.js similarity index 100% rename from ITP/create-template/appdirectory-build/lib/basicMath.js rename to ITP/create-template/src/basicMath.js diff --git a/ITP/create-template/src/core.js b/ITP/create-template/src/core.js new file mode 100644 index 0000000..0511141 --- /dev/null +++ b/ITP/create-template/src/core.js @@ -0,0 +1,13 @@ +define(function (require) { + + var CT = function(){ + + this._version = 'v.001'; + this._hello = 'hello'; + this._whatisup = 'whatisup'; + this._isThing = 'thing'; + } + + return CT; + +}); diff --git a/ITP/create-template/appdirectory-build/app/messages.js b/ITP/create-template/src/messages.js similarity index 100% rename from ITP/create-template/appdirectory-build/app/messages.js rename to ITP/create-template/src/messages.js diff --git a/ITP/create-template/appdirectory-build/lib/testObject.js b/ITP/create-template/src/objects/testObject.js similarity index 93% rename from ITP/create-template/appdirectory-build/lib/testObject.js rename to ITP/create-template/src/objects/testObject.js index 75c3d98..37d2021 100644 --- a/ITP/create-template/appdirectory-build/lib/testObject.js +++ b/ITP/create-template/src/objects/testObject.js @@ -1,4 +1,4 @@ -define(function () { +define(function (require) { var CT = require('core'); diff --git a/ITP/create-template/www/lib/testSubObject.js b/ITP/create-template/src/objects/testSubObject.js similarity index 92% rename from ITP/create-template/www/lib/testSubObject.js rename to ITP/create-template/src/objects/testSubObject.js index 1cc7958..64758b6 100644 --- a/ITP/create-template/www/lib/testSubObject.js +++ b/ITP/create-template/src/objects/testSubObject.js @@ -2,7 +2,7 @@ define(function () { // var CT = require('core'); - var CT = require('testObject'); + var CT = require('objects/testObject'); CT.subObj = function () { diff --git a/ITP/create-template/appdirectory-build/lib/print.js b/ITP/create-template/src/print.js similarity index 100% rename from ITP/create-template/appdirectory-build/lib/print.js rename to ITP/create-template/src/print.js diff --git a/ITP/create-template/appdirectory-build/lib/utility/mat.js b/ITP/create-template/src/utility/mat.js similarity index 100% rename from ITP/create-template/appdirectory-build/lib/utility/mat.js rename to ITP/create-template/src/utility/mat.js diff --git a/ITP/create-template/www-built/app.js b/ITP/create-template/www-built/app.js deleted file mode 100644 index 21b92bb..0000000 --- a/ITP/create-template/www-built/app.js +++ /dev/null @@ -1 +0,0 @@ -define("core",[],function(){return{version:function(){return"v.01"}}}),define("app/main",["require","core"],function(e){var t=e("core");console.log(t)}),requirejs.config({baseUrl:"lib",paths:{app:"../app"}}),requirejs(["app/main"]),define("app",function(){}); \ No newline at end of file diff --git a/ITP/create-template/www-built/app/main.js b/ITP/create-template/www-built/app/main.js deleted file mode 100644 index 3614553..0000000 --- a/ITP/create-template/www-built/app/main.js +++ /dev/null @@ -1 +0,0 @@ -define(["require","core"],function(e){var t=e("core");console.log(t)}); \ No newline at end of file diff --git a/ITP/create-template/www-built/app/messages.js b/ITP/create-template/www-built/app/messages.js deleted file mode 100644 index 712255c..0000000 --- a/ITP/create-template/www-built/app/messages.js +++ /dev/null @@ -1 +0,0 @@ -define([],function(){return{getHello:function(){return"Hello World"}}}); \ No newline at end of file diff --git a/ITP/create-template/www-built/build.txt b/ITP/create-template/www-built/build.txt deleted file mode 100644 index 91d9cad..0000000 --- a/ITP/create-template/www-built/build.txt +++ /dev/null @@ -1,6 +0,0 @@ - -app.js ----------------- -lib/core.js -app/main.js -app.js diff --git a/ITP/create-template/www-built/lib/DLtest_01.js b/ITP/create-template/www-built/lib/DLtest_01.js deleted file mode 100644 index 3452291..0000000 --- a/ITP/create-template/www-built/lib/DLtest_01.js +++ /dev/null @@ -1 +0,0 @@ -define(["core"],function(){var e=require("core");return e}); \ No newline at end of file diff --git a/ITP/create-template/www-built/lib/core.js b/ITP/create-template/www-built/lib/core.js deleted file mode 100644 index 5a2c66b..0000000 --- a/ITP/create-template/www-built/lib/core.js +++ /dev/null @@ -1 +0,0 @@ -define([],function(){return{version:function(){return"v.01"}}}); \ No newline at end of file diff --git a/ITP/create-template/www-built/lib/print.js b/ITP/create-template/www-built/lib/print.js deleted file mode 100644 index 8a2c534..0000000 --- a/ITP/create-template/www-built/lib/print.js +++ /dev/null @@ -1 +0,0 @@ -define([],function(){return function(t){document.write(t)}}); \ No newline at end of file diff --git a/ITP/create-template/www-built/lib/require.js b/ITP/create-template/www-built/lib/require.js deleted file mode 100644 index 9c4e293..0000000 --- a/ITP/create-template/www-built/lib/require.js +++ /dev/null @@ -1,7 +0,0 @@ -/** vim: et:ts=4:sw=4:sts=4 - * @license RequireJS 2.1.15 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved. - * Available via the MIT or new BSD license. - * see: http://github.com/jrburke/requirejs for details - */ - -var requirejs,require,define;(function(global){function isFunction(e){return ostring.call(e)==="[object Function]"}function isArray(e){return ostring.call(e)==="[object Array]"}function each(e,t){if(e){var n;for(n=0;n-1;n-=1)if(e[n]&&t(e[n],n,e))break}}function hasProp(e,t){return hasOwn.call(e,t)}function getOwn(e,t){return hasProp(e,t)&&e[t]}function eachProp(e,t){var n;for(n in e)if(hasProp(e,n)&&t(e[n],n))break}function mixin(e,t,n,r){return t&&eachProp(t,function(t,i){if(n||!hasProp(e,i))r&&typeof t=="object"&&t&&!isArray(t)&&!isFunction(t)&&!(t instanceof RegExp)?(e[i]||(e[i]={}),mixin(e[i],t,n,r)):e[i]=t}),e}function bind(e,t){return function(){return t.apply(e,arguments)}}function scripts(){return document.getElementsByTagName("script")}function defaultOnError(e){throw e}function getGlobal(e){if(!e)return e;var t=global;return each(e.split("."),function(e){t=t[e]}),t}function makeError(e,t,n,r){var i=new Error(t+"\nhttp://requirejs.org/docs/errors.html#"+e);return i.requireType=e,i.requireModules=r,n&&(i.originalError=n),i}function newContext(e){function m(e){var t,n;for(t=0;t0&&(e.splice(t-1,2),t-=2)}}}function g(e,t,n){var r,i,s,u,a,f,l,c,h,p,d,v,g=t&&t.split("/"),y=o.map,b=y&&y["*"];e&&(e=e.split("/"),l=e.length-1,o.nodeIdCompat&&jsSuffixRegExp.test(e[l])&&(e[l]=e[l].replace(jsSuffixRegExp,"")),e[0].charAt(0)==="."&&g&&(v=g.slice(0,g.length-1),e=v.concat(e)),m(e),e=e.join("/"));if(n&&y&&(g||b)){s=e.split("/");e:for(u=s.length;u>0;u-=1){f=s.slice(0,u).join("/");if(g)for(a=g.length;a>0;a-=1){i=getOwn(y,g.slice(0,a).join("/"));if(i){i=getOwn(i,f);if(i){c=i,h=u;break e}}}!p&&b&&getOwn(b,f)&&(p=getOwn(b,f),d=u)}!c&&p&&(c=p,h=d),c&&(s.splice(0,h,c),e=s.join("/"))}return r=getOwn(o.pkgs,e),r?r:e}function y(e){isBrowser&&each(scripts(),function(t){if(t.getAttribute("data-requiremodule")===e&&t.getAttribute("data-requirecontext")===r.contextName)return t.parentNode.removeChild(t),!0})}function b(e){var t=getOwn(o.paths,e);if(t&&isArray(t)&&t.length>1)return t.shift(),r.require.undef(e),r.makeRequire(null,{skipMap:!0})([e]),!0}function w(e){var t,n=e?e.indexOf("!"):-1;return n>-1&&(t=e.substring(0,n),e=e.substring(n+1,e.length)),[t,e]}function E(e,t,n,i){var s,o,u,a,f=null,l=t?t.name:null,h=e,p=!0,m="";return e||(p=!1,e="_@r"+(d+=1)),a=w(e),f=a[0],e=a[1],f&&(f=g(f,l,i),o=getOwn(c,f)),e&&(f?o&&o.normalize?m=o.normalize(e,function(e){return g(e,l,i)}):m=e.indexOf("!")===-1?g(e,l,i):e:(m=g(e,l,i),a=w(m),f=a[0],m=a[1],n=!0,s=r.nameToUrl(m))),u=f&&!o&&!n?"_unnormalized"+(v+=1):"",{prefix:f,name:m,parentMap:t,unnormalized:!!u,url:s,originalName:h,isDefine:p,id:(f?f+"!"+m:m)+u}}function S(e){var t=e.id,n=getOwn(u,t);return n||(n=u[t]=new r.Module(e)),n}function x(e,t,n){var r=e.id,i=getOwn(u,r);hasProp(c,r)&&(!i||i.defineEmitComplete)?t==="defined"&&n(c[r]):(i=S(e),i.error&&t==="error"?n(i.error):i.on(t,n))}function T(e,t){var n=e.requireModules,r=!1;t?t(e):(each(n,function(t){var n=getOwn(u,t);n&&(n.error=e,n.events.error&&(r=!0,n.emit("error",e)))}),r||req.onError(e))}function N(){globalDefQueue.length&&(apsp.apply(l,[l.length,0].concat(globalDefQueue)),globalDefQueue=[])}function C(e){delete u[e],delete a[e]}function k(e,t,n){var r=e.map.id;e.error?e.emit("error",e.error):(t[r]=!0,each(e.depMaps,function(r,i){var s=r.id,o=getOwn(u,s);o&&!e.depMatched[i]&&!n[s]&&(getOwn(t,s)?(e.defineDep(i,c[s]),e.check()):k(o,t,n))}),n[r]=!0)}function L(){var e,n,i=o.waitSeconds*1e3,u=i&&r.startTime+i<(new Date).getTime(),f=[],l=[],c=!1,h=!0;if(t)return;t=!0,eachProp(a,function(e){var t=e.map,r=t.id;if(!e.enabled)return;t.isDefine||l.push(e);if(!e.error)if(!e.inited&&u)b(r)?(n=!0,c=!0):(f.push(r),y(r));else if(!e.inited&&e.fetched&&t.isDefine){c=!0;if(!t.prefix)return h=!1}});if(u&&f.length)return e=makeError("timeout","Load timeout for modules: "+f,null,f),e.contextName=r.contextName,T(e);h&&each(l,function(e){k(e,{},{})}),(!u||n)&&c&&(isBrowser||isWebWorker)&&!s&&(s=setTimeout(function(){s=0,L()},50)),t=!1}function A(e){hasProp(c,e[0])||S(E(e[0],null,!0)).init(e[1],e[2])}function O(e,t,n,r){e.detachEvent&&!isOpera?r&&e.detachEvent(r,t):e.removeEventListener(n,t,!1)}function M(e){var t=e.currentTarget||e.srcElement;return O(t,r.onScriptLoad,"load","onreadystatechange"),O(t,r.onScriptError,"error"),{node:t,id:t&&t.getAttribute("data-requiremodule")}}function _(){var e;N();while(l.length){e=l.shift();if(e[0]===null)return T(makeError("mismatch","Mismatched anonymous define() module: "+e[e.length-1]));A(e)}}var t,n,r,i,s,o={waitSeconds:7,baseUrl:"./",paths:{},bundles:{},pkgs:{},shim:{},config:{}},u={},a={},f={},l=[],c={},h={},p={},d=1,v=1;return i={require:function(e){return e.require?e.require:e.require=r.makeRequire(e.map)},exports:function(e){e.usingExports=!0;if(e.map.isDefine)return e.exports?c[e.map.id]=e.exports:e.exports=c[e.map.id]={}},module:function(e){return e.module?e.module:e.module={id:e.map.id,uri:e.map.url,config:function(){return getOwn(o.config,e.map.id)||{}},exports:e.exports||(e.exports={})}}},n=function(e){this.events=getOwn(f,e.id)||{},this.map=e,this.shim=getOwn(o.shim,e.id),this.depExports=[],this.depMaps=[],this.depMatched=[],this.pluginMaps={},this.depCount=0},n.prototype={init:function(e,t,n,r){r=r||{};if(this.inited)return;this.factory=t,n?this.on("error",n):this.events.error&&(n=bind(this,function(e){this.emit("error",e)})),this.depMaps=e&&e.slice(0),this.errback=n,this.inited=!0,this.ignore=r.ignore,r.enabled||this.enabled?this.enable():this.check()},defineDep:function(e,t){this.depMatched[e]||(this.depMatched[e]=!0,this.depCount-=1,this.depExports[e]=t)},fetch:function(){if(this.fetched)return;this.fetched=!0,r.startTime=(new Date).getTime();var e=this.map;if(!this.shim)return e.prefix?this.callPlugin():this.load();r.makeRequire(this.map,{enableBuildCallback:!0})(this.shim.deps||[],bind(this,function(){return e.prefix?this.callPlugin():this.load()}))},load:function(){var e=this.map.url;h[e]||(h[e]=!0,r.load(this.map.id,e))},check:function(){if(!this.enabled||this.enabling)return;var e,t,n=this.map.id,i=this.depExports,s=this.exports,o=this.factory;if(!this.inited)this.fetch();else if(this.error)this.emit("error",this.error);else if(!this.defining){this.defining=!0;if(this.depCount<1&&!this.defined){if(isFunction(o)){if(this.events.error&&this.map.isDefine||req.onError!==defaultOnError)try{s=r.execCb(n,o,i,s)}catch(u){e=u}else s=r.execCb(n,o,i,s);this.map.isDefine&&s===undefined&&(t=this.module,t?s=t.exports:this.usingExports&&(s=this.exports));if(e)return e.requireMap=this.map,e.requireModules=this.map.isDefine?[this.map.id]:null,e.requireType=this.map.isDefine?"define":"require",T(this.error=e)}else s=o;this.exports=s,this.map.isDefine&&!this.ignore&&(c[n]=s,req.onResourceLoad&&req.onResourceLoad(r,this.map,this.depMaps)),C(n),this.defined=!0}this.defining=!1,this.defined&&!this.defineEmitted&&(this.defineEmitted=!0,this.emit("defined",this.exports),this.defineEmitComplete=!0)}},callPlugin:function(){var e=this.map,t=e.id,n=E(e.prefix);this.depMaps.push(n),x(n,"defined",bind(this,function(n){var i,s,a,f=getOwn(p,this.map.id),l=this.map.name,c=this.map.parentMap?this.map.parentMap.name:null,h=r.makeRequire(e.parentMap,{enableBuildCallback:!0});if(this.map.unnormalized){n.normalize&&(l=n.normalize(l,function(e){return g(e,c,!0)})||""),s=E(e.prefix+"!"+l,this.map.parentMap),x(s,"defined",bind(this,function(e){this.init([],function(){return e},null,{enabled:!0,ignore:!0})})),a=getOwn(u,s.id),a&&(this.depMaps.push(s),this.events.error&&a.on("error",bind(this,function(e){this.emit("error",e)})),a.enable());return}if(f){this.map.url=r.nameToUrl(f),this.load();return}i=bind(this,function(e){this.init([],function(){return e},null,{enabled:!0})}),i.error=bind(this,function(e){this.inited=!0,this.error=e,e.requireModules=[t],eachProp(u,function(e){e.map.id.indexOf(t+"_unnormalized")===0&&C(e.map.id)}),T(e)}),i.fromText=bind(this,function(n,s){var u=e.name,a=E(u),f=useInteractive;s&&(n=s),f&&(useInteractive=!1),S(a),hasProp(o.config,t)&&(o.config[u]=o.config[t]);try{req.exec(n)}catch(l){return T(makeError("fromtexteval","fromText eval for "+t+" failed: "+l,l,[t]))}f&&(useInteractive=!0),this.depMaps.push(a),r.completeLoad(u),h([u],i)}),n.load(e.name,h,i,o)})),r.enable(n,this),this.pluginMaps[n.id]=n},enable:function(){a[this.map.id]=this,this.enabled=!0,this.enabling=!0,each(this.depMaps,bind(this,function(e,t){var n,s,o;if(typeof e=="string"){e=E(e,this.map.isDefine?this.map:this.map.parentMap,!1,!this.skipMap),this.depMaps[t]=e,o=getOwn(i,e.id);if(o){this.depExports[t]=o(this);return}this.depCount+=1,x(e,"defined",bind(this,function(e){this.defineDep(t,e),this.check()})),this.errback&&x(e,"error",bind(this,this.errback))}n=e.id,s=u[n],!hasProp(i,n)&&s&&!s.enabled&&r.enable(e,this)})),eachProp(this.pluginMaps,bind(this,function(e){var t=getOwn(u,e.id);t&&!t.enabled&&r.enable(e,this)})),this.enabling=!1,this.check()},on:function(e,t){var n=this.events[e];n||(n=this.events[e]=[]),n.push(t)},emit:function(e,t){each(this.events[e],function(e){e(t)}),e==="error"&&delete this.events[e]}},r={config:o,contextName:e,registry:u,defined:c,urlFetched:h,defQueue:l,Module:n,makeModuleMap:E,nextTick:req.nextTick,onError:T,configure:function(e){e.baseUrl&&e.baseUrl.charAt(e.baseUrl.length-1)!=="/"&&(e.baseUrl+="/");var t=o.shim,n={paths:!0,bundles:!0,config:!0,map:!0};eachProp(e,function(e,t){n[t]?(o[t]||(o[t]={}),mixin(o[t],e,!0,!0)):o[t]=e}),e.bundles&&eachProp(e.bundles,function(e,t){each(e,function(e){e!==t&&(p[e]=t)})}),e.shim&&(eachProp(e.shim,function(e,n){isArray(e)&&(e={deps:e}),(e.exports||e.init)&&!e.exportsFn&&(e.exportsFn=r.makeShimExports(e)),t[n]=e}),o.shim=t),e.packages&&each(e.packages,function(e){var t,n;e=typeof e=="string"?{name:e}:e,n=e.name,t=e.location,t&&(o.paths[n]=e.location),o.pkgs[n]=e.name+"/"+(e.main||"main").replace(currDirRegExp,"").replace(jsSuffixRegExp,"")}),eachProp(u,function(e,t){!e.inited&&!e.map.unnormalized&&(e.map=E(t))}),(e.deps||e.callback)&&r.require(e.deps||[],e.callback)},makeShimExports:function(e){function t(){var t;return e.init&&(t=e.init.apply(global,arguments)),t||e.exports&&getGlobal(e.exports)}return t},makeRequire:function(t,n){function s(o,a,f){var l,h,p;return n.enableBuildCallback&&a&&isFunction(a)&&(a.__requireJsBuild=!0),typeof o=="string"?isFunction(a)?T(makeError("requireargs","Invalid require call"),f):t&&hasProp(i,o)?i[o](u[t.id]):req.get?req.get(r,o,t,s):(h=E(o,t,!1,!0),l=h.id,hasProp(c,l)?c[l]:T(makeError("notloaded",'Module name "'+l+'" has not been loaded yet for context: '+e+(t?"":". Use require([])")))):(_(),r.nextTick(function(){_(),p=S(E(null,t)),p.skipMap=n.skipMap,p.init(o,a,f,{enabled:!0}),L()}),s)}return n=n||{},mixin(s,{isBrowser:isBrowser,toUrl:function(e){var n,i=e.lastIndexOf("."),s=e.split("/")[0],o=s==="."||s==="..";return i!==-1&&(!o||i>1)&&(n=e.substring(i,e.length),e=e.substring(0,i)),r.nameToUrl(g(e,t&&t.id,!0),n,!0)},defined:function(e){return hasProp(c,E(e,t,!1,!0).id)},specified:function(e){return e=E(e,t,!1,!0).id,hasProp(c,e)||hasProp(u,e)}}),t||(s.undef=function(e){N();var n=E(e,t,!0),r=getOwn(u,e);y(e),delete c[e],delete h[n.url],delete f[e],eachReverse(l,function(t,n){t[0]===e&&l.splice(n,1)}),r&&(r.events.defined&&(f[e]=r.events),C(e))}),s},enable:function(e){var t=getOwn(u,e.id);t&&S(e).enable()},completeLoad:function(e){var t,n,r,i=getOwn(o.shim,e)||{},s=i.exports;N();while(l.length){n=l.shift();if(n[0]===null){n[0]=e;if(t)break;t=!0}else n[0]===e&&(t=!0);A(n)}r=getOwn(u,e);if(!t&&!hasProp(c,e)&&r&&!r.inited){if(o.enforceDefine&&(!s||!getGlobal(s))){if(b(e))return;return T(makeError("nodefine","No define call for "+e,null,[e]))}A([e,i.deps||[],i.exportsFn])}L()},nameToUrl:function(e,t,n){var i,s,u,a,f,l,c,h=getOwn(o.pkgs,e);h&&(e=h),c=getOwn(p,e);if(c)return r.nameToUrl(c,t,n);if(req.jsExtRegExp.test(e))f=e+(t||"");else{i=o.paths,s=e.split("/");for(u=s.length;u>0;u-=1){a=s.slice(0,u).join("/"),l=getOwn(i,a);if(l){isArray(l)&&(l=l[0]),s.splice(0,u,l);break}}f=s.join("/"),f+=t||(/^data\:|\?/.test(f)||n?"":".js"),f=(f.charAt(0)==="/"||f.match(/^[\w\+\.\-]+:/)?"":o.baseUrl)+f}return o.urlArgs?f+((f.indexOf("?")===-1?"?":"&")+o.urlArgs):f},load:function(e,t){req.load(r,e,t)},execCb:function(e,t,n,r){return t.apply(r,n)},onScriptLoad:function(e){if(e.type==="load"||readyRegExp.test((e.currentTarget||e.srcElement).readyState)){interactiveScript=null;var t=M(e);r.completeLoad(t.id)}},onScriptError:function(e){var t=M(e);if(!b(t.id))return T(makeError("scripterror","Script error for: "+t.id,e,[t.id]))}},r.require=r.makeRequire(),r}function getInteractiveScript(){return interactiveScript&&interactiveScript.readyState==="interactive"?interactiveScript:(eachReverse(scripts(),function(e){if(e.readyState==="interactive")return interactiveScript=e}),interactiveScript)}var req,s,head,baseElement,dataMain,src,interactiveScript,currentlyAddingScript,mainScript,subPath,version="2.1.15",commentRegExp=/(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,cjsRequireRegExp=/[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,jsSuffixRegExp=/\.js$/,currDirRegExp=/^\.\//,op=Object.prototype,ostring=op.toString,hasOwn=op.hasOwnProperty,ap=Array.prototype,apsp=ap.splice,isBrowser=typeof window!="undefined"&&typeof navigator!="undefined"&&!!window.document,isWebWorker=!isBrowser&&typeof importScripts!="undefined",readyRegExp=isBrowser&&navigator.platform==="PLAYSTATION 3"?/^complete$/:/^(complete|loaded)$/,defContextName="_",isOpera=typeof opera!="undefined"&&opera.toString()==="[object Opera]",contexts={},cfg={},globalDefQueue=[],useInteractive=!1;if(typeof define!="undefined")return;if(typeof requirejs!="undefined"){if(isFunction(requirejs))return;cfg=requirejs,requirejs=undefined}typeof require!="undefined"&&!isFunction(require)&&(cfg=require,require=undefined),req=requirejs=function(e,t,n,r){var i,s,o=defContextName;return!isArray(e)&&typeof e!="string"&&(s=e,isArray(t)?(e=t,t=n,n=r):e=[]),s&&s.context&&(o=s.context),i=getOwn(contexts,o),i||(i=contexts[o]=req.s.newContext(o)),s&&i.configure(s),i.require(e,t,n)},req.config=function(e){return req(e)},req.nextTick=typeof setTimeout!="undefined"?function(e){setTimeout(e,4)}:function(e){e()},require||(require=req),req.version=version,req.jsExtRegExp=/^\/|:|\?|\.js$/,req.isBrowser=isBrowser,s=req.s={contexts:contexts,newContext:newContext},req({}),each(["toUrl","undef","defined","specified"],function(e){req[e]=function(){var t=contexts[defContextName];return t.require[e].apply(t,arguments)}}),isBrowser&&(head=s.head=document.getElementsByTagName("head")[0],baseElement=document.getElementsByTagName("base")[0],baseElement&&(head=s.head=baseElement.parentNode)),req.onError=defaultOnError,req.createNode=function(e,t,n){var r=e.xhtml?document.createElementNS("http://www.w3.org/1999/xhtml","html:script"):document.createElement("script");return r.type=e.scriptType||"text/javascript",r.charset="utf-8",r.async=!0,r},req.load=function(e,t,n){var r=e&&e.config||{},i;if(isBrowser)return i=req.createNode(r,t,n),i.setAttribute("data-requirecontext",e.contextName),i.setAttribute("data-requiremodule",t),i.attachEvent&&!(i.attachEvent.toString&&i.attachEvent.toString().indexOf("[native code")<0)&&!isOpera?(useInteractive=!0,i.attachEvent("onreadystatechange",e.onScriptLoad)):(i.addEventListener("load",e.onScriptLoad,!1),i.addEventListener("error",e.onScriptError,!1)),i.src=n,currentlyAddingScript=i,baseElement?head.insertBefore(i,baseElement):head.appendChild(i),currentlyAddingScript=null,i;if(isWebWorker)try{importScripts(n),e.completeLoad(t)}catch(s){e.onError(makeError("importscripts","importScripts failed for "+t+" at "+n,s,[t]))}},isBrowser&&!cfg.skipDataMain&&eachReverse(scripts(),function(e){head||(head=e.parentNode),dataMain=e.getAttribute("data-main");if(dataMain)return mainScript=dataMain,cfg.baseUrl||(src=mainScript.split("/"),mainScript=src.pop(),subPath=src.length?src.join("/")+"/":"./",cfg.baseUrl=subPath),mainScript=mainScript.replace(jsSuffixRegExp,""),req.jsExtRegExp.test(mainScript)&&(mainScript=dataMain),cfg.deps=cfg.deps?cfg.deps.concat(mainScript):[mainScript],!0}),define=function(e,t,n){var r,i;typeof e!="string"&&(n=t,t=e,e=null),isArray(t)||(n=t,t=null),!t&&isFunction(n)&&(t=[],n.length&&(n.toString().replace(commentRegExp,"").replace(cjsRequireRegExp,function(e,n){t.push(n)}),t=(n.length===1?["require"]:["require","exports","module"]).concat(t))),useInteractive&&(r=currentlyAddingScript||getInteractiveScript(),r&&(e||(e=r.getAttribute("data-requiremodule")),i=contexts[r.getAttribute("data-requirecontext")])),(i?i.defQueue:globalDefQueue).push([e,t,n])},define.amd={jQuery:!0},req.exec=function(text){return eval(text)},req(cfg)})(this); \ No newline at end of file diff --git a/ITP/create-template/www-built/lib/utility/mat.js b/ITP/create-template/www-built/lib/utility/mat.js deleted file mode 100644 index 16a4315..0000000 --- a/ITP/create-template/www-built/lib/utility/mat.js +++ /dev/null @@ -1 +0,0 @@ -function cross(e,t,n){return n===undefined&&(n=[0,0,0]),n[0]=e[1]*t[2]-e[2]*t[1],n[1]=e[2]*t[0]-e[0]*t[2],n[2]=e[0]*t[1]-e[1]*t[0],n}function dot(e,t){return e.length<3||t.length<3?e[0]*t[0]+e[1]*t[1]:e[0]*t[0]+e[1]*t[1]+e[2]*t[2]}function lerp(e,t,n){return t+e*(n-t)}function norm(e){return sqrt(normSqr(e))}function normSqr(e){return dot(e,e)}function vecDiff(e,t){return[e[0]-t[0],e[1]-t[1],e[2]-t[2]]}function vecLerp(e,t,n){return[lerp(e,t[0],n[0]),lerp(e,t[1],n[1]),lerp(e,t[2],n[2])]}function vecScale(e,t){return[e[0]*t,e[1]*t,e[2]*t]}function vecSum(e,t){return[e[0]+t[0],e[1]+t[1],e[2]+t[2]]}function drawUnitDisk(e){renderUnitDisk(mDrawFace,e)}function fillUnitDisk(e){renderUnitDisk(mFillFace,e)}function renderUnitDisk(e,t){var n=[];for(var r=0;r<20;r++){var i=r*TAU/20+PI;n.push([cos(i),sin(i),0])}e(n)}function drawUnitTube(e){renderUnitTube(mDrawFace,e)}function fillUnitTube(e){renderUnitTube(mFillFace,e)}function renderUnitTube(e,t){for(var n=0;n<20;n++){var r=n*TAU/20+PI,i=(n+1)*TAU/20+PI,s=sin(r),o=cos(r),u=sin(i),a=cos(i);e([[o,s,1],[o,s,-1],[a,u,-1],[a,u,1]])}}function unitCubeCorners(){for(var e=0;e0&&drawPolygon(n)}function mFillFace(e,t){t===undefined&&(t="white");var n=[];for(var r=0;r0){var i=color();color("white"),fillPolygon(n),color(i)}}function mCurve(e){var t=[];for(var n=0;n -1; i -= 1) { - if (ary[i] && func(ary[i], i, ary)) { - break; - } - } - } - } - - function hasProp(obj, prop) { - return hasOwn.call(obj, prop); - } - - function getOwn(obj, prop) { - return hasProp(obj, prop) && obj[prop]; - } - - /** - * Cycles over properties in an object and calls a function for each - * property value. If the function returns a truthy value, then the - * iteration is stopped. - */ - function eachProp(obj, func) { - var prop; - for (prop in obj) { - if (hasProp(obj, prop)) { - if (func(obj[prop], prop)) { - break; - } - } - } - } - - /** - * Simple function to mix in properties from source into target, - * but only if target does not already have a property of the same name. - */ - function mixin(target, source, force, deepStringMixin) { - if (source) { - eachProp(source, function (value, prop) { - if (force || !hasProp(target, prop)) { - if (deepStringMixin && typeof value === 'object' && value && - !isArray(value) && !isFunction(value) && - !(value instanceof RegExp)) { - - if (!target[prop]) { - target[prop] = {}; - } - mixin(target[prop], value, force, deepStringMixin); - } else { - target[prop] = value; - } - } - }); - } - return target; - } - - //Similar to Function.prototype.bind, but the 'this' object is specified - //first, since it is easier to read/figure out what 'this' will be. - function bind(obj, fn) { - return function () { - return fn.apply(obj, arguments); - }; - } - - function scripts() { - return document.getElementsByTagName('script'); - } - - function defaultOnError(err) { - throw err; - } - - //Allow getting a global that is expressed in - //dot notation, like 'a.b.c'. - function getGlobal(value) { - if (!value) { - return value; - } - var g = global; - each(value.split('.'), function (part) { - g = g[part]; - }); - return g; - } - - /** - * Constructs an error with a pointer to an URL with more information. - * @param {String} id the error ID that maps to an ID on a web page. - * @param {String} message human readable error. - * @param {Error} [err] the original error, if there is one. - * - * @returns {Error} - */ - function makeError(id, msg, err, requireModules) { - var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#' + id); - e.requireType = id; - e.requireModules = requireModules; - if (err) { - e.originalError = err; - } - return e; - } - - if (typeof define !== 'undefined') { - //If a define is already in play via another AMD loader, - //do not overwrite. - return; - } - - if (typeof requirejs !== 'undefined') { - if (isFunction(requirejs)) { - //Do not overwrite an existing requirejs instance. - return; - } - cfg = requirejs; - requirejs = undefined; - } - - //Allow for a require config object - if (typeof require !== 'undefined' && !isFunction(require)) { - //assume it is a config object. - cfg = require; - require = undefined; - } - - function newContext(contextName) { - var inCheckLoaded, Module, context, handlers, - checkLoadedTimeoutId, - config = { - //Defaults. Do not set a default for map - //config to speed up normalize(), which - //will run faster if there is no default. - waitSeconds: 7, - baseUrl: './', - paths: {}, - bundles: {}, - pkgs: {}, - shim: {}, - config: {} - }, - registry = {}, - //registry of just enabled modules, to speed - //cycle breaking code when lots of modules - //are registered, but not activated. - enabledRegistry = {}, - undefEvents = {}, - defQueue = [], - defined = {}, - urlFetched = {}, - bundlesMap = {}, - requireCounter = 1, - unnormalizedCounter = 1; - - /** - * Trims the . and .. from an array of path segments. - * It will keep a leading path segment if a .. will become - * the first path segment, to help with module name lookups, - * which act like paths, but can be remapped. But the end result, - * all paths that use this function should look normalized. - * NOTE: this method MODIFIES the input array. - * @param {Array} ary the array of path segments. - */ - function trimDots(ary) { - var i, part; - for (i = 0; i < ary.length; i++) { - part = ary[i]; - if (part === '.') { - ary.splice(i, 1); - i -= 1; - } else if (part === '..') { - // If at the start, or previous value is still .., - // keep them so that when converted to a path it may - // still work when converted to a path, even though - // as an ID it is less than ideal. In larger point - // releases, may be better to just kick out an error. - if (i === 0 || (i == 1 && ary[2] === '..') || ary[i - 1] === '..') { - continue; - } else if (i > 0) { - ary.splice(i - 1, 2); - i -= 2; - } - } - } - } - - /** - * Given a relative module name, like ./something, normalize it to - * a real name that can be mapped to a path. - * @param {String} name the relative name - * @param {String} baseName a real name that the name arg is relative - * to. - * @param {Boolean} applyMap apply the map config to the value. Should - * only be done if this normalization is for a dependency ID. - * @returns {String} normalized name - */ - function normalize(name, baseName, applyMap) { - var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex, - foundMap, foundI, foundStarMap, starI, normalizedBaseParts, - baseParts = (baseName && baseName.split('/')), - map = config.map, - starMap = map && map['*']; - - //Adjust any relative paths. - if (name) { - name = name.split('/'); - lastIndex = name.length - 1; - - // If wanting node ID compatibility, strip .js from end - // of IDs. Have to do this here, and not in nameToUrl - // because node allows either .js or non .js to map - // to same file. - if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) { - name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, ''); - } - - // Starts with a '.' so need the baseName - if (name[0].charAt(0) === '.' && baseParts) { - //Convert baseName to array, and lop off the last part, - //so that . matches that 'directory' and not name of the baseName's - //module. For instance, baseName of 'one/two/three', maps to - //'one/two/three.js', but we want the directory, 'one/two' for - //this normalization. - normalizedBaseParts = baseParts.slice(0, baseParts.length - 1); - name = normalizedBaseParts.concat(name); - } - - trimDots(name); - name = name.join('/'); - } - - //Apply map config if available. - if (applyMap && map && (baseParts || starMap)) { - nameParts = name.split('/'); - - outerLoop: for (i = nameParts.length; i > 0; i -= 1) { - nameSegment = nameParts.slice(0, i).join('/'); - - if (baseParts) { - //Find the longest baseName segment match in the config. - //So, do joins on the biggest to smallest lengths of baseParts. - for (j = baseParts.length; j > 0; j -= 1) { - mapValue = getOwn(map, baseParts.slice(0, j).join('/')); - - //baseName segment has config, find if it has one for - //this name. - if (mapValue) { - mapValue = getOwn(mapValue, nameSegment); - if (mapValue) { - //Match, update name to the new value. - foundMap = mapValue; - foundI = i; - break outerLoop; - } - } - } - } - - //Check for a star map match, but just hold on to it, - //if there is a shorter segment match later in a matching - //config, then favor over this star map. - if (!foundStarMap && starMap && getOwn(starMap, nameSegment)) { - foundStarMap = getOwn(starMap, nameSegment); - starI = i; - } - } - - if (!foundMap && foundStarMap) { - foundMap = foundStarMap; - foundI = starI; - } - - if (foundMap) { - nameParts.splice(0, foundI, foundMap); - name = nameParts.join('/'); - } - } - - // If the name points to a package's name, use - // the package main instead. - pkgMain = getOwn(config.pkgs, name); - - return pkgMain ? pkgMain : name; - } - - function removeScript(name) { - if (isBrowser) { - each(scripts(), function (scriptNode) { - if (scriptNode.getAttribute('data-requiremodule') === name && - scriptNode.getAttribute('data-requirecontext') === context.contextName) { - scriptNode.parentNode.removeChild(scriptNode); - return true; - } - }); - } - } - - function hasPathFallback(id) { - var pathConfig = getOwn(config.paths, id); - if (pathConfig && isArray(pathConfig) && pathConfig.length > 1) { - //Pop off the first array value, since it failed, and - //retry - pathConfig.shift(); - context.require.undef(id); - - //Custom require that does not do map translation, since - //ID is "absolute", already mapped/resolved. - context.makeRequire(null, { - skipMap: true - })([id]); - - return true; - } - } - - //Turns a plugin!resource to [plugin, resource] - //with the plugin being undefined if the name - //did not have a plugin prefix. - function splitPrefix(name) { - var prefix, - index = name ? name.indexOf('!') : -1; - if (index > -1) { - prefix = name.substring(0, index); - name = name.substring(index + 1, name.length); - } - return [prefix, name]; - } - - /** - * Creates a module mapping that includes plugin prefix, module - * name, and path. If parentModuleMap is provided it will - * also normalize the name via require.normalize() - * - * @param {String} name the module name - * @param {String} [parentModuleMap] parent module map - * for the module name, used to resolve relative names. - * @param {Boolean} isNormalized: is the ID already normalized. - * This is true if this call is done for a define() module ID. - * @param {Boolean} applyMap: apply the map config to the ID. - * Should only be true if this map is for a dependency. - * - * @returns {Object} - */ - function makeModuleMap(name, parentModuleMap, isNormalized, applyMap) { - var url, pluginModule, suffix, nameParts, - prefix = null, - parentName = parentModuleMap ? parentModuleMap.name : null, - originalName = name, - isDefine = true, - normalizedName = ''; - - //If no name, then it means it is a require call, generate an - //internal name. - if (!name) { - isDefine = false; - name = '_@r' + (requireCounter += 1); - } - - nameParts = splitPrefix(name); - prefix = nameParts[0]; - name = nameParts[1]; - - if (prefix) { - prefix = normalize(prefix, parentName, applyMap); - pluginModule = getOwn(defined, prefix); - } - - //Account for relative paths if there is a base name. - if (name) { - if (prefix) { - if (pluginModule && pluginModule.normalize) { - //Plugin is loaded, use its normalize method. - normalizedName = pluginModule.normalize(name, function (name) { - return normalize(name, parentName, applyMap); - }); - } else { - // If nested plugin references, then do not try to - // normalize, as it will not normalize correctly. This - // places a restriction on resourceIds, and the longer - // term solution is not to normalize until plugins are - // loaded and all normalizations to allow for async - // loading of a loader plugin. But for now, fixes the - // common uses. Details in #1131 - normalizedName = name.indexOf('!') === -1 ? - normalize(name, parentName, applyMap) : - name; - } - } else { - //A regular module. - normalizedName = normalize(name, parentName, applyMap); - - //Normalized name may be a plugin ID due to map config - //application in normalize. The map config values must - //already be normalized, so do not need to redo that part. - nameParts = splitPrefix(normalizedName); - prefix = nameParts[0]; - normalizedName = nameParts[1]; - isNormalized = true; - - url = context.nameToUrl(normalizedName); - } - } - - //If the id is a plugin id that cannot be determined if it needs - //normalization, stamp it with a unique ID so two matching relative - //ids that may conflict can be separate. - suffix = prefix && !pluginModule && !isNormalized ? - '_unnormalized' + (unnormalizedCounter += 1) : - ''; - - return { - prefix: prefix, - name: normalizedName, - parentMap: parentModuleMap, - unnormalized: !!suffix, - url: url, - originalName: originalName, - isDefine: isDefine, - id: (prefix ? - prefix + '!' + normalizedName : - normalizedName) + suffix - }; - } - - function getModule(depMap) { - var id = depMap.id, - mod = getOwn(registry, id); - - if (!mod) { - mod = registry[id] = new context.Module(depMap); - } - - return mod; - } - - function on(depMap, name, fn) { - var id = depMap.id, - mod = getOwn(registry, id); - - if (hasProp(defined, id) && - (!mod || mod.defineEmitComplete)) { - if (name === 'defined') { - fn(defined[id]); - } - } else { - mod = getModule(depMap); - if (mod.error && name === 'error') { - fn(mod.error); - } else { - mod.on(name, fn); - } - } - } - - function onError(err, errback) { - var ids = err.requireModules, - notified = false; - - if (errback) { - errback(err); - } else { - each(ids, function (id) { - var mod = getOwn(registry, id); - if (mod) { - //Set error on module, so it skips timeout checks. - mod.error = err; - if (mod.events.error) { - notified = true; - mod.emit('error', err); - } - } - }); - - if (!notified) { - req.onError(err); - } - } - } - - /** - * Internal method to transfer globalQueue items to this context's - * defQueue. - */ - function takeGlobalQueue() { - //Push all the globalDefQueue items into the context's defQueue - if (globalDefQueue.length) { - //Array splice in the values since the context code has a - //local var ref to defQueue, so cannot just reassign the one - //on context. - apsp.apply(defQueue, - [defQueue.length, 0].concat(globalDefQueue)); - globalDefQueue = []; - } - } - - handlers = { - 'require': function (mod) { - if (mod.require) { - return mod.require; - } else { - return (mod.require = context.makeRequire(mod.map)); - } - }, - 'exports': function (mod) { - mod.usingExports = true; - if (mod.map.isDefine) { - if (mod.exports) { - return (defined[mod.map.id] = mod.exports); - } else { - return (mod.exports = defined[mod.map.id] = {}); - } - } - }, - 'module': function (mod) { - if (mod.module) { - return mod.module; - } else { - return (mod.module = { - id: mod.map.id, - uri: mod.map.url, - config: function () { - return getOwn(config.config, mod.map.id) || {}; - }, - exports: mod.exports || (mod.exports = {}) - }); - } - } - }; - - function cleanRegistry(id) { - //Clean up machinery used for waiting modules. - delete registry[id]; - delete enabledRegistry[id]; - } - - function breakCycle(mod, traced, processed) { - var id = mod.map.id; - - if (mod.error) { - mod.emit('error', mod.error); - } else { - traced[id] = true; - each(mod.depMaps, function (depMap, i) { - var depId = depMap.id, - dep = getOwn(registry, depId); - - //Only force things that have not completed - //being defined, so still in the registry, - //and only if it has not been matched up - //in the module already. - if (dep && !mod.depMatched[i] && !processed[depId]) { - if (getOwn(traced, depId)) { - mod.defineDep(i, defined[depId]); - mod.check(); //pass false? - } else { - breakCycle(dep, traced, processed); - } - } - }); - processed[id] = true; - } - } - - function checkLoaded() { - var err, usingPathFallback, - waitInterval = config.waitSeconds * 1000, - //It is possible to disable the wait interval by using waitSeconds of 0. - expired = waitInterval && (context.startTime + waitInterval) < new Date().getTime(), - noLoads = [], - reqCalls = [], - stillLoading = false, - needCycleCheck = true; - - //Do not bother if this call was a result of a cycle break. - if (inCheckLoaded) { - return; - } - - inCheckLoaded = true; - - //Figure out the state of all the modules. - eachProp(enabledRegistry, function (mod) { - var map = mod.map, - modId = map.id; - - //Skip things that are not enabled or in error state. - if (!mod.enabled) { - return; - } - - if (!map.isDefine) { - reqCalls.push(mod); - } - - if (!mod.error) { - //If the module should be executed, and it has not - //been inited and time is up, remember it. - if (!mod.inited && expired) { - if (hasPathFallback(modId)) { - usingPathFallback = true; - stillLoading = true; - } else { - noLoads.push(modId); - removeScript(modId); - } - } else if (!mod.inited && mod.fetched && map.isDefine) { - stillLoading = true; - if (!map.prefix) { - //No reason to keep looking for unfinished - //loading. If the only stillLoading is a - //plugin resource though, keep going, - //because it may be that a plugin resource - //is waiting on a non-plugin cycle. - return (needCycleCheck = false); - } - } - } - }); - - if (expired && noLoads.length) { - //If wait time expired, throw error of unloaded modules. - err = makeError('timeout', 'Load timeout for modules: ' + noLoads, null, noLoads); - err.contextName = context.contextName; - return onError(err); - } - - //Not expired, check for a cycle. - if (needCycleCheck) { - each(reqCalls, function (mod) { - breakCycle(mod, {}, {}); - }); - } - - //If still waiting on loads, and the waiting load is something - //other than a plugin resource, or there are still outstanding - //scripts, then just try back later. - if ((!expired || usingPathFallback) && stillLoading) { - //Something is still waiting to load. Wait for it, but only - //if a timeout is not already in effect. - if ((isBrowser || isWebWorker) && !checkLoadedTimeoutId) { - checkLoadedTimeoutId = setTimeout(function () { - checkLoadedTimeoutId = 0; - checkLoaded(); - }, 50); - } - } - - inCheckLoaded = false; - } - - Module = function (map) { - this.events = getOwn(undefEvents, map.id) || {}; - this.map = map; - this.shim = getOwn(config.shim, map.id); - this.depExports = []; - this.depMaps = []; - this.depMatched = []; - this.pluginMaps = {}; - this.depCount = 0; - - /* this.exports this.factory - this.depMaps = [], - this.enabled, this.fetched - */ - }; - - Module.prototype = { - init: function (depMaps, factory, errback, options) { - options = options || {}; - - //Do not do more inits if already done. Can happen if there - //are multiple define calls for the same module. That is not - //a normal, common case, but it is also not unexpected. - if (this.inited) { - return; - } - - this.factory = factory; - - if (errback) { - //Register for errors on this module. - this.on('error', errback); - } else if (this.events.error) { - //If no errback already, but there are error listeners - //on this module, set up an errback to pass to the deps. - errback = bind(this, function (err) { - this.emit('error', err); - }); - } - - //Do a copy of the dependency array, so that - //source inputs are not modified. For example - //"shim" deps are passed in here directly, and - //doing a direct modification of the depMaps array - //would affect that config. - this.depMaps = depMaps && depMaps.slice(0); - - this.errback = errback; - - //Indicate this module has be initialized - this.inited = true; - - this.ignore = options.ignore; - - //Could have option to init this module in enabled mode, - //or could have been previously marked as enabled. However, - //the dependencies are not known until init is called. So - //if enabled previously, now trigger dependencies as enabled. - if (options.enabled || this.enabled) { - //Enable this module and dependencies. - //Will call this.check() - this.enable(); - } else { - this.check(); - } - }, - - defineDep: function (i, depExports) { - //Because of cycles, defined callback for a given - //export can be called more than once. - if (!this.depMatched[i]) { - this.depMatched[i] = true; - this.depCount -= 1; - this.depExports[i] = depExports; - } - }, - - fetch: function () { - if (this.fetched) { - return; - } - this.fetched = true; - - context.startTime = (new Date()).getTime(); - - var map = this.map; - - //If the manager is for a plugin managed resource, - //ask the plugin to load it now. - if (this.shim) { - context.makeRequire(this.map, { - enableBuildCallback: true - })(this.shim.deps || [], bind(this, function () { - return map.prefix ? this.callPlugin() : this.load(); - })); - } else { - //Regular dependency. - return map.prefix ? this.callPlugin() : this.load(); - } - }, - - load: function () { - var url = this.map.url; - - //Regular dependency. - if (!urlFetched[url]) { - urlFetched[url] = true; - context.load(this.map.id, url); - } - }, - - /** - * Checks if the module is ready to define itself, and if so, - * define it. - */ - check: function () { - if (!this.enabled || this.enabling) { - return; - } - - var err, cjsModule, - id = this.map.id, - depExports = this.depExports, - exports = this.exports, - factory = this.factory; - - if (!this.inited) { - this.fetch(); - } else if (this.error) { - this.emit('error', this.error); - } else if (!this.defining) { - //The factory could trigger another require call - //that would result in checking this module to - //define itself again. If already in the process - //of doing that, skip this work. - this.defining = true; - - if (this.depCount < 1 && !this.defined) { - if (isFunction(factory)) { - //If there is an error listener, favor passing - //to that instead of throwing an error. However, - //only do it for define()'d modules. require - //errbacks should not be called for failures in - //their callbacks (#699). However if a global - //onError is set, use that. - if ((this.events.error && this.map.isDefine) || - req.onError !== defaultOnError) { - try { - exports = context.execCb(id, factory, depExports, exports); - } catch (e) { - err = e; - } - } else { - exports = context.execCb(id, factory, depExports, exports); - } - - // Favor return value over exports. If node/cjs in play, - // then will not have a return value anyway. Favor - // module.exports assignment over exports object. - if (this.map.isDefine && exports === undefined) { - cjsModule = this.module; - if (cjsModule) { - exports = cjsModule.exports; - } else if (this.usingExports) { - //exports already set the defined value. - exports = this.exports; - } - } - - if (err) { - err.requireMap = this.map; - err.requireModules = this.map.isDefine ? [this.map.id] : null; - err.requireType = this.map.isDefine ? 'define' : 'require'; - return onError((this.error = err)); - } - - } else { - //Just a literal value - exports = factory; - } - - this.exports = exports; - - if (this.map.isDefine && !this.ignore) { - defined[id] = exports; - - if (req.onResourceLoad) { - req.onResourceLoad(context, this.map, this.depMaps); - } - } - - //Clean up - cleanRegistry(id); - - this.defined = true; - } - - //Finished the define stage. Allow calling check again - //to allow define notifications below in the case of a - //cycle. - this.defining = false; - - if (this.defined && !this.defineEmitted) { - this.defineEmitted = true; - this.emit('defined', this.exports); - this.defineEmitComplete = true; - } - - } - }, - - callPlugin: function () { - var map = this.map, - id = map.id, - //Map already normalized the prefix. - pluginMap = makeModuleMap(map.prefix); - - //Mark this as a dependency for this plugin, so it - //can be traced for cycles. - this.depMaps.push(pluginMap); - - on(pluginMap, 'defined', bind(this, function (plugin) { - var load, normalizedMap, normalizedMod, - bundleId = getOwn(bundlesMap, this.map.id), - name = this.map.name, - parentName = this.map.parentMap ? this.map.parentMap.name : null, - localRequire = context.makeRequire(map.parentMap, { - enableBuildCallback: true - }); - - //If current map is not normalized, wait for that - //normalized name to load instead of continuing. - if (this.map.unnormalized) { - //Normalize the ID if the plugin allows it. - if (plugin.normalize) { - name = plugin.normalize(name, function (name) { - return normalize(name, parentName, true); - }) || ''; - } - - //prefix and name should already be normalized, no need - //for applying map config again either. - normalizedMap = makeModuleMap(map.prefix + '!' + name, - this.map.parentMap); - on(normalizedMap, - 'defined', bind(this, function (value) { - this.init([], function () { return value; }, null, { - enabled: true, - ignore: true - }); - })); - - normalizedMod = getOwn(registry, normalizedMap.id); - if (normalizedMod) { - //Mark this as a dependency for this plugin, so it - //can be traced for cycles. - this.depMaps.push(normalizedMap); - - if (this.events.error) { - normalizedMod.on('error', bind(this, function (err) { - this.emit('error', err); - })); - } - normalizedMod.enable(); - } - - return; - } - - //If a paths config, then just load that file instead to - //resolve the plugin, as it is built into that paths layer. - if (bundleId) { - this.map.url = context.nameToUrl(bundleId); - this.load(); - return; - } - - load = bind(this, function (value) { - this.init([], function () { return value; }, null, { - enabled: true - }); - }); - - load.error = bind(this, function (err) { - this.inited = true; - this.error = err; - err.requireModules = [id]; - - //Remove temp unnormalized modules for this module, - //since they will never be resolved otherwise now. - eachProp(registry, function (mod) { - if (mod.map.id.indexOf(id + '_unnormalized') === 0) { - cleanRegistry(mod.map.id); - } - }); - - onError(err); - }); - - //Allow plugins to load other code without having to know the - //context or how to 'complete' the load. - load.fromText = bind(this, function (text, textAlt) { - /*jslint evil: true */ - var moduleName = map.name, - moduleMap = makeModuleMap(moduleName), - hasInteractive = useInteractive; - - //As of 2.1.0, support just passing the text, to reinforce - //fromText only being called once per resource. Still - //support old style of passing moduleName but discard - //that moduleName in favor of the internal ref. - if (textAlt) { - text = textAlt; - } - - //Turn off interactive script matching for IE for any define - //calls in the text, then turn it back on at the end. - if (hasInteractive) { - useInteractive = false; - } - - //Prime the system by creating a module instance for - //it. - getModule(moduleMap); - - //Transfer any config to this other module. - if (hasProp(config.config, id)) { - config.config[moduleName] = config.config[id]; - } - - try { - req.exec(text); - } catch (e) { - return onError(makeError('fromtexteval', - 'fromText eval for ' + id + - ' failed: ' + e, - e, - [id])); - } - - if (hasInteractive) { - useInteractive = true; - } - - //Mark this as a dependency for the plugin - //resource - this.depMaps.push(moduleMap); - - //Support anonymous modules. - context.completeLoad(moduleName); - - //Bind the value of that module to the value for this - //resource ID. - localRequire([moduleName], load); - }); - - //Use parentName here since the plugin's name is not reliable, - //could be some weird string with no path that actually wants to - //reference the parentName's path. - plugin.load(map.name, localRequire, load, config); - })); - - context.enable(pluginMap, this); - this.pluginMaps[pluginMap.id] = pluginMap; - }, - - enable: function () { - enabledRegistry[this.map.id] = this; - this.enabled = true; - - //Set flag mentioning that the module is enabling, - //so that immediate calls to the defined callbacks - //for dependencies do not trigger inadvertent load - //with the depCount still being zero. - this.enabling = true; - - //Enable each dependency - each(this.depMaps, bind(this, function (depMap, i) { - var id, mod, handler; - - if (typeof depMap === 'string') { - //Dependency needs to be converted to a depMap - //and wired up to this module. - depMap = makeModuleMap(depMap, - (this.map.isDefine ? this.map : this.map.parentMap), - false, - !this.skipMap); - this.depMaps[i] = depMap; - - handler = getOwn(handlers, depMap.id); - - if (handler) { - this.depExports[i] = handler(this); - return; - } - - this.depCount += 1; - - on(depMap, 'defined', bind(this, function (depExports) { - this.defineDep(i, depExports); - this.check(); - })); - - if (this.errback) { - on(depMap, 'error', bind(this, this.errback)); - } - } - - id = depMap.id; - mod = registry[id]; - - //Skip special modules like 'require', 'exports', 'module' - //Also, don't call enable if it is already enabled, - //important in circular dependency cases. - if (!hasProp(handlers, id) && mod && !mod.enabled) { - context.enable(depMap, this); - } - })); - - //Enable each plugin that is used in - //a dependency - eachProp(this.pluginMaps, bind(this, function (pluginMap) { - var mod = getOwn(registry, pluginMap.id); - if (mod && !mod.enabled) { - context.enable(pluginMap, this); - } - })); - - this.enabling = false; - - this.check(); - }, - - on: function (name, cb) { - var cbs = this.events[name]; - if (!cbs) { - cbs = this.events[name] = []; - } - cbs.push(cb); - }, - - emit: function (name, evt) { - each(this.events[name], function (cb) { - cb(evt); - }); - if (name === 'error') { - //Now that the error handler was triggered, remove - //the listeners, since this broken Module instance - //can stay around for a while in the registry. - delete this.events[name]; - } - } - }; - - function callGetModule(args) { - //Skip modules already defined. - if (!hasProp(defined, args[0])) { - getModule(makeModuleMap(args[0], null, true)).init(args[1], args[2]); - } - } - - function removeListener(node, func, name, ieName) { - //Favor detachEvent because of IE9 - //issue, see attachEvent/addEventListener comment elsewhere - //in this file. - if (node.detachEvent && !isOpera) { - //Probably IE. If not it will throw an error, which will be - //useful to know. - if (ieName) { - node.detachEvent(ieName, func); - } - } else { - node.removeEventListener(name, func, false); - } - } - - /** - * Given an event from a script node, get the requirejs info from it, - * and then removes the event listeners on the node. - * @param {Event} evt - * @returns {Object} - */ - function getScriptData(evt) { - //Using currentTarget instead of target for Firefox 2.0's sake. Not - //all old browsers will be supported, but this one was easy enough - //to support and still makes sense. - var node = evt.currentTarget || evt.srcElement; - - //Remove the listeners once here. - removeListener(node, context.onScriptLoad, 'load', 'onreadystatechange'); - removeListener(node, context.onScriptError, 'error'); - - return { - node: node, - id: node && node.getAttribute('data-requiremodule') - }; - } - - function intakeDefines() { - var args; - - //Any defined modules in the global queue, intake them now. - takeGlobalQueue(); - - //Make sure any remaining defQueue items get properly processed. - while (defQueue.length) { - args = defQueue.shift(); - if (args[0] === null) { - return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1])); - } else { - //args are id, deps, factory. Should be normalized by the - //define() function. - callGetModule(args); - } - } - } - - context = { - config: config, - contextName: contextName, - registry: registry, - defined: defined, - urlFetched: urlFetched, - defQueue: defQueue, - Module: Module, - makeModuleMap: makeModuleMap, - nextTick: req.nextTick, - onError: onError, - - /** - * Set a configuration for the context. - * @param {Object} cfg config object to integrate. - */ - configure: function (cfg) { - //Make sure the baseUrl ends in a slash. - if (cfg.baseUrl) { - if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== '/') { - cfg.baseUrl += '/'; - } - } - - //Save off the paths since they require special processing, - //they are additive. - var shim = config.shim, - objs = { - paths: true, - bundles: true, - config: true, - map: true - }; - - eachProp(cfg, function (value, prop) { - if (objs[prop]) { - if (!config[prop]) { - config[prop] = {}; - } - mixin(config[prop], value, true, true); - } else { - config[prop] = value; - } - }); - - //Reverse map the bundles - if (cfg.bundles) { - eachProp(cfg.bundles, function (value, prop) { - each(value, function (v) { - if (v !== prop) { - bundlesMap[v] = prop; - } - }); - }); - } - - //Merge shim - if (cfg.shim) { - eachProp(cfg.shim, function (value, id) { - //Normalize the structure - if (isArray(value)) { - value = { - deps: value - }; - } - if ((value.exports || value.init) && !value.exportsFn) { - value.exportsFn = context.makeShimExports(value); - } - shim[id] = value; - }); - config.shim = shim; - } - - //Adjust packages if necessary. - if (cfg.packages) { - each(cfg.packages, function (pkgObj) { - var location, name; - - pkgObj = typeof pkgObj === 'string' ? { name: pkgObj } : pkgObj; - - name = pkgObj.name; - location = pkgObj.location; - if (location) { - config.paths[name] = pkgObj.location; - } - - //Save pointer to main module ID for pkg name. - //Remove leading dot in main, so main paths are normalized, - //and remove any trailing .js, since different package - //envs have different conventions: some use a module name, - //some use a file name. - config.pkgs[name] = pkgObj.name + '/' + (pkgObj.main || 'main') - .replace(currDirRegExp, '') - .replace(jsSuffixRegExp, ''); - }); - } - - //If there are any "waiting to execute" modules in the registry, - //update the maps for them, since their info, like URLs to load, - //may have changed. - eachProp(registry, function (mod, id) { - //If module already has init called, since it is too - //late to modify them, and ignore unnormalized ones - //since they are transient. - if (!mod.inited && !mod.map.unnormalized) { - mod.map = makeModuleMap(id); - } - }); - - //If a deps array or a config callback is specified, then call - //require with those args. This is useful when require is defined as a - //config object before require.js is loaded. - if (cfg.deps || cfg.callback) { - context.require(cfg.deps || [], cfg.callback); - } - }, - - makeShimExports: function (value) { - function fn() { - var ret; - if (value.init) { - ret = value.init.apply(global, arguments); - } - return ret || (value.exports && getGlobal(value.exports)); - } - return fn; - }, - - makeRequire: function (relMap, options) { - options = options || {}; - - function localRequire(deps, callback, errback) { - var id, map, requireMod; - - if (options.enableBuildCallback && callback && isFunction(callback)) { - callback.__requireJsBuild = true; - } - - if (typeof deps === 'string') { - if (isFunction(callback)) { - //Invalid call - return onError(makeError('requireargs', 'Invalid require call'), errback); - } - - //If require|exports|module are requested, get the - //value for them from the special handlers. Caveat: - //this only works while module is being defined. - if (relMap && hasProp(handlers, deps)) { - return handlers[deps](registry[relMap.id]); - } - - //Synchronous access to one module. If require.get is - //available (as in the Node adapter), prefer that. - if (req.get) { - return req.get(context, deps, relMap, localRequire); - } - - //Normalize module name, if it contains . or .. - map = makeModuleMap(deps, relMap, false, true); - id = map.id; - - if (!hasProp(defined, id)) { - return onError(makeError('notloaded', 'Module name "' + - id + - '" has not been loaded yet for context: ' + - contextName + - (relMap ? '' : '. Use require([])'))); - } - return defined[id]; - } - - //Grab defines waiting in the global queue. - intakeDefines(); - - //Mark all the dependencies as needing to be loaded. - context.nextTick(function () { - //Some defines could have been added since the - //require call, collect them. - intakeDefines(); - - requireMod = getModule(makeModuleMap(null, relMap)); - - //Store if map config should be applied to this require - //call for dependencies. - requireMod.skipMap = options.skipMap; - - requireMod.init(deps, callback, errback, { - enabled: true - }); - - checkLoaded(); - }); - - return localRequire; - } - - mixin(localRequire, { - isBrowser: isBrowser, - - /** - * Converts a module name + .extension into an URL path. - * *Requires* the use of a module name. It does not support using - * plain URLs like nameToUrl. - */ - toUrl: function (moduleNamePlusExt) { - var ext, - index = moduleNamePlusExt.lastIndexOf('.'), - segment = moduleNamePlusExt.split('/')[0], - isRelative = segment === '.' || segment === '..'; - - //Have a file extension alias, and it is not the - //dots from a relative path. - if (index !== -1 && (!isRelative || index > 1)) { - ext = moduleNamePlusExt.substring(index, moduleNamePlusExt.length); - moduleNamePlusExt = moduleNamePlusExt.substring(0, index); - } - - return context.nameToUrl(normalize(moduleNamePlusExt, - relMap && relMap.id, true), ext, true); - }, - - defined: function (id) { - return hasProp(defined, makeModuleMap(id, relMap, false, true).id); - }, - - specified: function (id) { - id = makeModuleMap(id, relMap, false, true).id; - return hasProp(defined, id) || hasProp(registry, id); - } - }); - - //Only allow undef on top level require calls - if (!relMap) { - localRequire.undef = function (id) { - //Bind any waiting define() calls to this context, - //fix for #408 - takeGlobalQueue(); - - var map = makeModuleMap(id, relMap, true), - mod = getOwn(registry, id); - - removeScript(id); - - delete defined[id]; - delete urlFetched[map.url]; - delete undefEvents[id]; - - //Clean queued defines too. Go backwards - //in array so that the splices do not - //mess up the iteration. - eachReverse(defQueue, function(args, i) { - if(args[0] === id) { - defQueue.splice(i, 1); - } - }); - - if (mod) { - //Hold on to listeners in case the - //module will be attempted to be reloaded - //using a different config. - if (mod.events.defined) { - undefEvents[id] = mod.events; - } - - cleanRegistry(id); - } - }; - } - - return localRequire; - }, - - /** - * Called to enable a module if it is still in the registry - * awaiting enablement. A second arg, parent, the parent module, - * is passed in for context, when this method is overridden by - * the optimizer. Not shown here to keep code compact. - */ - enable: function (depMap) { - var mod = getOwn(registry, depMap.id); - if (mod) { - getModule(depMap).enable(); - } - }, - - /** - * Internal method used by environment adapters to complete a load event. - * A load event could be a script load or just a load pass from a synchronous - * load call. - * @param {String} moduleName the name of the module to potentially complete. - */ - completeLoad: function (moduleName) { - var found, args, mod, - shim = getOwn(config.shim, moduleName) || {}, - shExports = shim.exports; - - takeGlobalQueue(); - - while (defQueue.length) { - args = defQueue.shift(); - if (args[0] === null) { - args[0] = moduleName; - //If already found an anonymous module and bound it - //to this name, then this is some other anon module - //waiting for its completeLoad to fire. - if (found) { - break; - } - found = true; - } else if (args[0] === moduleName) { - //Found matching define call for this script! - found = true; - } - - callGetModule(args); - } - - //Do this after the cycle of callGetModule in case the result - //of those calls/init calls changes the registry. - mod = getOwn(registry, moduleName); - - if (!found && !hasProp(defined, moduleName) && mod && !mod.inited) { - if (config.enforceDefine && (!shExports || !getGlobal(shExports))) { - if (hasPathFallback(moduleName)) { - return; - } else { - return onError(makeError('nodefine', - 'No define call for ' + moduleName, - null, - [moduleName])); - } - } else { - //A script that does not call define(), so just simulate - //the call for it. - callGetModule([moduleName, (shim.deps || []), shim.exportsFn]); - } - } - - checkLoaded(); - }, - - /** - * Converts a module name to a file path. Supports cases where - * moduleName may actually be just an URL. - * Note that it **does not** call normalize on the moduleName, - * it is assumed to have already been normalized. This is an - * internal API, not a public one. Use toUrl for the public API. - */ - nameToUrl: function (moduleName, ext, skipExt) { - var paths, syms, i, parentModule, url, - parentPath, bundleId, - pkgMain = getOwn(config.pkgs, moduleName); - - if (pkgMain) { - moduleName = pkgMain; - } - - bundleId = getOwn(bundlesMap, moduleName); - - if (bundleId) { - return context.nameToUrl(bundleId, ext, skipExt); - } - - //If a colon is in the URL, it indicates a protocol is used and it is just - //an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?) - //or ends with .js, then assume the user meant to use an url and not a module id. - //The slash is important for protocol-less URLs as well as full paths. - if (req.jsExtRegExp.test(moduleName)) { - //Just a plain path, not module name lookup, so just return it. - //Add extension if it is included. This is a bit wonky, only non-.js things pass - //an extension, this method probably needs to be reworked. - url = moduleName + (ext || ''); - } else { - //A module that needs to be converted to a path. - paths = config.paths; - - syms = moduleName.split('/'); - //For each module name segment, see if there is a path - //registered for it. Start with most specific name - //and work up from it. - for (i = syms.length; i > 0; i -= 1) { - parentModule = syms.slice(0, i).join('/'); - - parentPath = getOwn(paths, parentModule); - if (parentPath) { - //If an array, it means there are a few choices, - //Choose the one that is desired - if (isArray(parentPath)) { - parentPath = parentPath[0]; - } - syms.splice(0, i, parentPath); - break; - } - } - - //Join the path parts together, then figure out if baseUrl is needed. - url = syms.join('/'); - url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js')); - url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url; - } - - return config.urlArgs ? url + - ((url.indexOf('?') === -1 ? '?' : '&') + - config.urlArgs) : url; - }, - - //Delegates to req.load. Broken out as a separate function to - //allow overriding in the optimizer. - load: function (id, url) { - req.load(context, id, url); - }, - - /** - * Executes a module callback function. Broken out as a separate function - * solely to allow the build system to sequence the files in the built - * layer in the right sequence. - * - * @private - */ - execCb: function (name, callback, args, exports) { - return callback.apply(exports, args); - }, - - /** - * callback for script loads, used to check status of loading. - * - * @param {Event} evt the event from the browser for the script - * that was loaded. - */ - onScriptLoad: function (evt) { - //Using currentTarget instead of target for Firefox 2.0's sake. Not - //all old browsers will be supported, but this one was easy enough - //to support and still makes sense. - if (evt.type === 'load' || - (readyRegExp.test((evt.currentTarget || evt.srcElement).readyState))) { - //Reset interactive script so a script node is not held onto for - //to long. - interactiveScript = null; - - //Pull out the name of the module and the context. - var data = getScriptData(evt); - context.completeLoad(data.id); - } - }, - - /** - * Callback for script errors. - */ - onScriptError: function (evt) { - var data = getScriptData(evt); - if (!hasPathFallback(data.id)) { - return onError(makeError('scripterror', 'Script error for: ' + data.id, evt, [data.id])); - } - } - }; - - context.require = context.makeRequire(); - return context; - } - - /** - * Main entry point. - * - * If the only argument to require is a string, then the module that - * is represented by that string is fetched for the appropriate context. - * - * If the first argument is an array, then it will be treated as an array - * of dependency string names to fetch. An optional function callback can - * be specified to execute when all of those dependencies are available. - * - * Make a local req variable to help Caja compliance (it assumes things - * on a require that are not standardized), and to give a short - * name for minification/local scope use. - */ - req = requirejs = function (deps, callback, errback, optional) { - - //Find the right context, use default - var context, config, - contextName = defContextName; - - // Determine if have config object in the call. - if (!isArray(deps) && typeof deps !== 'string') { - // deps is a config object - config = deps; - if (isArray(callback)) { - // Adjust args if there are dependencies - deps = callback; - callback = errback; - errback = optional; - } else { - deps = []; - } - } - - if (config && config.context) { - contextName = config.context; - } - - context = getOwn(contexts, contextName); - if (!context) { - context = contexts[contextName] = req.s.newContext(contextName); - } - - if (config) { - context.configure(config); - } - - return context.require(deps, callback, errback); - }; - - /** - * Support require.config() to make it easier to cooperate with other - * AMD loaders on globally agreed names. - */ - req.config = function (config) { - return req(config); - }; - - /** - * Execute something after the current tick - * of the event loop. Override for other envs - * that have a better solution than setTimeout. - * @param {Function} fn function to execute later. - */ - req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) { - setTimeout(fn, 4); - } : function (fn) { fn(); }; - - /** - * Export require as a global, but only if it does not already exist. - */ - if (!require) { - require = req; - } - - req.version = version; - - //Used to filter out dependencies that are already paths. - req.jsExtRegExp = /^\/|:|\?|\.js$/; - req.isBrowser = isBrowser; - s = req.s = { - contexts: contexts, - newContext: newContext - }; - - //Create default context. - req({}); - - //Exports some context-sensitive methods on global require. - each([ - 'toUrl', - 'undef', - 'defined', - 'specified' - ], function (prop) { - //Reference from contexts instead of early binding to default context, - //so that during builds, the latest instance of the default context - //with its config gets used. - req[prop] = function () { - var ctx = contexts[defContextName]; - return ctx.require[prop].apply(ctx, arguments); - }; - }); - - if (isBrowser) { - head = s.head = document.getElementsByTagName('head')[0]; - //If BASE tag is in play, using appendChild is a problem for IE6. - //When that browser dies, this can be removed. Details in this jQuery bug: - //http://dev.jquery.com/ticket/2709 - baseElement = document.getElementsByTagName('base')[0]; - if (baseElement) { - head = s.head = baseElement.parentNode; - } - } - - /** - * Any errors that require explicitly generates will be passed to this - * function. Intercept/override it if you want custom error handling. - * @param {Error} err the error object. - */ - req.onError = defaultOnError; - - /** - * Creates the node for the load command. Only used in browser envs. - */ - req.createNode = function (config, moduleName, url) { - var node = config.xhtml ? - document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') : - document.createElement('script'); - node.type = config.scriptType || 'text/javascript'; - node.charset = 'utf-8'; - node.async = true; - return node; - }; - - /** - * Does the request to load a module for the browser case. - * Make this a separate function to allow other environments - * to override it. - * - * @param {Object} context the require context to find state. - * @param {String} moduleName the name of the module. - * @param {Object} url the URL to the module. - */ - req.load = function (context, moduleName, url) { - var config = (context && context.config) || {}, - node; - if (isBrowser) { - //In the browser so use a script tag - node = req.createNode(config, moduleName, url); - - node.setAttribute('data-requirecontext', context.contextName); - node.setAttribute('data-requiremodule', moduleName); - - //Set up load listener. Test attachEvent first because IE9 has - //a subtle issue in its addEventListener and script onload firings - //that do not match the behavior of all other browsers with - //addEventListener support, which fire the onload event for a - //script right after the script execution. See: - //https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution - //UNFORTUNATELY Opera implements attachEvent but does not follow the script - //script execution mode. - if (node.attachEvent && - //Check if node.attachEvent is artificially added by custom script or - //natively supported by browser - //read https://github.com/jrburke/requirejs/issues/187 - //if we can NOT find [native code] then it must NOT natively supported. - //in IE8, node.attachEvent does not have toString() - //Note the test for "[native code" with no closing brace, see: - //https://github.com/jrburke/requirejs/issues/273 - !(node.attachEvent.toString && node.attachEvent.toString().indexOf('[native code') < 0) && - !isOpera) { - //Probably IE. IE (at least 6-8) do not fire - //script onload right after executing the script, so - //we cannot tie the anonymous define call to a name. - //However, IE reports the script as being in 'interactive' - //readyState at the time of the define call. - useInteractive = true; - - node.attachEvent('onreadystatechange', context.onScriptLoad); - //It would be great to add an error handler here to catch - //404s in IE9+. However, onreadystatechange will fire before - //the error handler, so that does not help. If addEventListener - //is used, then IE will fire error before load, but we cannot - //use that pathway given the connect.microsoft.com issue - //mentioned above about not doing the 'script execute, - //then fire the script load event listener before execute - //next script' that other browsers do. - //Best hope: IE10 fixes the issues, - //and then destroys all installs of IE 6-9. - //node.attachEvent('onerror', context.onScriptError); - } else { - node.addEventListener('load', context.onScriptLoad, false); - node.addEventListener('error', context.onScriptError, false); - } - node.src = url; - - //For some cache cases in IE 6-8, the script executes before the end - //of the appendChild execution, so to tie an anonymous define - //call to the module name (which is stored on the node), hold on - //to a reference to this node, but clear after the DOM insertion. - currentlyAddingScript = node; - if (baseElement) { - head.insertBefore(node, baseElement); - } else { - head.appendChild(node); - } - currentlyAddingScript = null; - - return node; - } else if (isWebWorker) { - try { - //In a web worker, use importScripts. This is not a very - //efficient use of importScripts, importScripts will block until - //its script is downloaded and evaluated. However, if web workers - //are in play, the expectation that a build has been done so that - //only one script needs to be loaded anyway. This may need to be - //reevaluated if other use cases become common. - importScripts(url); - - //Account for anonymous modules - context.completeLoad(moduleName); - } catch (e) { - context.onError(makeError('importscripts', - 'importScripts failed for ' + - moduleName + ' at ' + url, - e, - [moduleName])); - } - } - }; - - function getInteractiveScript() { - if (interactiveScript && interactiveScript.readyState === 'interactive') { - return interactiveScript; - } - - eachReverse(scripts(), function (script) { - if (script.readyState === 'interactive') { - return (interactiveScript = script); - } - }); - return interactiveScript; - } - - //Look for a data-main script attribute, which could also adjust the baseUrl. - if (isBrowser && !cfg.skipDataMain) { - //Figure out baseUrl. Get it from the script tag with require.js in it. - eachReverse(scripts(), function (script) { - //Set the 'head' where we can append children by - //using the script's parent. - if (!head) { - head = script.parentNode; - } - - //Look for a data-main attribute to set main script for the page - //to load. If it is there, the path to data main becomes the - //baseUrl, if it is not already set. - dataMain = script.getAttribute('data-main'); - if (dataMain) { - //Preserve dataMain in case it is a path (i.e. contains '?') - mainScript = dataMain; - - //Set final baseUrl if there is not already an explicit one. - if (!cfg.baseUrl) { - //Pull off the directory of data-main for use as the - //baseUrl. - src = mainScript.split('/'); - mainScript = src.pop(); - subPath = src.length ? src.join('/') + '/' : './'; - - cfg.baseUrl = subPath; - } - - //Strip off any trailing .js since mainScript is now - //like a module name. - mainScript = mainScript.replace(jsSuffixRegExp, ''); - - //If mainScript is still a path, fall back to dataMain - if (req.jsExtRegExp.test(mainScript)) { - mainScript = dataMain; - } - - //Put the data-main script in the files to load. - cfg.deps = cfg.deps ? cfg.deps.concat(mainScript) : [mainScript]; - - return true; - } - }); - } - - /** - * The function that handles definitions of modules. Differs from - * require() in that a string for the module should be the first argument, - * and the function to execute after dependencies are loaded should - * return a value to define the module corresponding to the first argument's - * name. - */ - define = function (name, deps, callback) { - var node, context; - - //Allow for anonymous modules - if (typeof name !== 'string') { - //Adjust args appropriately - callback = deps; - deps = name; - name = null; - } - - //This module may not have dependencies - if (!isArray(deps)) { - callback = deps; - deps = null; - } - - //If no name, and callback is a function, then figure out if it a - //CommonJS thing with dependencies. - if (!deps && isFunction(callback)) { - deps = []; - //Remove comments from the callback string, - //look for require calls, and pull them into the dependencies, - //but only if there are function args. - if (callback.length) { - callback - .toString() - .replace(commentRegExp, '') - .replace(cjsRequireRegExp, function (match, dep) { - deps.push(dep); - }); - - //May be a CommonJS thing even without require calls, but still - //could use exports, and module. Avoid doing exports and module - //work though if it just needs require. - //REQUIRES the function to expect the CommonJS variables in the - //order listed below. - deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps); - } - } - - //If in IE 6-8 and hit an anonymous define() call, do the interactive - //work. - if (useInteractive) { - node = currentlyAddingScript || getInteractiveScript(); - if (node) { - if (!name) { - name = node.getAttribute('data-requiremodule'); - } - context = contexts[node.getAttribute('data-requirecontext')]; - } - } - - //Always save off evaluating the def call until the script onload handler. - //This allows multiple modules to be in a file without prematurely - //tracing dependencies, and allows for anonymous module support, - //where the module name is not known until the script onload event - //occurs. If no context, use the global queue, and get it processed - //in the onscript load callback. - (context ? context.defQueue : globalDefQueue).push([name, deps, callback]); - }; - - define.amd = { - jQuery: true - }; - - - /** - * Executes the text. Normally just uses eval, but can be modified - * to use a better, environment-specific call. Only used for transpiling - * loader plugins, not for plain JS modules. - * @param {String} text the text to execute/evaluate. - */ - req.exec = function (text) { - /*jslint evil: true */ - return eval(text); - }; - - //Set up with config info. - req(cfg); -}(this)); diff --git a/ITP/create-template/www/lib/testObject.js b/ITP/create-template/www/lib/testObject.js deleted file mode 100644 index 75c3d98..0000000 --- a/ITP/create-template/www/lib/testObject.js +++ /dev/null @@ -1,34 +0,0 @@ -define(function () { - - var CT = require('core'); - - CT.obj = function () { - - //public variables go here under 'this' - - this.publicVar = Math.PI; - - }; - - CT.obj.prototype = { - - _privateVar: 36, - - constructor: CT.obj, - - get privateVar() { - return this._privateVar; - }, - - set privateVar(a) { - if (typeof a == "number") - this._privateVar = a; - else - console.log('NaN'); - } - - }; - - return CT; - -}); \ No newline at end of file diff --git a/ITP/create-template/www/lib/utility/mat.js b/ITP/create-template/www/lib/utility/mat.js deleted file mode 100644 index 67c9b90..0000000 --- a/ITP/create-template/www/lib/utility/mat.js +++ /dev/null @@ -1,373 +0,0 @@ - - var M4 = function() { - - this._mS = []; - this._to = 0; - this._mS[0] = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]; - - this._m = function(arg) { - if (! (arg === undefined)) - this._mS[this._to] = arg; - return this._mS[this._to]; - }; - - this.aimX = function(mat) { - var A = this._m(); - var B = mat._m(); - var X = [B[12] - A[12], B[13] - A[13], B[14] - A[14]]; - var Z = [A[8], A[9], A[10]]; - var Y = cross(Z, X); - cross(X, Y, Z); - return this.setOrientation(X, Y, Z); - }; - - this.aimY = function(mat) { - var A = this._m(); - var B = mat._m(); - var Y = [B[12] - A[12], B[13] - A[13], B[14] - A[14]]; - var X = [A[0], A[1], A[2]]; - var Z = cross(X, Y); - cross(Y, Z, X); - return this.setOrientation(X, Y, Z); - }; - - this.aimZ = function(mat) { - var A = this._m(); - var B = mat._m(); - var Z = [B[12] - A[12], B[13] - A[13], B[14] - A[14]]; - var Y = [A[4], A[5], A[6]]; - var X = cross(Y, Z); - cross(Z, X, Y); - return this.setOrientation(X, Y, Z); - }; - - this.copy = function(m) { - for (var i = 0 ; i < 16 ; i++) - this._m()[i] = m._m()[i]; - return this; - } - - this.identity = function() { - this._m(this._id()); - return this; - }; - - this.normalMatrix = function(m) { - var a = m[0]*m[0] + m[1]*m[1] + m[ 2]*m[ 2], - b = m[4]*m[4] + m[5]*m[5] + m[ 6]*m[ 6], - c = m[8]*m[8] + m[9]*m[9] + m[10]*m[10]; - return [m[0]/a,m[1]/a,m[ 2]/a,0, - m[4]/b,m[5]/b,m[ 6]/b,0, - m[8]/c,m[9]/c,m[10]/c,0, - 0,0,0,1]; - }; - - this.normalize = function(v) { - var x = v[0],y = v[1],z = v[2],r = Math.sqrt(x*x + y*y + z*z); - v[0] /= r; - v[1] /= r; - v[2] /= r; - }; - - this.perspective = function(x,y,z) { - this._xf(this._pe(x,y,z)); - return this; - }; - - this.restore = function() { - --this._to; - }; - this.rotateX = function(a) { - this._xf(this._rX(a)); - return this; - }; - this.rotateY = function(a) { - this._xf(this._rY(a)); - return this; - }; - this.rotateZ = function(a) { - this._xf(this._rZ(a)); - return this; - }; - this.save = function() { - this._mS[this._to+1] = this._mS[this._to++]; - }; - this.scale = function(x,y,z) { - if (y === undefined) - z=y=x; - this._xf(this._sc(x,y,z)); - return this; - }; - this.setOrientation = function(X, Y, Z) { - this.normalize(X); - this.normalize(Y); - this.normalize(Z); - var v = this._m(); - v[0] = X[0]; v[1] = X[1]; v[ 2] = X[2]; - v[4] = Y[0]; v[5] = Y[1]; v[ 6] = Y[2]; - v[8] = Z[0]; v[9] = Z[1]; v[10] = Z[2]; - return this; - } - this.toString = function() { - var str = ""; - for (var i = 0 ; i < 16 ; i++) - str += (i==0 ? "[" : ",") + roundedString(this._m()[i]); - return str + "]"; - } - this.translate = function(x,y,z) { - this._xf(this._tr(x,y,z)); - return this; - }; - this.transpose = function(m) { - return [m[0],m[4],m[ 8],m[12], - m[1],m[5],m[ 9],m[13], - m[2],m[6],m[10],m[14], - m[3],m[7],m[11],m[15]]; - }; - this._xf = function(m) { - return this._m(this._mm(m,this._m())); - }; - this._id = function() { - return [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]; - }; - this._tr = function(x,y,z) { - return [1,0,0,0,0,1,0,0,0,0,1,0,x,y,z,1]; - }; - this._rX = function(a) { - var c = Math.cos(a),s = Math.sin(a); - return [1,0,0,0,0,c,s,0,0,-s,c,0,0,0,0,1]; - }; - this._rY = function(a) { - var c = Math.cos(a),s = Math.sin(a); - return [c,0,-s,0,0,1,0,0,s,0,c,0,0,0,0,1]; - }; - this._rZ = function(a) { - var c = Math.cos(a),s = Math.sin(a); - return [c,s,0,0,-s,c,0,0,0,0,1,0,0,0,0,1]; - }; - this._sc = function(x,y,z) { - return [x,0,0,0,0,y,0,0,0,0,z,0,0,0,0,1]; - }; - this._pe = function(x,y,z) { - var rr = x*x + y*y + z*z; - return [1,0,0,x/rr, 0,1,0,y/rr, 0,0,1,z/rr, 0,0,0,1]; - }; - this._d = function(a,b) { - return a[0]*b[0] + - a[1]*b[1] + - (b.length<3 ? 0 : a[2]*b[2]) + - (b.length<4 ? a[3] : a[3]*b[3]); - }; - this._x = function(m) { - return [m[0],m[1],m[2],m[3]]; - }; - this._y = function(m) { - return [m[4],m[5],m[6],m[7]]; - }; - this._z = function(m) { - return [m[8],m[9],m[10],m[11]]; - }; - this._w = function(m) { - return [m[12],m[13],m[14],m[15]]; - }; - this._mm = function(a,b) { - var t = this.transpose(b); - - var x = this._x(a), y = this._y(a), z = this._z(a), w = this._w(a); - var X = this._x(t), Y = this._y(t), Z = this._z(t), W = this._w(t); - - return [this._d(x, X), this._d(x, Y), this._d(x, Z), this._d(x, W), - this._d(y, X), this._d(y, Y), this._d(y, Z), this._d(y, W), - this._d(z, X), this._d(z, Y), this._d(z, Z), this._d(z, W), - this._d(w, X), this._d(w, Y), this._d(w, Z), this._d(w, W)]; - }; - this._mv = function(m,v) { - var M = this._m(); - var x = this._d( [M[0],M[4],M[ 8],M[12]] , v ); - var y = this._d( [M[1],M[5],M[ 9],M[13]] , v ); - var z = this._d( [M[2],M[6],M[10],M[14]] , v ); - var w = this._d( [M[3],M[7],M[11],M[15]] , v ); - return v.length == 4 ? [x, y, z, w] : [x / w, y / w, z / w]; - }; - this.transform = function(v) { - if (v[0] instanceof Array) { - var dst = []; - for (var n = 0 ; n < v.length ; n++) - dst.push(this.transform(v[n])); - return dst; - } - else - return this._mv(this._m(),v); - } - }; - - - function cross(a,b,c) { - if (c === undefined) - c = [0,0,0]; - c[0] = a[1] * b[2] - a[2] * b[1]; - c[1] = a[2] * b[0] - a[0] * b[2]; - c[2] = a[0] * b[1] - a[1] * b[0]; - return c; - }; - - function dot(a,b) { - if (a.length < 3 || b.length < 3) - return a[0]*b[0] + a[1]*b[1]; - return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; - }; - function lerp(t, a, b) { - return a + t * (b - a); - }; - function norm(v) { - return sqrt(normSqr(v)); - }; - function normSqr(v) { - return dot(v, v); - }; - function vecDiff(a, b) { - return [a[0]-b[0], a[1]-b[1], a[2]-b[2]]; - }; - function vecLerp(t,a,b) { - return [lerp(t,a[0],b[0]),lerp(t,a[1],b[1]),lerp(t,a[2],b[2])]; - }; - function vecScale(v, s) { - return [v[0] * s, v[1] * s, v[2] * s]; - }; - function vecSum(a, b) { - return [a[0] + b[0],a[1] + b[1],a[2] + b[2]]; - }; - - function drawUnitDisk(rgb) { renderUnitDisk(mDrawFace, rgb); } - function fillUnitDisk(rgb) { renderUnitDisk(mFillFace, rgb); } - function renderUnitDisk(renderFunction, rgb) { - var p = []; - for (var i = 0 ; i < 20 ; i++) { - var t = i * TAU / 20 + PI; - p.push([cos(t), sin(t), 0]); - } - renderFunction(p); - } - function drawUnitTube(rgb) { renderUnitTube(mDrawFace, rgb); } - function fillUnitTube(rgb) { renderUnitTube(mFillFace, rgb); } - function renderUnitTube(renderFunction, rgb) { - for (var i = 0 ; i < 20 ; i++) { - var t0 = i * TAU / 20 + PI; - var t1 = (i+1) * TAU / 20 + PI; - var s0 = sin(t0), c0 = cos(t0); - var s1 = sin(t1), c1 = cos(t1); - renderFunction([[c0,s0,1],[c0,s0,-1],[c1,s1,-1],[c1,s1,1]]); - } - } - function unitCubeCorners() { - for (var i = 0 ; i < pCube.length ; i++) - mPoint(pCube[i]); - } - function drawUnitCube(rgb) { renderUnitCube(mDrawFace, rgb); } - function fillUnitCube(rgb) { renderUnitCube(mFillFace, rgb); } - var pCube = [[-1,-1,-1],[ 1,-1,-1],[-1, 1,-1],[ 1, 1,-1], - [-1,-1, 1],[ 1,-1, 1],[-1, 1, 1],[ 1, 1, 1]]; - function renderUnitCube(renderFunction, rgb) { - var p = pCube; - var f = [[0,1,5,4],[0,2,3,1],[0,4,6,2],[1,3,7,5],[2,6,7,3],[6,4,5,7]]; - for (var i = 0 ; i < f.length ; i++) - renderFunction([p[f[i][0]],p[f[i][1]],p[f[i][2]],p[f[i][3]]], rgb); - } - function drawUnitSquare() { - mCurve([[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]); - } - function fillUnitSquare() { - mFillFace([[-1,-1],[1,-1],[1,1],[-1,1]]); - mDrawFace([[-1,-1],[1,-1],[1,1],[-1,1]]); - } - function standardView(x,y,phi,theta,psi,s, mat) { - if (mat === undefined) - mat = m; - - mat.identity(); - - mat.translate(width()*x,height()*(1-y),0); - mat.perspective(0,0,-width()*2); - - mat.rotateX(phi); - mat.rotateY(theta); - mat.rotateZ(psi); - - s *= width(); - mat.scale(s,-s,s); - }; - function standardViewInverse(x,y,phi,theta,psi,s, mat) { - if (mat === undefined) - mat = m; - - mat.identity(); - - s *= width(); - mat.scale(1/s,-1/s,1/s); - - mat.rotateZ(-psi); - mat.rotateY(-theta); - mat.rotateX(-phi); - - mat.perspective(0,0,width()*2); - mat.translate(-width()*x,-height()*(1-y),0); - } - function mLine(a,b) { - var A = m.transform(a); - var B = m.transform(b); - line(A[0],A[1],B[0],B[1]); - }; - function mPoint(p) { - var P = m.transform(p); - _g_moveTo(P[0], P[1]); - }; - function mDrawFace(c, rgb) { - if (rgb === undefined) rgb = 'black'; - var P = []; - for (var n = 0 ; n < c.length ; n++) - P.push(m.transform(c[n])); - if (polygonArea(P) > 0) - drawPolygon(P); - } - function mFillFace(c, rgb) { - if (rgb === undefined) rgb = 'white'; - var P = []; - for (var n = 0 ; n < c.length ; n++) - P.push(m.transform(c[n])); - if (polygonArea(P) > 0) { - var PUSHED_color = color(); - color('white'); - fillPolygon(P); - color(PUSHED_color); - } - } - function mCurve(c) { - var cc = []; - for (var n = 0 ; n < c.length ; n++) - cc.push(m.transform(c[n])); - drawCurve(cc); - }; - function mClosedCurve(c) { - var cc = []; - for (var n = 0 ; n < c.length ; n++) - cc.push(m.transform(c[n])); - drawClosedCurve(cc); - }; - function mFillCurve(c) { - var cc = []; - for (var n = 0 ; n < c.length ; n++) - cc.push(m.transform(c[n])); - fillCurve(cc); - }; - function mArrow(a,b){ - var A = m.transform(a); - var B = m.transform(b); - arrow(A[0],A[1],B[0],B[1],len(A[0]-B[0],A[1]-B[1])/10); - }; - function mText(str,p,ax,ay){ - var P = m.transform(p); - text(str,P[0],P[1],ax,ay); - }; - - var m = new M4(); - diff --git a/node.log b/node.log index b2d8b39..17adb63 100644 --- a/node.log +++ b/node.log @@ -1,2 +1,3 @@ Listening on port 8888 Listening on port 8888 +Listening on port 8888 diff --git a/utility.js b/utility.js index dd51b3c..4125d3c 100644 --- a/utility.js +++ b/utility.js @@ -1,1359 +1,1363 @@ +define(function (require) { + 'use strict' + /////////////// UTILITY FUNCTIONS /////////////////// + + // CHECKING FOR SYNTAX ERRORS IN JAVASCRIPT CODE. + + function findSyntaxError( code ) { + var error = []; + var save_onerror = onerror; + onerror = function(errorMsg, url, lineNumber) { + error = [lineNumber, errorMsg.replace("Uncaught ","")]; + } + var element = document.createElement('script'); + element.appendChild(document.createTextNode( code )); + document.body.appendChild(element); + onerror = save_onerror; + return error; + } + + + // GET AND SET STATE DATA VIA THE PERSISTENT SERVER. + + function Server() { + this.name = name; + + this.set = function(key, val) { + var setForm = new FormData(); + setForm.append("key", key + ".json"); + setForm.append("value", JSON.stringify(val)); + + var request = new XMLHttpRequest(); + request.open("POST", "set"); + request.send(setForm); + } + + this.get = function(key, fn) { + var getRequest = new XMLHttpRequest(); + getRequest.open("GET", key + ".json"); + getRequest.onloadend = function() { + fn(getRequest.responseText); + } + getRequest.send(); + } + } + + var server = new Server(); + + + // TYPES AND FORMAT CONVERSIONS. + + function hexChar(n) { + return String.fromCharCode((n < 10 ? 48 : 87) + n); + } + function hex(n) { + return hexChar(n >> 4) + hexChar(n & 15); + } + function isDef(v) { return ! (v === undefined); } + function isNumeric(v) { return ! isNaN(v); } + function roundedString(v, nDigits) { + var nd = nDigits === undefined ? 2 : nDigits; + if (typeof(v) == 'string') + v = parseFloat(v); + var p = nd<=0 ? 1 : nd==1 ? 10 : nd==2 ? 100 : 1000; + var str = "" + (floor(p * abs(v) + 0.5) / p); + + if (nDigits !== undefined && nd > 0) { + var i = str.indexOf("."); + if (i < 0) { + str += "."; + i = str.length - 1; + } + while (str.length - i < nd + 1) + str += "0"; + } + + return (v < 0 ? "-" : "") + str; + } + + + // HANDLE PLAYING AN AUDIO SIGNAL: + + var audioNode = null, audioIndex = 0; + + var setAudioSignal= function(f) { + if (audioNode == null) { + audioContext = 'AudioContext' in window ? new AudioContext() : + 'webkitAudioContext' in window ? new webkitAudioContext() : null; + if (audioContext != null) { + audioNode = audioContext.createScriptProcessor(1024, 0, 1); + audioNode.connect(audioContext.destination); + } + } + if (audioNode != null) { + audioNode.onaudioprocess = function(event) { + var output = event.outputBuffer; + var signal = output.getChannelData(0); + if (f instanceof Array) + for (var i = 0 ; i < output.length ; i++) + signal[i] = f[audioIndex++ % f.length]; + else + for (var i = 0 ; i < output.length ; i++) + signal[i] = f(audioIndex++ / output.sampleRate); + } + } + } + + + // SET OPERATIONS: + + function Set() { + this.debug = false; + this.add = function(item) { + if (! this.contains(item)) + this.push(item); + } + + this.remove = function(item) { + var index = this.indexOf(item); + if (index >= 0) + this.splice(index, 1); + } + + this.contains = function(item) { + return this.indexOf(item) >= 0; + } + + this.indexOf = function(item) { + for (var i = 0 ; i < this.length ; i++) + if (equals(item, this[i])) + return i; + return -1; + } + + function equals(a, b) { + if (a instanceof Array) { + for (var i = 0 ; i < a.length ; i++) + if (! equals(a[i], b[i])) + return false; + return true; + } + return a == b; + } + + this.toString = function() { + var str = "["; + for (var i = 0 ; i < this.length ; i++) + str += this[i] + (i maxEl) { + maxEl = Math.abs(A[k][i]); + maxRow = k; + } + // Swap maximum row with current row (column by column). + for (var k=i; k-1; i--) { + x[i] = A[i][n] / A[i][i]; + for (var k=i-1; k>-1; k--) + A[k][n] -= A[k][i] * x[i]; + } + return x; // Return n x 1 result vector. + } + function isEqualArray(a, b) { + if (a === undefined || b === undefined || + a == null || b == null || a.length != b.length) + return false; + for (var i = 0 ; i < a.length ; i++) + if (a[i] != b[i]) + return false; + return true; + } + function floor(t) { return Math.floor(t); } + function ik(a, b, C, D) { + var cc = dot(C,C), x = (1 + (a*a - b*b)/cc) / 2, y = dot(C,D)/cc; + for (var i = 0 ; i < 3 ; i++) D[i] -= y * C[i]; + y = sqrt(max(0,a*a - cc*x*x) / dot(D,D)); + for (var i = 0 ; i < 3 ; i++) D[i] = x * C[i] + y * D[i]; + } + function len(x, y) { + if (y === undefined) + return sqrt(x[0] * x[0] + x[1] * x[1]); + return sqrt(x * x + y * y); + } + + function lerp(t, a, b) { return a + t * (b - a); } + function max(a,b) { return Math.max(a,b); } + function min(a,b) { return Math.min(a,b); } + + var noise2P = [], noise2U = [], noise2V = []; + function fractal(x) { + var value = 0; + for (var f = 1 ; f <= 512 ; f *= 2) + value += noise2(x * f, 0.03 * x * f) / f; + return value; + } + function turbulence(x) { + var value = 0; + for (var f = 1 ; f <= 512 ; f *= 2) + value += abs(noise2(x * f, 0.03 * x * f) / f); + return value; + } + function noise(x) { return noise2(x, 0.03 * x); } + function noise2(x, y) { + if (noise2P.length == 0) { + var p = noise2P, u = noise2U, v = noise2V, i, j; + for (i = 0 ; i < 256 ; i++) { + p[i] = i; + u[i] = 2 * random() - 1; + v[i] = 2 * random() - 1; + var s = sqrt(u[i]*u[i] + v[i]*v[i]); + u[i] /= s; + v[i] /= s; + } + while (--i) { + var k = p[i]; + p[i] = p[j = floor(256 * random())]; + p[j] = k; + } + for (i = 0 ; i < 256 + 2 ; i++) { + p[256 + i] = p[i]; + u[256 + i] = u[i]; + v[256 + i] = v[i]; + } + } + var P = noise2P, U = noise2U, V = noise2V; + x = (x + 4096) % 256; + y = (y + 4096) % 256; + var i = floor(x), u = x - i, s = sCurve(u); + var j = floor(y), v = y - j, t = sCurve(v); + var a = P[P[i] + j ], b = P[P[i+1] + j ]; + var c = P[P[i] + j+1], d = P[P[i+1] + j+1]; + return lerp(t, lerp(s, u*U[a] + v *V[a], (u-1)*U[b] + v *V[b]), + lerp(s, u*U[c] + (v-1)*V[c], (u-1)*U[d] + (v-1)*V[d])); + } + function pieMenuIndex(x,y,n) { + if (n === undefined) + n = 4; + return floor(n+.5-atan2(y,x) / (TAU/n)) % n; + } + function pow(a,b) { return Math.pow(a,b); } + var random = function() { + var seed = 2; + var x = (seed % 30268) + 1; + seed = (seed - (seed % 30268)) / 30268; + var y = (seed % 30306) + 1; + seed = (seed - (seed % 30306)) / 30306; + var z = (seed % 30322) + 1; + return function() { + return ( ((x = (171 * x) % 30269) / 30269) + + ((y = (172 * y) % 30307) / 30307) + + ((z = (170 * z) % 30323) / 30323) ) % 1; + } + }(); + function round() { return Math.round(); } + function sCurve(t) { return max(0, min(1, t * t * (3 - t - t))); } + function saw(t) { t = 2*t % 2; return t<1 ? t : 2-t; } + function sign(t) { return Math.sign(t); } + function sin(t) { return Math.sin(t); } + function square_wave(t) { return 2 * floor(2*t % 2) - 1; } + function sqrt(t) { return Math.sqrt(t); } + function tan(t) { return Math.tan(t); } + + // CHARACTER CONSTANTS AND CONVERSIONS. + + var ALT = '\u22C0' ; + var C_PHI = '\u03A6' ; + var C_THETA = '\u0398' ; + var COMMAND = '\u2318' ; + var CONTROL = '\u2201' ; + var D_ARROW = '\u2193' ; + var L_ARROW = '\u2190' ; + var PAGE_UP = 'PAGE_UP'; + var PAGE_DN = 'PAGE_DN'; + var R_ARROW = '\u2192' ; + var S_PHI = '\u03C6' ; + var S_THETA = '\u03B8' ; + var U_ARROW = '\u2191' ; + + function charCodeToString(key) { + if (isShiftPressed) + switch (key) { + case 48: return ')'; // SHIFT 1 + case 49: return '!'; + case 50: return '@'; + case 51: return '#'; + case 52: return '$'; + case 53: return '%'; + case 54: return '^'; + case 55: return '&'; + case 56: return '*'; + case 57: return '('; // SHIFT 0 + + case 186: return ':'; + case 187: return '+'; + case 188: return '<'; + case 189: return '_'; + case 190: return '>'; + case 191: return '?'; + case 192: return '~'; + case 219: return '{'; + case 220: return '|'; + case 221: return '}'; + case 222: return '"'; + } + + switch (key) { + case 8: return 'del'; + case 13: return 'ret'; + case 16: return 'cap'; + case 17: return 'control'; + case 18: return 'alt'; + case 27: return 'esc'; + case 32: return 'spc'; + case 32: return 'spc'; + case 33: return PAGE_UP; + case 34: return PAGE_DN; + case 37: return L_ARROW; + case 38: return U_ARROW; + case 39: return R_ARROW; + case 40: return D_ARROW; + case 91: return 'command'; + case 186: return ';'; + case 187: return '='; + case 188: return ','; + case 189: return '-'; + case 190: return '.'; + case 191: return '/'; + case 192: return '`'; + case 219: return '['; + case 220: return '\\'; + case 221: return ']'; + case 222: return "'"; + } + + var str = String.fromCharCode(key); + + if (key >= 64 && key < 64 + 32 && ! isShiftPressed) + str = str.toLowerCase(); + + return str; + } + + + // STRING UTILITIES. + + function variableToValue(str, name, value) { + + var cp = '.'.charCodeAt(0); + var c_ = '_'.charCodeAt(0); + var c0 = '0'.charCodeAt(0); + var c9 = '9'.charCodeAt(0); + var ca = 'a'.charCodeAt(0); + var cz = 'z'.charCodeAt(0); + var cA = 'A'.charCodeAt(0); + var cZ = 'Z'.charCodeAt(0); + + for (var i = 0 ; i < str.length - name.length ; i++) { + + // FIND AN OCCURANCE OF name IN THE STRING. + + if (str.substring(i, i + name.length) == name) { + + // NO MATCH IF name IS PRECEDED BY . or _ or 0-9 or a-z or A-Z. + + if (i > 0) { + var n = str.charCodeAt(i-1); + if (n == cp || n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) + continue; + } + + // NO MATCH IF name IS FOLLOWED BY _ or 0-9 or a-z or A-Z. + + if (i + name.length < str.length) { + var n = str.charCodeAt(i-1); + if (n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) + continue; + } + + // OTHERWISE, DO THE SUBSTITUTION, AND ADJUST i ACCORDINGLY. + + str = str.substring(0, i) + value + str.substring(i + name.length, str.length); + i += value.length - name.length; + } + } + + return str; + } + + + // ARRAY UTILITIES. + + function arrayToString(a, level) { + if (a.length == 0) + return "[]"; + if (level === undefined) + level = 0; + var spacer = level == 0 ? " " : ""; + var str = "[" + spacer; + for (var i = 0 ; i < a.length ; i++) + str += (a[i] instanceof Array ? arrayToString(a[i], level+1) : a[i]) + + spacer + (i < a.length-1 ? "," + spacer : "]"); + return str; + } + + function cloneArray(src) { + var dst = []; + for (var i = 0 ; i < src.length ; i++) + if (src[i] instanceof Array) + dst[i] = cloneArray(src[i]); + else + dst[i] = src[i]; + return dst; + } + + function firstUndefinedArrayIndex(arr) { + var n = 0; + while (n < arr.length && isDef(arr[n]) && arr[n] != null) + n++; + return n; + } + + function getIndex(arr, obj) { + var i = arr.length; + while (--i >= 0 && arr[i] !== obj) ; + return i; + } + + function sample(arr, t) { + t = max(0, min(0.999, t)); + var n = arr.length; + if (n == 1) + return arr[0]; + var i = floor((n-1) * t); + var f = (n-1) * t - i; + return lerp(f, arr[i], arr[i+1]); + } + + function newZeroArray(size) { + var dst = []; + for (var i = 0 ; i < size ; i++) + dst.push(0); + return dst; + } + + + // IMAGE PROCESSING. + + function findConnectedComponents(src, nc, dst, f0) { + function findConnectedComponent(i, n) { + if (src[i] < f0) + return; + + dst[i] = n; + var c = i % nc; + var r = i / nc; + if (c > 0 && dst[i - 1 ] == 0) findConnectedComponent(i - 1 , n); + if (c < nc-1 && dst[i + 1 ] == 0) findConnectedComponent(i + 1 , n); + if (r > 0 && dst[i - nc] == 0) findConnectedComponent(i - nc, n); + if (r < nr-1 && dst[i + nc] == 0) findConnectedComponent(i + nc, n); + } + + if (f0 === undefined) + f0 = 0.5; + + var nr = src.length / nc; + + for (var i = 0 ; i < src.length ; i++) + dst[i] = 0; + + var n = 0; + for (var i = 0 ; i < src.length ; i++) + if (src[i] >= f0 && dst[i] == 0) + findConnectedComponent(i, ++n); + } + + function imageEnlarge(src, dst) { + if (this.tmp === undefined) + this.tmp = newZeroArray(dst.length); + + function index(i,j,w) { return max(0,min(w-1,i)) + w * max(0,min(w-1,j)); } + + var w = floor(sqrt(src.length)); + + for (var row = 0 ; row < w ; row++) + for (var col = 0 ; col < w ; col++) { + var i0 = index(row , col , w); + var i1 = index(row+1, col , w); + var i2 = index(row , col+1, w); + var i3 = index(row+1, col+1, w); + var j = index(2*row, 2*col, 2*w); + this.tmp[j ] = src[i0]; + this.tmp[j+1 ] = (src[i0] + src[i1]) / 2; + this.tmp[j +2*w] = (src[i0] + src[i2]) / 2; + this.tmp[j+1+2*w] = (src[i0] + src[i1] + src[i2] + src[i3]) / 4; + } + + var wt = [1/6,1/3,1/3,1/6]; + + for (var row = 0 ; row < 2*w ; row++) + for (var col = 0 ; col < 2*w ; col++) { + var sum = 0; + for (var u = -1 ; u <= 2 ; u++) + for (var v = -1 ; v <= 2 ; v++) + sum += this.tmp[index(col+u, row+v, 2*w)] * wt[u+1] * wt[v+1]; + dst[index(col, row, 2*w)] = sum; + } + } + + + // 2D GEOMETRY UTILITIES. + + // Change the length of a curve. + + function adjustCurveLength(curve, targetLength, i0) { + var n = curve.length; + + var ratio = targetLength / computeCurveLength(curve, i0); + + var x0 = (curve[1][0] + curve[n-1][0]) / 2; + var y0 = (curve[1][1] + curve[n-1][1]) / 2; + + var p = []; + for (var i = 0 ; i < n ; i++) + p.push([curve[i][0], curve[i][1]]); + + for (var i = 2 ; i <= n-2 ; i++) { + var t = 1 - 4*(i-n/2)*(i-n/2)/n/n; + var dr = t * (ratio - 1) + 1; + p[i][0] = lerp(dr, x0, p[i][0]); + p[i][1] = lerp(dr, y0, p[i][1]); + } + + for (var i = 2 ; i <= n-2 ; i++) { + curve[i][0] = p[i][0]; + curve[i][1] = p[i][1]; + } + } + + // Clip a curve to that part which is entirely outside of a rectangle. + + function clipCurveAgainstRect(src, R) { + if (src[0] == undefined) return []; + var dst = []; + var x1 = src[0][0]; + var y1 = src[0][1]; + if (! isInRect(x1,y1, R)) + dst.push([x1,y1]); + for (var n = 1 ; n < src.length ; n++) { + var x0 = x1, y0 = y1; + x1 = src[n][0]; + y1 = src[n][1]; + var draw0 = ! isInRect(x0,y0, R); + var draw1 = ! isInRect(x1,y1, R); + if (draw0 || draw1) { + if (! draw0) + dst.push(clipLineToRect(x0,y0, x1,y1, R)); + if (! draw1) + dst.push(clipLineToRect(x1,y1, x0,y0, R)); + else + dst.push([x1,y1]); + } + } + return dst; + } + + // Bend a curve toward a point, ending up at a target length. + + function bendCurve(curve, pt, totalLength, i0) { + if (i0 === undefined) i0 = 0; + + var n = curve.length; + + // FIND NEAREST POINT ON CURVE. + + var ddMin = Number.MAX_VALUE, im = 0; + for (var i = 0 ; i < n ; i++) { + var dx = curve[i][0] - pt[0]; + var dy = curve[i][1] - pt[1]; + var dd = dx * dx + dy * dy; + if (dd < ddMin) { + ddMin = dd; + im = i; + } + } + + // IF NOT AT THE ENDS, THEN WARP MIDDLE OF CURVE. + + if (im > n/8 && im < n*7/8) { + var dx = pt[0] - curve[im][0]; + var dy = pt[1] - curve[im][1]; + for (var i = i0+1 ; i < n-1 ; i++) { + var t = i < im ? sCurve((i-i0 ) / (im-i0 )) + : sCurve((n-1-i) / (n-1-im)); + curve[i][0] += t * dx; + curve[i][1] += t * dy; + } + return; + } + + //return; + + // IF AT THE ENDS, THEN MOVE ONE ENDPOINT AND PRESERVE LENGTH. + + var ax = curve[i0 ][0], ay = curve[i0 ][1]; + var bx = curve[n-1][0], by = curve[n-1][1]; + + var dxa = pt[0] - ax, dya = pt[1] - ay; + var dxb = pt[0] - bx, dyb = pt[1] - by; + + if (dxa * dxa + dya * dya < dxb * dxb + dyb * dyb) { + for (var i = n-2 ; i >= i0 ; i--) { + var t = (n-1-i) / (n-2); + curve[i][0] += t * dxa; + curve[i][1] += t * dya; + } + } + else + for (var i = i0 + 1 ; i <= n-1 ; i++) { + var t = (i-1) / (n-2); + curve[i][0] += t * dxb; + curve[i][1] += t * dyb; + } + //adjustCurveLength(curve, totalLength, i0); + } + + // FIND x,y,scale FOR ARRAY OF CURVES A TO BEST FIT ARRAY OF CURVES B. + + function bestCurvesFit(A, B) { + var x = 0, y = 0, z = 0, w = 0; + for (var n = 0 ; n < A.length ; n++) { + var xyz = bestCurveFit(A[n], B[n]); + var t = computeCurveLength(B[n]); + x += t * xyz[0]; + y += t * xyz[1]; + z += t * xyz[2]; + w += t; + } + return [x / w, y / w, z / w]; + } + + // FIND x,y,scale FOR CURVE P TO BEST FIT CURVE Q. + + function bestCurveFit(P, Q) { + var n = min(P.length, Q.length), a=0, b=0, c=0, d=0, e=0, f=0; + for (var i = 0 ; i < n ; i++) { + var px = P[i][0], py = P[i][1], qx = Q[i][0], qy = Q[i][1]; + a += px; + b += py; + c += qx; + d += qy; + e += px * px + py * py; + f += px * qx + py * qy; + } + return solve([ [n,0,a,c], [0,n,b,d], [a,b,e,f] ]); + } + + function clipLineToRect(ax,ay, bx,by, R) { + var tx = bx < R[0] ? (R[0] - ax) / (bx - ax) : + bx > R[2] ? (R[2] - ax) / (bx - ax) : 10000; + var ty = by < R[1] ? (R[1] - ay) / (by - ay) : + by > R[3] ? (R[3] - ay) / (by - ay) : 10000; + var t = max(0, min(1, min(tx, ty))); + return [lerp(t, ax, bx), lerp(t, ay, by)]; + } + + /* + Return the area of a 2D counterclockwise polygon. + */ + + function computeArea(P) { + var sum = 0; + for (var i = 0 ; i < P.length ; i++) { + var j = (i + 1) % P.length; + sum += (P[j][0] - P[i][0]) * (P[i][1] + P[j][1]); + } + return sum / 2; + } + + /* + Find out whether a 3D point is hidden by a 3D triangle. + */ + + var isPointBehindTriangle = function(p, tri) { + var L = [0,0,0]; + var W = [0,0,0]; + var dist = function(p, L) { return p[0] * L[0] + p[1] * L[1] + L[2]; } + + return function(p, tri) { + + // Loop through the three vertices of the triangle. + + for (var j = 0 ; j < 3 ; j++) { + + // Look at edge formed by the two vertices a and b opposite this vertex. + + var a = tri[(j+1)%3]; + var b = tri[(j+2)%3]; + + // From x,y coords of a and b, compute equation of 2d line through them. + + L[0] = b[1] - a[1]; + L[1] = a[0] - b[0]; + L[2] = -(a[0] * L[0] + a[1] * L[1]); + + // Compute fractional distance of point into triangle away from edge. + + W[j] = dist(p, L) / dist(tri[j], L); + + // If point is outside this edge, return false. + + if (W[j] < 0) + return false; + } + + // Compare barycentrically weighted z of triangle vertices to z of the point. + + return W[0] * tri[0][2] + W[1] * tri[1][2] + W[2] * tri[2][2] > p[2]; + } + }(); + + // Create an arc of a circle. + + function createArc(x, y, r, angle0, angle1, n) { + var c = []; + for (var i = 0 ; i <= n ; i++) { + var angle = lerp(i / n, angle0, angle1); + c.push([x + r * cos(angle), y + r * sin(angle)]); + } + return c; + } + + function createRoundRect(x, y, w, h, r) { + var c = []; + c = c.concat(createArc(x+r,y+h-r,r,PI/2,PI,8)); + c = c.concat([[x,y+h-r],[x,y+r]]); + c = c.concat(createArc(x+r,y+r,r,PI,3*PI/2,8)); + c = c.concat([[x+r,y],[x+w-r,y]]); + c = c.concat(createArc(x+w-r,y+r,r,-PI/2,0,8)); + c = c.concat([[x+w,y+r],[x+w,y+h-r]]); + c = c.concat(createArc(x+w-r,y+h-r,r,0,PI/2,8)); + c = c.concat([[x+w-r,y+h],[x+r,y+h]]); + return c; + } + + // Compute the bounding rectangle for a curve. + + function computeCurveBounds(src, i0) { + if (i0 === undefined) i0 = 0; + var xlo = 10000, ylo = xlo, xhi = -xlo, yhi = -ylo; + for (var n = i0 ; n < src.length ; n++) { + xlo = min(xlo, src[n][0]); + ylo = min(ylo, src[n][1]); + xhi = max(xhi, src[n][0]); + yhi = max(yhi, src[n][1]); + } + return [xlo,ylo,xhi,yhi]; + } + + // The union of two bounding rectangles. + + function computeUnionOfBounds(a, b) { + return [ min(a[0],b[0]), min(a[1], b[1]), max(a[2],b[2]), max(a[3],b[3]) ]; + } + + // Create a curved line. + + function createCurve(A, B, curvature, N) { + if (N === undefined) + N = 20; + + var ax = A[0], ay = A[1], bx = B[0], by = B[1]; + var dx = 4 * curvature * (bx - ax); + var dy = 4 * curvature * (by - ay); + + var dst = []; + + // STRAIGHT LINE + + if (curvature == 0) { + for (var n = 0 ; n <= N ; n++) + dst.push([lerp(n/N, ax, bx), lerp(n/N, ay, by)]); + return dst; + } + + // CIRCULAR LOOP + + if (abs(curvature) == loopFlag) { + var mx = (ax + bx) / 2, my = (ay + by) / 2; + var rx = (ax - bx) / 2, ry = (ay - by) / 2; + var dir = curvature > 0 ? 1 : -1; + + for (var n = 0 ; n <= N ; n++) { + var angle = TAU * n / N; + var c = cos(angle); + var s = sin(angle) * dir; + dst.push([ mx + rx * c + ry * s, + my - rx * s + ry * c ]); + } + return dst; + } + + // OPEN CURVE + + for (var n = 0 ; n <= N ; n++) { + var t = n / N; + var s = lerp(abs(curvature), t, sCurve(t)); + var e = t * (1 - t); + dst.push([lerp(s, ax, bx) - e * dy, + lerp(s, ay, by) + e * dx]); + } + return dst; + } + + // CREATE A SPLINE GUIDED BY A PATH OF KEY POINTS. + + function makeSpline(keys, N) { + function x(k) { return keys[k][0]; } + function y(k) { return keys[k][1]; } + function l(k) { return L[k]; } + function hermite(a, da, b, db) { + return a * ( 2 * ttt - 3 * tt + 1) + + da * ( ttt - 2 * tt + t ) + + b * (-2 * ttt + 3 * tt ) + + db * ( ttt - tt ); + } + if (N === undefined) + N = (keys.length - 1) * 4; + var nk = keys.length; + + var L = []; + for (var n = 0 ; n < nk-1 ; n++) + L.push(len(x(n+1) - x(n), y(n+1) - y(n))); + + var spline = []; + for (var n = 0 ; n < nk-1 ; n++) { + + var dx0 = n > 0 ? (l(n) * (x(n) - x(n-1)) + l(n-1) * (x(n+1) - x(n))) / (l(n-1) + l(n)) + : 3*x(n + 1) - 2*x(n) - x(n + 2); + var dy0 = n > 0 ? (l(n) * (y(n) - y(n-1)) + l(n-1) * (y(n+1) - y(n))) / (l(n-1) + l(n)) + : 3*y(n + 1) - 2*y(n) - y(n + 2); + + var dx1 = n < nk-2 ? (l(n+1) * (x(n+1) - x(n)) + l(n) * (x(n+2) - x(n+1))) / (l(n) + l(n+1)) + : 2*x(n + 1) - 3*x(n) + x(n - 1); + var dy1 = n < nk-2 ? (l(n+1) * (y(n+1) - y(n)) + l(n) * (y(n+2) - y(n+1))) / (l(n) + l(n+1)) + : 2*y(n + 1) - 3*y(n) + y(n - 1); + + for (var i = 0 ; i < N ; i++) { + var t = i / N, tt = t * t, ttt = t * tt; + spline.push([ hermite(x(n), dx0*.9, x(n+1), dx1*.9), + hermite(y(n), dy0*.9, y(n+1), dy1*.9) ]); + } + } + spline.push([ x(nk-1), y(nk-1) ]); + return spline; + } + + // Compute the curvature of a curved line from A to B which passes through M. + + function computeCurvature(A, M, B) { + if (M === undefined) { + M = A[floor(A.length / 2)]; + B = A[A.length - 1]; + A = A[0]; + } + var dx = B[0] - A[0]; + var dy = B[1] - A[1]; + var ex = M[0] - (A[0] + B[0]) / 2; + var ey = M[1] - (A[1] + B[1]) / 2; + return (dx * ey - dy * ex) / (dx * dx + dy * dy); + } + + // Compute the total geometric length of a curve. + + function computeCurveLength(curve, i0) { + var len = 0; + for (var i = (isDef(i0) ? i0 : 0) ; i < curve.length - 1 ; i++) { + var dx = curve[i+1][0] - curve[i][0]; + var dy = curve[i+1][1] - curve[i][1]; + len += sqrt(dx * dx + dy * dy); + } + return len; + } + + // Check whether a curve crosses a line. + + function curveIntersectLine(curve, a, b) { + var dst = [], p = null; + for (var i = 0 ; i < curve.length - 1 ; i++) + if ((p = lineIntersectLine(curve[i], curve[i+1], a, b)) != null) + dst.push(p); + return dst; + } + + // Return distance squared from point [x,y] to curve c. + + function dsqFromCurve(x, y, c) { + var dsq = 100000; + for (var i = 0 ; i < c.length - 1 ; i++) + dsq = min(dsq, dsqFromLine(x, y, c[i], c[i+1])); + return dsq; + } + + // Return distance squared from point [x,y] to line segment [a->b]. + + function dsqFromLine(x, y, a, b) { + var ax = a[0] - x, ay = a[1] - y; + var bx = b[0] - x, by = b[1] - y; + var dx = bx - ax, dy = by - ay; + if (ax * dx + ay * dy > 0 || bx * dx + by * dy < 0) + return min(ax * ax + ay * ay, bx * bx + by * by); + var aa = ax * ax + ay * ay; + var ad = ax * dx + ay * dy; + var dd = dx * dx + dy * dy; + return aa - ad * ad / dd; + } + + // Return the point parametric fractional distance t along a curve. + + function getPointOnCurve(curve, t) { + if (t <= 0) return curve[0]; + if (t >= 1) return curve[curve.length-1]; + var n = curve.length - 1; + var i = floor(t * n); + var f = t * n - i; + return [ lerp(f, curve[i][0], curve[i+1][0]) , + lerp(f, curve[i][1], curve[i+1][1]) ]; + } + + function isInRect(x,y, R) { + return x >= R[0] && y >= R[1] && x < R[2] && y < R[3]; + } + + // Find the intersection between two line segments. If no intersection, return null. + + function lineIntersectLine(a, b, c, d) { + function L(a) { return a[0] * A[0] + a[1] * A[1]; } + + // FIRST MAKE SURE [c,d] CROSSES [a,b]. + + var A = [ b[1] - a[1], a[0] - b[0] ]; + + var tb = L(b); + var tc = L(c); + var td = L(d); + + if ((tc > tb) == (td > tb)) + return null; + + // THEN FIND THE POINT OF INTERSECTION p. + + var f = (tb - tc) / (td - tc); + var p = [ lerp(f, c[0], d[0]), lerp(f, c[1], d[1]) ]; + + // THEN MAKE SURE p LIES BETWEEN a AND b. + + var A = [ b[0] - a[0], b[1] - a[1] ]; + + var tp = L(p); + var ta = L(a); + var tb = L(b); + + return tp >= ta && tp <= tb ? p : null; + } + + // Resample a curve to equal geometric spacing. + + function resampleCurve(src, count) { + if (count === undefined) count = 100; -/////////////// UTILITY FUNCTIONS /////////////////// - -// CHECKING FOR SYNTAX ERRORS IN JAVASCRIPT CODE. - - function findSyntaxError( code ) { - var error = []; - var save_onerror = onerror; - onerror = function(errorMsg, url, lineNumber) { - error = [lineNumber, errorMsg.replace("Uncaught ","")]; - } - var element = document.createElement('script'); - element.appendChild(document.createTextNode( code )); - document.body.appendChild(element); - onerror = save_onerror; - return error; - } - - -// GET AND SET STATE DATA VIA THE PERSISTENT SERVER. - - function Server() { - this.name = name; - - this.set = function(key, val) { - var setForm = new FormData(); - setForm.append("key", key + ".json"); - setForm.append("value", JSON.stringify(val)); - - var request = new XMLHttpRequest(); - request.open("POST", "set"); - request.send(setForm); - } - - this.get = function(key, fn) { - var getRequest = new XMLHttpRequest(); - getRequest.open("GET", key + ".json"); - getRequest.onloadend = function() { - fn(getRequest.responseText); - } - getRequest.send(); - } - } - - var server = new Server(); - - -// TYPES AND FORMAT CONVERSIONS. - - function hexChar(n) { - return String.fromCharCode((n < 10 ? 48 : 87) + n); - } - function hex(n) { - return hexChar(n >> 4) + hexChar(n & 15); - } - function isDef(v) { return ! (v === undefined); } - function isNumeric(v) { return ! isNaN(v); } - function roundedString(v, nDigits) { - var nd = nDigits === undefined ? 2 : nDigits; - if (typeof(v) == 'string') - v = parseFloat(v); - var p = nd<=0 ? 1 : nd==1 ? 10 : nd==2 ? 100 : 1000; - var str = "" + (floor(p * abs(v) + 0.5) / p); - - if (nDigits !== undefined && nd > 0) { - var i = str.indexOf("."); - if (i < 0) { - str += "."; - i = str.length - 1; - } - while (str.length - i < nd + 1) - str += "0"; - } - - return (v < 0 ? "-" : "") + str; - } - - -// HANDLE PLAYING AN AUDIO SIGNAL: - - var audioNode = null, audioIndex = 0; - - var setAudioSignal= function(f) { - if (audioNode == null) { - audioContext = 'AudioContext' in window ? new AudioContext() : - 'webkitAudioContext' in window ? new webkitAudioContext() : null; - if (audioContext != null) { - audioNode = audioContext.createScriptProcessor(1024, 0, 1); - audioNode.connect(audioContext.destination); - } - } - if (audioNode != null) { - audioNode.onaudioprocess = function(event) { - var output = event.outputBuffer; - var signal = output.getChannelData(0); - if (f instanceof Array) - for (var i = 0 ; i < output.length ; i++) - signal[i] = f[audioIndex++ % f.length]; - else - for (var i = 0 ; i < output.length ; i++) - signal[i] = f(audioIndex++ / output.sampleRate); - } - } - } - - -// SET OPERATIONS: - - function Set() { - this.debug = false; - this.add = function(item) { - if (! this.contains(item)) - this.push(item); - } - - this.remove = function(item) { - var index = this.indexOf(item); - if (index >= 0) - this.splice(index, 1); - } - - this.contains = function(item) { - return this.indexOf(item) >= 0; - } - - this.indexOf = function(item) { - for (var i = 0 ; i < this.length ; i++) - if (equals(item, this[i])) - return i; - return -1; - } - - function equals(a, b) { - if (a instanceof Array) { - for (var i = 0 ; i < a.length ; i++) - if (! equals(a[i], b[i])) - return false; - return true; - } - return a == b; - } - - this.toString = function() { - var str = "["; - for (var i = 0 ; i < this.length ; i++) - str += this[i] + (i maxEl) { - maxEl = Math.abs(A[k][i]); - maxRow = k; - } - // Swap maximum row with current row (column by column). - for (var k=i; k-1; i--) { - x[i] = A[i][n] / A[i][i]; - for (var k=i-1; k>-1; k--) - A[k][n] -= A[k][i] * x[i]; - } - return x; // Return n x 1 result vector. - } - function isEqualArray(a, b) { - if (a === undefined || b === undefined || - a == null || b == null || a.length != b.length) - return false; - for (var i = 0 ; i < a.length ; i++) - if (a[i] != b[i]) - return false; - return true; - } - function floor(t) { return Math.floor(t); } - function ik(a, b, C, D) { - var cc = dot(C,C), x = (1 + (a*a - b*b)/cc) / 2, y = dot(C,D)/cc; - for (var i = 0 ; i < 3 ; i++) D[i] -= y * C[i]; - y = sqrt(max(0,a*a - cc*x*x) / dot(D,D)); - for (var i = 0 ; i < 3 ; i++) D[i] = x * C[i] + y * D[i]; - } - function len(x, y) { - if (y === undefined) - return sqrt(x[0] * x[0] + x[1] * x[1]); - return sqrt(x * x + y * y); - } - - function lerp(t, a, b) { return a + t * (b - a); } - function max(a,b) { return Math.max(a,b); } - function min(a,b) { return Math.min(a,b); } - - var noise2P = [], noise2U = [], noise2V = []; - function fractal(x) { - var value = 0; - for (var f = 1 ; f <= 512 ; f *= 2) - value += noise2(x * f, 0.03 * x * f) / f; - return value; - } - function turbulence(x) { - var value = 0; - for (var f = 1 ; f <= 512 ; f *= 2) - value += abs(noise2(x * f, 0.03 * x * f) / f); - return value; - } - function noise(x) { return noise2(x, 0.03 * x); } - function noise2(x, y) { - if (noise2P.length == 0) { - var p = noise2P, u = noise2U, v = noise2V, i, j; - for (i = 0 ; i < 256 ; i++) { - p[i] = i; - u[i] = 2 * random() - 1; - v[i] = 2 * random() - 1; - var s = sqrt(u[i]*u[i] + v[i]*v[i]); - u[i] /= s; - v[i] /= s; - } - while (--i) { - var k = p[i]; - p[i] = p[j = floor(256 * random())]; - p[j] = k; - } - for (i = 0 ; i < 256 + 2 ; i++) { - p[256 + i] = p[i]; - u[256 + i] = u[i]; - v[256 + i] = v[i]; - } - } - var P = noise2P, U = noise2U, V = noise2V; - x = (x + 4096) % 256; - y = (y + 4096) % 256; - var i = floor(x), u = x - i, s = sCurve(u); - var j = floor(y), v = y - j, t = sCurve(v); - var a = P[P[i] + j ], b = P[P[i+1] + j ]; - var c = P[P[i] + j+1], d = P[P[i+1] + j+1]; - return lerp(t, lerp(s, u*U[a] + v *V[a], (u-1)*U[b] + v *V[b]), - lerp(s, u*U[c] + (v-1)*V[c], (u-1)*U[d] + (v-1)*V[d])); - } - function pieMenuIndex(x,y,n) { - if (n === undefined) - n = 4; - return floor(n+.5-atan2(y,x) / (TAU/n)) % n; - } - function pow(a,b) { return Math.pow(a,b); } - var random = function() { - var seed = 2; - var x = (seed % 30268) + 1; - seed = (seed - (seed % 30268)) / 30268; - var y = (seed % 30306) + 1; - seed = (seed - (seed % 30306)) / 30306; - var z = (seed % 30322) + 1; - return function() { - return ( ((x = (171 * x) % 30269) / 30269) + - ((y = (172 * y) % 30307) / 30307) + - ((z = (170 * z) % 30323) / 30323) ) % 1; - } - }(); - function round() { return Math.round(); } - function sCurve(t) { return max(0, min(1, t * t * (3 - t - t))); } - function saw(t) { t = 2*t % 2; return t<1 ? t : 2-t; } - function sign(t) { return Math.sign(t); } - function sin(t) { return Math.sin(t); } - function square_wave(t) { return 2 * floor(2*t % 2) - 1; } - function sqrt(t) { return Math.sqrt(t); } - function tan(t) { return Math.tan(t); } - -// CHARACTER CONSTANTS AND CONVERSIONS. - - var ALT = '\u22C0' ; - var C_PHI = '\u03A6' ; - var C_THETA = '\u0398' ; - var COMMAND = '\u2318' ; - var CONTROL = '\u2201' ; - var D_ARROW = '\u2193' ; - var L_ARROW = '\u2190' ; - var PAGE_UP = 'PAGE_UP'; - var PAGE_DN = 'PAGE_DN'; - var R_ARROW = '\u2192' ; - var S_PHI = '\u03C6' ; - var S_THETA = '\u03B8' ; - var U_ARROW = '\u2191' ; - - function charCodeToString(key) { - if (isShiftPressed) - switch (key) { - case 48: return ')'; // SHIFT 1 - case 49: return '!'; - case 50: return '@'; - case 51: return '#'; - case 52: return '$'; - case 53: return '%'; - case 54: return '^'; - case 55: return '&'; - case 56: return '*'; - case 57: return '('; // SHIFT 0 - - case 186: return ':'; - case 187: return '+'; - case 188: return '<'; - case 189: return '_'; - case 190: return '>'; - case 191: return '?'; - case 192: return '~'; - case 219: return '{'; - case 220: return '|'; - case 221: return '}'; - case 222: return '"'; - } - - switch (key) { - case 8: return 'del'; - case 13: return 'ret'; - case 16: return 'cap'; - case 17: return 'control'; - case 18: return 'alt'; - case 27: return 'esc'; - case 32: return 'spc'; - case 32: return 'spc'; - case 33: return PAGE_UP; - case 34: return PAGE_DN; - case 37: return L_ARROW; - case 38: return U_ARROW; - case 39: return R_ARROW; - case 40: return D_ARROW; - case 91: return 'command'; - case 186: return ';'; - case 187: return '='; - case 188: return ','; - case 189: return '-'; - case 190: return '.'; - case 191: return '/'; - case 192: return '`'; - case 219: return '['; - case 220: return '\\'; - case 221: return ']'; - case 222: return "'"; - } - - var str = String.fromCharCode(key); - - if (key >= 64 && key < 64 + 32 && ! isShiftPressed) - str = str.toLowerCase(); - - return str; - } - - -// STRING UTILITIES. - - function variableToValue(str, name, value) { - - var cp = '.'.charCodeAt(0); - var c_ = '_'.charCodeAt(0); - var c0 = '0'.charCodeAt(0); - var c9 = '9'.charCodeAt(0); - var ca = 'a'.charCodeAt(0); - var cz = 'z'.charCodeAt(0); - var cA = 'A'.charCodeAt(0); - var cZ = 'Z'.charCodeAt(0); - - for (var i = 0 ; i < str.length - name.length ; i++) { - - // FIND AN OCCURANCE OF name IN THE STRING. - - if (str.substring(i, i + name.length) == name) { - - // NO MATCH IF name IS PRECEDED BY . or _ or 0-9 or a-z or A-Z. - - if (i > 0) { - var n = str.charCodeAt(i-1); - if (n == cp || n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) - continue; - } - - // NO MATCH IF name IS FOLLOWED BY _ or 0-9 or a-z or A-Z. - - if (i + name.length < str.length) { - var n = str.charCodeAt(i-1); - if (n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) - continue; - } - - // OTHERWISE, DO THE SUBSTITUTION, AND ADJUST i ACCORDINGLY. - - str = str.substring(0, i) + value + str.substring(i + name.length, str.length); - i += value.length - name.length; - } - } - - return str; - } - - -// ARRAY UTILITIES. - - function arrayToString(a, level) { - if (a.length == 0) - return "[]"; - if (level === undefined) - level = 0; - var spacer = level == 0 ? " " : ""; - var str = "[" + spacer; - for (var i = 0 ; i < a.length ; i++) - str += (a[i] instanceof Array ? arrayToString(a[i], level+1) : a[i]) - + spacer + (i < a.length-1 ? "," + spacer : "]"); - return str; - } - - function cloneArray(src) { - var dst = []; - for (var i = 0 ; i < src.length ; i++) - if (src[i] instanceof Array) - dst[i] = cloneArray(src[i]); - else - dst[i] = src[i]; - return dst; - } - - function firstUndefinedArrayIndex(arr) { - var n = 0; - while (n < arr.length && isDef(arr[n]) && arr[n] != null) - n++; - return n; - } - - function getIndex(arr, obj) { - var i = arr.length; - while (--i >= 0 && arr[i] !== obj) ; - return i; - } - - function sample(arr, t) { - t = max(0, min(0.999, t)); - var n = arr.length; - if (n == 1) - return arr[0]; - var i = floor((n-1) * t); - var f = (n-1) * t - i; - return lerp(f, arr[i], arr[i+1]); - } - - function newZeroArray(size) { - var dst = []; - for (var i = 0 ; i < size ; i++) - dst.push(0); - return dst; - } - - -// IMAGE PROCESSING. - - function findConnectedComponents(src, nc, dst, f0) { - function findConnectedComponent(i, n) { - if (src[i] < f0) - return; - - dst[i] = n; - var c = i % nc; - var r = i / nc; - if (c > 0 && dst[i - 1 ] == 0) findConnectedComponent(i - 1 , n); - if (c < nc-1 && dst[i + 1 ] == 0) findConnectedComponent(i + 1 , n); - if (r > 0 && dst[i - nc] == 0) findConnectedComponent(i - nc, n); - if (r < nr-1 && dst[i + nc] == 0) findConnectedComponent(i + nc, n); - } - - if (f0 === undefined) - f0 = 0.5; - - var nr = src.length / nc; - - for (var i = 0 ; i < src.length ; i++) - dst[i] = 0; - - var n = 0; - for (var i = 0 ; i < src.length ; i++) - if (src[i] >= f0 && dst[i] == 0) - findConnectedComponent(i, ++n); - } - - function imageEnlarge(src, dst) { - if (this.tmp === undefined) - this.tmp = newZeroArray(dst.length); - - function index(i,j,w) { return max(0,min(w-1,i)) + w * max(0,min(w-1,j)); } - - var w = floor(sqrt(src.length)); - - for (var row = 0 ; row < w ; row++) - for (var col = 0 ; col < w ; col++) { - var i0 = index(row , col , w); - var i1 = index(row+1, col , w); - var i2 = index(row , col+1, w); - var i3 = index(row+1, col+1, w); - var j = index(2*row, 2*col, 2*w); - this.tmp[j ] = src[i0]; - this.tmp[j+1 ] = (src[i0] + src[i1]) / 2; - this.tmp[j +2*w] = (src[i0] + src[i2]) / 2; - this.tmp[j+1+2*w] = (src[i0] + src[i1] + src[i2] + src[i3]) / 4; - } - - var wt = [1/6,1/3,1/3,1/6]; - - for (var row = 0 ; row < 2*w ; row++) - for (var col = 0 ; col < 2*w ; col++) { - var sum = 0; - for (var u = -1 ; u <= 2 ; u++) - for (var v = -1 ; v <= 2 ; v++) - sum += this.tmp[index(col+u, row+v, 2*w)] * wt[u+1] * wt[v+1]; - dst[index(col, row, 2*w)] = sum; - } - } - - -// 2D GEOMETRY UTILITIES. - - // Change the length of a curve. - - function adjustCurveLength(curve, targetLength, i0) { - var n = curve.length; - - var ratio = targetLength / computeCurveLength(curve, i0); - - var x0 = (curve[1][0] + curve[n-1][0]) / 2; - var y0 = (curve[1][1] + curve[n-1][1]) / 2; - - var p = []; - for (var i = 0 ; i < n ; i++) - p.push([curve[i][0], curve[i][1]]); - - for (var i = 2 ; i <= n-2 ; i++) { - var t = 1 - 4*(i-n/2)*(i-n/2)/n/n; - var dr = t * (ratio - 1) + 1; - p[i][0] = lerp(dr, x0, p[i][0]); - p[i][1] = lerp(dr, y0, p[i][1]); - } - - for (var i = 2 ; i <= n-2 ; i++) { - curve[i][0] = p[i][0]; - curve[i][1] = p[i][1]; - } - } - - // Clip a curve to that part which is entirely outside of a rectangle. - - function clipCurveAgainstRect(src, R) { - if (src[0] == undefined) return []; - var dst = []; - var x1 = src[0][0]; - var y1 = src[0][1]; - if (! isInRect(x1,y1, R)) - dst.push([x1,y1]); - for (var n = 1 ; n < src.length ; n++) { - var x0 = x1, y0 = y1; - x1 = src[n][0]; - y1 = src[n][1]; - var draw0 = ! isInRect(x0,y0, R); - var draw1 = ! isInRect(x1,y1, R); - if (draw0 || draw1) { - if (! draw0) - dst.push(clipLineToRect(x0,y0, x1,y1, R)); - if (! draw1) - dst.push(clipLineToRect(x1,y1, x0,y0, R)); - else - dst.push([x1,y1]); - } - } - return dst; - } - - // Bend a curve toward a point, ending up at a target length. - - function bendCurve(curve, pt, totalLength, i0) { - if (i0 === undefined) i0 = 0; - - var n = curve.length; - - // FIND NEAREST POINT ON CURVE. - - var ddMin = Number.MAX_VALUE, im = 0; - for (var i = 0 ; i < n ; i++) { - var dx = curve[i][0] - pt[0]; - var dy = curve[i][1] - pt[1]; - var dd = dx * dx + dy * dy; - if (dd < ddMin) { - ddMin = dd; - im = i; - } - } - - // IF NOT AT THE ENDS, THEN WARP MIDDLE OF CURVE. - - if (im > n/8 && im < n*7/8) { - var dx = pt[0] - curve[im][0]; - var dy = pt[1] - curve[im][1]; - for (var i = i0+1 ; i < n-1 ; i++) { - var t = i < im ? sCurve((i-i0 ) / (im-i0 )) - : sCurve((n-1-i) / (n-1-im)); - curve[i][0] += t * dx; - curve[i][1] += t * dy; - } - return; - } - - //return; - - // IF AT THE ENDS, THEN MOVE ONE ENDPOINT AND PRESERVE LENGTH. - - var ax = curve[i0 ][0], ay = curve[i0 ][1]; - var bx = curve[n-1][0], by = curve[n-1][1]; - - var dxa = pt[0] - ax, dya = pt[1] - ay; - var dxb = pt[0] - bx, dyb = pt[1] - by; - - if (dxa * dxa + dya * dya < dxb * dxb + dyb * dyb) { - for (var i = n-2 ; i >= i0 ; i--) { - var t = (n-1-i) / (n-2); - curve[i][0] += t * dxa; - curve[i][1] += t * dya; - } - } - else - for (var i = i0 + 1 ; i <= n-1 ; i++) { - var t = (i-1) / (n-2); - curve[i][0] += t * dxb; - curve[i][1] += t * dyb; - } - //adjustCurveLength(curve, totalLength, i0); - } - - // FIND x,y,scale FOR ARRAY OF CURVES A TO BEST FIT ARRAY OF CURVES B. - - function bestCurvesFit(A, B) { - var x = 0, y = 0, z = 0, w = 0; - for (var n = 0 ; n < A.length ; n++) { - var xyz = bestCurveFit(A[n], B[n]); - var t = computeCurveLength(B[n]); - x += t * xyz[0]; - y += t * xyz[1]; - z += t * xyz[2]; - w += t; - } - return [x / w, y / w, z / w]; - } - - // FIND x,y,scale FOR CURVE P TO BEST FIT CURVE Q. - - function bestCurveFit(P, Q) { - var n = min(P.length, Q.length), a=0, b=0, c=0, d=0, e=0, f=0; - for (var i = 0 ; i < n ; i++) { - var px = P[i][0], py = P[i][1], qx = Q[i][0], qy = Q[i][1]; - a += px; - b += py; - c += qx; - d += qy; - e += px * px + py * py; - f += px * qx + py * qy; - } - return solve([ [n,0,a,c], [0,n,b,d], [a,b,e,f] ]); - } - - function clipLineToRect(ax,ay, bx,by, R) { - var tx = bx < R[0] ? (R[0] - ax) / (bx - ax) : - bx > R[2] ? (R[2] - ax) / (bx - ax) : 10000; - var ty = by < R[1] ? (R[1] - ay) / (by - ay) : - by > R[3] ? (R[3] - ay) / (by - ay) : 10000; - var t = max(0, min(1, min(tx, ty))); - return [lerp(t, ax, bx), lerp(t, ay, by)]; - } - - /* - Return the area of a 2D counterclockwise polygon. - */ - - function computeArea(P) { - var sum = 0; - for (var i = 0 ; i < P.length ; i++) { - var j = (i + 1) % P.length; - sum += (P[j][0] - P[i][0]) * (P[i][1] + P[j][1]); - } - return sum / 2; - } - - /* - Find out whether a 3D point is hidden by a 3D triangle. - */ - - var isPointBehindTriangle = function(p, tri) { - var L = [0,0,0]; - var W = [0,0,0]; - var dist = function(p, L) { return p[0] * L[0] + p[1] * L[1] + L[2]; } - - return function(p, tri) { - - // Loop through the three vertices of the triangle. - - for (var j = 0 ; j < 3 ; j++) { - - // Look at edge formed by the two vertices a and b opposite this vertex. - - var a = tri[(j+1)%3]; - var b = tri[(j+2)%3]; - - // From x,y coords of a and b, compute equation of 2d line through them. - - L[0] = b[1] - a[1]; - L[1] = a[0] - b[0]; - L[2] = -(a[0] * L[0] + a[1] * L[1]); - - // Compute fractional distance of point into triangle away from edge. - - W[j] = dist(p, L) / dist(tri[j], L); - - // If point is outside this edge, return false. - - if (W[j] < 0) - return false; - } - - // Compare barycentrically weighted z of triangle vertices to z of the point. - - return W[0] * tri[0][2] + W[1] * tri[1][2] + W[2] * tri[2][2] > p[2]; - } - }(); - - // Create an arc of a circle. - - function createArc(x, y, r, angle0, angle1, n) { - var c = []; - for (var i = 0 ; i <= n ; i++) { - var angle = lerp(i / n, angle0, angle1); - c.push([x + r * cos(angle), y + r * sin(angle)]); - } - return c; - } - - function createRoundRect(x, y, w, h, r) { - var c = []; - c = c.concat(createArc(x+r,y+h-r,r,PI/2,PI,8)); - c = c.concat([[x,y+h-r],[x,y+r]]); - c = c.concat(createArc(x+r,y+r,r,PI,3*PI/2,8)); - c = c.concat([[x+r,y],[x+w-r,y]]); - c = c.concat(createArc(x+w-r,y+r,r,-PI/2,0,8)); - c = c.concat([[x+w,y+r],[x+w,y+h-r]]); - c = c.concat(createArc(x+w-r,y+h-r,r,0,PI/2,8)); - c = c.concat([[x+w-r,y+h],[x+r,y+h]]); - return c; - } - - // Compute the bounding rectangle for a curve. - - function computeCurveBounds(src, i0) { - if (i0 === undefined) i0 = 0; - var xlo = 10000, ylo = xlo, xhi = -xlo, yhi = -ylo; - for (var n = i0 ; n < src.length ; n++) { - xlo = min(xlo, src[n][0]); - ylo = min(ylo, src[n][1]); - xhi = max(xhi, src[n][0]); - yhi = max(yhi, src[n][1]); - } - return [xlo,ylo,xhi,yhi]; - } - - // The union of two bounding rectangles. - - function computeUnionOfBounds(a, b) { - return [ min(a[0],b[0]), min(a[1], b[1]), max(a[2],b[2]), max(a[3],b[3]) ]; - } - - // Create a curved line. - - function createCurve(A, B, curvature, N) { - if (N === undefined) - N = 20; - - var ax = A[0], ay = A[1], bx = B[0], by = B[1]; - var dx = 4 * curvature * (bx - ax); - var dy = 4 * curvature * (by - ay); - - var dst = []; - - // STRAIGHT LINE - - if (curvature == 0) { - for (var n = 0 ; n <= N ; n++) - dst.push([lerp(n/N, ax, bx), lerp(n/N, ay, by)]); - return dst; - } - - // CIRCULAR LOOP - - if (abs(curvature) == loopFlag) { - var mx = (ax + bx) / 2, my = (ay + by) / 2; - var rx = (ax - bx) / 2, ry = (ay - by) / 2; - var dir = curvature > 0 ? 1 : -1; - - for (var n = 0 ; n <= N ; n++) { - var angle = TAU * n / N; - var c = cos(angle); - var s = sin(angle) * dir; - dst.push([ mx + rx * c + ry * s, - my - rx * s + ry * c ]); - } - return dst; - } - - // OPEN CURVE - - for (var n = 0 ; n <= N ; n++) { - var t = n / N; - var s = lerp(abs(curvature), t, sCurve(t)); - var e = t * (1 - t); - dst.push([lerp(s, ax, bx) - e * dy, - lerp(s, ay, by) + e * dx]); - } - return dst; - } - - // CREATE A SPLINE GUIDED BY A PATH OF KEY POINTS. - - function makeSpline(keys, N) { - function x(k) { return keys[k][0]; } - function y(k) { return keys[k][1]; } - function l(k) { return L[k]; } - function hermite(a, da, b, db) { - return a * ( 2 * ttt - 3 * tt + 1) - + da * ( ttt - 2 * tt + t ) - + b * (-2 * ttt + 3 * tt ) - + db * ( ttt - tt ); - } - if (N === undefined) - N = (keys.length - 1) * 4; - var nk = keys.length; - - var L = []; - for (var n = 0 ; n < nk-1 ; n++) - L.push(len(x(n+1) - x(n), y(n+1) - y(n))); - - var spline = []; - for (var n = 0 ; n < nk-1 ; n++) { - - var dx0 = n > 0 ? (l(n) * (x(n) - x(n-1)) + l(n-1) * (x(n+1) - x(n))) / (l(n-1) + l(n)) - : 3*x(n + 1) - 2*x(n) - x(n + 2); - var dy0 = n > 0 ? (l(n) * (y(n) - y(n-1)) + l(n-1) * (y(n+1) - y(n))) / (l(n-1) + l(n)) - : 3*y(n + 1) - 2*y(n) - y(n + 2); - - var dx1 = n < nk-2 ? (l(n+1) * (x(n+1) - x(n)) + l(n) * (x(n+2) - x(n+1))) / (l(n) + l(n+1)) - : 2*x(n + 1) - 3*x(n) + x(n - 1); - var dy1 = n < nk-2 ? (l(n+1) * (y(n+1) - y(n)) + l(n) * (y(n+2) - y(n+1))) / (l(n) + l(n+1)) - : 2*y(n + 1) - 3*y(n) + y(n - 1); - - for (var i = 0 ; i < N ; i++) { - var t = i / N, tt = t * t, ttt = t * tt; - spline.push([ hermite(x(n), dx0*.9, x(n+1), dx1*.9), - hermite(y(n), dy0*.9, y(n+1), dy1*.9) ]); - } - } - spline.push([ x(nk-1), y(nk-1) ]); - return spline; - } - - // Compute the curvature of a curved line from A to B which passes through M. - - function computeCurvature(A, M, B) { - if (M === undefined) { - M = A[floor(A.length / 2)]; - B = A[A.length - 1]; - A = A[0]; - } - var dx = B[0] - A[0]; - var dy = B[1] - A[1]; - var ex = M[0] - (A[0] + B[0]) / 2; - var ey = M[1] - (A[1] + B[1]) / 2; - return (dx * ey - dy * ex) / (dx * dx + dy * dy); - } - - // Compute the total geometric length of a curve. - - function computeCurveLength(curve, i0) { - var len = 0; - for (var i = (isDef(i0) ? i0 : 0) ; i < curve.length - 1 ; i++) { - var dx = curve[i+1][0] - curve[i][0]; - var dy = curve[i+1][1] - curve[i][1]; - len += sqrt(dx * dx + dy * dy); - } - return len; - } - - // Check whether a curve crosses a line. - - function curveIntersectLine(curve, a, b) { - var dst = [], p = null; - for (var i = 0 ; i < curve.length - 1 ; i++) - if ((p = lineIntersectLine(curve[i], curve[i+1], a, b)) != null) - dst.push(p); - return dst; - } - - // Return distance squared from point [x,y] to curve c. - - function dsqFromCurve(x, y, c) { - var dsq = 100000; - for (var i = 0 ; i < c.length - 1 ; i++) - dsq = min(dsq, dsqFromLine(x, y, c[i], c[i+1])); - return dsq; - } - - // Return distance squared from point [x,y] to line segment [a->b]. - - function dsqFromLine(x, y, a, b) { - var ax = a[0] - x, ay = a[1] - y; - var bx = b[0] - x, by = b[1] - y; - var dx = bx - ax, dy = by - ay; - if (ax * dx + ay * dy > 0 || bx * dx + by * dy < 0) - return min(ax * ax + ay * ay, bx * bx + by * by); - var aa = ax * ax + ay * ay; - var ad = ax * dx + ay * dy; - var dd = dx * dx + dy * dy; - return aa - ad * ad / dd; - } - - // Return the point parametric fractional distance t along a curve. - - function getPointOnCurve(curve, t) { - if (t <= 0) return curve[0]; - if (t >= 1) return curve[curve.length-1]; - var n = curve.length - 1; - var i = floor(t * n); - var f = t * n - i; - return [ lerp(f, curve[i][0], curve[i+1][0]) , - lerp(f, curve[i][1], curve[i+1][1]) ]; - } - - function isInRect(x,y, R) { - return x >= R[0] && y >= R[1] && x < R[2] && y < R[3]; - } - - // Find the intersection between two line segments. If no intersection, return null. - - function lineIntersectLine(a, b, c, d) { - function L(a) { return a[0] * A[0] + a[1] * A[1]; } - - // FIRST MAKE SURE [c,d] CROSSES [a,b]. - - var A = [ b[1] - a[1], a[0] - b[0] ]; - - var tb = L(b); - var tc = L(c); - var td = L(d); - - if ((tc > tb) == (td > tb)) - return null; - - // THEN FIND THE POINT OF INTERSECTION p. - - var f = (tb - tc) / (td - tc); - var p = [ lerp(f, c[0], d[0]), lerp(f, c[1], d[1]) ]; - - // THEN MAKE SURE p LIES BETWEEN a AND b. - - var A = [ b[0] - a[0], b[1] - a[1] ]; - - var tp = L(p); - var ta = L(a); - var tb = L(b); - - return tp >= ta && tp <= tb ? p : null; - } - - // Resample a curve to equal geometric spacing. - - function resampleCurve(src, count) { - if (count === undefined) count = 100; + var D = []; + for (var i = 0 ; i < src.length ; i++) + D.push(i == 0 ? 0 : D[i-1] + len(src[i][0]-src[i-1][0], + src[i][1]-src[i-1][1])); + var dst = []; + dst.push([src[0][0], src[0][1]]); + var i = 1; + var sum = D[src.length-1]; + for (var j = 1 ; j < count ; j++) { + var d = sum * j / count; + while (D[i] < d && i < src.length-1) + i++; + var f = (d - D[i-1]) / (D[i] - D[i-1]); + dst.push([lerp(f, src[i-1][0], src[i][0]), + lerp(f, src[i-1][1], src[i][1])]); + } - var D = []; - for (var i = 0 ; i < src.length ; i++) - D.push(i == 0 ? 0 : D[i-1] + len(src[i][0]-src[i-1][0], - src[i][1]-src[i-1][1])); - var dst = []; - dst.push([src[0][0], src[0][1]]); - var i = 1; - var sum = D[src.length-1]; - for (var j = 1 ; j < count ; j++) { - var d = sum * j / count; - while (D[i] < d && i < src.length-1) - i++; - var f = (d - D[i-1]) / (D[i] - D[i-1]); - dst.push([lerp(f, src[i-1][0], src[i][0]), - lerp(f, src[i-1][1], src[i][1])]); - } + // ACCOUNT FOR THE SOURCE CURVE BEING A CLOSED LOOP. - // ACCOUNT FOR THE SOURCE CURVE BEING A CLOSED LOOP. + if ( src[0][0] == src[src.length-1][0] && + src[0][1] == src[src.length-1][1] ) + dst.push([ src[0][0], src[0][1] ]); - if ( src[0][0] == src[src.length-1][0] && - src[0][1] == src[src.length-1][1] ) - dst.push([ src[0][0], src[0][1] ]); + return dst; + } - return dst; - } + function segmentCurve(src) { - function segmentCurve(src) { + // IF SRC POINTS ARE TOO CLOSELY SPACED, SKIP OVER SOME. - // IF SRC POINTS ARE TOO CLOSELY SPACED, SKIP OVER SOME. + var curve = []; + var i = 0; + for (var j = i ; j < src.length ; j++) { + var dx = src[j][0] - src[i][0]; + var dy = src[j][1] - src[i][1]; + if (j == 0 || len(dx, dy) > 2) { + curve.push([src[j][0],src[j][1]]); + i = j; + } + } - var curve = []; - var i = 0; - for (var j = i ; j < src.length ; j++) { - var dx = src[j][0] - src[i][0]; - var dy = src[j][1] - src[i][1]; - if (j == 0 || len(dx, dy) > 2) { - curve.push([src[j][0],src[j][1]]); - i = j; - } - } + // COMPUTE DIRECTIONS BETWEEN SUCCESSIVE POINTS. - // COMPUTE DIRECTIONS BETWEEN SUCCESSIVE POINTS. + function Dx(j) { return directions[j][0]; } + function Dy(j) { return directions[j][1]; } - function Dx(j) { return directions[j][0]; } - function Dy(j) { return directions[j][1]; } + var directions = []; + for (var i = 1 ; i < curve.length ; i++) { + var dx = curve[i][0] - curve[i-1][0]; + var dy = curve[i][1] - curve[i-1][1]; + var d = len(dx, dy); + directions.push([dx / d, dy / d]); + } - var directions = []; - for (var i = 1 ; i < curve.length ; i++) { - var dx = curve[i][0] - curve[i-1][0]; - var dy = curve[i][1] - curve[i-1][1]; - var d = len(dx, dy); - directions.push([dx / d, dy / d]); - } + // WHEREVER CURVE BENDS, SPLIT IT. - // WHEREVER CURVE BENDS, SPLIT IT. + var dst = []; + for (var j = 0 ; j < directions.length ; j++) { + if (j==0 || (Dx(j-1) * Dx(j) + Dy(j-1) * Dy(j) < 0.5)) + dst.push([]); + dst[dst.length-1].push([curve[j][0],curve[j][1]]); + } - var dst = []; - for (var j = 0 ; j < directions.length ; j++) { - if (j==0 || (Dx(j-1) * Dx(j) + Dy(j-1) * Dy(j) < 0.5)) - dst.push([]); - dst[dst.length-1].push([curve[j][0],curve[j][1]]); - } + // DISCARD ALL SUB-CURVES THAT ARE TOO SMALL. - // DISCARD ALL SUB-CURVES THAT ARE TOO SMALL. + for (var n = dst.length - 1 ; n >= 0 ; n--) { + var a = dst[n][0]; + var m = dst[n][floor(dst[n].length / 2)]; + var b = dst[n][dst[n].length - 1]; + if (max(distance(a,m),max(distance(m,b),distance(a,b))) < 10) + dst.splice(n, 1); + } - for (var n = dst.length - 1 ; n >= 0 ; n--) { - var a = dst[n][0]; - var m = dst[n][floor(dst[n].length / 2)]; - var b = dst[n][dst[n].length - 1]; - if (max(distance(a,m),max(distance(m,b),distance(a,b))) < 10) - dst.splice(n, 1); - } + // RETURN ARRAY OF CURVES. - // RETURN ARRAY OF CURVES. + return dst; + } - return dst; - } + /////////////////////////////////////////////////////////////////// + // Rearrange edges into long chains, suitable for defining a glyph. + /////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// - // Rearrange edges into long chains, suitable for defining a glyph. - /////////////////////////////////////////////////////////////////// + function edgesToStrokes(e2) { - function edgesToStrokes(e2) { + // Given side s of connection m, return the xy of the corresponding edge point. - // Given side s of connection m, return the xy of the corresponding edge point. + function c2xy(m, s) { + var n = C[m][s][0]; + var j = C[m][s][1]; + return e2[n][j]; + } - function c2xy(m, s) { - var n = C[m][s][0]; - var j = C[m][s][1]; - return e2[n][j]; - } + var hash = {}, C = []; - var hash = {}, C = []; + // Hash all the edges to find pairwise connections of matching vertices between them. - // Hash all the edges to find pairwise connections of matching vertices between them. + for (var n = 0 ; n < e2.length ; n++) - for (var n = 0 ; n < e2.length ; n++) + // Look at both points of the edge. - // Look at both points of the edge. + for (var j = 0 ; j < 2 ; j++) { - for (var j = 0 ; j < 2 ; j++) { + // Create a unique hash string for the point. - // Create a unique hash string for the point. + var p = e2[n][j]; + var h = p[0] + "," + p[1]; - var p = e2[n][j]; - var h = p[0] + "," + p[1]; + // If this is the first time we are seeing this point, make a new hash entry. - // If this is the first time we are seeing this point, make a new hash entry. + if (hash[h] === undefined) + hash[h] = [n,j]; - if (hash[h] === undefined) - hash[h] = [n,j]; + // Otherwise, it's a match! Add both sides of the connection to connections array. - // Otherwise, it's a match! Add both sides of the connection to connections array. + else + C.push([ hash[h], [n,j] ]); + } - else - C.push([ hash[h], [n,j] ]); - } + // Build long chains, using these pairwise connections between edges. - // Build long chains, using these pairwise connections between edges. + for (var m1 = 0 ; m1 < C.length - 1 ; m1++) - for (var m1 = 0 ; m1 < C.length - 1 ; m1++) + // Try all remaining connections to see whether this chain can be added to. - // Try all remaining connections to see whether this chain can be added to. + for (var m2 = m1 + 1 ; m2 < C.length ; m2++) { - for (var m2 = m1 + 1 ; m2 < C.length ; m2++) { + // Try prepending each side of the connection to the chain. - // Try prepending each side of the connection to the chain. + for (var s = 0 ; s < 2 ; s++) { + var i = 0; - for (var s = 0 ; s < 2 ; s++) { - var i = 0; + // The chain must not already have the same side of the same connection. - // The chain must not already have the same side of the same connection. + var hasIt = false; + for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) + hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; - var hasIt = false; - for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) - hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; + if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { + var c = C.splice(m2, 1)[0]; + C[m1] = [c[1-s], c[s]].concat(C[m1]); + m2 = m1; + break; + } + } - if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { - var c = C.splice(m2, 1)[0]; - C[m1] = [c[1-s], c[s]].concat(C[m1]); - m2 = m1; - break; - } - } + if (m2 == m1) + continue; - if (m2 == m1) - continue; + // If that didn't work, try postpending each side of the connection to the chain. - // If that didn't work, try postpending each side of the connection to the chain. + for (var s = 0 ; s < 2 ; s++) { + var i = C[m1].length - 1; - for (var s = 0 ; s < 2 ; s++) { - var i = C[m1].length - 1; + // The chain must not already have the same side of the same connection. - // The chain must not already have the same side of the same connection. + var hasIt = false; + for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) + hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; - var hasIt = false; - for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) - hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; + if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { + var c = C.splice(m2, 1)[0]; + C[m1] = C[m1].concat([c[s], c[1-s]]); + m2 = m1; + break; + } + } + } - if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { - var c = C.splice(m2, 1)[0]; - C[m1] = C[m1].concat([c[s], c[1-s]]); - m2 = m1; - break; - } - } - } + // Add in any edges which have been left out. - // Add in any edges which have been left out. + for (var n = 0 ; n < e2.length ; n++) { + var count = 0; + for (var m = 0 ; m < C.length ; m++) + for (k = 0 ; k < C[m].length ; k++) + if (C[m][k][0] == n) + count++; - for (var n = 0 ; n < e2.length ; n++) { - var count = 0; - for (var m = 0 ; m < C.length ; m++) - for (k = 0 ; k < C[m].length ; k++) - if (C[m][k][0] == n) - count++; + if (count < 2) + C.push( [ [n, 0], [n, 1] ] ); + } - if (count < 2) - C.push( [ [n, 0], [n, 1] ] ); - } + // Finally, package as a set of strokes that can be used to define a glyph. - // Finally, package as a set of strokes that can be used to define a glyph. + var c2 = []; + for (var m = 0 ; m < C.length ; m++) { + c2.push( [ c2xy(m, 0) ] ); + for (var k = 1 ; k < C[m].length ; k++) + c2[m].push( c2xy(m, k) ); + if (C[m][0][0] == C[m][C[m].length-1][0]) + c2[m].push( c2xy(m, 0) ); + } - var c2 = []; - for (var m = 0 ; m < C.length ; m++) { - c2.push( [ c2xy(m, 0) ] ); - for (var k = 1 ; k < C[m].length ; k++) - c2[m].push( c2xy(m, k) ); - if (C[m][0][0] == C[m][C[m].length-1][0]) - c2[m].push( c2xy(m, 0) ); - } + return c2; + } - return c2; - } + // VARIOUS MANIPULATIONS OF HTML ELEMENTS. -// VARIOUS MANIPULATIONS OF HTML ELEMENTS. + // Replace the text of an html element: - // Replace the text of an html element: + function replaceText(id, newText) { + document.getElementById(id).firstChild.nodeValue = newText; + } - function replaceText(id, newText) { - document.getElementById(id).firstChild.nodeValue = newText; - } + // Set the document's background color: - // Set the document's background color: + function setBackgroundColor(color) { + document.body.style.background = color; + } - function setBackgroundColor(color) { - document.body.style.background = color; - } + // Give "text-like" style to all the buttons of a document: - // Give "text-like" style to all the buttons of a document: + function textlike(tagtype, textColor, hoverColor, pressColor) { + var buttons = document.getElementsByTagName(tagtype); + for (var i = 0 ; i < buttons.length ; i++) { + var b = buttons[i]; + b.onmousedown = function() { this.style.color = pressColor; }; + b.onmouseup = function() { this.style.color = hoverColor; }; + b.onmouseover = function() { this.style.color = hoverColor; }; + b.onmouseout = function() { this.style.color = textColor; }; + b.style.border = '0px solid black'; + b.style.outline = '0px solid black'; + b.style.margin = 0; + b.style.padding = 0; + b.style.color = textColor; + b.style.fontFamily = 'Helvetica'; + b.style.fontSize = '12pt'; + b.style.backgroundColor = document.body.style.background; + } + } - function textlike(tagtype, textColor, hoverColor, pressColor) { - var buttons = document.getElementsByTagName(tagtype); - for (var i = 0 ; i < buttons.length ; i++) { - var b = buttons[i]; - b.onmousedown = function() { this.style.color = pressColor; }; - b.onmouseup = function() { this.style.color = hoverColor; }; - b.onmouseover = function() { this.style.color = hoverColor; }; - b.onmouseout = function() { this.style.color = textColor; }; - b.style.border = '0px solid black'; - b.style.outline = '0px solid black'; - b.style.margin = 0; - b.style.padding = 0; - b.style.color = textColor; - b.style.fontFamily = 'Helvetica'; - b.style.fontSize = '12pt'; - b.style.backgroundColor = document.body.style.background; - } - } + // Object that makes a button cycle through a set of choices: - // Object that makes a button cycle through a set of choices: + function choice(id, // id of the button's html tag + data) { // data is an array of strings + this.index = 0; + this.data = (typeof data === 'string') ? data.split('|') : data; - function choice(id, // id of the button's html tag - data) { // data is an array of strings - this.index = 0; - this.data = (typeof data === 'string') ? data.split('|') : data; + // The button that this choice object will control: - // The button that this choice object will control: + var button = document.getElementById(id); - var button = document.getElementById(id); + // The button needs to know about this choice object: - // The button needs to know about this choice object: + button.choice = this; - button.choice = this; + // Initially, set the button's text to the first choice: - // Initially, set the button's text to the first choice: + button.firstChild.nodeValue = this.data[0]; - button.firstChild.nodeValue = this.data[0]; + // Every click will set the button's text to the next choice: - // Every click will set the button's text to the next choice: + button.onclick = function() { + var choice = this.choice; + choice.index = (choice.index + 1) % choice.data.length; + this.firstChild.nodeValue = choice.data[choice.index]; + } + } - button.onclick = function() { - var choice = this.choice; - choice.index = (choice.index + 1) % choice.data.length; - this.firstChild.nodeValue = choice.data[choice.index]; - } - } + function getSpan(id) { + return document.getElementById(id).firstChild.nodeValue; + } - function getSpan(id) { - return document.getElementById(id).firstChild.nodeValue; - } + function setSpan(id, str) { + document.getElementById(id).firstChild.nodeValue = str; + } - function setSpan(id, str) { - document.getElementById(id).firstChild.nodeValue = str; - } + return server; +}); From e2df9e02b5428117ff73b95f03209fd02ff8fa34 Mon Sep 17 00:00:00 2001 From: Jason Sigal Date: Fri, 21 Nov 2014 15:50:03 -0500 Subject: [PATCH 29/30] putting utility.js back to normal --- utility.js | 2574 ++++++++++++++++++++++++++-------------------------- 1 file changed, 1285 insertions(+), 1289 deletions(-) diff --git a/utility.js b/utility.js index 4125d3c..0583e19 100644 --- a/utility.js +++ b/utility.js @@ -1,1363 +1,1359 @@ -define(function (require) { - 'use strict' - /////////////// UTILITY FUNCTIONS /////////////////// - - // CHECKING FOR SYNTAX ERRORS IN JAVASCRIPT CODE. - - function findSyntaxError( code ) { - var error = []; - var save_onerror = onerror; - onerror = function(errorMsg, url, lineNumber) { - error = [lineNumber, errorMsg.replace("Uncaught ","")]; - } - var element = document.createElement('script'); - element.appendChild(document.createTextNode( code )); - document.body.appendChild(element); - onerror = save_onerror; - return error; - } - - - // GET AND SET STATE DATA VIA THE PERSISTENT SERVER. - - function Server() { - this.name = name; - - this.set = function(key, val) { - var setForm = new FormData(); - setForm.append("key", key + ".json"); - setForm.append("value", JSON.stringify(val)); - - var request = new XMLHttpRequest(); - request.open("POST", "set"); - request.send(setForm); - } - - this.get = function(key, fn) { - var getRequest = new XMLHttpRequest(); - getRequest.open("GET", key + ".json"); - getRequest.onloadend = function() { - fn(getRequest.responseText); - } - getRequest.send(); - } - } - - var server = new Server(); - - - // TYPES AND FORMAT CONVERSIONS. - - function hexChar(n) { - return String.fromCharCode((n < 10 ? 48 : 87) + n); - } - function hex(n) { - return hexChar(n >> 4) + hexChar(n & 15); - } - function isDef(v) { return ! (v === undefined); } - function isNumeric(v) { return ! isNaN(v); } - function roundedString(v, nDigits) { - var nd = nDigits === undefined ? 2 : nDigits; - if (typeof(v) == 'string') - v = parseFloat(v); - var p = nd<=0 ? 1 : nd==1 ? 10 : nd==2 ? 100 : 1000; - var str = "" + (floor(p * abs(v) + 0.5) / p); - - if (nDigits !== undefined && nd > 0) { - var i = str.indexOf("."); - if (i < 0) { - str += "."; - i = str.length - 1; - } - while (str.length - i < nd + 1) - str += "0"; - } - - return (v < 0 ? "-" : "") + str; - } - - - // HANDLE PLAYING AN AUDIO SIGNAL: - - var audioNode = null, audioIndex = 0; - - var setAudioSignal= function(f) { - if (audioNode == null) { - audioContext = 'AudioContext' in window ? new AudioContext() : - 'webkitAudioContext' in window ? new webkitAudioContext() : null; - if (audioContext != null) { - audioNode = audioContext.createScriptProcessor(1024, 0, 1); - audioNode.connect(audioContext.destination); - } - } - if (audioNode != null) { - audioNode.onaudioprocess = function(event) { - var output = event.outputBuffer; - var signal = output.getChannelData(0); - if (f instanceof Array) - for (var i = 0 ; i < output.length ; i++) - signal[i] = f[audioIndex++ % f.length]; - else - for (var i = 0 ; i < output.length ; i++) - signal[i] = f(audioIndex++ / output.sampleRate); - } - } - } - - - // SET OPERATIONS: - - function Set() { - this.debug = false; - this.add = function(item) { - if (! this.contains(item)) - this.push(item); - } - - this.remove = function(item) { - var index = this.indexOf(item); - if (index >= 0) - this.splice(index, 1); - } - - this.contains = function(item) { - return this.indexOf(item) >= 0; - } - - this.indexOf = function(item) { - for (var i = 0 ; i < this.length ; i++) - if (equals(item, this[i])) - return i; - return -1; - } - - function equals(a, b) { - if (a instanceof Array) { - for (var i = 0 ; i < a.length ; i++) - if (! equals(a[i], b[i])) - return false; - return true; - } - return a == b; - } - - this.toString = function() { - var str = "["; - for (var i = 0 ; i < this.length ; i++) - str += this[i] + (i maxEl) { - maxEl = Math.abs(A[k][i]); - maxRow = k; - } - // Swap maximum row with current row (column by column). - for (var k=i; k-1; i--) { - x[i] = A[i][n] / A[i][i]; - for (var k=i-1; k>-1; k--) - A[k][n] -= A[k][i] * x[i]; - } - return x; // Return n x 1 result vector. - } - function isEqualArray(a, b) { - if (a === undefined || b === undefined || - a == null || b == null || a.length != b.length) - return false; - for (var i = 0 ; i < a.length ; i++) - if (a[i] != b[i]) - return false; - return true; - } - function floor(t) { return Math.floor(t); } - function ik(a, b, C, D) { - var cc = dot(C,C), x = (1 + (a*a - b*b)/cc) / 2, y = dot(C,D)/cc; - for (var i = 0 ; i < 3 ; i++) D[i] -= y * C[i]; - y = sqrt(max(0,a*a - cc*x*x) / dot(D,D)); - for (var i = 0 ; i < 3 ; i++) D[i] = x * C[i] + y * D[i]; - } - function len(x, y) { - if (y === undefined) - return sqrt(x[0] * x[0] + x[1] * x[1]); - return sqrt(x * x + y * y); - } - - function lerp(t, a, b) { return a + t * (b - a); } - function max(a,b) { return Math.max(a,b); } - function min(a,b) { return Math.min(a,b); } - - var noise2P = [], noise2U = [], noise2V = []; - function fractal(x) { - var value = 0; - for (var f = 1 ; f <= 512 ; f *= 2) - value += noise2(x * f, 0.03 * x * f) / f; - return value; - } - function turbulence(x) { - var value = 0; - for (var f = 1 ; f <= 512 ; f *= 2) - value += abs(noise2(x * f, 0.03 * x * f) / f); - return value; - } - function noise(x) { return noise2(x, 0.03 * x); } - function noise2(x, y) { - if (noise2P.length == 0) { - var p = noise2P, u = noise2U, v = noise2V, i, j; - for (i = 0 ; i < 256 ; i++) { - p[i] = i; - u[i] = 2 * random() - 1; - v[i] = 2 * random() - 1; - var s = sqrt(u[i]*u[i] + v[i]*v[i]); - u[i] /= s; - v[i] /= s; - } - while (--i) { - var k = p[i]; - p[i] = p[j = floor(256 * random())]; - p[j] = k; - } - for (i = 0 ; i < 256 + 2 ; i++) { - p[256 + i] = p[i]; - u[256 + i] = u[i]; - v[256 + i] = v[i]; - } - } - var P = noise2P, U = noise2U, V = noise2V; - x = (x + 4096) % 256; - y = (y + 4096) % 256; - var i = floor(x), u = x - i, s = sCurve(u); - var j = floor(y), v = y - j, t = sCurve(v); - var a = P[P[i] + j ], b = P[P[i+1] + j ]; - var c = P[P[i] + j+1], d = P[P[i+1] + j+1]; - return lerp(t, lerp(s, u*U[a] + v *V[a], (u-1)*U[b] + v *V[b]), - lerp(s, u*U[c] + (v-1)*V[c], (u-1)*U[d] + (v-1)*V[d])); - } - function pieMenuIndex(x,y,n) { - if (n === undefined) - n = 4; - return floor(n+.5-atan2(y,x) / (TAU/n)) % n; - } - function pow(a,b) { return Math.pow(a,b); } - var random = function() { - var seed = 2; - var x = (seed % 30268) + 1; - seed = (seed - (seed % 30268)) / 30268; - var y = (seed % 30306) + 1; - seed = (seed - (seed % 30306)) / 30306; - var z = (seed % 30322) + 1; - return function() { - return ( ((x = (171 * x) % 30269) / 30269) + - ((y = (172 * y) % 30307) / 30307) + - ((z = (170 * z) % 30323) / 30323) ) % 1; - } - }(); - function round() { return Math.round(); } - function sCurve(t) { return max(0, min(1, t * t * (3 - t - t))); } - function saw(t) { t = 2*t % 2; return t<1 ? t : 2-t; } - function sign(t) { return Math.sign(t); } - function sin(t) { return Math.sin(t); } - function square_wave(t) { return 2 * floor(2*t % 2) - 1; } - function sqrt(t) { return Math.sqrt(t); } - function tan(t) { return Math.tan(t); } - - // CHARACTER CONSTANTS AND CONVERSIONS. - - var ALT = '\u22C0' ; - var C_PHI = '\u03A6' ; - var C_THETA = '\u0398' ; - var COMMAND = '\u2318' ; - var CONTROL = '\u2201' ; - var D_ARROW = '\u2193' ; - var L_ARROW = '\u2190' ; - var PAGE_UP = 'PAGE_UP'; - var PAGE_DN = 'PAGE_DN'; - var R_ARROW = '\u2192' ; - var S_PHI = '\u03C6' ; - var S_THETA = '\u03B8' ; - var U_ARROW = '\u2191' ; - - function charCodeToString(key) { - if (isShiftPressed) - switch (key) { - case 48: return ')'; // SHIFT 1 - case 49: return '!'; - case 50: return '@'; - case 51: return '#'; - case 52: return '$'; - case 53: return '%'; - case 54: return '^'; - case 55: return '&'; - case 56: return '*'; - case 57: return '('; // SHIFT 0 - - case 186: return ':'; - case 187: return '+'; - case 188: return '<'; - case 189: return '_'; - case 190: return '>'; - case 191: return '?'; - case 192: return '~'; - case 219: return '{'; - case 220: return '|'; - case 221: return '}'; - case 222: return '"'; - } - - switch (key) { - case 8: return 'del'; - case 13: return 'ret'; - case 16: return 'cap'; - case 17: return 'control'; - case 18: return 'alt'; - case 27: return 'esc'; - case 32: return 'spc'; - case 32: return 'spc'; - case 33: return PAGE_UP; - case 34: return PAGE_DN; - case 37: return L_ARROW; - case 38: return U_ARROW; - case 39: return R_ARROW; - case 40: return D_ARROW; - case 91: return 'command'; - case 186: return ';'; - case 187: return '='; - case 188: return ','; - case 189: return '-'; - case 190: return '.'; - case 191: return '/'; - case 192: return '`'; - case 219: return '['; - case 220: return '\\'; - case 221: return ']'; - case 222: return "'"; - } - - var str = String.fromCharCode(key); - - if (key >= 64 && key < 64 + 32 && ! isShiftPressed) - str = str.toLowerCase(); - - return str; - } - - - // STRING UTILITIES. - - function variableToValue(str, name, value) { - - var cp = '.'.charCodeAt(0); - var c_ = '_'.charCodeAt(0); - var c0 = '0'.charCodeAt(0); - var c9 = '9'.charCodeAt(0); - var ca = 'a'.charCodeAt(0); - var cz = 'z'.charCodeAt(0); - var cA = 'A'.charCodeAt(0); - var cZ = 'Z'.charCodeAt(0); - - for (var i = 0 ; i < str.length - name.length ; i++) { - - // FIND AN OCCURANCE OF name IN THE STRING. - - if (str.substring(i, i + name.length) == name) { - - // NO MATCH IF name IS PRECEDED BY . or _ or 0-9 or a-z or A-Z. - - if (i > 0) { - var n = str.charCodeAt(i-1); - if (n == cp || n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) - continue; - } - - // NO MATCH IF name IS FOLLOWED BY _ or 0-9 or a-z or A-Z. - - if (i + name.length < str.length) { - var n = str.charCodeAt(i-1); - if (n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) - continue; - } - - // OTHERWISE, DO THE SUBSTITUTION, AND ADJUST i ACCORDINGLY. - - str = str.substring(0, i) + value + str.substring(i + name.length, str.length); - i += value.length - name.length; - } - } - - return str; - } - - - // ARRAY UTILITIES. - - function arrayToString(a, level) { - if (a.length == 0) - return "[]"; - if (level === undefined) - level = 0; - var spacer = level == 0 ? " " : ""; - var str = "[" + spacer; - for (var i = 0 ; i < a.length ; i++) - str += (a[i] instanceof Array ? arrayToString(a[i], level+1) : a[i]) - + spacer + (i < a.length-1 ? "," + spacer : "]"); - return str; - } - - function cloneArray(src) { - var dst = []; - for (var i = 0 ; i < src.length ; i++) - if (src[i] instanceof Array) - dst[i] = cloneArray(src[i]); - else - dst[i] = src[i]; - return dst; - } - - function firstUndefinedArrayIndex(arr) { - var n = 0; - while (n < arr.length && isDef(arr[n]) && arr[n] != null) - n++; - return n; - } - - function getIndex(arr, obj) { - var i = arr.length; - while (--i >= 0 && arr[i] !== obj) ; - return i; - } - - function sample(arr, t) { - t = max(0, min(0.999, t)); - var n = arr.length; - if (n == 1) - return arr[0]; - var i = floor((n-1) * t); - var f = (n-1) * t - i; - return lerp(f, arr[i], arr[i+1]); - } - - function newZeroArray(size) { - var dst = []; - for (var i = 0 ; i < size ; i++) - dst.push(0); - return dst; - } - - - // IMAGE PROCESSING. - - function findConnectedComponents(src, nc, dst, f0) { - function findConnectedComponent(i, n) { - if (src[i] < f0) - return; - - dst[i] = n; - var c = i % nc; - var r = i / nc; - if (c > 0 && dst[i - 1 ] == 0) findConnectedComponent(i - 1 , n); - if (c < nc-1 && dst[i + 1 ] == 0) findConnectedComponent(i + 1 , n); - if (r > 0 && dst[i - nc] == 0) findConnectedComponent(i - nc, n); - if (r < nr-1 && dst[i + nc] == 0) findConnectedComponent(i + nc, n); - } - - if (f0 === undefined) - f0 = 0.5; - - var nr = src.length / nc; - - for (var i = 0 ; i < src.length ; i++) - dst[i] = 0; - - var n = 0; - for (var i = 0 ; i < src.length ; i++) - if (src[i] >= f0 && dst[i] == 0) - findConnectedComponent(i, ++n); - } - - function imageEnlarge(src, dst) { - if (this.tmp === undefined) - this.tmp = newZeroArray(dst.length); - - function index(i,j,w) { return max(0,min(w-1,i)) + w * max(0,min(w-1,j)); } - - var w = floor(sqrt(src.length)); - - for (var row = 0 ; row < w ; row++) - for (var col = 0 ; col < w ; col++) { - var i0 = index(row , col , w); - var i1 = index(row+1, col , w); - var i2 = index(row , col+1, w); - var i3 = index(row+1, col+1, w); - var j = index(2*row, 2*col, 2*w); - this.tmp[j ] = src[i0]; - this.tmp[j+1 ] = (src[i0] + src[i1]) / 2; - this.tmp[j +2*w] = (src[i0] + src[i2]) / 2; - this.tmp[j+1+2*w] = (src[i0] + src[i1] + src[i2] + src[i3]) / 4; - } - - var wt = [1/6,1/3,1/3,1/6]; - - for (var row = 0 ; row < 2*w ; row++) - for (var col = 0 ; col < 2*w ; col++) { - var sum = 0; - for (var u = -1 ; u <= 2 ; u++) - for (var v = -1 ; v <= 2 ; v++) - sum += this.tmp[index(col+u, row+v, 2*w)] * wt[u+1] * wt[v+1]; - dst[index(col, row, 2*w)] = sum; - } - } - - - // 2D GEOMETRY UTILITIES. - - // Change the length of a curve. - - function adjustCurveLength(curve, targetLength, i0) { - var n = curve.length; - - var ratio = targetLength / computeCurveLength(curve, i0); - - var x0 = (curve[1][0] + curve[n-1][0]) / 2; - var y0 = (curve[1][1] + curve[n-1][1]) / 2; - - var p = []; - for (var i = 0 ; i < n ; i++) - p.push([curve[i][0], curve[i][1]]); - - for (var i = 2 ; i <= n-2 ; i++) { - var t = 1 - 4*(i-n/2)*(i-n/2)/n/n; - var dr = t * (ratio - 1) + 1; - p[i][0] = lerp(dr, x0, p[i][0]); - p[i][1] = lerp(dr, y0, p[i][1]); - } - - for (var i = 2 ; i <= n-2 ; i++) { - curve[i][0] = p[i][0]; - curve[i][1] = p[i][1]; - } - } - - // Clip a curve to that part which is entirely outside of a rectangle. - - function clipCurveAgainstRect(src, R) { - if (src[0] == undefined) return []; - var dst = []; - var x1 = src[0][0]; - var y1 = src[0][1]; - if (! isInRect(x1,y1, R)) - dst.push([x1,y1]); - for (var n = 1 ; n < src.length ; n++) { - var x0 = x1, y0 = y1; - x1 = src[n][0]; - y1 = src[n][1]; - var draw0 = ! isInRect(x0,y0, R); - var draw1 = ! isInRect(x1,y1, R); - if (draw0 || draw1) { - if (! draw0) - dst.push(clipLineToRect(x0,y0, x1,y1, R)); - if (! draw1) - dst.push(clipLineToRect(x1,y1, x0,y0, R)); - else - dst.push([x1,y1]); - } - } - return dst; - } - - // Bend a curve toward a point, ending up at a target length. - - function bendCurve(curve, pt, totalLength, i0) { - if (i0 === undefined) i0 = 0; - - var n = curve.length; - - // FIND NEAREST POINT ON CURVE. - - var ddMin = Number.MAX_VALUE, im = 0; - for (var i = 0 ; i < n ; i++) { - var dx = curve[i][0] - pt[0]; - var dy = curve[i][1] - pt[1]; - var dd = dx * dx + dy * dy; - if (dd < ddMin) { - ddMin = dd; - im = i; - } - } - - // IF NOT AT THE ENDS, THEN WARP MIDDLE OF CURVE. - - if (im > n/8 && im < n*7/8) { - var dx = pt[0] - curve[im][0]; - var dy = pt[1] - curve[im][1]; - for (var i = i0+1 ; i < n-1 ; i++) { - var t = i < im ? sCurve((i-i0 ) / (im-i0 )) - : sCurve((n-1-i) / (n-1-im)); - curve[i][0] += t * dx; - curve[i][1] += t * dy; - } - return; - } - - //return; - - // IF AT THE ENDS, THEN MOVE ONE ENDPOINT AND PRESERVE LENGTH. - - var ax = curve[i0 ][0], ay = curve[i0 ][1]; - var bx = curve[n-1][0], by = curve[n-1][1]; - - var dxa = pt[0] - ax, dya = pt[1] - ay; - var dxb = pt[0] - bx, dyb = pt[1] - by; - - if (dxa * dxa + dya * dya < dxb * dxb + dyb * dyb) { - for (var i = n-2 ; i >= i0 ; i--) { - var t = (n-1-i) / (n-2); - curve[i][0] += t * dxa; - curve[i][1] += t * dya; - } - } - else - for (var i = i0 + 1 ; i <= n-1 ; i++) { - var t = (i-1) / (n-2); - curve[i][0] += t * dxb; - curve[i][1] += t * dyb; - } - //adjustCurveLength(curve, totalLength, i0); - } - - // FIND x,y,scale FOR ARRAY OF CURVES A TO BEST FIT ARRAY OF CURVES B. - - function bestCurvesFit(A, B) { - var x = 0, y = 0, z = 0, w = 0; - for (var n = 0 ; n < A.length ; n++) { - var xyz = bestCurveFit(A[n], B[n]); - var t = computeCurveLength(B[n]); - x += t * xyz[0]; - y += t * xyz[1]; - z += t * xyz[2]; - w += t; - } - return [x / w, y / w, z / w]; - } - - // FIND x,y,scale FOR CURVE P TO BEST FIT CURVE Q. - - function bestCurveFit(P, Q) { - var n = min(P.length, Q.length), a=0, b=0, c=0, d=0, e=0, f=0; - for (var i = 0 ; i < n ; i++) { - var px = P[i][0], py = P[i][1], qx = Q[i][0], qy = Q[i][1]; - a += px; - b += py; - c += qx; - d += qy; - e += px * px + py * py; - f += px * qx + py * qy; - } - return solve([ [n,0,a,c], [0,n,b,d], [a,b,e,f] ]); - } - - function clipLineToRect(ax,ay, bx,by, R) { - var tx = bx < R[0] ? (R[0] - ax) / (bx - ax) : - bx > R[2] ? (R[2] - ax) / (bx - ax) : 10000; - var ty = by < R[1] ? (R[1] - ay) / (by - ay) : - by > R[3] ? (R[3] - ay) / (by - ay) : 10000; - var t = max(0, min(1, min(tx, ty))); - return [lerp(t, ax, bx), lerp(t, ay, by)]; - } - - /* - Return the area of a 2D counterclockwise polygon. - */ - - function computeArea(P) { - var sum = 0; - for (var i = 0 ; i < P.length ; i++) { - var j = (i + 1) % P.length; - sum += (P[j][0] - P[i][0]) * (P[i][1] + P[j][1]); - } - return sum / 2; - } - - /* - Find out whether a 3D point is hidden by a 3D triangle. - */ - - var isPointBehindTriangle = function(p, tri) { - var L = [0,0,0]; - var W = [0,0,0]; - var dist = function(p, L) { return p[0] * L[0] + p[1] * L[1] + L[2]; } - - return function(p, tri) { - - // Loop through the three vertices of the triangle. - - for (var j = 0 ; j < 3 ; j++) { - - // Look at edge formed by the two vertices a and b opposite this vertex. - - var a = tri[(j+1)%3]; - var b = tri[(j+2)%3]; - - // From x,y coords of a and b, compute equation of 2d line through them. - - L[0] = b[1] - a[1]; - L[1] = a[0] - b[0]; - L[2] = -(a[0] * L[0] + a[1] * L[1]); - - // Compute fractional distance of point into triangle away from edge. - - W[j] = dist(p, L) / dist(tri[j], L); - - // If point is outside this edge, return false. - - if (W[j] < 0) - return false; - } - - // Compare barycentrically weighted z of triangle vertices to z of the point. - - return W[0] * tri[0][2] + W[1] * tri[1][2] + W[2] * tri[2][2] > p[2]; - } - }(); - - // Create an arc of a circle. - - function createArc(x, y, r, angle0, angle1, n) { - var c = []; - for (var i = 0 ; i <= n ; i++) { - var angle = lerp(i / n, angle0, angle1); - c.push([x + r * cos(angle), y + r * sin(angle)]); - } - return c; - } - - function createRoundRect(x, y, w, h, r) { - var c = []; - c = c.concat(createArc(x+r,y+h-r,r,PI/2,PI,8)); - c = c.concat([[x,y+h-r],[x,y+r]]); - c = c.concat(createArc(x+r,y+r,r,PI,3*PI/2,8)); - c = c.concat([[x+r,y],[x+w-r,y]]); - c = c.concat(createArc(x+w-r,y+r,r,-PI/2,0,8)); - c = c.concat([[x+w,y+r],[x+w,y+h-r]]); - c = c.concat(createArc(x+w-r,y+h-r,r,0,PI/2,8)); - c = c.concat([[x+w-r,y+h],[x+r,y+h]]); - return c; - } - - // Compute the bounding rectangle for a curve. - - function computeCurveBounds(src, i0) { - if (i0 === undefined) i0 = 0; - var xlo = 10000, ylo = xlo, xhi = -xlo, yhi = -ylo; - for (var n = i0 ; n < src.length ; n++) { - xlo = min(xlo, src[n][0]); - ylo = min(ylo, src[n][1]); - xhi = max(xhi, src[n][0]); - yhi = max(yhi, src[n][1]); - } - return [xlo,ylo,xhi,yhi]; - } - - // The union of two bounding rectangles. - - function computeUnionOfBounds(a, b) { - return [ min(a[0],b[0]), min(a[1], b[1]), max(a[2],b[2]), max(a[3],b[3]) ]; - } - - // Create a curved line. - - function createCurve(A, B, curvature, N) { - if (N === undefined) - N = 20; - - var ax = A[0], ay = A[1], bx = B[0], by = B[1]; - var dx = 4 * curvature * (bx - ax); - var dy = 4 * curvature * (by - ay); - - var dst = []; - - // STRAIGHT LINE - - if (curvature == 0) { - for (var n = 0 ; n <= N ; n++) - dst.push([lerp(n/N, ax, bx), lerp(n/N, ay, by)]); - return dst; - } - - // CIRCULAR LOOP - - if (abs(curvature) == loopFlag) { - var mx = (ax + bx) / 2, my = (ay + by) / 2; - var rx = (ax - bx) / 2, ry = (ay - by) / 2; - var dir = curvature > 0 ? 1 : -1; - - for (var n = 0 ; n <= N ; n++) { - var angle = TAU * n / N; - var c = cos(angle); - var s = sin(angle) * dir; - dst.push([ mx + rx * c + ry * s, - my - rx * s + ry * c ]); - } - return dst; - } - - // OPEN CURVE - - for (var n = 0 ; n <= N ; n++) { - var t = n / N; - var s = lerp(abs(curvature), t, sCurve(t)); - var e = t * (1 - t); - dst.push([lerp(s, ax, bx) - e * dy, - lerp(s, ay, by) + e * dx]); - } - return dst; - } - - // CREATE A SPLINE GUIDED BY A PATH OF KEY POINTS. - - function makeSpline(keys, N) { - function x(k) { return keys[k][0]; } - function y(k) { return keys[k][1]; } - function l(k) { return L[k]; } - function hermite(a, da, b, db) { - return a * ( 2 * ttt - 3 * tt + 1) - + da * ( ttt - 2 * tt + t ) - + b * (-2 * ttt + 3 * tt ) - + db * ( ttt - tt ); - } - if (N === undefined) - N = (keys.length - 1) * 4; - var nk = keys.length; - - var L = []; - for (var n = 0 ; n < nk-1 ; n++) - L.push(len(x(n+1) - x(n), y(n+1) - y(n))); - - var spline = []; - for (var n = 0 ; n < nk-1 ; n++) { - - var dx0 = n > 0 ? (l(n) * (x(n) - x(n-1)) + l(n-1) * (x(n+1) - x(n))) / (l(n-1) + l(n)) - : 3*x(n + 1) - 2*x(n) - x(n + 2); - var dy0 = n > 0 ? (l(n) * (y(n) - y(n-1)) + l(n-1) * (y(n+1) - y(n))) / (l(n-1) + l(n)) - : 3*y(n + 1) - 2*y(n) - y(n + 2); - - var dx1 = n < nk-2 ? (l(n+1) * (x(n+1) - x(n)) + l(n) * (x(n+2) - x(n+1))) / (l(n) + l(n+1)) - : 2*x(n + 1) - 3*x(n) + x(n - 1); - var dy1 = n < nk-2 ? (l(n+1) * (y(n+1) - y(n)) + l(n) * (y(n+2) - y(n+1))) / (l(n) + l(n+1)) - : 2*y(n + 1) - 3*y(n) + y(n - 1); - - for (var i = 0 ; i < N ; i++) { - var t = i / N, tt = t * t, ttt = t * tt; - spline.push([ hermite(x(n), dx0*.9, x(n+1), dx1*.9), - hermite(y(n), dy0*.9, y(n+1), dy1*.9) ]); - } - } - spline.push([ x(nk-1), y(nk-1) ]); - return spline; - } - - // Compute the curvature of a curved line from A to B which passes through M. - - function computeCurvature(A, M, B) { - if (M === undefined) { - M = A[floor(A.length / 2)]; - B = A[A.length - 1]; - A = A[0]; - } - var dx = B[0] - A[0]; - var dy = B[1] - A[1]; - var ex = M[0] - (A[0] + B[0]) / 2; - var ey = M[1] - (A[1] + B[1]) / 2; - return (dx * ey - dy * ex) / (dx * dx + dy * dy); - } - - // Compute the total geometric length of a curve. - - function computeCurveLength(curve, i0) { - var len = 0; - for (var i = (isDef(i0) ? i0 : 0) ; i < curve.length - 1 ; i++) { - var dx = curve[i+1][0] - curve[i][0]; - var dy = curve[i+1][1] - curve[i][1]; - len += sqrt(dx * dx + dy * dy); - } - return len; - } - - // Check whether a curve crosses a line. - - function curveIntersectLine(curve, a, b) { - var dst = [], p = null; - for (var i = 0 ; i < curve.length - 1 ; i++) - if ((p = lineIntersectLine(curve[i], curve[i+1], a, b)) != null) - dst.push(p); - return dst; - } - - // Return distance squared from point [x,y] to curve c. - - function dsqFromCurve(x, y, c) { - var dsq = 100000; - for (var i = 0 ; i < c.length - 1 ; i++) - dsq = min(dsq, dsqFromLine(x, y, c[i], c[i+1])); - return dsq; - } - - // Return distance squared from point [x,y] to line segment [a->b]. - - function dsqFromLine(x, y, a, b) { - var ax = a[0] - x, ay = a[1] - y; - var bx = b[0] - x, by = b[1] - y; - var dx = bx - ax, dy = by - ay; - if (ax * dx + ay * dy > 0 || bx * dx + by * dy < 0) - return min(ax * ax + ay * ay, bx * bx + by * by); - var aa = ax * ax + ay * ay; - var ad = ax * dx + ay * dy; - var dd = dx * dx + dy * dy; - return aa - ad * ad / dd; - } - - // Return the point parametric fractional distance t along a curve. - - function getPointOnCurve(curve, t) { - if (t <= 0) return curve[0]; - if (t >= 1) return curve[curve.length-1]; - var n = curve.length - 1; - var i = floor(t * n); - var f = t * n - i; - return [ lerp(f, curve[i][0], curve[i+1][0]) , - lerp(f, curve[i][1], curve[i+1][1]) ]; - } - - function isInRect(x,y, R) { - return x >= R[0] && y >= R[1] && x < R[2] && y < R[3]; - } - - // Find the intersection between two line segments. If no intersection, return null. - - function lineIntersectLine(a, b, c, d) { - function L(a) { return a[0] * A[0] + a[1] * A[1]; } - - // FIRST MAKE SURE [c,d] CROSSES [a,b]. - - var A = [ b[1] - a[1], a[0] - b[0] ]; - - var tb = L(b); - var tc = L(c); - var td = L(d); - - if ((tc > tb) == (td > tb)) - return null; - - // THEN FIND THE POINT OF INTERSECTION p. - - var f = (tb - tc) / (td - tc); - var p = [ lerp(f, c[0], d[0]), lerp(f, c[1], d[1]) ]; - - // THEN MAKE SURE p LIES BETWEEN a AND b. - - var A = [ b[0] - a[0], b[1] - a[1] ]; - - var tp = L(p); - var ta = L(a); - var tb = L(b); - - return tp >= ta && tp <= tb ? p : null; - } - - // Resample a curve to equal geometric spacing. - - function resampleCurve(src, count) { - if (count === undefined) count = 100; - var D = []; - for (var i = 0 ; i < src.length ; i++) - D.push(i == 0 ? 0 : D[i-1] + len(src[i][0]-src[i-1][0], - src[i][1]-src[i-1][1])); - var dst = []; - dst.push([src[0][0], src[0][1]]); - var i = 1; - var sum = D[src.length-1]; - for (var j = 1 ; j < count ; j++) { - var d = sum * j / count; - while (D[i] < d && i < src.length-1) - i++; - var f = (d - D[i-1]) / (D[i] - D[i-1]); - dst.push([lerp(f, src[i-1][0], src[i][0]), - lerp(f, src[i-1][1], src[i][1])]); - } +/////////////// UTILITY FUNCTIONS /////////////////// + +// CHECKING FOR SYNTAX ERRORS IN JAVASCRIPT CODE. + + function findSyntaxError( code ) { + var error = []; + var save_onerror = onerror; + onerror = function(errorMsg, url, lineNumber) { + error = [lineNumber, errorMsg.replace("Uncaught ","")]; + } + var element = document.createElement('script'); + element.appendChild(document.createTextNode( code )); + document.body.appendChild(element); + onerror = save_onerror; + return error; + } + + +// GET AND SET STATE DATA VIA THE PERSISTENT SERVER. + + function Server() { + this.name = name; + + this.set = function(key, val) { + var setForm = new FormData(); + setForm.append("key", key + ".json"); + setForm.append("value", JSON.stringify(val)); + + var request = new XMLHttpRequest(); + request.open("POST", "set"); + request.send(setForm); + } + + this.get = function(key, fn) { + var getRequest = new XMLHttpRequest(); + getRequest.open("GET", key + ".json"); + getRequest.onloadend = function() { + fn(getRequest.responseText); + } + getRequest.send(); + } + } + + var server = new Server(); + + +// TYPES AND FORMAT CONVERSIONS. + + function hexChar(n) { + return String.fromCharCode((n < 10 ? 48 : 87) + n); + } + function hex(n) { + return hexChar(n >> 4) + hexChar(n & 15); + } + function isDef(v) { return ! (v === undefined); } + function isNumeric(v) { return ! isNaN(v); } + function roundedString(v, nDigits) { + var nd = nDigits === undefined ? 2 : nDigits; + if (typeof(v) == 'string') + v = parseFloat(v); + var p = nd<=0 ? 1 : nd==1 ? 10 : nd==2 ? 100 : 1000; + var str = "" + (floor(p * abs(v) + 0.5) / p); + + if (nDigits !== undefined && nd > 0) { + var i = str.indexOf("."); + if (i < 0) { + str += "."; + i = str.length - 1; + } + while (str.length - i < nd + 1) + str += "0"; + } + + return (v < 0 ? "-" : "") + str; + } + + +// HANDLE PLAYING AN AUDIO SIGNAL: + + var audioNode = null, audioIndex = 0; + + var setAudioSignal= function(f) { + if (audioNode == null) { + audioContext = 'AudioContext' in window ? new AudioContext() : + 'webkitAudioContext' in window ? new webkitAudioContext() : null; + if (audioContext != null) { + audioNode = audioContext.createScriptProcessor(1024, 0, 1); + audioNode.connect(audioContext.destination); + } + } + if (audioNode != null) { + audioNode.onaudioprocess = function(event) { + var output = event.outputBuffer; + var signal = output.getChannelData(0); + if (f instanceof Array) + for (var i = 0 ; i < output.length ; i++) + signal[i] = f[audioIndex++ % f.length]; + else + for (var i = 0 ; i < output.length ; i++) + signal[i] = f(audioIndex++ / output.sampleRate); + } + } + } + + +// SET OPERATIONS: + + function Set() { + this.debug = false; + this.add = function(item) { + if (! this.contains(item)) + this.push(item); + } + + this.remove = function(item) { + var index = this.indexOf(item); + if (index >= 0) + this.splice(index, 1); + } + + this.contains = function(item) { + return this.indexOf(item) >= 0; + } + + this.indexOf = function(item) { + for (var i = 0 ; i < this.length ; i++) + if (equals(item, this[i])) + return i; + return -1; + } + + function equals(a, b) { + if (a instanceof Array) { + for (var i = 0 ; i < a.length ; i++) + if (! equals(a[i], b[i])) + return false; + return true; + } + return a == b; + } + + this.toString = function() { + var str = "["; + for (var i = 0 ; i < this.length ; i++) + str += this[i] + (i maxEl) { + maxEl = Math.abs(A[k][i]); + maxRow = k; + } + // Swap maximum row with current row (column by column). + for (var k=i; k-1; i--) { + x[i] = A[i][n] / A[i][i]; + for (var k=i-1; k>-1; k--) + A[k][n] -= A[k][i] * x[i]; + } + return x; // Return n x 1 result vector. + } + function isEqualArray(a, b) { + if (a === undefined || b === undefined || + a == null || b == null || a.length != b.length) + return false; + for (var i = 0 ; i < a.length ; i++) + if (a[i] != b[i]) + return false; + return true; + } + function floor(t) { return Math.floor(t); } + function ik(a, b, C, D) { + var cc = dot(C,C), x = (1 + (a*a - b*b)/cc) / 2, y = dot(C,D)/cc; + for (var i = 0 ; i < 3 ; i++) D[i] -= y * C[i]; + y = sqrt(max(0,a*a - cc*x*x) / dot(D,D)); + for (var i = 0 ; i < 3 ; i++) D[i] = x * C[i] + y * D[i]; + } + function len(x, y) { + if (y === undefined) + return sqrt(x[0] * x[0] + x[1] * x[1]); + return sqrt(x * x + y * y); + } + + function lerp(t, a, b) { return a + t * (b - a); } + function max(a,b) { return Math.max(a,b); } + function min(a,b) { return Math.min(a,b); } + + var noise2P = [], noise2U = [], noise2V = []; + function fractal(x) { + var value = 0; + for (var f = 1 ; f <= 512 ; f *= 2) + value += noise2(x * f, 0.03 * x * f) / f; + return value; + } + function turbulence(x) { + var value = 0; + for (var f = 1 ; f <= 512 ; f *= 2) + value += abs(noise2(x * f, 0.03 * x * f) / f); + return value; + } + function noise(x) { return noise2(x, 0.03 * x); } + function noise2(x, y) { + if (noise2P.length == 0) { + var p = noise2P, u = noise2U, v = noise2V, i, j; + for (i = 0 ; i < 256 ; i++) { + p[i] = i; + u[i] = 2 * random() - 1; + v[i] = 2 * random() - 1; + var s = sqrt(u[i]*u[i] + v[i]*v[i]); + u[i] /= s; + v[i] /= s; + } + while (--i) { + var k = p[i]; + p[i] = p[j = floor(256 * random())]; + p[j] = k; + } + for (i = 0 ; i < 256 + 2 ; i++) { + p[256 + i] = p[i]; + u[256 + i] = u[i]; + v[256 + i] = v[i]; + } + } + var P = noise2P, U = noise2U, V = noise2V; + x = (x + 4096) % 256; + y = (y + 4096) % 256; + var i = floor(x), u = x - i, s = sCurve(u); + var j = floor(y), v = y - j, t = sCurve(v); + var a = P[P[i] + j ], b = P[P[i+1] + j ]; + var c = P[P[i] + j+1], d = P[P[i+1] + j+1]; + return lerp(t, lerp(s, u*U[a] + v *V[a], (u-1)*U[b] + v *V[b]), + lerp(s, u*U[c] + (v-1)*V[c], (u-1)*U[d] + (v-1)*V[d])); + } + function pieMenuIndex(x,y,n) { + if (n === undefined) + n = 4; + return floor(n+.5-atan2(y,x) / (TAU/n)) % n; + } + function pow(a,b) { return Math.pow(a,b); } + var random = function() { + var seed = 2; + var x = (seed % 30268) + 1; + seed = (seed - (seed % 30268)) / 30268; + var y = (seed % 30306) + 1; + seed = (seed - (seed % 30306)) / 30306; + var z = (seed % 30322) + 1; + return function() { + return ( ((x = (171 * x) % 30269) / 30269) + + ((y = (172 * y) % 30307) / 30307) + + ((z = (170 * z) % 30323) / 30323) ) % 1; + } + }(); + function round() { return Math.round(); } + function sCurve(t) { return max(0, min(1, t * t * (3 - t - t))); } + function saw(t) { t = 2*t % 2; return t<1 ? t : 2-t; } + function sign(t) { return Math.sign(t); } + function sin(t) { return Math.sin(t); } + function square_wave(t) { return 2 * floor(2*t % 2) - 1; } + function sqrt(t) { return Math.sqrt(t); } + function tan(t) { return Math.tan(t); } + +// CHARACTER CONSTANTS AND CONVERSIONS. + + var ALT = '\u22C0' ; + var C_PHI = '\u03A6' ; + var C_THETA = '\u0398' ; + var COMMAND = '\u2318' ; + var CONTROL = '\u2201' ; + var D_ARROW = '\u2193' ; + var L_ARROW = '\u2190' ; + var PAGE_UP = 'PAGE_UP'; + var PAGE_DN = 'PAGE_DN'; + var R_ARROW = '\u2192' ; + var S_PHI = '\u03C6' ; + var S_THETA = '\u03B8' ; + var U_ARROW = '\u2191' ; + + function charCodeToString(key) { + if (isShiftPressed) + switch (key) { + case 48: return ')'; // SHIFT 1 + case 49: return '!'; + case 50: return '@'; + case 51: return '#'; + case 52: return '$'; + case 53: return '%'; + case 54: return '^'; + case 55: return '&'; + case 56: return '*'; + case 57: return '('; // SHIFT 0 + + case 186: return ':'; + case 187: return '+'; + case 188: return '<'; + case 189: return '_'; + case 190: return '>'; + case 191: return '?'; + case 192: return '~'; + case 219: return '{'; + case 220: return '|'; + case 221: return '}'; + case 222: return '"'; + } + + switch (key) { + case 8: return 'del'; + case 13: return 'ret'; + case 16: return 'cap'; + case 17: return 'control'; + case 18: return 'alt'; + case 27: return 'esc'; + case 32: return 'spc'; + case 32: return 'spc'; + case 33: return PAGE_UP; + case 34: return PAGE_DN; + case 37: return L_ARROW; + case 38: return U_ARROW; + case 39: return R_ARROW; + case 40: return D_ARROW; + case 91: return 'command'; + case 186: return ';'; + case 187: return '='; + case 188: return ','; + case 189: return '-'; + case 190: return '.'; + case 191: return '/'; + case 192: return '`'; + case 219: return '['; + case 220: return '\\'; + case 221: return ']'; + case 222: return "'"; + } + + var str = String.fromCharCode(key); + + if (key >= 64 && key < 64 + 32 && ! isShiftPressed) + str = str.toLowerCase(); + + return str; + } + + +// STRING UTILITIES. + + function variableToValue(str, name, value) { + + var cp = '.'.charCodeAt(0); + var c_ = '_'.charCodeAt(0); + var c0 = '0'.charCodeAt(0); + var c9 = '9'.charCodeAt(0); + var ca = 'a'.charCodeAt(0); + var cz = 'z'.charCodeAt(0); + var cA = 'A'.charCodeAt(0); + var cZ = 'Z'.charCodeAt(0); + + for (var i = 0 ; i < str.length - name.length ; i++) { + + // FIND AN OCCURANCE OF name IN THE STRING. + + if (str.substring(i, i + name.length) == name) { + + // NO MATCH IF name IS PRECEDED BY . or _ or 0-9 or a-z or A-Z. + + if (i > 0) { + var n = str.charCodeAt(i-1); + if (n == cp || n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) + continue; + } + + // NO MATCH IF name IS FOLLOWED BY _ or 0-9 or a-z or A-Z. + + if (i + name.length < str.length) { + var n = str.charCodeAt(i-1); + if (n == c_ || n >= c0 && n <= c9 || n >= ca && n <= cz || n >= cA && n <= cZ) + continue; + } + + // OTHERWISE, DO THE SUBSTITUTION, AND ADJUST i ACCORDINGLY. + + str = str.substring(0, i) + value + str.substring(i + name.length, str.length); + i += value.length - name.length; + } + } + + return str; + } + + +// ARRAY UTILITIES. + + function arrayToString(a, level) { + if (a.length == 0) + return "[]"; + if (level === undefined) + level = 0; + var spacer = level == 0 ? " " : ""; + var str = "[" + spacer; + for (var i = 0 ; i < a.length ; i++) + str += (a[i] instanceof Array ? arrayToString(a[i], level+1) : a[i]) + + spacer + (i < a.length-1 ? "," + spacer : "]"); + return str; + } + + function cloneArray(src) { + var dst = []; + for (var i = 0 ; i < src.length ; i++) + if (src[i] instanceof Array) + dst[i] = cloneArray(src[i]); + else + dst[i] = src[i]; + return dst; + } + + function firstUndefinedArrayIndex(arr) { + var n = 0; + while (n < arr.length && isDef(arr[n]) && arr[n] != null) + n++; + return n; + } + + function getIndex(arr, obj) { + var i = arr.length; + while (--i >= 0 && arr[i] !== obj) ; + return i; + } + + function sample(arr, t) { + t = max(0, min(0.999, t)); + var n = arr.length; + if (n == 1) + return arr[0]; + var i = floor((n-1) * t); + var f = (n-1) * t - i; + return lerp(f, arr[i], arr[i+1]); + } + + function newZeroArray(size) { + var dst = []; + for (var i = 0 ; i < size ; i++) + dst.push(0); + return dst; + } + + +// IMAGE PROCESSING. + + function findConnectedComponents(src, nc, dst, f0) { + function findConnectedComponent(i, n) { + if (src[i] < f0) + return; + + dst[i] = n; + var c = i % nc; + var r = i / nc; + if (c > 0 && dst[i - 1 ] == 0) findConnectedComponent(i - 1 , n); + if (c < nc-1 && dst[i + 1 ] == 0) findConnectedComponent(i + 1 , n); + if (r > 0 && dst[i - nc] == 0) findConnectedComponent(i - nc, n); + if (r < nr-1 && dst[i + nc] == 0) findConnectedComponent(i + nc, n); + } + + if (f0 === undefined) + f0 = 0.5; + + var nr = src.length / nc; + + for (var i = 0 ; i < src.length ; i++) + dst[i] = 0; + + var n = 0; + for (var i = 0 ; i < src.length ; i++) + if (src[i] >= f0 && dst[i] == 0) + findConnectedComponent(i, ++n); + } + + function imageEnlarge(src, dst) { + if (this.tmp === undefined) + this.tmp = newZeroArray(dst.length); + + function index(i,j,w) { return max(0,min(w-1,i)) + w * max(0,min(w-1,j)); } + + var w = floor(sqrt(src.length)); + + for (var row = 0 ; row < w ; row++) + for (var col = 0 ; col < w ; col++) { + var i0 = index(row , col , w); + var i1 = index(row+1, col , w); + var i2 = index(row , col+1, w); + var i3 = index(row+1, col+1, w); + var j = index(2*row, 2*col, 2*w); + this.tmp[j ] = src[i0]; + this.tmp[j+1 ] = (src[i0] + src[i1]) / 2; + this.tmp[j +2*w] = (src[i0] + src[i2]) / 2; + this.tmp[j+1+2*w] = (src[i0] + src[i1] + src[i2] + src[i3]) / 4; + } + + var wt = [1/6,1/3,1/3,1/6]; + + for (var row = 0 ; row < 2*w ; row++) + for (var col = 0 ; col < 2*w ; col++) { + var sum = 0; + for (var u = -1 ; u <= 2 ; u++) + for (var v = -1 ; v <= 2 ; v++) + sum += this.tmp[index(col+u, row+v, 2*w)] * wt[u+1] * wt[v+1]; + dst[index(col, row, 2*w)] = sum; + } + } + + +// 2D GEOMETRY UTILITIES. + + // Change the length of a curve. + + function adjustCurveLength(curve, targetLength, i0) { + var n = curve.length; + + var ratio = targetLength / computeCurveLength(curve, i0); + + var x0 = (curve[1][0] + curve[n-1][0]) / 2; + var y0 = (curve[1][1] + curve[n-1][1]) / 2; + + var p = []; + for (var i = 0 ; i < n ; i++) + p.push([curve[i][0], curve[i][1]]); + + for (var i = 2 ; i <= n-2 ; i++) { + var t = 1 - 4*(i-n/2)*(i-n/2)/n/n; + var dr = t * (ratio - 1) + 1; + p[i][0] = lerp(dr, x0, p[i][0]); + p[i][1] = lerp(dr, y0, p[i][1]); + } + + for (var i = 2 ; i <= n-2 ; i++) { + curve[i][0] = p[i][0]; + curve[i][1] = p[i][1]; + } + } + + // Clip a curve to that part which is entirely outside of a rectangle. + + function clipCurveAgainstRect(src, R) { + if (src[0] == undefined) return []; + var dst = []; + var x1 = src[0][0]; + var y1 = src[0][1]; + if (! isInRect(x1,y1, R)) + dst.push([x1,y1]); + for (var n = 1 ; n < src.length ; n++) { + var x0 = x1, y0 = y1; + x1 = src[n][0]; + y1 = src[n][1]; + var draw0 = ! isInRect(x0,y0, R); + var draw1 = ! isInRect(x1,y1, R); + if (draw0 || draw1) { + if (! draw0) + dst.push(clipLineToRect(x0,y0, x1,y1, R)); + if (! draw1) + dst.push(clipLineToRect(x1,y1, x0,y0, R)); + else + dst.push([x1,y1]); + } + } + return dst; + } + + // Bend a curve toward a point, ending up at a target length. + + function bendCurve(curve, pt, totalLength, i0) { + if (i0 === undefined) i0 = 0; + + var n = curve.length; + + // FIND NEAREST POINT ON CURVE. + + var ddMin = Number.MAX_VALUE, im = 0; + for (var i = 0 ; i < n ; i++) { + var dx = curve[i][0] - pt[0]; + var dy = curve[i][1] - pt[1]; + var dd = dx * dx + dy * dy; + if (dd < ddMin) { + ddMin = dd; + im = i; + } + } + + // IF NOT AT THE ENDS, THEN WARP MIDDLE OF CURVE. + + if (im > n/8 && im < n*7/8) { + var dx = pt[0] - curve[im][0]; + var dy = pt[1] - curve[im][1]; + for (var i = i0+1 ; i < n-1 ; i++) { + var t = i < im ? sCurve((i-i0 ) / (im-i0 )) + : sCurve((n-1-i) / (n-1-im)); + curve[i][0] += t * dx; + curve[i][1] += t * dy; + } + return; + } + + //return; + + // IF AT THE ENDS, THEN MOVE ONE ENDPOINT AND PRESERVE LENGTH. + + var ax = curve[i0 ][0], ay = curve[i0 ][1]; + var bx = curve[n-1][0], by = curve[n-1][1]; + + var dxa = pt[0] - ax, dya = pt[1] - ay; + var dxb = pt[0] - bx, dyb = pt[1] - by; + + if (dxa * dxa + dya * dya < dxb * dxb + dyb * dyb) { + for (var i = n-2 ; i >= i0 ; i--) { + var t = (n-1-i) / (n-2); + curve[i][0] += t * dxa; + curve[i][1] += t * dya; + } + } + else + for (var i = i0 + 1 ; i <= n-1 ; i++) { + var t = (i-1) / (n-2); + curve[i][0] += t * dxb; + curve[i][1] += t * dyb; + } + //adjustCurveLength(curve, totalLength, i0); + } + + // FIND x,y,scale FOR ARRAY OF CURVES A TO BEST FIT ARRAY OF CURVES B. + + function bestCurvesFit(A, B) { + var x = 0, y = 0, z = 0, w = 0; + for (var n = 0 ; n < A.length ; n++) { + var xyz = bestCurveFit(A[n], B[n]); + var t = computeCurveLength(B[n]); + x += t * xyz[0]; + y += t * xyz[1]; + z += t * xyz[2]; + w += t; + } + return [x / w, y / w, z / w]; + } + + // FIND x,y,scale FOR CURVE P TO BEST FIT CURVE Q. + + function bestCurveFit(P, Q) { + var n = min(P.length, Q.length), a=0, b=0, c=0, d=0, e=0, f=0; + for (var i = 0 ; i < n ; i++) { + var px = P[i][0], py = P[i][1], qx = Q[i][0], qy = Q[i][1]; + a += px; + b += py; + c += qx; + d += qy; + e += px * px + py * py; + f += px * qx + py * qy; + } + return solve([ [n,0,a,c], [0,n,b,d], [a,b,e,f] ]); + } + + function clipLineToRect(ax,ay, bx,by, R) { + var tx = bx < R[0] ? (R[0] - ax) / (bx - ax) : + bx > R[2] ? (R[2] - ax) / (bx - ax) : 10000; + var ty = by < R[1] ? (R[1] - ay) / (by - ay) : + by > R[3] ? (R[3] - ay) / (by - ay) : 10000; + var t = max(0, min(1, min(tx, ty))); + return [lerp(t, ax, bx), lerp(t, ay, by)]; + } + + /* + Return the area of a 2D counterclockwise polygon. + */ + + function computeArea(P) { + var sum = 0; + for (var i = 0 ; i < P.length ; i++) { + var j = (i + 1) % P.length; + sum += (P[j][0] - P[i][0]) * (P[i][1] + P[j][1]); + } + return sum / 2; + } + + /* + Find out whether a 3D point is hidden by a 3D triangle. + */ + + var isPointBehindTriangle = function(p, tri) { + var L = [0,0,0]; + var W = [0,0,0]; + var dist = function(p, L) { return p[0] * L[0] + p[1] * L[1] + L[2]; } + + return function(p, tri) { + + // Loop through the three vertices of the triangle. + + for (var j = 0 ; j < 3 ; j++) { + + // Look at edge formed by the two vertices a and b opposite this vertex. + + var a = tri[(j+1)%3]; + var b = tri[(j+2)%3]; + + // From x,y coords of a and b, compute equation of 2d line through them. + + L[0] = b[1] - a[1]; + L[1] = a[0] - b[0]; + L[2] = -(a[0] * L[0] + a[1] * L[1]); + + // Compute fractional distance of point into triangle away from edge. + + W[j] = dist(p, L) / dist(tri[j], L); + + // If point is outside this edge, return false. + + if (W[j] < 0) + return false; + } + + // Compare barycentrically weighted z of triangle vertices to z of the point. + + return W[0] * tri[0][2] + W[1] * tri[1][2] + W[2] * tri[2][2] > p[2]; + } + }(); + + // Create an arc of a circle. + + function createArc(x, y, r, angle0, angle1, n) { + var c = []; + for (var i = 0 ; i <= n ; i++) { + var angle = lerp(i / n, angle0, angle1); + c.push([x + r * cos(angle), y + r * sin(angle)]); + } + return c; + } + + function createRoundRect(x, y, w, h, r) { + var c = []; + c = c.concat(createArc(x+r,y+h-r,r,PI/2,PI,8)); + c = c.concat([[x,y+h-r],[x,y+r]]); + c = c.concat(createArc(x+r,y+r,r,PI,3*PI/2,8)); + c = c.concat([[x+r,y],[x+w-r,y]]); + c = c.concat(createArc(x+w-r,y+r,r,-PI/2,0,8)); + c = c.concat([[x+w,y+r],[x+w,y+h-r]]); + c = c.concat(createArc(x+w-r,y+h-r,r,0,PI/2,8)); + c = c.concat([[x+w-r,y+h],[x+r,y+h]]); + return c; + } + + // Compute the bounding rectangle for a curve. + + function computeCurveBounds(src, i0) { + if (i0 === undefined) i0 = 0; + var xlo = 10000, ylo = xlo, xhi = -xlo, yhi = -ylo; + for (var n = i0 ; n < src.length ; n++) { + xlo = min(xlo, src[n][0]); + ylo = min(ylo, src[n][1]); + xhi = max(xhi, src[n][0]); + yhi = max(yhi, src[n][1]); + } + return [xlo,ylo,xhi,yhi]; + } + + // The union of two bounding rectangles. + + function computeUnionOfBounds(a, b) { + return [ min(a[0],b[0]), min(a[1], b[1]), max(a[2],b[2]), max(a[3],b[3]) ]; + } + + // Create a curved line. + + function createCurve(A, B, curvature, N) { + if (N === undefined) + N = 20; + + var ax = A[0], ay = A[1], bx = B[0], by = B[1]; + var dx = 4 * curvature * (bx - ax); + var dy = 4 * curvature * (by - ay); + + var dst = []; + + // STRAIGHT LINE + + if (curvature == 0) { + for (var n = 0 ; n <= N ; n++) + dst.push([lerp(n/N, ax, bx), lerp(n/N, ay, by)]); + return dst; + } + + // CIRCULAR LOOP + + if (abs(curvature) == loopFlag) { + var mx = (ax + bx) / 2, my = (ay + by) / 2; + var rx = (ax - bx) / 2, ry = (ay - by) / 2; + var dir = curvature > 0 ? 1 : -1; + + for (var n = 0 ; n <= N ; n++) { + var angle = TAU * n / N; + var c = cos(angle); + var s = sin(angle) * dir; + dst.push([ mx + rx * c + ry * s, + my - rx * s + ry * c ]); + } + return dst; + } + + // OPEN CURVE + + for (var n = 0 ; n <= N ; n++) { + var t = n / N; + var s = lerp(abs(curvature), t, sCurve(t)); + var e = t * (1 - t); + dst.push([lerp(s, ax, bx) - e * dy, + lerp(s, ay, by) + e * dx]); + } + return dst; + } + + // CREATE A SPLINE GUIDED BY A PATH OF KEY POINTS. + + function makeSpline(keys, N) { + function x(k) { return keys[k][0]; } + function y(k) { return keys[k][1]; } + function l(k) { return L[k]; } + function hermite(a, da, b, db) { + return a * ( 2 * ttt - 3 * tt + 1) + + da * ( ttt - 2 * tt + t ) + + b * (-2 * ttt + 3 * tt ) + + db * ( ttt - tt ); + } + if (N === undefined) + N = (keys.length - 1) * 4; + var nk = keys.length; + + var L = []; + for (var n = 0 ; n < nk-1 ; n++) + L.push(len(x(n+1) - x(n), y(n+1) - y(n))); + + var spline = []; + for (var n = 0 ; n < nk-1 ; n++) { + + var dx0 = n > 0 ? (l(n) * (x(n) - x(n-1)) + l(n-1) * (x(n+1) - x(n))) / (l(n-1) + l(n)) + : 3*x(n + 1) - 2*x(n) - x(n + 2); + var dy0 = n > 0 ? (l(n) * (y(n) - y(n-1)) + l(n-1) * (y(n+1) - y(n))) / (l(n-1) + l(n)) + : 3*y(n + 1) - 2*y(n) - y(n + 2); + + var dx1 = n < nk-2 ? (l(n+1) * (x(n+1) - x(n)) + l(n) * (x(n+2) - x(n+1))) / (l(n) + l(n+1)) + : 2*x(n + 1) - 3*x(n) + x(n - 1); + var dy1 = n < nk-2 ? (l(n+1) * (y(n+1) - y(n)) + l(n) * (y(n+2) - y(n+1))) / (l(n) + l(n+1)) + : 2*y(n + 1) - 3*y(n) + y(n - 1); + + for (var i = 0 ; i < N ; i++) { + var t = i / N, tt = t * t, ttt = t * tt; + spline.push([ hermite(x(n), dx0*.9, x(n+1), dx1*.9), + hermite(y(n), dy0*.9, y(n+1), dy1*.9) ]); + } + } + spline.push([ x(nk-1), y(nk-1) ]); + return spline; + } + + // Compute the curvature of a curved line from A to B which passes through M. + + function computeCurvature(A, M, B) { + if (M === undefined) { + M = A[floor(A.length / 2)]; + B = A[A.length - 1]; + A = A[0]; + } + var dx = B[0] - A[0]; + var dy = B[1] - A[1]; + var ex = M[0] - (A[0] + B[0]) / 2; + var ey = M[1] - (A[1] + B[1]) / 2; + return (dx * ey - dy * ex) / (dx * dx + dy * dy); + } + + // Compute the total geometric length of a curve. + + function computeCurveLength(curve, i0) { + var len = 0; + for (var i = (isDef(i0) ? i0 : 0) ; i < curve.length - 1 ; i++) { + var dx = curve[i+1][0] - curve[i][0]; + var dy = curve[i+1][1] - curve[i][1]; + len += sqrt(dx * dx + dy * dy); + } + return len; + } + + // Check whether a curve crosses a line. + + function curveIntersectLine(curve, a, b) { + var dst = [], p = null; + for (var i = 0 ; i < curve.length - 1 ; i++) + if ((p = lineIntersectLine(curve[i], curve[i+1], a, b)) != null) + dst.push(p); + return dst; + } + + // Return distance squared from point [x,y] to curve c. + + function dsqFromCurve(x, y, c) { + var dsq = 100000; + for (var i = 0 ; i < c.length - 1 ; i++) + dsq = min(dsq, dsqFromLine(x, y, c[i], c[i+1])); + return dsq; + } + + // Return distance squared from point [x,y] to line segment [a->b]. + + function dsqFromLine(x, y, a, b) { + var ax = a[0] - x, ay = a[1] - y; + var bx = b[0] - x, by = b[1] - y; + var dx = bx - ax, dy = by - ay; + if (ax * dx + ay * dy > 0 || bx * dx + by * dy < 0) + return min(ax * ax + ay * ay, bx * bx + by * by); + var aa = ax * ax + ay * ay; + var ad = ax * dx + ay * dy; + var dd = dx * dx + dy * dy; + return aa - ad * ad / dd; + } + + // Return the point parametric fractional distance t along a curve. + + function getPointOnCurve(curve, t) { + if (t <= 0) return curve[0]; + if (t >= 1) return curve[curve.length-1]; + var n = curve.length - 1; + var i = floor(t * n); + var f = t * n - i; + return [ lerp(f, curve[i][0], curve[i+1][0]) , + lerp(f, curve[i][1], curve[i+1][1]) ]; + } + + function isInRect(x,y, R) { + return x >= R[0] && y >= R[1] && x < R[2] && y < R[3]; + } + + // Find the intersection between two line segments. If no intersection, return null. + + function lineIntersectLine(a, b, c, d) { + function L(a) { return a[0] * A[0] + a[1] * A[1]; } + + // FIRST MAKE SURE [c,d] CROSSES [a,b]. + + var A = [ b[1] - a[1], a[0] - b[0] ]; + + var tb = L(b); + var tc = L(c); + var td = L(d); + + if ((tc > tb) == (td > tb)) + return null; + + // THEN FIND THE POINT OF INTERSECTION p. + + var f = (tb - tc) / (td - tc); + var p = [ lerp(f, c[0], d[0]), lerp(f, c[1], d[1]) ]; + + // THEN MAKE SURE p LIES BETWEEN a AND b. + + var A = [ b[0] - a[0], b[1] - a[1] ]; + + var tp = L(p); + var ta = L(a); + var tb = L(b); + + return tp >= ta && tp <= tb ? p : null; + } + + // Resample a curve to equal geometric spacing. + + function resampleCurve(src, count) { + if (count === undefined) count = 100; - // ACCOUNT FOR THE SOURCE CURVE BEING A CLOSED LOOP. + var D = []; + for (var i = 0 ; i < src.length ; i++) + D.push(i == 0 ? 0 : D[i-1] + len(src[i][0]-src[i-1][0], + src[i][1]-src[i-1][1])); + var dst = []; + dst.push([src[0][0], src[0][1]]); + var i = 1; + var sum = D[src.length-1]; + for (var j = 1 ; j < count ; j++) { + var d = sum * j / count; + while (D[i] < d && i < src.length-1) + i++; + var f = (d - D[i-1]) / (D[i] - D[i-1]); + dst.push([lerp(f, src[i-1][0], src[i][0]), + lerp(f, src[i-1][1], src[i][1])]); + } - if ( src[0][0] == src[src.length-1][0] && - src[0][1] == src[src.length-1][1] ) - dst.push([ src[0][0], src[0][1] ]); + // ACCOUNT FOR THE SOURCE CURVE BEING A CLOSED LOOP. - return dst; - } + if ( src[0][0] == src[src.length-1][0] && + src[0][1] == src[src.length-1][1] ) + dst.push([ src[0][0], src[0][1] ]); - function segmentCurve(src) { + return dst; + } - // IF SRC POINTS ARE TOO CLOSELY SPACED, SKIP OVER SOME. + function segmentCurve(src) { - var curve = []; - var i = 0; - for (var j = i ; j < src.length ; j++) { - var dx = src[j][0] - src[i][0]; - var dy = src[j][1] - src[i][1]; - if (j == 0 || len(dx, dy) > 2) { - curve.push([src[j][0],src[j][1]]); - i = j; - } - } + // IF SRC POINTS ARE TOO CLOSELY SPACED, SKIP OVER SOME. - // COMPUTE DIRECTIONS BETWEEN SUCCESSIVE POINTS. + var curve = []; + var i = 0; + for (var j = i ; j < src.length ; j++) { + var dx = src[j][0] - src[i][0]; + var dy = src[j][1] - src[i][1]; + if (j == 0 || len(dx, dy) > 2) { + curve.push([src[j][0],src[j][1]]); + i = j; + } + } - function Dx(j) { return directions[j][0]; } - function Dy(j) { return directions[j][1]; } + // COMPUTE DIRECTIONS BETWEEN SUCCESSIVE POINTS. - var directions = []; - for (var i = 1 ; i < curve.length ; i++) { - var dx = curve[i][0] - curve[i-1][0]; - var dy = curve[i][1] - curve[i-1][1]; - var d = len(dx, dy); - directions.push([dx / d, dy / d]); - } + function Dx(j) { return directions[j][0]; } + function Dy(j) { return directions[j][1]; } - // WHEREVER CURVE BENDS, SPLIT IT. + var directions = []; + for (var i = 1 ; i < curve.length ; i++) { + var dx = curve[i][0] - curve[i-1][0]; + var dy = curve[i][1] - curve[i-1][1]; + var d = len(dx, dy); + directions.push([dx / d, dy / d]); + } - var dst = []; - for (var j = 0 ; j < directions.length ; j++) { - if (j==0 || (Dx(j-1) * Dx(j) + Dy(j-1) * Dy(j) < 0.5)) - dst.push([]); - dst[dst.length-1].push([curve[j][0],curve[j][1]]); - } + // WHEREVER CURVE BENDS, SPLIT IT. - // DISCARD ALL SUB-CURVES THAT ARE TOO SMALL. + var dst = []; + for (var j = 0 ; j < directions.length ; j++) { + if (j==0 || (Dx(j-1) * Dx(j) + Dy(j-1) * Dy(j) < 0.5)) + dst.push([]); + dst[dst.length-1].push([curve[j][0],curve[j][1]]); + } - for (var n = dst.length - 1 ; n >= 0 ; n--) { - var a = dst[n][0]; - var m = dst[n][floor(dst[n].length / 2)]; - var b = dst[n][dst[n].length - 1]; - if (max(distance(a,m),max(distance(m,b),distance(a,b))) < 10) - dst.splice(n, 1); - } + // DISCARD ALL SUB-CURVES THAT ARE TOO SMALL. - // RETURN ARRAY OF CURVES. + for (var n = dst.length - 1 ; n >= 0 ; n--) { + var a = dst[n][0]; + var m = dst[n][floor(dst[n].length / 2)]; + var b = dst[n][dst[n].length - 1]; + if (max(distance(a,m),max(distance(m,b),distance(a,b))) < 10) + dst.splice(n, 1); + } - return dst; - } + // RETURN ARRAY OF CURVES. - /////////////////////////////////////////////////////////////////// - // Rearrange edges into long chains, suitable for defining a glyph. - /////////////////////////////////////////////////////////////////// + return dst; + } - function edgesToStrokes(e2) { + /////////////////////////////////////////////////////////////////// + // Rearrange edges into long chains, suitable for defining a glyph. + /////////////////////////////////////////////////////////////////// - // Given side s of connection m, return the xy of the corresponding edge point. + function edgesToStrokes(e2) { - function c2xy(m, s) { - var n = C[m][s][0]; - var j = C[m][s][1]; - return e2[n][j]; - } + // Given side s of connection m, return the xy of the corresponding edge point. - var hash = {}, C = []; + function c2xy(m, s) { + var n = C[m][s][0]; + var j = C[m][s][1]; + return e2[n][j]; + } - // Hash all the edges to find pairwise connections of matching vertices between them. + var hash = {}, C = []; - for (var n = 0 ; n < e2.length ; n++) + // Hash all the edges to find pairwise connections of matching vertices between them. - // Look at both points of the edge. + for (var n = 0 ; n < e2.length ; n++) - for (var j = 0 ; j < 2 ; j++) { + // Look at both points of the edge. - // Create a unique hash string for the point. + for (var j = 0 ; j < 2 ; j++) { - var p = e2[n][j]; - var h = p[0] + "," + p[1]; + // Create a unique hash string for the point. - // If this is the first time we are seeing this point, make a new hash entry. + var p = e2[n][j]; + var h = p[0] + "," + p[1]; - if (hash[h] === undefined) - hash[h] = [n,j]; + // If this is the first time we are seeing this point, make a new hash entry. - // Otherwise, it's a match! Add both sides of the connection to connections array. + if (hash[h] === undefined) + hash[h] = [n,j]; - else - C.push([ hash[h], [n,j] ]); - } + // Otherwise, it's a match! Add both sides of the connection to connections array. - // Build long chains, using these pairwise connections between edges. + else + C.push([ hash[h], [n,j] ]); + } - for (var m1 = 0 ; m1 < C.length - 1 ; m1++) + // Build long chains, using these pairwise connections between edges. - // Try all remaining connections to see whether this chain can be added to. + for (var m1 = 0 ; m1 < C.length - 1 ; m1++) - for (var m2 = m1 + 1 ; m2 < C.length ; m2++) { + // Try all remaining connections to see whether this chain can be added to. - // Try prepending each side of the connection to the chain. + for (var m2 = m1 + 1 ; m2 < C.length ; m2++) { - for (var s = 0 ; s < 2 ; s++) { - var i = 0; + // Try prepending each side of the connection to the chain. - // The chain must not already have the same side of the same connection. + for (var s = 0 ; s < 2 ; s++) { + var i = 0; - var hasIt = false; - for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) - hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; + // The chain must not already have the same side of the same connection. - if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { - var c = C.splice(m2, 1)[0]; - C[m1] = [c[1-s], c[s]].concat(C[m1]); - m2 = m1; - break; - } - } + var hasIt = false; + for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) + hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; - if (m2 == m1) - continue; + if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { + var c = C.splice(m2, 1)[0]; + C[m1] = [c[1-s], c[s]].concat(C[m1]); + m2 = m1; + break; + } + } - // If that didn't work, try postpending each side of the connection to the chain. + if (m2 == m1) + continue; - for (var s = 0 ; s < 2 ; s++) { - var i = C[m1].length - 1; + // If that didn't work, try postpending each side of the connection to the chain. - // The chain must not already have the same side of the same connection. + for (var s = 0 ; s < 2 ; s++) { + var i = C[m1].length - 1; - var hasIt = false; - for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) - hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; + // The chain must not already have the same side of the same connection. - if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { - var c = C.splice(m2, 1)[0]; - C[m1] = C[m1].concat([c[s], c[1-s]]); - m2 = m1; - break; - } - } - } + var hasIt = false; + for (var k = 0 ; k < C[m1].length && ! hasIt ; k++) + hasIt = C[m1][k][0] == C[m2][s][0] && C[m1][k][1] == C[m2][s][1]; - // Add in any edges which have been left out. + if (! hasIt && C[m1][i][0] == C[m2][s][0] && C[m1][i][1] != C[m2][s][1]) { + var c = C.splice(m2, 1)[0]; + C[m1] = C[m1].concat([c[s], c[1-s]]); + m2 = m1; + break; + } + } + } - for (var n = 0 ; n < e2.length ; n++) { - var count = 0; - for (var m = 0 ; m < C.length ; m++) - for (k = 0 ; k < C[m].length ; k++) - if (C[m][k][0] == n) - count++; + // Add in any edges which have been left out. - if (count < 2) - C.push( [ [n, 0], [n, 1] ] ); - } + for (var n = 0 ; n < e2.length ; n++) { + var count = 0; + for (var m = 0 ; m < C.length ; m++) + for (k = 0 ; k < C[m].length ; k++) + if (C[m][k][0] == n) + count++; - // Finally, package as a set of strokes that can be used to define a glyph. + if (count < 2) + C.push( [ [n, 0], [n, 1] ] ); + } - var c2 = []; - for (var m = 0 ; m < C.length ; m++) { - c2.push( [ c2xy(m, 0) ] ); - for (var k = 1 ; k < C[m].length ; k++) - c2[m].push( c2xy(m, k) ); - if (C[m][0][0] == C[m][C[m].length-1][0]) - c2[m].push( c2xy(m, 0) ); - } + // Finally, package as a set of strokes that can be used to define a glyph. - return c2; - } + var c2 = []; + for (var m = 0 ; m < C.length ; m++) { + c2.push( [ c2xy(m, 0) ] ); + for (var k = 1 ; k < C[m].length ; k++) + c2[m].push( c2xy(m, k) ); + if (C[m][0][0] == C[m][C[m].length-1][0]) + c2[m].push( c2xy(m, 0) ); + } + return c2; + } - // VARIOUS MANIPULATIONS OF HTML ELEMENTS. - // Replace the text of an html element: +// VARIOUS MANIPULATIONS OF HTML ELEMENTS. - function replaceText(id, newText) { - document.getElementById(id).firstChild.nodeValue = newText; - } + // Replace the text of an html element: - // Set the document's background color: + function replaceText(id, newText) { + document.getElementById(id).firstChild.nodeValue = newText; + } - function setBackgroundColor(color) { - document.body.style.background = color; - } + // Set the document's background color: - // Give "text-like" style to all the buttons of a document: + function setBackgroundColor(color) { + document.body.style.background = color; + } - function textlike(tagtype, textColor, hoverColor, pressColor) { - var buttons = document.getElementsByTagName(tagtype); - for (var i = 0 ; i < buttons.length ; i++) { - var b = buttons[i]; - b.onmousedown = function() { this.style.color = pressColor; }; - b.onmouseup = function() { this.style.color = hoverColor; }; - b.onmouseover = function() { this.style.color = hoverColor; }; - b.onmouseout = function() { this.style.color = textColor; }; - b.style.border = '0px solid black'; - b.style.outline = '0px solid black'; - b.style.margin = 0; - b.style.padding = 0; - b.style.color = textColor; - b.style.fontFamily = 'Helvetica'; - b.style.fontSize = '12pt'; - b.style.backgroundColor = document.body.style.background; - } - } + // Give "text-like" style to all the buttons of a document: - // Object that makes a button cycle through a set of choices: + function textlike(tagtype, textColor, hoverColor, pressColor) { + var buttons = document.getElementsByTagName(tagtype); + for (var i = 0 ; i < buttons.length ; i++) { + var b = buttons[i]; + b.onmousedown = function() { this.style.color = pressColor; }; + b.onmouseup = function() { this.style.color = hoverColor; }; + b.onmouseover = function() { this.style.color = hoverColor; }; + b.onmouseout = function() { this.style.color = textColor; }; + b.style.border = '0px solid black'; + b.style.outline = '0px solid black'; + b.style.margin = 0; + b.style.padding = 0; + b.style.color = textColor; + b.style.fontFamily = 'Helvetica'; + b.style.fontSize = '12pt'; + b.style.backgroundColor = document.body.style.background; + } + } - function choice(id, // id of the button's html tag - data) { // data is an array of strings - this.index = 0; - this.data = (typeof data === 'string') ? data.split('|') : data; + // Object that makes a button cycle through a set of choices: - // The button that this choice object will control: + function choice(id, // id of the button's html tag + data) { // data is an array of strings + this.index = 0; + this.data = (typeof data === 'string') ? data.split('|') : data; - var button = document.getElementById(id); + // The button that this choice object will control: - // The button needs to know about this choice object: + var button = document.getElementById(id); - button.choice = this; + // The button needs to know about this choice object: - // Initially, set the button's text to the first choice: + button.choice = this; - button.firstChild.nodeValue = this.data[0]; + // Initially, set the button's text to the first choice: - // Every click will set the button's text to the next choice: + button.firstChild.nodeValue = this.data[0]; - button.onclick = function() { - var choice = this.choice; - choice.index = (choice.index + 1) % choice.data.length; - this.firstChild.nodeValue = choice.data[choice.index]; - } - } + // Every click will set the button's text to the next choice: - function getSpan(id) { - return document.getElementById(id).firstChild.nodeValue; - } + button.onclick = function() { + var choice = this.choice; + choice.index = (choice.index + 1) % choice.data.length; + this.firstChild.nodeValue = choice.data[choice.index]; + } + } - function setSpan(id, str) { - document.getElementById(id).firstChild.nodeValue = str; - } + function getSpan(id) { + return document.getElementById(id).firstChild.nodeValue; + } - return server; + function setSpan(id, str) { + document.getElementById(id).firstChild.nodeValue = str; + } -}); From d7dd7f2068a285af7a818f5d58ea3716ef16d437 Mon Sep 17 00:00:00 2001 From: Jason Sigal Date: Fri, 21 Nov 2014 15:50:51 -0500 Subject: [PATCH 30/30] removing unused grunt module --- ITP/create-template/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/ITP/create-template/package.json b/ITP/create-template/package.json index c228d5e..e1e4ede 100644 --- a/ITP/create-template/package.json +++ b/ITP/create-template/package.json @@ -6,7 +6,6 @@ "devDependencies": { "grunt": "~0.4.5", "grunt-contrib-requirejs": "~0.4.4", - "grunt-contrib-concat": "~0.5.0", "amdclean": "~2.3.0", "grunt-contrib-watch": "~0.6.1" }