This repository has been archived by the owner on Feb 22, 2021. It is now read-only.
/
canvaswrapper.js
84 lines (73 loc) · 2.17 KB
/
canvaswrapper.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
/*
simple file to make using html5 canvases simpler and more readable
i would be ok with using canvases directly but i don't want to force
people to learn canvases just to read my tutorial
*/
function CanvasWrapper(canvasId) {
var canvas = document.getElementById(canvasId);
this.width = canvas.width;
this.height = canvas.height;
var events = [];
var ctx = canvas.getContext("2d") // TODO: is using same context for everything like this bad?
function addKeyHandler(original, eventCreator) {
var oldHandler = original || function(){};
return function (evt) {
if (document.activeElement !== canvas) {
return oldHandler(evt);
}
// prevent scrolling with arrow keys so the demo can use them
if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].indexOf(evt.key) >= 0) {
evt.preventDefault();
}
events.push(eventCreator(evt));
}
}
document.onkeydown = addKeyHandler(document.onkeydown, function(evt) {
return { type: 'keydown', key: evt.key };
});
document.onkeyup = addKeyHandler(document.onkeyup, function(evt) {
return { type: 'keyup', key: evt.key };
});
this.getEvents = function() {
var res = events;
events = [];
return res;
}
this.drawRectangle = function(left, top, width, height, color) {
ctx.beginPath();
ctx.rect(left, top, width, height);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
this.fill = function(color) {
ctx.clearRect(0, 0, this.width, this.height); // doesn't accept colors
ctx.beginPath();
ctx.rect(0, 0, this.width, this.height);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
this.drawLine = function(x1, y1, x2, y2, color) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = color;
ctx.stroke();
ctx.closePath();
}
this.drawCircle = function(centerx, centery, radius, color) {
ctx.beginPath();
ctx.arc(centerx, centery, radius, 0, 2*Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
}
function runRepeatedly(callback) {
function run() {
callback();
window.requestAnimationFrame(run);
}
run();
}