-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add renderer option, abstract classes and refactor code
- Loading branch information
1 parent
9c6bb50
commit c26573b
Showing
12 changed files
with
185 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { Engine, createCanvas } from '../../lib'; | ||
import { Engine, createCanvas, Point } from '../../lib'; | ||
|
||
const { canvas } = createCanvas(); | ||
const engine = new Engine(canvas); | ||
|
||
const renderer = engine.renderer; | ||
|
||
renderer.drawPoint({ x: 100, y: 100 }); | ||
for (let i = 0; i < 100; i++) { | ||
renderer.addObject( | ||
new Point({ x: Math.random() * 400, y: Math.random() * 400 }), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { v4 } from 'uuid'; | ||
import { DrawableObjectOptions, BaseObjectInterface } from '../../types'; | ||
import { Renderer } from '../renderer'; | ||
|
||
/** | ||
* The Base Object Class | ||
*/ | ||
export abstract class BaseObject<T, K> implements BaseObjectInterface { | ||
id: string; | ||
options: T & DrawableObjectOptions; | ||
context: CanvasRenderingContext2D; | ||
constructor(options: DrawableObjectOptions<T>, renderer?: Renderer) { | ||
this.options = options; | ||
// Either the specifically given render or the global renderer | ||
this.context = renderer?.context || window.MiniPointDefaultRenderer.context; | ||
this.id = v4(); | ||
this.checkDrawConditionAndDraw(); | ||
} | ||
|
||
abstract draw(): K; | ||
|
||
checkDrawConditionAndDraw() { | ||
if (this.options.show) { | ||
this.draw(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './base-object'; | ||
export * from './point'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PointOptions } from '../../types'; | ||
import { DefaultPointOptions } from '../../constants'; | ||
import { BaseObject } from './base-object'; | ||
import { Renderer } from '../renderer'; | ||
/** | ||
* The Point Class | ||
*/ | ||
export class Point extends BaseObject<PointOptions, Point> { | ||
constructor( | ||
options: PointOptions = DefaultPointOptions, | ||
renderer?: Renderer, | ||
) { | ||
options = { ...DefaultPointOptions, ...options }; | ||
super(options, renderer); | ||
} | ||
|
||
draw() { | ||
if (!this.options.show) return this; | ||
this.context.beginPath(); | ||
this.context.arc( | ||
this.options.x, | ||
this.options.y, | ||
this.options.radius!, | ||
0, | ||
Math.PI * 2, | ||
); | ||
|
||
this.context.fill(); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import { PointOptions, RendererInterface } from '../types'; | ||
import { forEach } from 'lodash'; | ||
import { DefaultPointOptions } from '../constants'; | ||
import { BaseRenderer, PointOptions } from '../types'; | ||
import { Point } from './objects'; | ||
/** | ||
* The class that is responsible for drawing | ||
*/ | ||
export class Renderer implements RendererInterface { | ||
context: CanvasRenderingContext2D; | ||
export class Renderer extends BaseRenderer { | ||
constructor(context: CanvasRenderingContext2D) { | ||
this.context = context; | ||
super(context); | ||
} | ||
|
||
drawPoint(options?: PointOptions) { | ||
options = { | ||
...DefaultPointOptions, | ||
...options, | ||
}; | ||
render() { | ||
forEach(this.objects, (object) => { | ||
object.checkDrawConditionAndDraw(); | ||
}); | ||
} | ||
|
||
this.context.beginPath(); | ||
// @ts-ignore | ||
this.context.arc(options.x, options.y, options.radius, 0, Math.PI * 2); | ||
this.context.fill(); | ||
point(options: PointOptions = DefaultPointOptions) { | ||
const point = new Point(options, this); | ||
this.objects.push(point); | ||
return point; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
export interface RendererInterface {} | ||
declare global { | ||
interface Window { | ||
MiniPointDefaultRenderer: Renderer; | ||
MiniPointDefaultEngine: Engine; | ||
} | ||
} | ||
|
||
import { Engine } from '../render'; | ||
import { Renderer } from '../render/renderer'; | ||
import { BaseObjectInterface } from './shapes'; | ||
|
||
export abstract class BaseRenderer { | ||
context: CanvasRenderingContext2D; | ||
objects: BaseObjectInterface[]; | ||
|
||
constructor(context: CanvasRenderingContext2D) { | ||
this.context = context; | ||
this.objects = []; | ||
} | ||
abstract render(): void; | ||
|
||
addObject<T>(object: T): T { | ||
this.objects.push(object as BaseObjectInterface<T>); | ||
return object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
export type PointOptions = { | ||
export interface BaseObjectInterface<T = unknown> { | ||
// draw: () => T; | ||
checkDrawConditionAndDraw: () => T; | ||
/** | ||
* A Unique Id that will help us identify each object in the Engine | ||
*/ | ||
id?: string; | ||
} | ||
|
||
export type DrawableObjectOptions<T = unknown> = T & { | ||
/** | ||
* Weather to show the object or not | ||
*/ | ||
show?: boolean; | ||
}; | ||
|
||
export type PointOptions = DrawableObjectOptions<{ | ||
x: number; | ||
y: number; | ||
radius?: number; | ||
}; | ||
}>; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters