-
Notifications
You must be signed in to change notification settings - Fork 0
/
blit.js
56 lines (48 loc) · 2.64 KB
/
blit.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
var gamescreen;
if (!gamescreen) gamescreen = {}; // initialise the top-level module if it does not exist
if (!gamescreen.screens) gamescreen.screens = {};
gamescreen.screens.blit = function(game,width,height) {
$("#gamescreenarea").append("<canvas id=\"canvas2d\" width=\"" + width + "\" height=\"" + height + "\" />");
var ctx = $("#canvas2d")[0].getContext("2d");
var data = ctx.createImageData(width + 1, height + 1);
return {
cleanup: function() {
$("#canvas2d").remove();
},
create: function(sectors, x1, y1, x2, y2) {
var c2s = new gamescreen.util.Cartesian2Screen(x1,y1,x2,y2);
var drawers = gamescreen.renderutil.scanPolys(_.map(sectors, function(s) { return s.poly; }), x1,y1,x2,y2);
return {
draw: function(textures) {
gamescreen.util.Timer.start("Sectordraw");
gamescreen.util.Timer.substart("clean");
var length = ctx.canvas.width * ctx.canvas.height * 4, i = 0;
for (; i < length; i++) {
data.data[i] = 0;
}
gamescreen.util.Timer.subend();
gamescreen.renderutil.fillBuffer(drawers, textures, data);
gamescreen.util.Timer.substart("Put image buffer");
ctx.putImageData(data, 0, 0);
gamescreen.util.Timer.subend();
gamescreen.util.Timer.end();
for (i = 0; i < sectors.length; i++) {
for (var k = 0; k < sectors[i].poly.edges.length; k++) {
ctx.beginPath();
var one = sectors[i].poly.edges[k].origin;
var two = sectors[i].poly.edges[k].end;
var g = ctx.createLinearGradient(c2s.cartesian2screenx(one.x), c2s.cartesian2screeny(one.y),
c2s.cartesian2screenx(two.x), c2s.cartesian2screeny(two.y));
g.addColorStop(0.0, 'rgba(255, 0, 0, 1.0)');
g.addColorStop(1.0, 'rgba(0, 255, 0, 1.0)');
ctx.strokeStyle = g;
ctx.moveTo(c2s.cartesian2screenx(one.x), c2s.cartesian2screeny(one.y));
ctx.lineTo(c2s.cartesian2screenx(two.x), c2s.cartesian2screeny(two.y));
ctx.stroke();
}
}
}
};
}
};
};