Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions cube.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
const vs = [
const vs1 = [
{x: 0.125, y: 0.125, z: 0.125},
{x: -0.125, y: 0.125, z: 0.125},
{x: -0.125, y: -0.125, z: 0.125},
{x: 0.125, y: -0.125, z: 0.125},

{x: 0.125, y: 0.125, z: -0.125},
{x: -0.125, y: 0.125, z: -0.125},
{x: -0.125, y: -0.125, z: -0.125},
{x: 0.125, y: -0.125, z: -0.125},

{x: 0.25, y: 0.25, z: 0.25},
{x: -0.25, y: 0.25, z: 0.25},
{x: -0.25, y: -0.25, z: 0.25},
Expand All @@ -8,13 +18,34 @@ const vs = [
{x: -0.25, y: 0.25, z: -0.25},
{x: -0.25, y: -0.25, z: -0.25},
{x: 0.25, y: -0.25, z: -0.25},

]

const fs = [
const fs1 = [
[0, 1, 2, 3],
[4, 5, 6, 7],

[8, 9, 10, 11],
[12, 13, 14, 15],

[0, 4],
[1, 5],
[2, 6],
[3, 7],

[8, 12],
[9, 13],
[10, 14],
[11, 15],

[0, 8],
[1, 9],
[2, 10],
[3, 11],

[4, 12],
[5, 13],
[6 ,14],
[7, 15]

]
Binary file added imgs/multi_model_demo.mp4
Binary file not shown.
33 changes: 30 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
<canvas id="game"></canvas>
<script src="penger.js"></script>
<!-- <script src="cube.js"></script> -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3d Animation</title>
<style>
.background-color {
background-color:darkgrey;
}
#current-fps {
background-color: black;
color: white;
}
</style>
</head>
<body class="background-color">
<div>
<canvas id="game"></canvas>
<canvas id="game2"></canvas>
</div>

<a id="current-fps"></a>
<label for="speed">Speed (fast to slow):</label>
<input type="range" id="speed" name="speed" min="1" max="1000" value="1000">

</body>
<script src="penger.js"></script>
<script src="cube.js"></script>
<script src="index.js"></script>
</html>
82 changes: 54 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
const BACKGROUND = "#101010"
const FOREGROUND = "#50FF50"
const HEIGHT = 600
const WIDTH = 600

console.log(game)
game.width = 800
game.height = 800
const ctx = game.getContext("2d")
console.log(ctx)
game.width = WIDTH
game.height = HEIGHT
const game1_ctx = game.getContext("2d")
console.log(game1_ctx)

function clear() {
console.log(game2)
game2.width = WIDTH
game2.height = HEIGHT
const game2_ctx = game2.getContext("2d")
console.log(game2_ctx)

function clear(ctx) {
ctx.fillStyle = BACKGROUND
ctx.fillRect(0, 0, game.width, game.height)
}

function point({x, y}) {
function point({x, y}, ctx) {
const s = 20;
ctx.fillStyle = FOREGROUND
ctx.fillRect(x - s/2, y - s/2, s, s)
ctx.fillStyle = FOREGROUND
}

function line(p1, p2) {
ctx.lineWidth = 3;
function line(p1, p2, ctx) {
ctx.lineWidth = 3
ctx.strokeStyle = FOREGROUND
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.beginPath();
ctx.moveTo(p1.x, p1.y)
ctx.lineTo(p2.x, p2.y)
ctx.stroke();
}


function screen(p) {
// -1..1 => 0..2 => 0..1 => 0..w
return {
Expand All @@ -42,8 +51,11 @@ function project({x, y, z}) {
}
}

const FPS = 60;
const FPS = 60
let dz = 1;
let angle = 0;

document.getElementById("current-fps").innerHTML = "FPS: " + FPS

function translate_z({x, y, z}, dz) {
return {x, y, z: z + dz};
Expand All @@ -59,25 +71,39 @@ function rotate_xz({x, y, z}, angle) {
};
}

let dz = 1;
let angle = 0;
function draw(fs, vs, ctx) {
for(const f of fs) {
for(let i=0;i<f.length;++i){
const a = vs[f[i]];
const b = vs[f[(i+1)%f.length]]
line(screen(project(translate_z(rotate_xz(a, angle), dz))),
screen(project(translate_z(rotate_xz(b, angle), dz))),
ctx)
}
}
}

function show_points(vs, ctx) {
for (const v of vs) {
point(screen(project(translate_z(rotate_xz(v, angle), dz))), ctx)
}
}


function frame() {
const dt = 1/FPS;
// dz += 1*dt;
angle += Math.PI*dt;
clear()
// for (const v of vs) {
// point(screen(project(translate_z(rotate_xz(v, angle), dz))))
// }
for (const f of fs) {
for (let i = 0; i < f.length; ++i) {
const a = vs[f[i]];
const b = vs[f[(i+1)%f.length]];
line(screen(project(translate_z(rotate_xz(a, angle), dz))),
screen(project(translate_z(rotate_xz(b, angle), dz))))
}
}
setTimeout(frame, 1000/FPS);
clear(game1_ctx)
clear(game2_ctx)

// show_points(vs, game1_ctx)

draw(fs, vs, game1_ctx)
draw(fs1, vs1, game2_ctx)

const val = document.getElementById("speed").value

setTimeout(frame, val/FPS);
}
setTimeout(frame, 1000/FPS);