Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Kinda sorta made it work
Browse files Browse the repository at this point in the history
  • Loading branch information
Butt4cak3 committed Feb 23, 2018
1 parent f6331ae commit 9753d44
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[.editorconfig]
trim_trailing_whitespace = true
insert_final_newline = true

[*.{html,css,ts,json}]
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js
*.js.map
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
<title>Simulation</title>
<script src="js/vector.js"></script>
<script src="js/scene.js"></script>
<script src="js/shapes.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
27 changes: 27 additions & 0 deletions js/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
document.addEventListener("DOMContentLoaded", () => {
const Circle = Shapes.Circle;

const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const scene = new Scene.Scene(canvas);

scene.setCanvasSize(600, 600);
scene.setViewport(0, 0, 10);

const balls: Shapes.Circle[] = [
new Circle(2, 2, 1, '#00FF00'),
new Circle(8, 8, 1, '#0000FF'),
];

const frame = function frame() {
scene.getContext().clearRect(0, 0, canvas.width, canvas.height);

for (let ball of balls) {
ball.draw(scene);
}

window.requestAnimationFrame(frame);
};

window.requestAnimationFrame(frame);
});

67 changes: 67 additions & 0 deletions js/scene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Scene {
export interface Viewport {
x: number;
y: number;
w: number;
h: number;
scale: number;
}

export interface Point {
x: number;
y: number;
}

export class Scene {
public canvas: HTMLCanvasElement;
private ctx: CanvasRenderingContext2D;
public viewport: Viewport;
private aspectRatio: number;

constructor(canvas: HTMLCanvasElement) {
this.canvas = canvas;
let ctx = canvas.getContext("2d");
if (ctx) {
this.ctx = ctx;
}
}

public setCanvasSize(width: number, height: number) {
this.canvas.width = width;
this.canvas.height = height;
this.aspectRatio = this.canvas.width / this.canvas.height;
}

public setViewport(x: number, y: number, width: number) {
let scale = this.canvas.width / width;
let height = width / this.aspectRatio;

this.viewport = {
x,
y,
w: width,
h: height,
scale: scale
};
}

public toCanvasSpace(len: number): number;
public toCanvasSpace(vec: Vector): Point;
public toCanvasSpace(vec: any) {
if (vec instanceof Vector) {
const { x, y, scale } = this.viewport;

return {
x: (vec.x - x) * scale,
y: (vec.y - y) * scale
};
} else if (typeof vec === "number") {
return vec * this.viewport.scale;
}
}

public getContext(): CanvasRenderingContext2D {
return this.ctx;
}
}
}
35 changes: 35 additions & 0 deletions js/shapes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Shapes {
type Point = Scene.Point;

export abstract class Shape {
public pos: Vector;
public color: string;

constructor(x: number, y: number) {
this.pos = new Vector(x, y);
}

public abstract draw(scene: Scene.Scene): void;
}

export class Circle extends Shape {
public radius: number;

constructor(x: number, y: number, radius: number, color: string) {
super(x, y);
this.radius = radius;
this.color = color;
}

public draw(scene: Scene.Scene): void {
const { x, y } = scene.toCanvasSpace(this.pos);
const radius = scene.toCanvasSpace(this.radius);
const ctx = scene.getContext();

ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
}
}
}
45 changes: 45 additions & 0 deletions js/vector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Vector {
public x: number;
public y: number;

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}

public copy(): Vector {
return new Vector(this.x, this.y);
}

public add(x: number, y: number): Vector;
public add(vec: Vector, y: undefined): Vector;
public add(x: any, y: any) {
if (x instanceof Vector) {
this.x += x.x;
this.y += y.y;
} else {
this.x += x;
this.y += y;
}
return this;
}

public sub(x: number, y: number): Vector;
public sub(vec: Vector, y: undefined): Vector;
public sub(x: any, y: any) {
if (x instanceof Vector) {
this.x -= x.x;
this.y -= y.y;
} else {
this.x -= x;
this.y -= y;
}
return this;
}

public mult(scale: number): Vector {
this.x *= scale;
this.y *= scale;
return this;
}
}
58 changes: 58 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation: */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": [
"js/**/*.ts"
]
}

0 comments on commit 9753d44

Please sign in to comment.