[CT404]: Lint Week 3 lecture examples

This commit is contained in:
2024-09-27 21:27:30 +01:00
parent 65eb318882
commit 38b9e2f2b2
9 changed files with 572 additions and 549 deletions

View File

@ -1,5 +1,6 @@
<html> <html>
<head>
<head>
<script src="three.js"></script> <script src="three.js"></script>
<script src="OrbitControls.js"></script> <script src="OrbitControls.js"></script>
@ -15,20 +16,20 @@
function draw() { function draw() {
// create renderer attached to HTML Canvas object // create renderer attached to HTML Canvas object
var c = document.getElementById("canvas"); var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({ canvas: c, antialias: true }); renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// create the scenegraph // create the scenegraph
scene = new THREE.Scene(); scene = new THREE.Scene();
// create a camera // create a camera
var fov = 75; var fov = 75;
var aspect = 600/600; var aspect = 600 / 600;
var near = 0.1; var near = 0.1;
var far = 1000; var far = 1000;
camera = new THREE.PerspectiveCamera( fov, aspect, near, far ); camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 40); camera.position.set(0, 0, 40);
orbitcontrols = new THREE.OrbitControls (camera, c); orbitcontrols = new THREE.OrbitControls(camera, c);
orbitcontrols.enableDamping = true; orbitcontrols.enableDamping = true;
orbitcontrols.dampingFactor = 0.25; orbitcontrols.dampingFactor = 0.25;
orbitcontrols.enableZoom = true; orbitcontrols.enableZoom = true;
@ -37,7 +38,7 @@
// point light and wireframe marker to indicate position // point light and wireframe marker to indicate position
pointLight = new THREE.PointLight(0xFFFFFF); pointLight = new THREE.PointLight(0xFFFFFF);
var wfMaterial = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2}); var wfMaterial = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2});
var markergeometry = new THREE.EdgesGeometry(new THREE.SphereGeometry(0.5,10,10)); var markergeometry = new THREE.EdgesGeometry(new THREE.SphereGeometry(0.5, 10, 10));
var pointlightmarker = new THREE.LineSegments(markergeometry, wfMaterial); var pointlightmarker = new THREE.LineSegments(markergeometry, wfMaterial);
pointLight.add(pointlightmarker); pointLight.add(pointlightmarker);
pointLight.position.set(-10, 20, 0); pointLight.position.set(-10, 20, 0);
@ -64,31 +65,31 @@
// args: radius, widthSegments, heightSegments // args: radius, widthSegments, heightSegments
// FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true // FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true
var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading:true}); var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading: true});
var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), material1); var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), material1);
sphere1.position.set(-20,0,0); sphere1.position.set(-20, 0, 0);
scene.add(sphere1); scene.add(sphere1);
// SMOOTH SHADING is the default setting for MeshLambertMaterial // SMOOTH SHADING is the default setting for MeshLambertMaterial
var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), new THREE.MeshLambertMaterial({color: 0xffff00})); var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshLambertMaterial({color: 0xffff00}));
sphere2.position.set(0,0,0); sphere2.position.set(0, 0, 0);
scene.add(sphere2); scene.add(sphere2);
// NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial // NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial
var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), new THREE.MeshPhongMaterial({color: 0x0000ff})); var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshPhongMaterial({color: 0x0000ff}));
sphere3.position.set(20,0,0); sphere3.position.set(20, 0, 0);
scene.add(sphere3); scene.add(sphere3);
// the "ground" // the "ground"
var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00})); var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00}));
scene.add(box); scene.add(box);
box.position.set(0,-15,0); box.position.set(0, -15, 0);
animate(); animate();
} }
function animate() { function animate() {
setTimeout(animate, 1000/60); setTimeout(animate, 1000 / 60);
orbitcontrols.update(); orbitcontrols.update();
@ -127,9 +128,9 @@
scene.remove(directionalLight); scene.remove(directionalLight);
} }
</script> </script>
</head> </head>
<body onload="draw();"> <body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas> <canvas id="canvas" width="600" height="600"></canvas>
<div style="position:absolute; left:20px; top:20px;"> <div style="position:absolute; left:20px; top:20px;">
<button onclick="togglePointLight();">PointLight</button> <button onclick="togglePointLight();">PointLight</button>
@ -137,5 +138,6 @@
<button onclick="toggleHemisphereLight();">HemisphereLight</button> <button onclick="toggleHemisphereLight();">HemisphereLight</button>
<button onclick="toggleDirectionalLight();">DirectionalLight</button> <button onclick="toggleDirectionalLight();">DirectionalLight</button>
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,5 +1,6 @@
<html> <html>
<head>
<head>
<script src="three.js"></script> <script src="three.js"></script>
<script src="OrbitControls.js"></script> <script src="OrbitControls.js"></script>
@ -13,6 +14,7 @@
// values that we're animating // values that we're animating
var mat2hue = 1; var mat2hue = 1;
var mat3green = 0; var mat3green = 0;
// animation controller values // animation controller values
var mat2dir = -0.001; var mat2dir = -0.001;
var mat3dir = 0.001; var mat3dir = 0.001;
@ -20,20 +22,20 @@
function draw() { function draw() {
// create renderer attached to HTML Canvas object // create renderer attached to HTML Canvas object
var c = document.getElementById("canvas"); var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({ canvas: c, antialias: true }); renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// create the scenegraph // create the scenegraph
scene = new THREE.Scene(); scene = new THREE.Scene();
// create a camera // create a camera
var fov = 75; var fov = 75;
var aspect = 600/600; var aspect = 600 / 600;
var near = 0.1; var near = 0.1;
var far = 1000; var far = 1000;
camera = new THREE.PerspectiveCamera( fov, aspect, near, far ); camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 40); camera.position.set(0, 0, 40);
orbitcontrols = new THREE.OrbitControls (camera, c); orbitcontrols = new THREE.OrbitControls(camera, c);
orbitcontrols.enableDamping = true; orbitcontrols.enableDamping = true;
orbitcontrols.dampingFactor = 0.25; orbitcontrols.dampingFactor = 0.25;
orbitcontrols.enableZoom = true; orbitcontrols.enableZoom = true;
@ -50,43 +52,43 @@
// args: radius, widthSegments, heightSegments // args: radius, widthSegments, heightSegments
// FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true // FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true
var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading:true, emissive: 0x003300}); var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading: true, emissive: 0x003300});
var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), material1); var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), material1);
sphere1.position.set(-20,0,0); sphere1.position.set(-20, 0, 0);
scene.add(sphere1); scene.add(sphere1);
// SMOOTH SHADING is the default setting for MeshLambertMaterial // SMOOTH SHADING is the default setting for MeshLambertMaterial
sphere2material = new THREE.MeshLambertMaterial({color: 0xffff00}); sphere2material = new THREE.MeshLambertMaterial({color: 0xffff00});
var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), sphere2material); var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), sphere2material);
sphere2.position.set(0,0,0); sphere2.position.set(0, 0, 0);
scene.add(sphere2); scene.add(sphere2);
// NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial // NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial
sphere3material = new THREE.MeshPhongMaterial({color: 0x0000ff, specular: 0x00ff00, shininess: 100}); sphere3material = new THREE.MeshPhongMaterial({color: 0x0000ff, specular: 0x00ff00, shininess: 100});
var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), sphere3material); var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), sphere3material);
sphere3.position.set(20,0,0); sphere3.position.set(20, 0, 0);
scene.add(sphere3); scene.add(sphere3);
// the "ground" // the "ground"
var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00})); var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00}));
scene.add(box); scene.add(box);
box.position.set(0,-15,0); box.position.set(0, -15, 0);
animate(); animate();
} }
function animate() { function animate() {
setTimeout(animate, 1000/60); setTimeout(animate, 1000 / 60);
orbitcontrols.update(); orbitcontrols.update();
// animate sphere2material between red and yellow using HSL // animate sphere2material between red and yellow using HSL
mat2hue += mat2dir; mat2hue += mat2dir;
if (mat2hue<=0) { if (mat2hue <= 0) {
mat2hue = 0; mat2hue = 0;
mat2dir = Math.abs(mat2dir); mat2dir = Math.abs(mat2dir);
} }
else if (mat2hue>=0.2) { else if (mat2hue >= 0.2) {
mat2hue = 0.2; mat2hue = 0.2;
mat2dir = -Math.abs(mat2dir); mat2dir = -Math.abs(mat2dir);
} }
@ -94,11 +96,11 @@
// animate sphere3material between red and yellow using RGB // animate sphere3material between red and yellow using RGB
mat3green += mat3dir; mat3green += mat3dir;
if (mat3green<=0) { if (mat3green <= 0) {
mat3green = 0; mat3green = 0;
mat3dir = Math.abs(mat3dir); mat3dir = Math.abs(mat3dir);
} }
else if (mat3green>=1) { else if (mat3green >= 1) {
mat3green = 1; mat3green = 1;
mat3dir = -Math.abs(mat3dir); mat3dir = -Math.abs(mat3dir);
} }
@ -107,9 +109,10 @@
renderer.render(scene, camera); renderer.render(scene, camera);
} }
</script> </script>
</head> </head>
<body onload="draw();"> <body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas> <canvas id="canvas" width="600" height="600"></canvas>
</body> </body>
</html> </html>

View File

@ -1,5 +1,6 @@
<html> <html>
<head>
<head>
<script src="three.js"></script> <script src="three.js"></script>
<script src="OrbitControls.js"></script> <script src="OrbitControls.js"></script>
@ -15,7 +16,7 @@
function draw() { function draw() {
// create renderer attached to HTML Canvas object // create renderer attached to HTML Canvas object
var c = document.getElementById("canvas"); var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({ canvas: c, antialias: true }); renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// SHADOW CODE ADDED HERE // SHADOW CODE ADDED HERE
renderer.shadowMap.enabled = true; renderer.shadowMap.enabled = true;
@ -25,13 +26,13 @@
// create a camera // create a camera
var fov = 75; var fov = 75;
var aspect = 600/600; var aspect = 600 / 600;
var near = 0.1; var near = 0.1;
var far = 1000; var far = 1000;
camera = new THREE.PerspectiveCamera( fov, aspect, near, far ); camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 40); camera.position.set(0, 0, 40);
orbitcontrols = new THREE.OrbitControls (camera, c); orbitcontrols = new THREE.OrbitControls(camera, c);
orbitcontrols.enableDamping = true; orbitcontrols.enableDamping = true;
orbitcontrols.dampingFactor = 0.25; orbitcontrols.dampingFactor = 0.25;
orbitcontrols.enableZoom = true; orbitcontrols.enableZoom = true;
@ -40,7 +41,7 @@
// point light and wireframe marker to indicate position // point light and wireframe marker to indicate position
pointLight = new THREE.PointLight(0xFFFFFF); pointLight = new THREE.PointLight(0xFFFFFF);
var wfMaterial = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2}); var wfMaterial = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2});
var markergeometry = new THREE.EdgesGeometry(new THREE.SphereGeometry(0.5,10,10)); var markergeometry = new THREE.EdgesGeometry(new THREE.SphereGeometry(0.5, 10, 10));
var pointlightmarker = new THREE.LineSegments(markergeometry, wfMaterial); var pointlightmarker = new THREE.LineSegments(markergeometry, wfMaterial);
pointLight.add(pointlightmarker); pointLight.add(pointlightmarker);
pointLight.position.set(-10, 20, 0); pointLight.position.set(-10, 20, 0);
@ -69,23 +70,23 @@
// args: radius, widthSegments, heightSegments // args: radius, widthSegments, heightSegments
// FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true // FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true
var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading:true}); var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading: true});
var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), material1); var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), material1);
sphere1.position.set(-20,0,0); sphere1.position.set(-20, 0, 0);
scene.add(sphere1); scene.add(sphere1);
sphere1.castShadow = true; // SHADOW CODE ADDED HERE sphere1.castShadow = true; // SHADOW CODE ADDED HERE
sphere1.receiveShadow = true; // SHADOW CODE ADDED HERE sphere1.receiveShadow = true; // SHADOW CODE ADDED HERE
// SMOOTH SHADING is the default setting for MeshLambertMaterial // SMOOTH SHADING is the default setting for MeshLambertMaterial
var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), new THREE.MeshLambertMaterial({color: 0xffff00})); var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshLambertMaterial({color: 0xffff00}));
sphere2.position.set(0,0,0); sphere2.position.set(0, 0, 0);
scene.add(sphere2); scene.add(sphere2);
sphere2.castShadow = true; // SHADOW CODE ADDED HERE sphere2.castShadow = true; // SHADOW CODE ADDED HERE
sphere2.receiveShadow = true; // SHADOW CODE ADDED HERE sphere2.receiveShadow = true; // SHADOW CODE ADDED HERE
// NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial // NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial
var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7,10,10), new THREE.MeshPhongMaterial({color: 0x0000ff})); var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshPhongMaterial({color: 0x0000ff}));
sphere3.position.set(20,0,0); sphere3.position.set(20, 0, 0);
scene.add(sphere3); scene.add(sphere3);
sphere3.castShadow = true; // SHADOW CODE ADDED HERE sphere3.castShadow = true; // SHADOW CODE ADDED HERE
sphere3.receiveShadow = true; // SHADOW CODE ADDED HERE sphere3.receiveShadow = true; // SHADOW CODE ADDED HERE
@ -93,14 +94,14 @@
// the "ground" // the "ground"
var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00})); var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00}));
scene.add(box); scene.add(box);
box.position.set(0,-15,0); box.position.set(0, -15, 0);
box.receiveShadow = true; // SHADOW CODE ADDED HERE box.receiveShadow = true; // SHADOW CODE ADDED HERE
animate(); animate();
} }
function animate() { function animate() {
setTimeout(animate, 1000/60); setTimeout(animate, 1000 / 60);
orbitcontrols.update(); orbitcontrols.update();
@ -139,9 +140,9 @@
scene.remove(directionalLight); scene.remove(directionalLight);
} }
</script> </script>
</head> </head>
<body onload="draw();"> <body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas> <canvas id="canvas" width="600" height="600"></canvas>
<div style="position:absolute; left:20px; top:20px;"> <div style="position:absolute; left:20px; top:20px;">
<button onclick="togglePointLight();">PointLight</button> <button onclick="togglePointLight();">PointLight</button>
@ -149,5 +150,6 @@
<button onclick="toggleHemisphereLight();">HemisphereLight</button> <button onclick="toggleHemisphereLight();">HemisphereLight</button>
<button onclick="toggleDirectionalLight();">DirectionalLight</button> <button onclick="toggleDirectionalLight();">DirectionalLight</button>
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,11 +1,12 @@
<html> <html>
<head>
<head>
<script> <script>
var x=0, y=0; var x = 0, y = 0;
var dx=4, dy=5; var dx = 4, dy = 5;
function draw() { function draw() {
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
@ -13,32 +14,33 @@ function draw() {
// remove previous translation if any // remove previous translation if any
context.save(); context.save();
// over-write previous content, with a grey rectangle // over-write previous content, with a grey rectangle
context.fillStyle="#DDDDDD"; context.fillStyle = "#DDDDDD";
context.fillRect(0,0,600,600); context.fillRect(0, 0, 600, 600);
// perform movement, and translate to position // perform movement, and translate to position
x+=dx; x += dx;
y+=dy; y += dy;
if (x<=0) if (x <= 0)
dx=4; dx = 4;
else if (x>=550) else if (x >= 550)
dx=-4; dx = -4;
if (y<=0) if (y <= 0)
dy=5; dy = 5;
else if (y>=550) else if (y >= 550)
dy=-5; dy = -5;
context.translate(x,y); context.translate(x, y);
// purple rectangle // purple rectangle
context.fillStyle="#CC00FF"; context.fillStyle = "#CC00FF";
context.fillRect(0,0,50,50); context.fillRect(0, 0, 50, 50);
context.restore(); context.restore();
// do it all again in 1/30th of a second // do it all again in 1/30th of a second
window.setTimeout("draw();",1000/30); window.setTimeout("draw();", 1000 / 30);
} }
</script> </script>
</head> </head>
<body onload="draw();"> <body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas> <canvas id="canvas" width="600" height="600"></canvas>
</body> </body>
</html> </html>

View File

@ -1,12 +1,12 @@
<html> <html>
<head>
<head>
<script> <script>
var x = 0, y = 0;
var dx = 4, dy = 5;
var now = Date.now();
var x=0, y=0; function draw() {
var dx=4, dy=5;
var now = Date.now();
function draw() {
// do it all again in 1/60th of a second // do it all again in 1/60th of a second
window.requestAnimationFrame(draw); window.requestAnimationFrame(draw);
@ -18,30 +18,36 @@ function draw() {
// remove previous translation if any // remove previous translation if any
context.save(); context.save();
// over-write previous content, with a grey rectangle
context.fillStyle="#DDDDDD";
context.fillRect(0,0,600,600);
// perform movement, and translate to position
x+=dx*elapsedMs/16.7;
y+=dy*elapsedMs/16.7;
if (x<=0)
dx=4;
else if (x>=550)
dx=-4;
if (y<=0)
dy=5;
else if (y>=550)
dy=-5;
context.translate(x,y);
// purple rectangle
context.fillStyle="#CC00FF";
context.fillRect(0,0,50,50);
context.restore();
}
</script>
</head>
<body onload="draw();"> // over-write previous content, with a grey rectangle
context.fillStyle = "#DDDDDD";
context.fillRect(0, 0, 600, 600);
// perform movement, and translate to position
x += dx * elapsedMs / 16.7;
y += dy * elapsedMs / 16.7;
if (x <= 0)
dx = 4;
else if (x >= 550)
dx = -4;
if (y <= 0)
dy = 5;
else if (y >= 550)
dy = -5;
context.translate(x, y);
// purple rectangle
context.fillStyle = "#CC00FF";
context.fillRect(0, 0, 50, 50);
context.restore();
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas> <canvas id="canvas" width="600" height="600"></canvas>
</body> </body>
</html> </html>

View File

@ -1,46 +1,47 @@
<html> <html>
<head>
<script>
var boxes = [];
function attachEvents() { <head>
document.onmousedown = function(event) { <script>
var boxes = [];
function attachEvents() {
document.onmousedown = function (event) {
// adds a new box at the mouse position // adds a new box at the mouse position
// step 1: find a spare index in the sparse array // step 1: find a spare index in the sparse array
var idx=Math.floor(Math.random()*1000); var idx = Math.floor(Math.random() * 1000);
while (typeof boxes[idx]!="undefined") while (typeof boxes[idx] != "undefined")
idx=Math.floor(Math.random()*1000); idx = Math.floor(Math.random() * 1000);
// step 2: create a new box object and add to the array // step 2: create a new box object and add to the array
// setting up its data properties // setting up its data properties
boxes[idx]=new Object(); boxes[idx] = new Object();
boxes[idx].x = event.clientX; boxes[idx].x = event.clientX;
boxes[idx].y = event.clientY; boxes[idx].y = event.clientY;
var r = Math.floor(Math.random()*256); var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random()*256); var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random()*256); var b = Math.floor(Math.random() * 256);
boxes[idx].colr = "rgb("+r+","+g+","+b+")"; boxes[idx].colr = "rgb(" + r + "," + g + "," + b + ")";
boxes[idx].dy = Math.floor(1+Math.random()*8); boxes[idx].dy = Math.floor(1 + Math.random() * 8);
}
} }
}
function draw() { function draw() {
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
// over-write previous content, with a grey rectangle // over-write previous content, with a grey rectangle
context.fillStyle="#DDDDDD"; context.fillStyle = "#DDDDDD";
context.fillRect(0,0,600,600); context.fillRect(0, 0, 600, 600);
// iterate thru the objects in our sparse array // iterate thru the objects in our sparse array
// the for..in construct obtains *indices* rather than *data values* // the for..in construct obtains *indices* rather than *data values*
for (var idx in boxes) { for (var idx in boxes) {
var y=boxes[idx].y+boxes[idx].dy; // animate box downwards var y = boxes[idx].y + boxes[idx].dy; // animate box downwards
if (y<600) { if (y < 600) {
context.save(); context.save();
boxes[idx].y=y; boxes[idx].y = y;
context.translate(boxes[idx].x,y); context.translate(boxes[idx].x, y);
context.fillStyle=boxes[idx].colr; context.fillStyle = boxes[idx].colr;
context.fillRect(0,0,20,20); context.fillRect(0, 0, 20, 20);
context.restore(); context.restore();
} }
else else
@ -48,12 +49,13 @@ function draw() {
} }
// do it all again in 1/30th of a second // do it all again in 1/30th of a second
window.setTimeout("draw();",1000/30); window.setTimeout("draw();", 1000 / 30);
} }
</script> </script>
</head> </head>
<body onload="attachEvents(); draw();"> <body onload="attachEvents(); draw();">
<canvas id="canvas" width="600" height="600"></canvas> <canvas id="canvas" width="600" height="600"></canvas>
</body> </body>
</html> </html>

View File

@ -1,34 +1,38 @@
<html> <html>
<head>
<head>
<script> <script>
function attachEvents() { function attachEvents() {
document.onkeypress = function(event) { document.onkeypress = function (event) {
var xoffset=10*parseInt(String.fromCharCode(event.keyCode || event.charCode)); var xoffset = 10 * parseInt(String.fromCharCode(event.keyCode || event.charCode));
draw(xoffset); draw(xoffset);
} }
} }
function draw(xoffset) {
function draw(xoffset) {
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
// remove previous translation if any // remove previous translation if any
context.save(); context.save();
// over-write previous content, with a white rectangle // over-write previous content, with a white rectangle
context.fillStyle="#FFFFFF"; context.fillStyle = "#FFFFFF";
context.fillRect(0,0,300,300); context.fillRect(0, 0, 300, 300);
// translate based on numerical keypress // translate based on numerical keypress
context.translate(xoffset,0); context.translate(xoffset, 0);
// purple rectangle // purple rectangle
context.fillStyle="#CC00FF"; context.fillStyle = "#CC00FF";
context.fillRect(0,0,50,50); context.fillRect(0, 0, 50, 50);
context.restore(); context.restore();
}
}
</script> </script>
</head> </head>
<body onload="attachEvents();"> <body onload="attachEvents();">
<canvas id="canvas" width="300" height="300"></canvas> <canvas id="canvas" width="300" height="300"></canvas>
</body> </body>
</html> </html>

View File

@ -1,22 +1,23 @@
<html> <html>
<head>
<head>
<script> <script>
var isMouseDown=false; var isMouseDown = false;
function attachEvents() { function attachEvents() {
document.onmousedown = function(event) { document.onmousedown = function (event) {
isMouseDown=true; isMouseDown = true;
draw(event.clientX, event.clientY); draw(event.clientX, event.clientY);
} }
document.onmouseup = function(event) { document.onmouseup = function (event) {
isMouseDown=false; isMouseDown = false;
} }
document.onmousemove = function(event) { document.onmousemove = function (event) {
if ( isMouseDown ) { if (isMouseDown) {
draw(event.clientX, event.clientY); draw(event.clientX, event.clientY);
} }
} }
} }
function draw(xoffset,yoffset) { function draw(xoffset, yoffset) {
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
@ -24,22 +25,23 @@ function draw(xoffset,yoffset) {
// remove previous translation if any // remove previous translation if any
context.save(); context.save();
// over-write previous content, with a grey rectangle // over-write previous content, with a grey rectangle
context.fillStyle="#DDDDDD"; context.fillStyle = "#DDDDDD";
context.fillRect(0,0,600,600); context.fillRect(0, 0, 600, 600);
// translate based on position of mouseclick // translate based on position of mouseclick
context.translate(xoffset,yoffset); context.translate(xoffset, yoffset);
// purple rectangle // purple rectangle
context.fillStyle="#CC00FF"; context.fillStyle = "#CC00FF";
context.fillRect(-25,-25,50,50); // centred on coord system context.fillRect(-25, -25, 50, 50); // centred on coord system
context.restore(); context.restore();
} }
</script> </script>
</head> </head>
<body onload="attachEvents(); draw(0,0);"> <body onload="attachEvents(); draw(0,0);">
hello<br> hello<br>
<div id='canvasdiv' style='position:absolute; left:0px; top:0px;'><canvas id="canvas" width="600" height="600"></canvas></div> <div id='canvasdiv' style='position:absolute; left:0px; top:0px;'><canvas id="canvas" width="600"
</body> height="600"></canvas></div>
</html> </body>
</html>

View File

@ -11,29 +11,29 @@ function orientationInterpolator(data) {
orientationInterpolator.prototype.getEulerAngles = function() { orientationInterpolator.prototype.getEulerAngles = function() {
var now = Date.now(); var now = Date.now();
var elapsed = now-this.startTime; var elapsed = now - this.startTime;
while (elapsed>=this.data.times[this.keyframe2]) { while (elapsed >= this.data.times[this.keyframe2]) {
this.keyframe1++; this.keyframe1++;
this.keyframe2++; this.keyframe2++;
if (this.keyframe2>=this.data.times.length) { // loop back to keyframe 0 if (this.keyframe2 >= this.data.times.length) { // loop back to keyframe 0
var timeSinceLastFrame = elapsed - this.data.times[this.keyframe1]; var timeSinceLastFrame = elapsed - this.data.times[this.keyframe1];
this.startTime = now - timeSinceLastFrame; this.startTime = now - timeSinceLastFrame;
this.keyframe1 = 0; this.keyframe1 = 0;
this.keyframe2 = 1; this.keyframe2 = 1;
elapsed = now-this.startTime; elapsed = now - this.startTime;
} }
} }
var timeBetweenKeyFrames = this.data.times[this.keyframe2] - this.data.times[this.keyframe1]; var timeBetweenKeyFrames = this.data.times[this.keyframe2] - this.data.times[this.keyframe1];
var timeSinceKeyframe1 = elapsed - this.data.times[this.keyframe1]; var timeSinceKeyframe1 = elapsed - this.data.times[this.keyframe1];
var fractionBetweenFrames = timeSinceKeyframe1/timeBetweenKeyFrames; var fractionBetweenFrames = timeSinceKeyframe1 / timeBetweenKeyFrames;
var frame1rot = this.data.angles[this.keyframe1]; var frame1rot = this.data.angles[this.keyframe1];
var frame2rot = this.data.angles[this.keyframe2]; var frame2rot = this.data.angles[this.keyframe2];
var rot = { var rot = {
x: frame1rot.x*(1-fractionBetweenFrames) + frame2rot.x*fractionBetweenFrames, x: frame1rot.x * (1 - fractionBetweenFrames) + frame2rot.x * fractionBetweenFrames,
y: frame1rot.y*(1-fractionBetweenFrames) + frame2rot.y*fractionBetweenFrames, y: frame1rot.y * (1 - fractionBetweenFrames) + frame2rot.y * fractionBetweenFrames,
z: frame1rot.z*(1-fractionBetweenFrames) + frame2rot.z*fractionBetweenFrames z: frame1rot.z * (1 - fractionBetweenFrames) + frame2rot.z * fractionBetweenFrames
}; };
return rot; return rot;