build/pgo/js-input/3d-thingy.html

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 <html>
michael@0 2 <head>
michael@0 3 <title>3d thingy</title>
michael@0 4 <style type="text/css">
michael@0 5 div.z2 { position:absolute; z-index:2; }
michael@0 6 div.z1 { position:absolute; z-index:1; }
michael@0 7 </style>
michael@0 8 <script type="text/javascript">
michael@0 9 /**************************************************************************
michael@0 10 JavaScript Graphics Library 0.0.1, Updated Source Code at Scriptersoft.com
michael@0 11 Copyright (C) 2005 Kurt L. Whicher
michael@0 12 November,13,2005
michael@0 13
michael@0 14 This library is free software; you can redistribute it and/or
michael@0 15 modify it under the terms of the GNU Lesser General Public
michael@0 16 License as published by the Free Software Foundation; either
michael@0 17 version 2.1 of the License, or (at your option) any later version.
michael@0 18
michael@0 19 This library is distributed in the hope that it will be useful,
michael@0 20 but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michael@0 22 Lesser General Public License for more details.
michael@0 23
michael@0 24 You should have received a copy of the GNU Lesser General Public
michael@0 25 License along with this library; if not, write to the Free Software
michael@0 26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
michael@0 27 **************************************************************************/
michael@0 28
michael@0 29 //________________________________________ global variables
michael@0 30
michael@0 31 var S_piDoubled=Math.PI*2;
michael@0 32 var S_deg2Rad=Math.PI/180;
michael@0 33
michael@0 34 //_______________________________________________ functions
michael@0 35
michael@0 36 function S_matrix() {
michael@0 37 return [1,0,0,0,
michael@0 38 0,1,0,0,
michael@0 39 0,0,1,0,
michael@0 40 0,0,0,1];
michael@0 41 }
michael@0 42 function S_vec2D(x,y) { this.x=x; this.y=y; }
michael@0 43 function S_vec3D(x,y,z) { this.x=x; this.y=y; this.z=z; }
michael@0 44 function S_subVec2D(a,b) {
michael@0 45 return new S_vec2D(a.x-b.x, a.y-b.y);
michael@0 46 }
michael@0 47 function S_subVec3D(a,b) {
michael@0 48 return new S_vec3D(a.x-b.x, a.y-b.y, a.z-b.z);
michael@0 49 }
michael@0 50 function S_dotVec3D(a, b) { return a.x*b.x+a.y*b.y+a.z*b.z; }
michael@0 51 function S_cross(a,b) {
michael@0 52 return new S_vec3D( a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
michael@0 53 }
michael@0 54 function S_lengthSquaredVec3D(v) { return S_dotVec3D(v,v); }
michael@0 55 function S_lengthVec3D(v) { return Math.sqrt(S_lengthSquaredVec3D(v)); }
michael@0 56 function S_normalizeVec3D(v) {
michael@0 57 var l=S_lengthVec3D(v), nv=new S_vec3D(0,0,0);
michael@0 58 if(l!=0) { nv.x=v.x/l; nv.y=v.y/l; nv.z=v.z/l; }
michael@0 59 return nv;
michael@0 60 }
michael@0 61 function S_rotate(m,ax,a) { // transformation matrix, axis, angle
michael@0 62 var i,j,ij=new Array(),v=new Array(),c=Math.cos(a),s=Math.sin(a);
michael@0 63 if (ax=="x") ij=[1,2,5,6,9,10,13,14];
michael@0 64 else if (ax=="y") ij=[2,0,6,4,10,8,14,12];
michael@0 65 else if (ax=="z") ij=[0,1,4,5,8,9,12,13];
michael@0 66 for (i=0;i<8;i++) v[i]=m[ij[i]];
michael@0 67 for (i=0,j=1;i<8;i+=2,j+=2) {
michael@0 68 m[ij[i]]=v[i]*c-v[j]*s;
michael@0 69 m[ij[j]]=v[i]*s+v[j]*c
michael@0 70 }
michael@0 71 }
michael@0 72 function S_checkBrowser() {
michael@0 73 if (document.getElementById) return true; else return false;
michael@0 74 }
michael@0 75 function S_zIndex(e,z) { document.getElementById(e).style.zIndex=z; }
michael@0 76 function S_rgbColor(r,g,b) {
michael@0 77 var i, c=[r,g,b];
michael@0 78 for(i=0; i<3; i++) {
michael@0 79 c[i]=Math.floor(c[i]);
michael@0 80 if(c[i]<0) c[i]=0; else if(c[i]>255) c[i]=255;
michael@0 81 }
michael@0 82 return c;
michael@0 83 }
michael@0 84 function S_rgbColorString(c) {
michael@0 85 return "rgb("+c[0]+","+c[1]+","+c[2]+")";
michael@0 86 }
michael@0 87 function S_vertice(x,y,z) {
michael@0 88 this.x=x; this.y=y; this.z=z; this.w=1;
michael@0 89 this.t=new S_vec3D(x,y,z); // transformed 3d
michael@0 90 this.p=new S_vec2D(0,0); // projected 2d
michael@0 91 }
michael@0 92 function S_face(v0,v1,v2,c) { // 3 vertice faces
michael@0 93 this.v=[v0,v1,v2]; this.c=c; this.b=0; // b:brightness
michael@0 94 this.d=true; // display: true or false
michael@0 95 }
michael@0 96 // x coordinate, number of vertices, distance
michael@0 97 function S_verticeRing(x,nv,d) {
michael@0 98 var i,a,v=new Array();
michael@0 99 for(i=0;i<nv;i++) {
michael@0 100 a=S_piDoubled*i/nv;
michael@0 101 v[i]=new S_vertice(x,d*Math.sin(a),d*Math.cos(a));
michael@0 102 }
michael@0 103 return v;
michael@0 104 }
michael@0 105 function S_triangleRing(r1,r2,c,clr) { // rows 1 & 2, cols, color
michael@0 106 var i,j,tr=new Array();
michael@0 107 for(i=0,j=1;i<c;i++,j=++j%c) {
michael@0 108 tr.push(new S_face(r1+i,r2+i,r2+j,clr));
michael@0 109 tr.push(new S_face(r1+i,r2+j,r1+j,clr));
michael@0 110 }
michael@0 111 return tr;
michael@0 112 }
michael@0 113 function S_model(v,f) {
michael@0 114 // vertice & face arrays, transformation matrix, display boolean
michael@0 115 this.v=v; this.f=f, this.tm=S_matrix(), this.d=true;
michael@0 116 }
michael@0 117 S_model.prototype.S_rotateX=function(a) {
michael@0 118 S_rotate(this.tm,"x",a*=S_deg2Rad);
michael@0 119 }
michael@0 120 S_model.prototype.S_rotateY=function(a) {
michael@0 121 S_rotate(this.tm,"y",a*=S_deg2Rad);
michael@0 122 }
michael@0 123 S_model.prototype.S_rotateZ=function(a) {
michael@0 124 S_rotate(this.tm,"z",a*=S_deg2Rad);
michael@0 125 }
michael@0 126 S_model.prototype.S_show=function() { this.d=true; }
michael@0 127 S_model.prototype.S_hide=function() { this.d=false; }
michael@0 128 function S_cube(d,c) { //distance & color
michael@0 129 return new S_cone(d,d,Math.cos(Math.PI/4)*d*2,1,4,c);
michael@0 130 }
michael@0 131 function S_cylinder(w,h,r,c,clr,e) {
michael@0 132 return new S_cone(w,w,h,r,c,clr,e);
michael@0 133 }
michael@0 134 // width, height, "rows", "columns", color, ends
michael@0 135 function S_cone(w1,w2,h,r,c,clr,e) {
michael@0 136 var i,r1=0,r2=c,v=new Array(),t=new Array(),rxc=r*c;
michael@0 137 for(i=0;i<=r;i++)
michael@0 138 v=v.concat(S_verticeRing(h*(0.5-i/r),c,w1*i/r+w2*(r-i)/r));
michael@0 139 for(i=0;i<r;i++,r1+=c,r2+=c)
michael@0 140 t=t.concat(S_triangleRing(r1,r2,c,clr));
michael@0 141 if (e!="hideEnds")
michael@0 142 for(i=1;i<(c-1);i++) {
michael@0 143 t.push(new S_face(0,i,i+1,clr));
michael@0 144 t.push(new S_face(rxc,rxc+i+1,rxc+i,clr));
michael@0 145 }
michael@0 146 return new S_model(v,t);
michael@0 147 }
michael@0 148 function S_sphere(d,r,c,clr) {
michael@0 149 // distance, "rows">=2, "columns">=3, color paramaters
michael@0 150 var v=new Array(),t=new Array(),r_1xc=(r-1)*c,r_2xc=(r-2)*c;
michael@0 151 var i,j,tmp,r1=0,r2=c;
michael@0 152 for(i=1;i<r;i++) {
michael@0 153 tmp=Math.PI*i/r;
michael@0 154 v=v.concat(S_verticeRing(d*Math.cos(tmp),c,Math.sin(tmp)*d));
michael@0 155 }
michael@0 156 v.push(new S_vertice( d,0,0));
michael@0 157 v.push(new S_vertice(-d,0,0));
michael@0 158 for(i=0;i<(r-2);i++,r1+=c,r2+=c)
michael@0 159 t=t.concat(S_triangleRing(r1,r2,c,clr));
michael@0 160 for(i=0,j=1;i<c;i++,j=++j%c) {
michael@0 161 t.push(new S_face(r_1xc,i,j,clr));
michael@0 162 t.push(new S_face(r_1xc+1,r_2xc+j,r_2xc+i,clr));
michael@0 163 }
michael@0 164 return new S_model(v,t);
michael@0 165 }
michael@0 166 S_model.prototype.S_scale=function(x) {
michael@0 167 this.tm[0]*=x; this.tm[5]*=x; this.tm[10]*=x;
michael@0 168 }
michael@0 169 S_model.prototype.S_faceColor=function(i,c) { this.f[i].c=c; }
michael@0 170 S_model.prototype.S_scaleX=function(s) { this.tm[0]*=s; }
michael@0 171 S_model.prototype.S_scaleY=function(s) { this.tm[5]*=s; }
michael@0 172 S_model.prototype.S_scaleZ=function(s) { this.tm[10]*=s; }
michael@0 173 function S_scene(dv,l,t,w,h,cmra) { // left, top, width, height
michael@0 174 this.dv=dv;
michael@0 175 this.ps=1; // pixel size
michael@0 176 this.l=l; this.t=t; this.w=w; this.h=h;
michael@0 177 this.cx=l+w/2; this.cy=t+h/2; // center x, center y
michael@0 178 this.dt="paint"; // output type
michael@0 179 this.m=new Array(); // model array
michael@0 180 this.lght=new S_light();
michael@0 181 this.lc=S_rgbColor(255,255,255); // light color
michael@0 182 this.cmra=-cmra; // camera on z axis
michael@0 183 this.bfr=S_buffer(h,w);
michael@0 184 }
michael@0 185 function S_buffer(h,w) {
michael@0 186 var i, j, b=new Array();
michael@0 187 for(i=0;i<h;i++) {
michael@0 188 b[i]=new Array();
michael@0 189 for(j=0;j<w;j++) b[i][j]=new S_pixel();
michael@0 190 }
michael@0 191 return b;
michael@0 192 }
michael@0 193 function S_pixel() { // display boolean, color
michael@0 194 this.d=true; this.c=0;
michael@0 195 }
michael@0 196 S_pixel.prototype.S_setColor=function(c) {
michael@0 197 this.d=true; this.c=c;
michael@0 198 }
michael@0 199 S_pixel.prototype.S_hide=function() { this.d=false; }
michael@0 200 S_scene.prototype.S_pixelSize=function(ps){ this.ps=ps; }
michael@0 201 S_scene.prototype.S_widthAndHeight=function(w,h){ this.w=w; this.h=h; }
michael@0 202 S_scene.prototype.S_center=function(cx,cy){ this.cx=cx; this.cy=cy; }
michael@0 203 S_scene.prototype.S_paint=function(){ this.dt="paint"; }
michael@0 204 S_scene.prototype.S_models=function() {
michael@0 205 var i; this.m=new Array();
michael@0 206 for(i=0;i<arguments.length;i++) this.m.push(arguments[i]);
michael@0 207 }
michael@0 208 S_scene.prototype.S_lightColor=function(c){ this.lc=c; }
michael@0 209 S_scene.prototype.S_project=function() {
michael@0 210 var i, j, v, tm, d, m;
michael@0 211 for(i=0;i<this.m.length;i++) {
michael@0 212 m=this.m[i]; tm=this.m[i].tm;
michael@0 213 for(j=0;j<m.v.length;j++) {
michael@0 214 v=m.v[j];
michael@0 215 v.t.x=v.x*tm[0]+v.y*tm[4]+v.z*tm[8]+v.w*tm[12];
michael@0 216 v.t.y=v.x*tm[1]+v.y*tm[5]+v.z*tm[9]+v.w*tm[13];
michael@0 217 v.t.z=v.x*tm[2]+v.y*tm[6]+v.z*tm[10]+v.w*tm[14];
michael@0 218 d=(this.cmra-v.t.z/2);
michael@0 219 if (d<0) {
michael@0 220 v.p.x=(this.cmra*v.t.x/d)+this.cx;
michael@0 221 v.p.y=-(this.cmra*v.t.y/d)+this.cy;
michael@0 222 }
michael@0 223 }
michael@0 224 }
michael@0 225 }
michael@0 226 S_scene.prototype.S_display=function(disp){
michael@0 227 var i, j, k, s="", ds, c, cnt=0; // ds:div start
michael@0 228 this.tr=new Array(); // triangles ready to draw
michael@0 229 this.S_project();
michael@0 230 this.S_adjustLight();
michael@0 231 this.S_clearBuffer();
michael@0 232 for(i=0;i<this.m.length;i++) {
michael@0 233 this.m[i].S_setupFaces(this.tr,this.lght.t);
michael@0 234 for(j=0;j<this.tr.length;j++) { // loop through triangles
michael@0 235 c=S_divColor(this.tr[j].c,this.lc,this.tr[j].b);
michael@0 236 S_setupBuffer(this,this.tr[j].p,c);
michael@0 237 }
michael@0 238 }
michael@0 239 for(i=0;i<this.h;i++) {
michael@0 240 ds=-1;
michael@0 241 for(j=0,k=1;j<this.w;j++,k++) {
michael@0 242 if((this.bfr[i][j].d==true)&&(ds==-1)) ds=j;
michael@0 243 if( (this.bfr[i][j].d==true)&&
michael@0 244 ( (k==this.w)||
michael@0 245 (this.bfr[i][k].d==false)||
michael@0 246 (!S_sameColor(this.bfr[i][j].c, this.bfr[i][k].c)) ) ) {
michael@0 247 s+=S_divString(S_rgbColorString(this.bfr[i][j].c),this.t+i*this.ps,this.l+ds*this.ps,this.ps,(k-ds)*this.ps);
michael@0 248 ds=-1;
michael@0 249 cnt++;
michael@0 250 }
michael@0 251 }
michael@0 252 }
michael@0 253 S_writeInnerHTML(this.dv,s);
michael@0 254 if(disp=="ShowCount") alert(cnt);
michael@0 255 }
michael@0 256 S_scene.prototype.S_displayAndShowCount=function(){
michael@0 257 this.S_display("ShowCount");
michael@0 258 }
michael@0 259 S_model.prototype.S_setupFaces=function(tr,lght) {
michael@0 260 var i, j, fn, v, p=new Array(); // vertice & projection arrays
michael@0 261 var z=new Array();
michael@0 262 for(i=0;i<this.f.length;i++) { // loop through faces
michael@0 263 v=this.f[i].v;
michael@0 264 for(j=0;j<3;j++) { p[j]=this.v[v[j]].p; }
michael@0 265 for(j=0;j<3;j++) { z[j]=this.v[v[j]].t.z; }
michael@0 266 if (((p[1].x-p[0].x)*(p[2].y-p[0].y))<((p[2].x-p[0].x)*(p[1].y-p[0].y))) {
michael@0 267 this.f[i].d=true;
michael@0 268 fn=S_faceNormal(this.v[v[0]].t, this.v[v[1]].t, this.v[v[2]].t);
michael@0 269 this.f[i].b=S_faceIntensity(fn,lght);
michael@0 270 tr.push(new S_triangle(fn,this.f[i].b,p.slice(),this.f[i].c,z));
michael@0 271 } else { this.f[i].d=false; }
michael@0 272 }
michael@0 273 }
michael@0 274 // normal, brightness, array of 2D projection coordinates, and z depth
michael@0 275 function S_triangle(fn,b,p,c,z) {
michael@0 276 this.fn=fn; this.b=b; this.p=p; this.z=z; this.c=c;
michael@0 277 }
michael@0 278 function S_faceNormal(a,b,c){
michael@0 279 var cr=S_cross(S_subVec3D(b,a), S_subVec3D(b,c));
michael@0 280 return S_normalizeVec3D(cr);
michael@0 281 }
michael@0 282 function S_faceIntensity(fn,lght) {
michael@0 283 var i=S_dotVec3D(fn,lght); return (i>0)?i:0;
michael@0 284 }
michael@0 285 function S_divColor(c,lc,b) { // c:array of colors
michael@0 286 var i, clr=new Array();
michael@0 287 for(i=0;i<3;i++) clr[i]=Math.floor(c[i]+(lc[i]-c[i]+1)*b);
michael@0 288 for(i=0;i<3;i++) if (clr[i]>lc[i]) { clr[i]=lc[i]; }
michael@0 289 return S_rgbColor(clr[0],clr[1],clr[2]);
michael@0 290 }
michael@0 291 function S_sameColor(a,b) {
michael@0 292 for(var i=0;i<3;i++) { if(a[i]!=b[i]) return false; }
michael@0 293 return true;
michael@0 294 }
michael@0 295 function S_setupBuffer(scn,p,c) {
michael@0 296 // temp, counters, min, max, scanline, vertice & slope arrays
michael@0 297 var t,i,j,xmin=new Array(),xmax=new Array(),sl;
michael@0 298 var v=new Array(), m=new Array();
michael@0 299 p.sort(function(a,b) { return a.y-b.y; } );
michael@0 300 for(i=0;i<3;i++) p[i].y=Math.floor(p[i].y);
michael@0 301 v[0]=S_subVec2D(p[1],p[0]);
michael@0 302 v[1]=S_subVec2D(p[2],p[0]);
michael@0 303 v[2]=S_subVec2D(p[2],p[1]);
michael@0 304 for(i=0;i<3;i++) { m[i]=(v[i].y!=0)?v[i].x/v[i].y:0; }
michael@0 305 for(i=0,sl=scn.t;i<scn.h;i++,sl++) {
michael@0 306 xmin[i]=1000;xmax[i]=0;
michael@0 307 if((sl>=p[0].y)&&(sl<=p[2].y)) {
michael@0 308 xmin[i]=xmax[i]=Math.floor(p[0].x+m[1]*(sl-p[0].y));
michael@0 309 }
michael@0 310 if((sl>=p[0].y)&&(sl<=p[1].y)) {
michael@0 311 t=Math.floor(p[0].x+m[0]*(sl-p[0].y));
michael@0 312 if(t<xmin[i]) xmin[i]=Math.floor(t);
michael@0 313 else if(t>xmax[i]) xmax[i]=Math.floor(t);
michael@0 314 }
michael@0 315 if((sl>=p[1].y)&&(sl<=p[2].y)) {
michael@0 316 t=Math.floor(p[1].x+m[2]*(sl-p[1].y));
michael@0 317 if(t<xmin[i]) xmin[i]=Math.floor(t);
michael@0 318 else if(t>xmax[i]) xmax[i]=Math.floor(t);
michael@0 319 }
michael@0 320 for(j=0;j<scn.w;j++)
michael@0 321 if((j>=(xmin[i]-scn.l))&&(j<=(xmax[i]-scn.l))) {
michael@0 322 scn.bfr[i][j].d=true; scn.bfr[i][j].c=c;
michael@0 323 }
michael@0 324 }
michael@0 325 }
michael@0 326 function S_light() {
michael@0 327 this.x=0; this.y=1; this.z=0; this.w=1; // original coordinates
michael@0 328 this.t=new S_vec3D(0,1,0); // transformed coordinates
michael@0 329 this.tm=new S_matrix();
michael@0 330 }
michael@0 331 S_scene.prototype.S_adjustLight=function() {
michael@0 332 var m=this.lght.tm, l=this.lght;
michael@0 333 l.t.x=l.x*m[0]+l.y*m[4]+ l.z*m[8]+l.w*m[12];
michael@0 334 l.t.y=l.x*m[1]+l.y*m[5]+ l.z*m[9]+l.w*m[13];
michael@0 335 l.t.z=l.x*m[2]+l.y*m[6]+ l.z*m[10]+l.w*m[14];
michael@0 336 l.t=S_normalizeVec3D(l.t);
michael@0 337 }
michael@0 338 S_scene.prototype.S_lightRotateX=function(a) {
michael@0 339 S_rotate(this.lght.tm,"x",a*=S_deg2Rad);
michael@0 340 }
michael@0 341 S_scene.prototype.S_lightRotateY=function(a) {
michael@0 342 S_rotate(this.lght.tm,"y",a*=S_deg2Rad);
michael@0 343 }
michael@0 344 S_scene.prototype.S_lightRotateZ=function(a) {
michael@0 345 S_rotate(this.lght.tm,"z",a*=S_deg2Rad);
michael@0 346 }
michael@0 347 S_scene.prototype.S_clearBuffer=function() {
michael@0 348 for(var i=0;i<this.h;i++)
michael@0 349 for(var j=0;j<this.w;j++) this.bfr[i][j].d=false;
michael@0 350 }
michael@0 351 function S_divString(b,t,l,h,w) {
michael@0 352 var s='<div style="background-color:'+b+';position:absolute;';
michael@0 353 s+='top:'+t+'px;left:'+l+'px;height:'+h+'px;width:'+w;
michael@0 354 return s+'px;font-size:0;visibility:visible"></div>';
michael@0 355 }
michael@0 356 function S_writeInnerHTML(id,text) {
michael@0 357 document.getElementById(id).innerHTML = text;
michael@0 358 }
michael@0 359 </script>
michael@0 360 </head>
michael@0 361 <body>
michael@0 362 <div class="z1" id="graphicsDiv">Text to be replaced with graphics.</div>
michael@0 363 <script type="text/javascript">
michael@0 364 if(S_checkBrowser()) {
michael@0 365 var intrvl;
michael@0 366 // Create a new scene with parameters for
michael@0 367 // div id, left, top, width, height, and camera distance
michael@0 368 var scn=new S_scene("graphicsDiv",75,25,100,100,300);
michael@0 369 scn.S_pixelSize(3); // set scene pixel size
michael@0 370 var c=S_rgbColor(0,0,127); // color
michael@0 371 var c2=S_rgbColor(0,127,127); // color
michael@0 372 var m=new S_cube(18,c); // model
michael@0 373 m.S_faceColor(4,c2);
michael@0 374 m.S_faceColor(5,c2);
michael@0 375 m.S_scaleX(2.5); // scale model along x axis
michael@0 376 scn.S_models(m); // add model(s) to scene
michael@0 377 scn.S_lightRotateX(-25); // adjust light
michael@0 378 function r(){ // rotation function
michael@0 379 m.S_rotateX(11); // rotate model around y axis
michael@0 380 m.S_rotateY(5); // rotate model around y axis
michael@0 381 m.S_rotateZ(7); // rotate model around z axis
michael@0 382 scn.S_display(); // display scene
michael@0 383 } // end rotation function
michael@0 384 intrvl=setInterval('r();',75);
michael@0 385 }
michael@0 386 </script>
michael@0 387
michael@0 388 </body>
michael@0 389 </html>
michael@0 390

mercurial