[CT404]: Week 09 lecture materials, notes, & directory restructuring

This commit is contained in:
2024-11-08 01:06:15 +00:00
parent ca71d634e5
commit 6a982c72c9
36 changed files with 1 additions and 0 deletions

View File

@ -0,0 +1,183 @@
<html>
<head>
<script src="../../week2/examples/three.js"></script>
<script>
'use strict'
var raycaster, renderer, scene, camera;
var selectedObject = null;
var selectableObjects = [];
var lastMousePos = {x: 0, y: 0};
function draw() {
// create renderer attached to HTML Canvas object
var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// create the scenegraph
scene = new THREE.Scene();
// create a camera
var fov = 75;
var aspect = 600 / 600;
var near = 0.1;
var far = 1000;
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(-5, 1.5, 6);
// add a light to the scene
var light = new THREE.PointLight(0xFFFFFF);
light.position.set(0, 10, 30);
scene.add(light);
// desk lamp base
// args: radiusTop, radiusBottom, height, radialSegments
var base = new THREE.Mesh(
new THREE.CylinderBufferGeometry(1, 1, 0.1, 12),
new THREE.MeshLambertMaterial({color: 0xAAAAAA}));
scene.add(base);
base.position.set(-5, -2, 0);
selectableObjects.push(base);
base.canTranslate = true; // I added this property
// desk lamp first arm piece
var arm = new THREE.Mesh(
new THREE.CylinderBufferGeometry(0.1, 0.1, 3, 12),
new THREE.MeshLambertMaterial({color: 0xAAAAAA}));
// since we want to rotate around a point other than the arm's centre,
// we can create a pivot point as the parent of the arm, position the
// arm relative to that pivot point, and apply rotation on the pivot point
var pivot = new THREE.Object3D();
pivot.position.set(0, 0, 0); // centre of rotation we want
pivot.add(arm); // pivot is parent of arm
base.add(pivot); // base is parent of pivot
selectableObjects.push(arm);
arm.canRotate = true; // I added this property
// translate arm relative to pivot point
arm.position.set(0, 1.5, 0);
// rotate pivot point relative to the world
pivot.rotateOnAxis(new THREE.Vector3(0, 0, 1), -Math.PI / 6);
// second arm piece (consisting of a pivot with a cylinder as its child)
var pivot2 = pivot.clone();
pivot.add(pivot2);
// rotate the 2nd pivot relative to the 1st pivot (since it's nested)
pivot2.rotation.z = Math.PI / 3;
// translate the 2nd pivot relative to the 1st pivot
pivot2.position.set(0, 3, 0);
var arm2 = pivot2.children[0];
selectableObjects.push(arm2);
arm2.canRotate = true; // I added this property
// args: radius, height, segments
var lampshade = new THREE.Mesh(
new THREE.ConeBufferGeometry(1, 0.7, 24),
new THREE.MeshLambertMaterial({color: 0xAAAAAA})
);
var shadePivot = new THREE.Object3D();
pivot2.add(shadePivot); // lampshade pivot is a child of the 2nd arm pivot
shadePivot.add(lampshade);
shadePivot.position.set(0, 3, 0);
shadePivot.rotation.x = Math.PI;
selectableObjects.push(lampshade);
lampshade.canRotate = true; // I added this property
raycaster = new THREE.Raycaster();
c.onmousedown = handleMouseDown;
c.onmousemove = handleMouseMove;
c.onmouseup = function (e) {
selectedObject = null;
};
animate();
}
function animate() {
setTimeout(animate, 1000 / 60);
// render the scene as seen by the camera
renderer.render(scene, camera);
}
function handleMouseDown(e) {
// handle mouse-clicks on the canvas
// did the user click a mesh?
/* note that 0,0 is the centre of the canvas according to WebGL,
and the canvas extends from (-1,-1) to (1,1)
but 0,0 is the top-left of the canvas according to e.clientX,e.clientY,
and the canvas extends from (0,0) to (599,599)
*/
var x = 2 * (e.clientX - 300) / 600;
var y = -2 * ((e.clientY - 300) / 600);
lastMousePos.x = x;
lastMousePos.y = y;
// set up and apply the raycaster (we are returned an array of intersection objects)
raycaster.setFromCamera({x: x, y: y}, camera);
var intersects = raycaster.intersectObjects(selectableObjects);
if (intersects.length > 0) {
var closestObj, closestDist;
for (var i = 0; i < intersects.length; i++) {
/*
An intersection has the following properties :
- object : intersected object (THREE.Mesh)
- distance : distance from ray start to intersection (number)
- face : intersected face (THREE.Face3)
- faceIndex : intersected face index (number)
- point : intersection point (THREE.Vector3)
- uv : intersection point in the object's UV coordinates (THREE.Vector2)
*/
if (i == 0 || intersects[i].distance < closestDist) {
closestObj = intersects[i].object;
closestDist = intersects[i].distance;
}
}
selectedObject = closestObj;
}
else
selectedObject = null;
}
function handleMouseMove(e) {
if (selectedObject != null) {
var x = 2 * (e.clientX - 300) / 600;
var y = -2 * ((e.clientY - 300) / 600);
// dx,dy is the amount the mouse just moved by in pixels
var dx = x - lastMousePos.x;
var dy = y - lastMousePos.y;
if (selectedObject.canRotate) {
// rotate the parent ('pivot') that the object is a child of
selectedObject.parent.rotation.x += dx;
selectedObject.parent.rotation.z += dy;
}
else if (selectedObject.canTranslate) {
// translate the object
selectedObject.position.x += dx * 4;
selectedObject.position.z -= dy * 4;
}
lastMousePos.x = x;
lastMousePos.y = y;
}
}
</script>
</head>
<body onload="draw();">
<!-- Note that the canvas has been positioned precisely at 0,0 so that mouse positions on the browser
are the same as mouse positions on the canvas -->
<canvas id="canvas" width="600" height="600" style="position:absolute; left:0px; top:0px"></canvas>
</body>
</html>

View File

@ -0,0 +1,143 @@
<html>
<head>
<script src="three.js"></script>
<script src="OrbitControls.js"></script>
<script>
'use strict'
var renderer, scene, camera, orbitcontrols;
var pointLight, pointLightVisible = false;
var ambientLight, ambientLightVisible = false;
var hemisphereLight, hemisphereLightVisible = false;
var directionalLight, directionalLightVisible = false;
function draw() {
// create renderer attached to HTML Canvas object
var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// create the scenegraph
scene = new THREE.Scene();
// create a camera
var fov = 75;
var aspect = 600 / 600;
var near = 0.1;
var far = 1000;
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 40);
orbitcontrols = new THREE.OrbitControls(camera, c);
orbitcontrols.enableDamping = true;
orbitcontrols.dampingFactor = 0.25;
orbitcontrols.enableZoom = true;
orbitcontrols.autoRotate = false;
// point light and wireframe marker to indicate position
pointLight = new THREE.PointLight(0xFFFFFF);
var wfMaterial = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2});
var markergeometry = new THREE.EdgesGeometry(new THREE.SphereGeometry(0.5, 10, 10));
var pointlightmarker = new THREE.LineSegments(markergeometry, wfMaterial);
pointLight.add(pointlightmarker);
pointLight.position.set(-10, 20, 0);
// ambient light and wireframe marker
ambientLight = new THREE.AmbientLight(0xFFFFFF, 0.2); // args: color, intensity
var ambientlightmarker = pointlightmarker.clone();
ambientLight.add(ambientlightmarker);
ambientLight.position.set(0, 20, 0); // position of ambientLight has no lighting effect, but does affect ambientlightmarker
// hemisphere light and wireframe marker
hemisphereLight = new THREE.HemisphereLight(0xFFFFFF, 0x000000, 0.3); // args: skyColor, groundColor, intensity
var hemispherelightmarker = pointlightmarker.clone();
hemisphereLight.add(hemispherelightmarker);
hemisphereLight.position.set(10, 20, 0); // position of hemisphereLight has no lighting effect, but does affect hemispherelightmarker
// directional light and wireframe marker
directionalLight = new THREE.DirectionalLight(0xffffff, 0.6); // args: color, intensity
var directionallightmarker = pointlightmarker.clone();
directionalLight.add(directionallightmarker);
directionalLight.position.set(-10, 15, 0);
directionalLight.target.position.set(10, 20, 0);
scene.add(directionalLight.target); // the target object must be in the scene
// args: radius, widthSegments, heightSegments
// FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true
var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading: true});
var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), material1);
sphere1.position.set(-20, 0, 0);
scene.add(sphere1);
// SMOOTH SHADING is the default setting for MeshLambertMaterial
var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshLambertMaterial({color: 0xffff00}));
sphere2.position.set(0, 0, 0);
scene.add(sphere2);
// NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial
var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshPhongMaterial({color: 0x0000ff}));
sphere3.position.set(20, 0, 0);
scene.add(sphere3);
// the "ground"
var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00}));
scene.add(box);
box.position.set(0, -15, 0);
animate();
}
function animate() {
setTimeout(animate, 1000 / 60);
orbitcontrols.update();
renderer.render(scene, camera);
}
function togglePointLight() {
pointLightVisible = !pointLightVisible;
if (pointLightVisible)
scene.add(pointLight);
else
scene.remove(pointLight);
}
function toggleAmbientLight() {
ambientLightVisible = !ambientLightVisible;
if (ambientLightVisible)
scene.add(ambientLight);
else
scene.remove(ambientLight);
}
function toggleHemisphereLight() {
hemisphereLightVisible = !hemisphereLightVisible;
if (hemisphereLightVisible)
scene.add(hemisphereLight);
else
scene.remove(hemisphereLight);
}
function toggleDirectionalLight() {
directionalLightVisible = !directionalLightVisible;
if (directionalLightVisible)
scene.add(directionalLight);
else
scene.remove(directionalLight);
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas>
<div style="position:absolute; left:20px; top:20px;">
<button onclick="togglePointLight();">PointLight</button>
<button onclick="toggleAmbientLight();">AmbientLight</button>
<button onclick="toggleHemisphereLight();">HemisphereLight</button>
<button onclick="toggleDirectionalLight();">DirectionalLight</button>
</div>
</body>
</html>

View File

@ -0,0 +1,118 @@
<html>
<head>
<script src="three.js"></script>
<script src="OrbitControls.js"></script>
<script>
'use strict'
var renderer, scene, camera, orbitcontrols;
var sphere2material, sphere3material;
var pointLight, directionalLight;
// values that we're animating
var mat2hue = 1;
var mat3green = 0;
// animation controller values
var mat2dir = -0.001;
var mat3dir = 0.001;
function draw() {
// create renderer attached to HTML Canvas object
var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// create the scenegraph
scene = new THREE.Scene();
// create a camera
var fov = 75;
var aspect = 600 / 600;
var near = 0.1;
var far = 1000;
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 40);
orbitcontrols = new THREE.OrbitControls(camera, c);
orbitcontrols.enableDamping = true;
orbitcontrols.dampingFactor = 0.25;
orbitcontrols.enableZoom = true;
orbitcontrols.autoRotate = false;
// lights
pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.set(-10, 20, 0);
scene.add(pointLight);
directionalLight = new THREE.DirectionalLight(0xffffff, 0.6); // args: color, intensity
directionalLight.position.set(-10, 15, 0);
scene.add(directionalLight);
scene.add(directionalLight.target); // the target object must be in the scene
// args: radius, widthSegments, heightSegments
// 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 sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), material1);
sphere1.position.set(-20, 0, 0);
scene.add(sphere1);
// SMOOTH SHADING is the default setting for MeshLambertMaterial
sphere2material = new THREE.MeshLambertMaterial({color: 0xffff00});
var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), sphere2material);
sphere2.position.set(0, 0, 0);
scene.add(sphere2);
// NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial
sphere3material = new THREE.MeshPhongMaterial({color: 0x0000ff, specular: 0x00ff00, shininess: 100});
var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), sphere3material);
sphere3.position.set(20, 0, 0);
scene.add(sphere3);
// the "ground"
var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00}));
scene.add(box);
box.position.set(0, -15, 0);
animate();
}
function animate() {
setTimeout(animate, 1000 / 60);
orbitcontrols.update();
// animate sphere2material between red and yellow using HSL
mat2hue += mat2dir;
if (mat2hue <= 0) {
mat2hue = 0;
mat2dir = Math.abs(mat2dir);
}
else if (mat2hue >= 0.2) {
mat2hue = 0.2;
mat2dir = -Math.abs(mat2dir);
}
sphere2material.color.setHSL(mat2hue, 1, 0.5);
// animate sphere3material between red and yellow using RGB
mat3green += mat3dir;
if (mat3green <= 0) {
mat3green = 0;
mat3dir = Math.abs(mat3dir);
}
else if (mat3green >= 1) {
mat3green = 1;
mat3dir = -Math.abs(mat3dir);
}
sphere3material.color.setRGB(1, mat3green, 0);
renderer.render(scene, camera);
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas>
</body>
</html>

View File

@ -0,0 +1,155 @@
<html>
<head>
<script src="three.js"></script>
<script src="OrbitControls.js"></script>
<script>
'use strict'
var renderer, scene, camera, orbitcontrols;
var pointLight, pointLightVisible = false;
var ambientLight, ambientLightVisible = false;
var hemisphereLight, hemisphereLightVisible = false;
var directionalLight, directionalLightVisible = false;
function draw() {
// create renderer attached to HTML Canvas object
var c = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({canvas: c, antialias: true});
// SHADOW CODE ADDED HERE
renderer.shadowMap.enabled = true;
// create the scenegraph
scene = new THREE.Scene();
// create a camera
var fov = 75;
var aspect = 600 / 600;
var near = 0.1;
var far = 1000;
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 40);
orbitcontrols = new THREE.OrbitControls(camera, c);
orbitcontrols.enableDamping = true;
orbitcontrols.dampingFactor = 0.25;
orbitcontrols.enableZoom = true;
orbitcontrols.autoRotate = false;
// point light and wireframe marker to indicate position
pointLight = new THREE.PointLight(0xFFFFFF);
var wfMaterial = new THREE.LineBasicMaterial({color: 0xffffff, linewidth: 2});
var markergeometry = new THREE.EdgesGeometry(new THREE.SphereGeometry(0.5, 10, 10));
var pointlightmarker = new THREE.LineSegments(markergeometry, wfMaterial);
pointLight.add(pointlightmarker);
pointLight.position.set(-10, 20, 0);
pointLight.castShadow = true; // SHADOW CODE ADDED HERE
// ambient light and wireframe marker
ambientLight = new THREE.AmbientLight(0xFFFFFF, 0.2); // args: color, intensity
var ambientlightmarker = pointlightmarker.clone();
ambientLight.add(ambientlightmarker);
ambientLight.position.set(0, 20, 0); // position of ambientLight has no lighting effect, but does affect ambientlightmarker
// hemisphere light and wireframe marker
hemisphereLight = new THREE.HemisphereLight(0xFFFFFF, 0x000000, 0.3); // args: skyColor, groundColor, intensity
var hemispherelightmarker = pointlightmarker.clone();
hemisphereLight.add(hemispherelightmarker);
hemisphereLight.position.set(10, 20, 0); // position of hemisphereLight has no lighting effect, but does affect hemispherelightmarker
// directional light and wireframe marker
directionalLight = new THREE.DirectionalLight(0xffffff, 0.6); // args: color, intensity
var directionallightmarker = pointlightmarker.clone();
directionalLight.add(directionallightmarker);
directionalLight.position.set(-10, 15, 0);
directionalLight.target.position.set(0, 5, 0);
scene.add(directionalLight.target); // the target object must be in the scene
directionalLight.castShadow = true; // SHADOW CODE ADDED HERE
// args: radius, widthSegments, heightSegments
// FLAT SHADING is achieved in Threejs using MeshPhongMaterial with flatShading set to true
var material1 = new THREE.MeshPhongMaterial({color: 0xff0000, flatShading: true});
var sphere1 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), material1);
sphere1.position.set(-20, 0, 0);
scene.add(sphere1);
sphere1.castShadow = true; // SHADOW CODE ADDED HERE
sphere1.receiveShadow = true; // SHADOW CODE ADDED HERE
// SMOOTH SHADING is the default setting for MeshLambertMaterial
var sphere2 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshLambertMaterial({color: 0xffff00}));
sphere2.position.set(0, 0, 0);
scene.add(sphere2);
sphere2.castShadow = true; // SHADOW CODE ADDED HERE
sphere2.receiveShadow = true; // SHADOW CODE ADDED HERE
// NORMAL-INTERPOLATING SHADING is the default for MeshPhongMaterial
var sphere3 = new THREE.Mesh(new THREE.SphereBufferGeometry(7, 10, 10), new THREE.MeshPhongMaterial({color: 0x0000ff}));
sphere3.position.set(20, 0, 0);
scene.add(sphere3);
sphere3.castShadow = true; // SHADOW CODE ADDED HERE
sphere3.receiveShadow = true; // SHADOW CODE ADDED HERE
// the "ground"
var box = new THREE.Mesh(new THREE.BoxGeometry(60, 5, 60), new THREE.MeshLambertMaterial({color: 0x00ff00}));
scene.add(box);
box.position.set(0, -15, 0);
box.receiveShadow = true; // SHADOW CODE ADDED HERE
animate();
}
function animate() {
setTimeout(animate, 1000 / 60);
orbitcontrols.update();
renderer.render(scene, camera);
}
function togglePointLight() {
pointLightVisible = !pointLightVisible;
if (pointLightVisible)
scene.add(pointLight);
else
scene.remove(pointLight);
}
function toggleAmbientLight() {
ambientLightVisible = !ambientLightVisible;
if (ambientLightVisible)
scene.add(ambientLight);
else
scene.remove(ambientLight);
}
function toggleHemisphereLight() {
hemisphereLightVisible = !hemisphereLightVisible;
if (hemisphereLightVisible)
scene.add(hemisphereLight);
else
scene.remove(hemisphereLight);
}
function toggleDirectionalLight() {
directionalLightVisible = !directionalLightVisible;
if (directionalLightVisible)
scene.add(directionalLight);
else
scene.remove(directionalLight);
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas>
<div style="position:absolute; left:20px; top:20px;">
<button onclick="togglePointLight();">PointLight</button>
<button onclick="toggleAmbientLight();">AmbientLight</button>
<button onclick="toggleHemisphereLight();">HemisphereLight</button>
<button onclick="toggleDirectionalLight();">DirectionalLight</button>
</div>
</body>
</html>

View File

@ -0,0 +1,46 @@
<html>
<head>
<script>
var x = 0, y = 0;
var dx = 4, dy = 5;
function draw() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
// remove previous translation if any
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;
y += dy;
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();
// do it all again in 1/30th of a second
window.setTimeout("draw();", 1000 / 30);
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas>
</body>
</html>

View File

@ -0,0 +1,50 @@
<html>
<head>
<script>
var x = 0, y = 0;
var dx = 4, dy = 5;
var now = Date.now();
function draw() {
// do it all again in 1/60th of a second
window.requestAnimationFrame(draw);
var elapsedMs = Date.now() - now;
now = Date.now();
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
// remove previous translation if any
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();">
<canvas id="canvas" width="600" height="600"></canvas>
</body>
</html>

View File

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

View File

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

View File

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

View File

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