Repository navigation
Expand file tree
/
Copy pathrender.js
More file actions
151 lines (137 loc) · 5.54 KB
/
Copy pathrender.js
File metadata and controls
151 lines (137 loc) · 5.54 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// render.js
// a more general use render script
// with generic drawing functions, as well
// as "world" variants that automatically
// translate and scale according to the camera
// state
// import utils and camera from utils
import { utils, camera } from "./utils.js"
// class for the render object
class Render {
constructor() {
// create context member and initialize it to null;
this.ctx = null;
}
// initialize the canvas and set its configurations
initCanvas(canvas) {
canvas.width = utils.screenSize.x;
canvas.height = canvas.width * 3 / 4;
utils.screenSize.w = canvas.width;
utils.screenSize.h = canvas.height;
}
// grab the context from index
initializeContext(context) {
this.ctx = context;
}
// set both the fill color and stroke color to abstract and avoid confusion
setColor(r, g, b, a = 1) {
this.ctx.fillStyle = 'rgb(' + r.toString() + "," + g.toString() + "," + b.toString() + "," + a.toString() + ")";
this.ctx.strokeStyle = 'rgb(' + r.toString() + "," + g.toString() + "," + b.toString() + "," + a.toString() + ")";
}
// set the line width, mostly unnecessary besides automatically accessing the context
// used for unfilled shapes
setLineWidth(width) {
this.ctx.lineWidth = width;
}
// sets the font size for text rendering
// only sets font to bolded courier
setFont(size) {
this.ctx.font = "bold " + size.toString() + "px courier";
}
// clear the screen and fill it with a certain color
clearScreen(r=255, g=255, b=255) {
this.ctx.beginPath();
this.ctx.clearRect(0, 0, utils.screenSize.w, utils.screenSize.h);
this.setColor(r, g, b);
this.ctx.fillRect(0, 0, utils.screenSize.w, utils.screenSize.h);
}
// generic finish draw for fill vs. stroke drawing
finishDraw(fill) {
if (fill) this.ctx.fill();
else this.ctx.stroke();
}
// draw a circle at point p in screen coordinates, with radius r in screen units
// can be filled or outline
drawCircle(p, r, fill = true) {
this.ctx.beginPath();
this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2.0);
this.finishDraw(fill);
}
// draw a circle at point p in game world coordinates, transformed according to the camera,
// with radius r in world units
// can be filled or outline
drawCircleWorld(p, r, fill = true) {
let camOffset = utils.subtract(camera.pos, utils.halfView);
this.drawCircle(utils.toScreenXY(utils.subtract(p, camOffset)), utils.toScreenX(r), fill);
}
// draw a rectangle using a rect structure in screen coordinates
// can be filled or outline
drawRect(rect, fill = true) {
this.ctx.beginPath();
this.ctx.rect(rect.x, rect.y, rect.w, rect.h);
this.finishDraw(fill);
}
// draw a rectangle using a rect structure in world coordinates
// can be filled or outline
drawRectWorld(rect, fill = true) {
let camOffset = utils.subtract(camera.pos, utils.halfView);
let r = utils.Rect2(utils.toScreenXY(utils.subtract(utils.Vector2(rect.x, rect.y), camOffset)),
utils.toScreenXY(utils.subtract(utils.add(utils.Vector2(rect.x, rect.y), utils.Vector2(rect.w, rect.h)), camOffset)))
this.drawRect(r, fill);
}
// connect a list of points with lines in screen coordinates
// uses lineWidth for line thickness
drawPolyLine(points) {
if (points.length == 0) { return; }
this.ctx.moveTo(points[0].x, points[0].y);
this.ctx.beginPath();
for (let i = 0; i < points.length; i++) {
this.ctx.lineTo(points[i].x, points[i].y);
}
this.ctx.stroke();
}
// connect a list of points with lines in world coordinates
// uses lineWidth for line thickness
drawPolyLineWorld(points) {
if (points.length == 0) { return; }
let camOffset = utils.subtract(camera.pos, utils.halfView);
let n = [];
for (let i = 0; i < points.length; i++) {
n.push(utils.toScreenXY(utils.subtract(points[i], camOffset)));
}
this.drawPolyLine(n);
}
// draw a polygon defined by a list of points in screen coordinates
// can be filled or outline
drawPolygon(points, fill = true) {
if (points.length == 0) { return; }
this.ctx.moveTo(points[0].x, points[0].y);
this.ctx.beginPath();
for (let i = 0; i < points.length; i++) {
this.ctx.lineTo(points[i].x, points[i].y);
}
this.ctx.closePath();
this.finishDraw(fill);
}
// draw a polygon defined by a list of points in world coordinates
// can be filled or outline
drawPolygonWorld(points, fill = true) {
let camOffset = utils.subtract(camera.pos, utils.halfView);
let p = [];
for (let i = 0; i < points.length; i++) {
p.push(utils.toScreenXY(utils.subtract(points[i], camOffset)));
}
this.drawPolygon(p, fill);
}
// draw horizontally centered text at pos
drawText(text, pos) {
this.ctx.fillText(text, pos.x - this.ctx.measureText(text).width / 2, pos.y);
}
// render the loaded tutorial image from github
// from img component
drawTutorialImage() {
let image = document.getElementById("tutorial");
this.ctx.drawImage(image, 200, 120, 400, 400);
}
}
export const render = new Render();