~jsalsman/canvask3d/trunk

« back to all changes in this revision

Viewing changes to scripts/widget.js

  • Committer: James Salsman
  • Author(s): Kevin Roast
  • Date: 2011-09-26 20:17:00 UTC
  • Revision ID: jsalsman@gmail.com-20110926201700-a8su52h05irrpzvo
update from Kevin's zip distribution

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * K3D widget demo
 
3
 * 
 
4
 * Copyright (C) Kevin Roast 2010
 
5
 * http://www.kevs3d.co.uk/dev
 
6
 * email: kevtoast at yahoo.com
 
7
 * twitter: @kevinroast
 
8
 * 
 
9
 * I place this code in the public domain - because it's not rocket science
 
10
 * and it won't make me any money, so do whatever you want with it, go crazy
 
11
 */
 
12
 
 
13
// bind to window events
 
14
window.addEventListener('load', onloadHandler, false);
 
15
 
 
16
/**
 
17
 * Window onload handler
 
18
 */
 
19
function onloadHandler()
 
20
{
 
21
   var CANVASSIZE = 64;
 
22
   var OFFSET = 10;
 
23
   
 
24
   // create canvas element for the k3d render output
 
25
   var canvas = document.createElement('canvas');
 
26
   if (!canvas) return;
 
27
   canvas.width = CANVASSIZE;
 
28
   canvas.height = CANVASSIZE;
 
29
   canvas.title = "Kevs 3D";
 
30
   canvas.style.position = "absolute";
 
31
   canvas.style.zIndex = 100;
 
32
   canvas.style.cursor = "pointer";
 
33
   document.body.appendChild(canvas);
 
34
   canvas.style.left = canvas.style.top = OFFSET + "px";
 
35
   
 
36
   // track scroll position to update widget pos
 
37
   var destinationY = positionY = OFFSET;
 
38
   
 
39
   // bind scrolling event listener - needs reference to the canvas element
 
40
   window.addEventListener('scroll', function onscroll()
 
41
      {
 
42
         destinationY = window.pageYOffset + OFFSET;
 
43
      }, false);
 
44
   
 
45
   // create k3d controller and bind to the canvas
 
46
   var k3d = new K3D.Controller(canvas, true);
 
47
   k3d.fps = 50;
 
48
   
 
49
   // generate object
 
50
   var obj = new K3D.K3DObject();
 
51
   
 
52
   // Icosahedron
 
53
   var t = (1+Math.sqrt(5))/2,
 
54
       tau = t/Math.sqrt(1+t*t),
 
55
       one = 1/Math.sqrt(1+t*t);
 
56
   with (obj)
 
57
   {
 
58
      drawmode = "solid";
 
59
      shademode = "lightsource";
 
60
      fillstroke = false;
 
61
      addgamma = 1; addtheta = -0.5; addphi = 0.8;
 
62
      scale = 32;
 
63
      init(
 
64
         [{x:tau,y:one,z:0}, {x:-tau,y:one,z:0}, {x:-tau,y:-one,z:0}, {x:tau,y:-one,z:0}, {x:one,y:0,z:tau}, {x:one,y:0,z:-tau}, {x:-one,y:0,z:-tau}, {x:-one,y:0,z:tau}, {x:0,y:tau,z:one}, {x:0,y:-tau,z:one}, {x:0,y:-tau,z:-one}, {x:0,y:tau,z:-one}],
 
65
         [{a:4,b:8}, {a:8,b:7}, {a:7,b:4}, {a:7,b:9}, {a:9,b:4}, {a:5,b:6}, {a:6,b:11}, {a:11,b:5}, {a:5,b:10}, {a:10,b:6}, {a:0,b:4}, {a:4,b:3}, {a:3,b:0}, {a:3,b:5}, {a:5,b:0}, {a:2,b:7}, {a:7,b:1}, {a:1,b:2}, {a:1,b:6}, {a:6,b:2}, {a:8,b:0}, {a:0,b:11}, {a:11,b:8}, {a:11,b:1}, {a:1,b:8}, {a:9,b:10}, {a:10,b:3}, {a:3,b:9}, {a:9,b:2}, {a:2,b:10} ],
 
66
         [{color:[255,255,255],vertices:[4, 8, 7]}, {color:[255,255,0],vertices:[4, 7, 9]}, {color:[0,255,255],vertices:[5, 6, 11]}, {color:[128,0,255],vertices:[5, 10, 6]}, {color:[0,0,255],vertices:[0, 4, 3]}, {color:[255,0,0],vertices:[0, 3, 5]}, {color:[0,255,0],vertices:[2, 7, 1]}, {color:[255,0,0],vertices:[2, 1, 6]}, {color:[128,128,128],vertices:[8, 0, 11]}, {color:[255,128,0],vertices:[8, 11, 1]}, {color:[0,128,255],vertices:[9, 10, 3]}, {color:[255,0,128],vertices:[9, 2, 10]}, {color:[0,128,255],vertices:[8, 4, 0]}, {color:[128,255,0],vertices:[11, 0, 5]}, {color:[0,255,128],vertices:[4, 9, 3]}, {color:[128,255,255],vertices:[5, 3, 10]}, {color:[255,128,255],vertices:[7, 8, 1]}, {color:[128,0,255],vertices:[6, 1, 11]}, {color:[0,255,128],vertices:[7, 2, 9]}, {color:[255,0,255],vertices:[6, 10, 2]}]
 
67
      );
 
68
   }
 
69
   k3d.addK3DObject(obj);
 
70
   
 
71
   // canvas position state
 
72
   k3d.callback = function()
 
73
   {
 
74
      if (positionY != destinationY)
 
75
      {
 
76
         // track towards new destination Y position
 
77
         positionY += (destinationY - positionY) / OFFSET;
 
78
         canvas.style.top = Math.floor(positionY) + "px";
 
79
      }
 
80
   };
 
81
   
 
82
   canvas.addEventListener('click', function click() {
 
83
      window.location.href = INDEX;
 
84
      }, false);
 
85
   
 
86
   // start demo loop
 
87
   k3d.paused = false;
 
88
   k3d.frame();
 
89
}
 
 
b'\\ No newline at end of file'