-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add canvas component & renderer
- Loading branch information
1 parent
3667b4d
commit f7b42b5
Showing
10 changed files
with
145 additions
and
15 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,5 +1,12 @@ | ||
import "@/style/main.less"; | ||
import { Panel } from "@/ui/panel/panel"; | ||
import { Canvas } from "@/ui/canvas/canvas"; | ||
import { Render } from "@/render/render"; | ||
|
||
const root = document.getElementById("root"); | ||
new Panel(root); | ||
|
||
const canvas = new Canvas(root); | ||
const render = new Render(canvas); | ||
|
||
const panel = new Panel(root); | ||
panel.linkRenderer(render); |
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,24 @@ | ||
import { Disposable } from "@/common/lifecycle"; | ||
import { Canvas } from "@/ui/canvas/canvas"; | ||
|
||
export class Render extends Disposable { | ||
private _ctx: CanvasRenderingContext2D; | ||
|
||
public constructor(private _canvas: Canvas) { | ||
super(); | ||
|
||
this._ctx = this._canvas.ctx; | ||
this._init(); | ||
} | ||
|
||
private _init() { | ||
this._ctx.fillStyle = "#dddddd"; | ||
this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height); | ||
this._ctx.fill(); | ||
} | ||
|
||
/** @todo */ | ||
public refresh() { | ||
console.log("Refreshed."); | ||
} | ||
} |
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,3 @@ | ||
// canvas.motive-canvas { | ||
|
||
// } |
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,15 @@ | ||
import { Canvas } from "./canvas"; | ||
|
||
describe("canvas-component-tests", () => { | ||
const canvas = new Canvas(document.body); | ||
|
||
it("canvas-properties", () => { | ||
expect(canvas.element.classList.contains("motive-canvas")).toBeTruthy(); | ||
}); | ||
|
||
it("canvas-dispose", () => { | ||
canvas.dispose(); | ||
|
||
expect(canvas.element).toBeNull(); | ||
}); | ||
}); |
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,53 @@ | ||
import { Component, ComponentLike, IComponent } from "@/ui/ui"; | ||
|
||
import "./canvas.less"; | ||
|
||
export interface CanvasOptions { | ||
width?: number | ||
height?: number | ||
} | ||
|
||
const defaultOptions: CanvasOptions = { | ||
width: window.innerWidth, | ||
height: window.innerHeight | ||
}; | ||
|
||
interface ICanvas extends IComponent { | ||
width: number | ||
height: number | ||
ctx: CanvasRenderingContext2D | ||
} | ||
|
||
export class Canvas extends Component<HTMLCanvasElement, CanvasOptions> implements ICanvas { | ||
private _ctx: CanvasRenderingContext2D; | ||
|
||
public constructor(target: ComponentLike, _options?: CanvasOptions) { | ||
super( | ||
<canvas className="motive-canvas"/>, | ||
target, | ||
defaultOptions, | ||
_options | ||
); | ||
|
||
if(this._options.width) this._element.width = this._options.width; | ||
if(this._options.height) this._element.height = this._options.height; | ||
|
||
this._ctx = this._element.getContext("2d"); | ||
|
||
if(!this._ctx) { | ||
throw new Error("Unable to get the 2D context from canvas."); | ||
} | ||
} | ||
|
||
public get width() { | ||
return this._element.width; | ||
} | ||
|
||
public get height() { | ||
return this._element.height; | ||
} | ||
|
||
public get ctx() { | ||
return this._ctx; | ||
} | ||
} |
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