-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackingCanvas.js
86 lines (72 loc) · 3.41 KB
/
backingCanvas.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
var gamescreen;
if (!gamescreen) gamescreen = {}; // initialise the top-level module if it does not exist
if (!gamescreen.screens) gamescreen.screens = {};
gamescreen.screens.backingCanvas = function(where, game, width, height, background) {
var bg = background === undefined ? "#ffffff" : background;
var extents_width = game.extents.x2 - game.extents.x1 + width;
var extents_height = game.extents.y2 - game.extents.y1 + height;
$(where).width(width);
$(where).height(height + 60);
var canvas = $("<canvas class=\"gamecanvas\" width=\"" + width + "\" height=\"" + height + "\"></canvas>").appendTo($(where));
var canvas_hidden = $("<canvas width=\"" + extents_width +"\" height=\"" + extents_height + "\" style=\"display:none\"></canvas>").appendTo($(where));
var consoleDiv = $('<div></div>').appendTo($(where));
consoleDiv.width(width);
var _console = gamescreen.console($(consoleDiv));
//console.height(height);
_console.log("BackingCanvas: " + gamescreen.console.util.point(width,height));
var ctx_front = canvas[0].getContext("2d");
var ctx_back = canvas_hidden[0].getContext("2d");
return {
cleanup: function() {
canvas.remove();
canvas_hidden.remove();
consoleDiv.remove();
},
create: function(x1, y1, x2, y2) {
var half_viewportwidth = Math.round(width/2, 0);
var half_viewportheight = Math.round(height/2, 0);
var c2s = new gamescreen.util.Screen(game.extents.x1 - half_viewportwidth,
game.extents.y1 - half_viewportheight,
game.extents.x2 + half_viewportwidth,
game.extents.y2 + half_viewportheight);
//_console.frame_log(gamescreen.console.util.rect(x1,y1,x2,y2));
return {
draw: function(d) {
_console.frame_log(gamescreen.console.util.rect(
c2s.cartesian2screeny(x1),
c2s.cartesian2screeny(y2),
width,
height
));
gamescreen.util.Timer.start("BackingCanvas");
gamescreen.util.Timer.substart("clean back");
ctx_back.globalCompositeOperation = "none";
ctx_back.fillStyle = bg;
ctx_back.fillRect(
c2s.cartesian2screenx(x1),
c2s.cartesian2screeny(y1),
width,
height
);
gamescreen.util.Timer.subend();
gamescreen.util.Timer.substart("draw-all");
d(c2s, ctx_back);
gamescreen.util.Timer.subend();
gamescreen.util.Timer.substart("copy to front");
ctx_back.globalCompositeOperation = "none";
ctx_front.drawImage(ctx_back.canvas,
c2s.cartesian2screenx(x1),
c2s.cartesian2screeny(y1),
width,
height,
0, 0,
width,
height);
gamescreen.util.Timer.subend();
gamescreen.util.Timer.end();
},
console: _console
};
}
};
};