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..e1e4ede 100644 --- a/ITP/create-template/package.json +++ b/ITP/create-template/package.json @@ -2,5 +2,11 @@ "amd": {}, "volo": { "baseUrl": "www/lib" + }, + "devDependencies": { + "grunt": "~0.4.5", + "grunt-contrib-requirejs": "~0.4.4", + "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/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/four.js b/four.js index 9ca1424..1d6d4f6 100644 --- a/four.js +++ b/four.js @@ -7,6 +7,30 @@ renderer.camera.updateProjectionMatrix(); }); + 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) { + 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; + } + } + THREE.Object3D.prototype.setMaterial = function(material) { if (isShowingMeshEdges) material = bgMaterial(); @@ -49,12 +73,14 @@ 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 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); } - 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 +122,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); @@ -136,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); @@ -252,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)); @@ -296,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; } @@ -307,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. @@ -451,54 +484,76 @@ } 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) { - 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); + if (! status) + console.log(gl().getShaderInfoLog(shader)); + return status; }; 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 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]; - for (var n = 0 ; n < typeInfo.length ; n += 3) + 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; } + } } } } } -function formFragmentShader(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: +// 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 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) }; @@ -510,88 +565,138 @@ 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 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() {' + ,' vNormal = normalize((modelViewMatrix * vec4(normal, 0.)).xyz);' + ,' vPosition = position*.03;' + ,' 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"); + +var defaultFragmentShader = [ + 'uniform vec3 ambient;' + ,'uniform vec3 diffuse;' + ,'uniform vec4 specular;' + ,'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 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);' + ,'}' +].join("\n"); // DEFINES FRAGMENT SHADER FUNCTIONS noise() and turbulence() AND VARS x, y, time and alpha. -var fragmentShaderHeader = ["\ - 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 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'); + +// 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;' +,'' +].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(); @@ -605,7 +710,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/g.js b/g.js index b4e2407..7fcd476 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 @@ -13,7 +16,6 @@ var isAltPressed = false; var isBottomGesture = false; var isExpertMode = true; - var isMakingGlyph = false; var isMouseOverBackground = true; var isShowing2DMeshEdges = false; var isShowingMeshEdges = false; @@ -408,7 +410,7 @@ if (! isk()) return; - if (isMakingGlyph) { + if (sk().isMakingGlyph) { if (! (sk() instanceof Sketch2D)) y = -y; buildTrace(glyphTrace, x, y, isLine); @@ -601,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(); @@ -612,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; @@ -651,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: @@ -956,9 +977,9 @@ // CREATE GLYPH SHAPE INFO. glyphTrace = []; - isMakingGlyph = true; + sk().isMakingGlyph = true; sk().renderWrapper(0.02); - isMakingGlyph = false; + sk().isMakingGlyph = undefined; // REGISTER THE GLYPH. @@ -2110,8 +2131,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); @@ -2858,6 +2879,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); @@ -2972,6 +2996,8 @@ console.log(lo + " " + hi); } addSketch(s.clone()); + if (sk().createMesh !== undefined) + sk().mesh = undefined; sk().sketchProgress = 1; sk().sketchState = 'finished'; @@ -3183,7 +3209,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; @@ -3305,12 +3330,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 +3484,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/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(); 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/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..ead8b3d 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 = .381872 * height(); + 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, -7 * height() / s); + + 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); @@ -93,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(); @@ -114,10 +132,12 @@ _g.restore(); } } - this.extendBounds = function(ax, ay, bx, by) { + this.extendBounds = function(points) { this.afterSketch(function() { + var saveStrokeStyle = _g.strokeStyle; color('rgba(0,0,0,.01)'); - mLine([ax, ay], [bx, by]); + mCurve(points); + _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; } @@ -596,6 +620,9 @@ this.render(elapsed); m.restore(); _g.restore(); + if (this.isMakingGlyph === undefined && this.createMesh !== undefined) { + this._updateMesh(); + } } this.sc = 1; this.scale = function(value) { @@ -708,6 +735,10 @@ _g.restore(); } + this.setUniform = function(name, val) { + if (this.mesh !== undefined) + this.mesh.material.setUniform(name, val); + } this.sketchLength = 1; this.cursorTransition = 0; this.sketchProgress = 0; @@ -742,6 +773,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 +861,115 @@ 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() { + var material = shaderMaterial(this.vertexShader, this.fragmentShader); + 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]]); + return material; + } + + this.updateVertexShader = function() { + if (this.vertexShader != codeTextArea.value) { + var isValid = isValidVertexShader(formSyntaxCheckVertexShader(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)); + if (isValid) { + this.fragmentShader = codeTextArea.value; + this.mesh.material = this.shaderMaterial(); + } + } + } + + 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) + this.meshBounds = [ [-1, -1] , [1, 1] ]; + this.mesh = this.createMesh(); + root.add(this.mesh); + this.is3D = true; + + // DEFAULT VALUES FOR PHONG COEFFICIENTS. + this.meshColorId = this.colorId; + } + if (this.mesh !== undefined) { + + // UPDATE MESH COLOR IF NEEDED. + + 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]; + 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; + } + + 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. + + this.setRenderMatrix(this.mesh.getMatrix()); + + // SET OPACITY. + + var alpha = max(0, this.glyphTransition) * + (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); + } + + if (this.updateMesh !== undefined) + this.updateMesh(); + + // FORCE BOUNDING BOX OF SKETCH EVEN IF IT HAS NO STROKES. + + this.extendBounds(this.meshBounds); + } + } this.value = null; this.x = 0; this.xyz = []; diff --git a/sketchPage.js b/sketchPage.js index 2230d8e..82c355a 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; } @@ -740,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().onClick === undefined && sk().code != null) { if (isCodeWidget && codeSketch != sk()) toggleCodeWidget(); codeSketch = sk(); @@ -1375,9 +1376,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; @@ -1721,7 +1719,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/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..a242404 100644 --- a/sketches/c2s.js +++ b/sketches/c2s.js @@ -1,7 +1,17 @@ +function C2S() { - function C2S() { - this.initSketchTo3D("c2s", [ makeOval(-1,-1,2,2,20,-PI/2,3*PI/2) ], function() { return root.addGlobe(); }); + this.labels = "c2s".split(' '); + + this.render = function() { + this.duringSketch(function() { + mCurve(makeOval(-1,-1,2,2,20,-PI/2,3*PI/2)); + }); + } + + this.createMesh = function() { + return new THREE.Mesh(globeGeometry(80,40), this.shaderMaterial()); } - C2S.prototype = new SketchTo3D; - addSketchType("C2S"); +} +C2S.prototype = new Sketch; +addSketchType("C2S"); 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/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/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; + } + } + } +}; + + + 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/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"); diff --git a/sketches/s2c.js b/sketches/s2c.js index 2a419be..3e339ef 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 Sketch; +addSketchType("S2C"); diff --git a/sketches/tactonic.js b/sketches/tactonic.js index 97ff8e0..3350505 100644 --- a/sketches/tactonic.js +++ b/sketches/tactonic.js @@ -1,39 +1,29 @@ function Tactonic() { this.labels = "tactonic".split(' '); - 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; - 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.render = function() { this.duringSketch(function() { mLine([-1,1],[1,1]); mLine([0,1],[0,-1]); }); + this.afterSketch(function() { - 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; - 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++; - } - } + mClosedCurve([ [-1.1,-1.1], [1.1,-1.1], [1.1,1.1], [-1.1,1.1] ]); }); } + + 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/sketches/torus.js b/sketches/torus.js new file mode 100644 index 0000000..e8e7a9b --- /dev/null +++ b/sketches/torus.js @@ -0,0 +1,28 @@ +function Torus() { + this.labels = "torus".split(' '); + this.r = .4; + + this.meshBounds = []; + var r = this.r; + for (var theta = 0 ; theta < TAU ; theta += TAU / 8) { + 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() { + this.duringSketch(function() { + 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.createMesh = function() { + return new THREE.Mesh(torusGeometry(this.r, 80, 80), this.shaderMaterial()); + } +} +Torus.prototype = new Sketch; +addSketchType("Torus"); + 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"); - 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"); + + 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/utility.js b/utility.js index affaf0c..0583e19 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) { @@ -64,12 +64,12 @@ if (nDigits !== undefined && nd > 0) { var i = str.indexOf("."); - if (i < 0) { - str += "."; - i = str.length - 1; + if (i < 0) { + str += "."; + i = str.length - 1; } while (str.length - i < nd + 1) - str += "0"; + str += "0"; } return (v < 0 ? "-" : "") + str; @@ -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; } @@ -571,15 +571,15 @@ function findConnectedComponents(src, nc, dst, f0) { function findConnectedComponent(i, n) { if (src[i] < f0) - return; + 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 (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) @@ -593,7 +593,7 @@ var n = 0; for (var i = 0 ; i < src.length ; i++) if (src[i] >= f0 && dst[i] == 0) - findConnectedComponent(i, ++n); + findConnectedComponent(i, ++n); } function imageEnlarge(src, dst) { @@ -981,9 +981,9 @@ function computeCurvature(A, M, B) { if (M === undefined) { - M = A[floor(A.length / 2)]; - B = A[A.length - 1]; - A = A[0]; + 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]; @@ -1010,7 +1010,7 @@ 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); + dst.push(p); return dst; } diff --git a/widgets.js b/widgets.js index d2de0a6..428598f 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,12 +677,27 @@ FOR WHEN WE HAVE DRAW_PATH SHORTCUT: updateF(); }, updateF = function() { - codeSketch.evalCode(codeTextArea.value); + 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)); + codeSketch._temporaryFunction(); } else if (code() != null) { - code()[codeSelector.selectedIndex][1] = codeTextArea.value; - codeSketch.selectedIndex = codeSelector.selectedIndex; + var index = codeSelector.selectedIndex; + code()[index][1] = text; + codeSketch.selectedIndex = index; + if (code()[index][2] !== undefined) { + codeSketch._temporaryFunction = code()[index][2]; + codeSketch._temporaryFunction(); + } + else + codeSketch.evalCode(text); } };