Repository navigation
Expand file tree
/
Copy pathsnake.js
More file actions
87 lines (74 loc) · 1.91 KB
/
Copy pathsnake.js
File metadata and controls
87 lines (74 loc) · 1.91 KB
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
/**
* New node file Snake Object
*/
function Snake(length, scaleSize, color, speed, ctx) {
this.length = length;
this.scaleSize = scaleSize;
this.color = color;
this.speed = speed;
this.ctx = ctx;
};
Snake.prototype = {
length: '',
scaleSize: '',
color: '',
Scale: function(x, y) {
this.ctx.fillStyle = this.color;
this.ctx.fillRect(x, y, this.scaleSize - 1, this.scaleSize);
this.ctx.fill();
},
animate: function(fn) {
window.requestAnimationFrame(fn);
},
body: function(x, y) {
for (var i = 0; i <= this.length; i++) {
if ((x + (i * this.scaleSize)) < this.ctx.canvas.width) {
this.Scale(x + (i * this.scaleSize), y);
} else {
this.Scale(this.ctx.canvas.width - (x + (i * this.scaleSize)), y);
}
}
}
};
function createSnake(canvas) {
var ctx = canvas.getContext('2d');
var snake = new Snake(4, 30, 'blue', 5, ctx);
var start = null;
var y = Math.random() * 500;
var step = function(timestamp) {
if (!start) start = timestamp;
var newX = (timestamp - start) / snake.speed;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (newX > canvas.width) {
start = timestamp;
};
snake.body(newX, y);
//if ((snake.length + 5 * snake.scaleSize + newX) < canvas.width) {
snake.animate(step);
};
snake.animate(step);
}
function resize_canvas(canvas) {
if (canvas.width < window.innerWidth) {
canvas.width = window.innerWidth;
}
if (canvas.height < window.innerHeight) {
canvas.height = window.innerHeight;
}
createSnake(canvas);
}
$(document).ready(function() {
canvas = document.getElementById("canvas1");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
user = ["darpan"];
//user.forEach(createSnake(canvas));
createSnake(canvas);
});
var socket = io();
socket.on('broadcast', function(data) {
$('#message').text(data.message);
});
{
name: 'darpan'
};