Skip to content

Commit 0ec91a6

Browse files
committed
add types, add a try at JSWindow.show(), make Control & Rectangle into classes
1 parent bec751b commit 0ec91a6

3 files changed

Lines changed: 187 additions & 148 deletions

File tree

src/JSTools.ts

Lines changed: 129 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ function getRandomColor() {
2020
/*--------------------------------------Function-4------------------------------------------------------------------*/
2121
var windowRatio;
2222
var canvasRatio;
23-
const toolBarHeight: number = 27;
23+
(window as any).toolBarHeight = 27;
2424
let canvasScale : number;
2525
function resizeCanvas(){
2626
//Get Window Ratio
27-
windowRatio = window.innerWidth / (window.innerHeight - toolBarHeight);
27+
windowRatio = window.innerWidth / (window.innerHeight - (window as any).toolBarHeight);
2828
//Get Canvas Ratio
2929
canvasRatio = canvas.width / canvas.height;
3030
if (canvasRatio > windowRatio) {
3131
canvas.style.width = window.innerWidth + "px";
3232
canvas.style.height = "";
3333
canvas.style.left = "0px";
34-
canvas.style.top = ((window.innerHeight - toolBarHeight) / 2 - (window.innerWidth / canvasRatio) / 2 + toolBarHeight) + "px";
35-
canvasScale = canvas.style.width.replace("px", "") / canvas.width;
34+
canvas.style.top = ((window.innerHeight - (window as any).toolBarHeight) / 2 - (window.innerWidth / canvasRatio) / 2 + (window as any).toolBarHeight) + "px";
35+
canvasScale = Number(canvas.style.width.replace("px", "")) / canvas.width;
3636
} else if (canvasRatio <= windowRatio) {
3737
canvas.style.width = "";
38-
canvas.style.height = window.innerHeight - toolBarHeight + "px";
39-
canvas.style.left = (window.innerWidth / 2 - ((window.innerHeight - toolBarHeight) * canvasRatio) / 2) + "px";
40-
canvas.style.top = toolBarHeight + "px";
41-
canvasScale = canvas.style.height.replace("px", "") / canvas.height;
38+
canvas.style.height = window.innerHeight - (window as any).toolBarHeight + "px";
39+
canvas.style.left = (window.innerWidth / 2 - ((window.innerHeight - (window as any).toolBarHeight) * canvasRatio) / 2) + "px";
40+
canvas.style.top = (window as any).toolBarHeight + "px";
41+
canvasScale = Number(canvas.style.height.replace("px", "")) / canvas.height;
4242
}
4343
}
4444
/*--------------------------------------Function-5------------------------------------------------------------------*/
@@ -154,108 +154,135 @@ function AnimationLOOP (funct: () => void) {
154154
}
155155
/*--------------------------CLASSES-----Function-17-----------------------------------------------------------------*/
156156
var transparent = "rgba(0,0,0,0)";
157-
function Control(x, y) {
158-
this.x = x;
159-
this.y = y;
160-
this.initX = x; //[Inserir razão destes dois]
161-
this.initY = y;
162-
this.color = transparent;
163-
this.outlineColor = "black";
164-
this.outlineWidth = 2;
165-
this.visible = true;
166-
this.showIndex = 0; //Lower is drawn first, in back.
167-
//this.name = ""; //Ver função getControlByName
168-
}
169-
Control.prototype.show = function () {
170-
this.visible = true;
171-
}
172-
Control.prototype.hide = function () {
173-
this.visible = false;
157+
class Control {
158+
x: number;
159+
y: number;
160+
initX: number;
161+
initY: number;
162+
color: string;
163+
outlineColor: string;
164+
outlineWidth: number;
165+
visible: boolean;
166+
showIndex: number;
167+
constructor(x: number, y: number) {
168+
this.x = x;
169+
this.y = y;
170+
this.initX = x; //[Inserir razão destes dois]
171+
this.initY = y;
172+
this.color = transparent;
173+
this.outlineColor = "black";
174+
this.outlineWidth = 2;
175+
this.visible = true;
176+
this.showIndex = 0; //Lower is drawn first, in back.
177+
//this.name = ""; //Ver função getControlByName
178+
179+
// @ts-ignore
180+
window[this.constructor.name + Math.random() * 10**18] = this;
181+
}
182+
183+
/**
184+
* show
185+
*/
186+
public show() {
187+
this.visible = true;
188+
}
189+
public hide() {
190+
this.visible = false;
191+
};
192+
174193
}
175194
/*--------------------------------------Function-18-----------------------------------------------------------------*/
176-
function Rectangle(x, y, width, height) {
177-
Control.call(this, x, y); //Inherit from Control
178-
this.width = width;
179-
this.height = height;
180-
this.outlineWidth = 4;
181-
this.outlinePosition = "center";
182-
this.doTranslate = false;
183-
this.doPattern = "";
184-
}
185-
Rectangle.prototype = Object.create(Control.prototype);
186-
Rectangle.prototype.constructor = Rectangle;
187-
Rectangle.prototype.draw = function () {
188-
if (this.visible) {
189-
ctx.fillStyle = this.color;
190-
ctx.strokeStyle = this.outlineColor;
191-
ctx.lineWidth = this.outlineWidth;
192-
ctx.fillRect(this.x, this.y, this.width, this.height);
193-
this.drawParts_outline();
194-
this.drawParts_patterns();
195-
if (this.doTranslate) {
196-
ctx.translate(this.x, this.y);
195+
class Rectangle extends Control {
196+
width: number;
197+
height: number;
198+
outlinePosition: string;
199+
doTranslate: boolean;
200+
doPattern: string;
201+
constructor (x: number, y: number, width: number, height: number) {
202+
super(x, y);
203+
this.width = width;
204+
this.height = height;
205+
this.outlineWidth = 4;
206+
this.outlinePosition = "center";
207+
this.doTranslate = false;
208+
this.doPattern = "";
209+
}
210+
211+
public draw() {
212+
if (this.visible) {
213+
ctx.fillStyle = this.color;
214+
ctx.strokeStyle = this.outlineColor;
215+
ctx.lineWidth = this.outlineWidth;
216+
ctx.fillRect(this.x, this.y, this.width, this.height);
217+
this.drawParts_outline();
218+
this.drawParts_patterns();
219+
if (this.doTranslate) {
220+
ctx.translate(this.x, this.y);
221+
}
197222
}
198223
}
199-
};
200-
Rectangle.prototype.drawParts_outline = function () {
201-
switch (this.outlinePosition.toLowerCase()) {
202-
case "inner":
203-
ctx.strokeRect(this.x + this.outlineWidth / 2, this.y + this.outlineWidth / 2, this.width - this.outlineWidth, this.height - this.outlineWidth);
204-
break;
205-
case "center":
206-
ctx.strokeRect(this.x, this.y, this.width, this.height);
207-
break;
208-
case "outer":
209-
ctx.strokeRect(this.x - this.outlineWidth / 2, this.y - this.outlineWidth / 2, this.width + this.outlineWidth, this.height + this.outlineWidth);
210-
break;
224+
225+
drawParts_outline() {
226+
switch (this.outlinePosition.toLowerCase()) {
227+
case "inner":
228+
ctx.strokeRect(this.x + this.outlineWidth / 2, this.y + this.outlineWidth / 2, this.width - this.outlineWidth, this.height - this.outlineWidth);
229+
break;
230+
case "center":
231+
ctx.strokeRect(this.x, this.y, this.width, this.height);
232+
break;
233+
case "outer":
234+
ctx.strokeRect(this.x - this.outlineWidth / 2, this.y - this.outlineWidth / 2, this.width + this.outlineWidth, this.height + this.outlineWidth);
235+
break;
236+
}
211237
}
212-
};
213-
Rectangle.prototype.drawParts_patterns = function () {
214-
switch (this.doPattern.toLowerCase()) {
215-
case "diagonals":
216-
ctx.beginPath();
217-
var x1 = this.x;
218-
var y1 = this.y;
219-
var x2 = this.x;
220-
var y2 = this.y;
221-
var increment = 6;
222-
var count = 0;
223-
while (!(x1 == this.x + this.width)) {
224-
x1++;
225-
y2++;
226-
count++;
227-
if (count == increment) {
228-
count = 0;
229-
ctx.moveTo(x1, y1);
230-
ctx.lineTo(x2, y2);
238+
239+
drawParts_patterns() {
240+
switch (this.doPattern.toLowerCase()) {
241+
case "diagonals":
242+
ctx.beginPath();
243+
var x1 = this.x;
244+
var y1 = this.y;
245+
var x2 = this.x;
246+
var y2 = this.y;
247+
var increment = 6;
248+
var count = 0;
249+
while (!(x1 == this.x + this.width)) {
250+
x1++;
251+
y2++;
252+
count++;
253+
if (count == increment) {
254+
count = 0;
255+
ctx.moveTo(x1, y1);
256+
ctx.lineTo(x2, y2);
257+
}
231258
}
232-
}
233-
while (!(y2 == this.y + this.height)) {
234-
y1++;
235-
y2++;
236-
count++;
237-
if (count == increment) {
238-
count = 0;
239-
ctx.moveTo(x1, y1);
240-
ctx.lineTo(x2, y2);
259+
while (!(y2 == this.y + this.height)) {
260+
y1++;
261+
y2++;
262+
count++;
263+
if (count == increment) {
264+
count = 0;
265+
ctx.moveTo(x1, y1);
266+
ctx.lineTo(x2, y2);
267+
}
241268
}
242-
}
243-
while (!(y1 == this.y + this.height)) {
244-
y1++;
245-
x2++;
246-
count++;
247-
if (count == increment) {
248-
count = 0;
249-
ctx.moveTo(x1, y1);
250-
ctx.lineTo(x2, y2);
269+
while (!(y1 == this.y + this.height)) {
270+
y1++;
271+
x2++;
272+
count++;
273+
if (count == increment) {
274+
count = 0;
275+
ctx.moveTo(x1, y1);
276+
ctx.lineTo(x2, y2);
277+
}
251278
}
252-
}
253-
ctx.stroke();
254-
this.x = this.initX;
255-
this.y = this.initY;
256-
break;
257-
} //
258-
};
279+
ctx.stroke();
280+
this.x = this.initX;
281+
this.y = this.initY;
282+
break;
283+
} //
284+
};
285+
}
259286
/*--------------------------------------Function-19-----------------------------------------------------------------*/
260287
function Circle(x: number, y: number, radius: number) {
261288
Control.call(this, x, y); //Inherit from Control

src/Monopoly/Classes.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Rectangle } from "../JSTools";
33
class Place extends Rectangle {
44
type: string;
55
price: number;
6-
constructor(x, y, width, height, type) {
6+
constructor(x: number, y: number, width: number, height: number, type: string) {
77
super(x, y, width, height); //Inherit from Rectangle
88
this.type = type;
99
this.price = 20;
@@ -14,12 +14,19 @@ class JSWindow extends Rectangle {
1414
name: string;
1515
color: string;
1616
visible: boolean;
17-
constructor(x, y, width, height, name: string) {
17+
constructor(x: number, y: number, width: number, height: number, name: string) {
1818
super(x, y, width, height); //Inherit from Rectangle
1919
this.name = name;
2020
this.color = 'grey';
2121
this.visible = false;
2222
}
23+
24+
show() {
25+
this.y = -this.height - this.outlineWidth*2;
26+
for (let i = 0; i < (this.initY - this.y) * 100; i+=1) {
27+
this.y += 1/100;
28+
}
29+
}
2330
}
2431

2532
export { Place, JSWindow };

0 commit comments

Comments
 (0)