JSFiddle - React, Tailwind, and code Playground
HTML
<div class="container">
<canvas class="game" id="game">game area</canvas>
</div>
<script type="text/javascript" src="https://cdn.rawgit.com/ofeldt/jix/master/ext/jquery.js"></script>
CSS
.container {
width: 401px;
height: 301px;
margin: 0 auto;
background-color: #FFFFFF
}
.game {
background-color: #000000;
}
JavaScript
(function () {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
},
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}());
var Point = function (x, y) {
this.x = x;
this.y = y;
};
var Polygon = function (coords) {
this.coords = coords;
var size = coords.length;
this.x_min = coords[0][0];
this.x_max = coords[0][0];
this.y_min = coords[0][1];
this.y_max = coords[0][1];
for (var i = 0; i < size - 1; i++) {
if (this.x_max < coords[i][0]) {
this.x_max = coords[i][0];
}
if (this.x_min > coords[i][0]) {
this.x_min = coords[i][0];
}
if (this.y_max < coords[i][1]) {
this.y_max = coords[i][1];
}
if (this.y_min > coords[i][1]) {
this.y_min = coords[i][1];
}
}
};
Polygon.prototype.lines = function () {
var size = this.coords.length;
var out = [];
for (var i = 0; i < size - 1; i++) {
out.push(new Line([this.coords[i][0], this.coords[i][1]], [this.coords[i + 1][0], this.coords[i + 1][1]]));
}
out.push(new Line([this.coords[size - 1][0], this.coords[size - 1][1]], [this.coords[0][0], this.coords[0][1]]));
...