-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathDisplay.js
97 lines (87 loc) · 2.8 KB
/
Display.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
85
86
87
88
89
90
91
92
93
94
95
96
/*global define,_*/
var profile = false;
define(['requestAnimationFrame','stats'],function(requestAnimationFrame,stats){
"use strict";
function Display(canvas) {
this.canvas = canvas;
this.context = undefined;
this.numFrames = 0;
this.paused = true;
this.scale = 1;
this.draw = {
continuous : false,
info : false
};
}
_.extend(Display.prototype, Backbone.Events, {
init : function () {
this.context = this.canvas.getContext("2d");
this.context.scale(this.scale, this.scale);
this.width = this.canvas.width / this.scale;
this.height = this.canvas.height / this.scale;
this.canvas.onmousedown = function (evt) {
this.trigger('mouseDown', evt);
return false;
}.bind(this);
this.canvas.onmouseup = function (evt) {
this.trigger('mouseUp', evt);
return false;
}.bind(this);
this.canvas.onmouseover = function (evt) {this.trigger('mouseOver', evt);}.bind(this);
this.canvas.onmousemove = function (evt) {this.trigger('mouseMove', evt);}.bind(this);
profile && console.profile('Display');
this.main();
},
main : function () {
stats.begin();
if (!this.paused) this.nextFrame();
if (profile && this.numFrames > 1000) return console.profileEnd('Display');
requestAnimationFrame(this.main.bind(this));
stats.end();
},
nextFrame : function () {
if (!this.draw.continuous) {
this.clear();
}
this.trigger('newFrame');
this.trigger('beforeUpdate');
this.trigger('update');
this.trigger('afterUpdate');
this.trigger('beforeDraw');
this.tick();
this.trigger('draw');
this.trigger('afterDraw');
},
drawLine : function (startPoint, endPoint) {
this.context.beginPath();
this.context.moveTo(startPoint.x, startPoint.y);
this.context.lineTo(endPoint.x, endPoint.y);
this.context.stroke();
},
drawText : function (txt, point, width) {
this.context.fillText(txt, point.x, point.y, width);
},
drawCircle : function (point, radius) {
this.context.beginPath();
this.context.arc(point.x, point.y, radius, 0, Math.PI * 2);
this.context.closePath();
this.context.fill();
},
fillStyle : function (fill) { this.context.fillStyle = fill; },
strokeStyle : function (fill) { this.context.strokeStyle = fill; },
tick : function () {
this.numFrames++;
},
clear : function () {
this.context.clearRect(0, 0, this.width, this.height);
},
start : function () { this.paused = false; },
stop : function () { this.paused = true; },
togglePause : function () { this.paused = !this.paused;},
step : function () {
this.stop();
this.nextFrame();
}
});
return Display;
});