-
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.
- Loading branch information
Showing
9 changed files
with
223 additions
and
180 deletions.
There are no files selected for viewing
This file was deleted.
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
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,42 @@ | ||
// model for a frame of canvas | ||
class Picture { | ||
public width: number; | ||
public height: number; | ||
public pixels: string[]; // array to store colors | ||
|
||
constructor(width: number, height: number, pixels: string[]) { | ||
this.width = width; | ||
this.height = height; | ||
this.pixels = pixels; | ||
} | ||
public static empty(width: number, height: number, color: string) { | ||
const pixels = new Array(width * height).fill(color); | ||
return new Picture(width, height, pixels); | ||
} | ||
|
||
public getPixel(x: number, y: number) { | ||
return this.pixels[x + y * this.width]; | ||
} | ||
|
||
public draw(pixels: Pixel[]) { | ||
const copy = this.pixels.slice(); | ||
for (const { x, y, color } of pixels) { | ||
copy[x + y * this.width] = color; | ||
} | ||
return new Picture(this.width, this.height, copy); | ||
} | ||
} | ||
|
||
// store coordinates of color | ||
export class Pixel { | ||
public x: number; | ||
public y: number; | ||
public color: string; // like "#000000" | ||
constructor(x: number, y: number, color: string) { | ||
this.x = x; | ||
this.y = y; | ||
this.color = color; | ||
} | ||
} | ||
|
||
export default Picture; |
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 { EditorState } from "../types"; | ||
import Picture from "./Picture"; | ||
|
||
// undo state reducer | ||
export function historyUpdateState( | ||
state: EditorState, | ||
action: { undo: boolean; picture: Picture } | ||
) { | ||
if (action.undo == true) { | ||
if (state.done.length == 0) return state; | ||
return Object.assign({}, state, { | ||
picture: state.done[0], | ||
done: state.done.slice(1), | ||
doneAt: 0, | ||
}); | ||
} else if (action.picture && state.doneAt < Date.now() - 1000) { | ||
return Object.assign({}, state, action, { | ||
done: [state.picture, ...state.done], | ||
doneAt: Date.now(), | ||
}); | ||
} else { | ||
return Object.assign({}, state, action); | ||
} | ||
} |
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
Oops, something went wrong.