-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
237 lines (197 loc) · 6.08 KB
/
index.html
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<html>
<head>
<title>
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var util = (function() {
function equilateralBToH(b) {
return Math.floor(Math.sqrt(3) * b / 2);
}
return {
equilateralBToH: equilateralBToH
}
})();
Function.prototype.inheritsFrom = function( parentClassOrObject ){
if ( parentClassOrObject.constructor == Function )
{
//Normal Inheritance
this.prototype = new parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject.prototype;
}
else
{
//Pure Virtual Inheritance
this.prototype = parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject;
}
return this;
}
// invoke parent constructor of this
function Super(obj) {
if (obj.parent) return obj.parent.constructor;
}
function CanvasState(canvas) {
this.canvas = canvas;
this.width = canvas.width;
this.height = canvas.height;
this.ctx = canvas.getContext('2d');
this.valid = false; // when set to true, canvas will redraw
this.shapes = [];
this.interval = 30; // redraw time
var self = this;
// redraw
setInterval(function() {
self.draw();
}, self.interval);
}
CanvasState.prototype.redraw = function() {
this.valid = false;
// force redraw. if we don't, we delegate drawing scheduling back to CanvasState timer
// this.draw();
}
CanvasState.prototype.draw = function() {
if (!this.valid) {
var ctx = this.ctx;
var shapes = this.shapes;
this.clear();
for (var i = 0 ; i < shapes.length; i++) {
var shape = shapes[i];
if (shape.x > this.width || shape.y > this.height ||
shape.x + shape.w < 0 || shape.y + shape.h < 0) continue;
shapes[i].draw(ctx);
}
// draw selection
// right now this is just a stroke along the edge of the selected Shape
if (this.selection != null) {
ctx.strokeStyle = this.selectionColor;
ctx.lineWidth = this.selectionWidth;
var mySel = this.selection;
ctx.strokeRect(mySel.x,mySel.y,mySel.w,mySel.h);
}
this.valid = true;
}
}
CanvasState.prototype.addShape = function(shape) {
this.shapes.push(shape);
this.valid = false;
}
CanvasState.prototype.clear = function() {
this.ctx.clearRect(0, 0, this.width, this.height);
}
// Triangle
function Shape(x, y, color) {
this.x = x || 0;
this.y = y || 0;
this.color = color || '#8e44ad';
}
Shape.prototype.draw = function(ctx) {
}
Square.inheritsFrom(Shape);
function Square(x, y, color, w) {
Super(this).call(this, x, y, color);
this.w = w;
}
Square.prototype.draw = function(ctx) {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.fillRect(this.x, this.y, this.w, this.w);
}
// Upside Down Triangle
Triangle.inheritsFrom(Shape);
// Constructor
function Triangle(x, y, b, color, num) {
// Invoke Super
Super(this).call(this, x, y, color);
this.b = b;
this.h = util.equilateralBToH(b);
this.num = num;
}
Triangle.prototype.draw = function(ctx) {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.moveTo(this.x + 0, this.y + this.h);
ctx.lineTo(this.x + this.b, this.y + this.h);
ctx.lineTo(this.x + (this.b/2), this.y + 0);
ctx.fill();
ctx.fillStyle = "white";
ctx.font = "12px Arial";
ctx.fillText(this.num, this.x + this.b/2 - 6, this.y + this.h/2 +12);
}
function Tile(x, y, size, color, canvas, magnitude) {
this.shape = new Square(x, y, "#BFD37B", size);
this.magnitude = magnitude || 0;
this.shape.color = gradient(magnitude);
this.shape.color = makeColorGradient(.3,.3,.3,0,2,4, 127, magnitude / 10);
// delegate redrawing management to canvas
canvas.addShape(this.shape);
}
Tile.prototype.setMagnitude = function (m) {
this.m = m;
}
// utility function. magnitude -> color
function gradient(i) {
var start = parseInt("0x000000", 16);
var end = parseInt("0xFFFFFF", 16);
var numSteps = 1200;
var step = Math.floor((end - start) / numSteps);
var number = start + step*i;
var hex = number.toString(16);
while (hex.length < 6) {
hex = "0" + hex;
}
hex = hex.toUpperCase();
hex = "#" + hex;
return hex;
}
function makeColorGradient(frequency1, frequency2, frequency3,
phase1, phase2, phase3, width, i){
if (width == undefined) width = 127;
var red = Math.sin(frequency1*i + phase1) * width;
var grn = Math.sin(frequency2*i + phase2) * width;
var blu = Math.sin(frequency3*i + phase3) * width;
return RGB2Color(red,grn,blu);
}
function RGB2Color(r,g,b){
return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}
function byte2Hex(n) {
var nybHexString = "0123456789ABCDEF";
return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
}
function Grid(canvas) {
this.canvasState = canvas;
this.tiles = [];
var TILE_SIZE = 20;
var NUM_ROWS = canvas.height / TILE_SIZE;
var NUM_COLS = canvas.width / TILE_SIZE;
for(var i = 0; i < NUM_COLS; i++) {
for (var j = 0; j < NUM_ROWS; j++) {
var color = (i + j) % 2 === 0 ? "#FF0000" : "#000000";
this.tiles.push(new Tile(TILE_SIZE*i, TILE_SIZE*j, TILE_SIZE, color, canvas, Math.abs(NUM_COLS/2 - i) + Math.abs(NUM_ROWS/2 - j)));
}
}
}
// Updates the grid. And redraw
Grid.prototype.update = function() {
this.canvasState.redraw();
}
$(document).ready(function() {
var canvas = new CanvasState(document.getElementById("game"));
var g = new Grid(canvas);
setInterval(function() {
g.update();
}, 50);
});
</script>
<style>
</style>
</head>
<body>
<canvas id="game" width="800" height="600">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
</body>
</html>