-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-canvas.js
158 lines (108 loc) · 4.34 KB
/
index-canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//TODO: make all cubes 1 cube or make them all rotate around a single point instead of their own center
// making it into 1 cube might be more efficient to run but takes more time
//will figure this out later, low priority.
/*
//initialize canvas
let canvas = document.querySelector('.pokemon-wrapper canvas');
let context = canvas.getContext('2d');
canvas.width = window.innerHeight / 2;
canvas.height = canvas.width;
//canvas centers
let centX = canvas.width / 2;
let centY = canvas.height / 2;
const Point2D = function (x, y) { this.x = x; this.y = y; };
const Point3D = function (x, y, z) { this.x = x; this.y = y; this.z = z; };
const Cube = function (x, y, z, size) {
Point3D.call(this, x, y, z);
size *= 0.5;
this.vertices = [new Point3D(x - size, y - size, z - size),
new Point3D(x + size, y - size, z - size),
new Point3D(x + size, y + size, z - size),
new Point3D(x - size, y + size, z - size),
new Point3D(x - size, y - size, z + size),
new Point3D(x + size, y - size, z + size),
new Point3D(x + size, y + size, z + size),
new Point3D(x - size, y + size, z + size)];
this.faces = [[0, 1, 2, 3], [0, 4, 5, 1], [1, 5, 6, 2], [3, 2, 6, 7], [0, 3, 7, 4], [4, 7, 6, 5]];
};
Cube.prototype = {
rotateX: function (radian) {
var cosine = Math.cos(radian);
var sine = Math.sin(radian);
for (let index = this.vertices.length - 1; index > -1; --index) {
let p = this.vertices[index];
//console.log(this.x + " " + this.y + " " + this.z);
//console.log(this.x);
//$('#button-back').html(this.x + " || " + this.y + " || " + this.z);
//$('#button-back').html(p.x + " || " + p.y + " || " + p.z);
let y = (p.y - this.y) * cosine - (p.z - this.z) * sine;
let z = (p.y - this.y) * sine + (p.z - this.z) * cosine;
p.y = y + this.y;
p.z = z + this.z;
}
},
rotateY: function (radian) {
var cosine = Math.cos(radian);
var sine = Math.sin(radian);
for (let index = this.vertices.length - 1; index > -1; --index) {
let p = this.vertices[index];
let x = p.z * sine;// (p.z - this.z) * sine + (p.x - this.x) * cosine;
let z = p.z * cosine;// (p.z - this.z) * cosine - (p.x - this.x) * sine;
p.x = x + this.x;
p.z = z + this.z;
}
}
};
let cubeList = [];
for (let i = 0; i < 4; ++i) {
for (let j = 0; j < 4; ++j) {
for (let k = 0; k < 4; ++k) {
var pointer = new Point2D(0, 0);
let scale = 4;
var cube = new Cube(i * scale, j * scale, 20 + k * scale, scale);
cubeList.push(cube);
}
}
}
var height = document.documentElement.clientHeight;
var width = document.documentElement.clientWidth;
function project(points3d, width, height) {
var points2d = new Array(points3d.length);
var focal_length = 500;
for (let index = points3d.length - 1; index > -1; --index) {
let p = points3d[index];
let x = p.x * (focal_length / p.z) + width * 0.5;
let y = p.y * (focal_length / p.z) + height * 0.5;
points2d[index] = new Point2D(x, y);
}
return points2d;
}
function loop() {
window.requestAnimationFrame(loop);
context.clearRect(0, 0, canvas.width, canvas.height); //clear html5 canvas
context.strokeStyle = "#ffffff";
cubeList.forEach(function(cube, i){
cube.rotateX(pointer.y * 0.0001);
cube.rotateY(-pointer.x * 0.0001);
context.fillStyle = "#0080f0";
var vertices = project(cube.vertices, centX, centY);
for (let index = cube.faces.length - 1; index > -1; --index) {
let face = cube.faces[index];
context.beginPath();
context.moveTo(vertices[face[0]].x, vertices[face[0]].y);
context.lineTo(vertices[face[1]].x, vertices[face[1]].y);
context.lineTo(vertices[face[2]].x, vertices[face[2]].y);
context.lineTo(vertices[face[3]].x, vertices[face[3]].y);
context.closePath();
context.stroke();
}
});
}
loop();
pointer.x -= 100;
pointer.y -= 100;
window.addEventListener("click", (event) => {
});
$(window).on('resize', function () {
scale = (window.innerHeight + window.innerHeight) / 1280;
}); */