Skip to content

Commit

Permalink
doc: add README
Browse files Browse the repository at this point in the history
  • Loading branch information
went2 committed Jul 10, 2023
1 parent 7717976 commit 59a338d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Description

A pixel editor allows you to edit pixel icon and export.

The original idea and code structure is bought from [Eloquent JavaScript Chapter 19](https://eloquentjavascript.net/19_paint.html).

I converted the code into TypeScript and expanded somehow in order to explore how to draw pixel in HTML canvas and how to manage states in vanilla way.

It turns out the way to manage states in vanilla JavaScript(TypeScript) is quite "Reduxful" in which component dispatches actions, and reducer function caculates the latest state and notifies every component to sync the state(by calling component's syncState()). Component decides whether and how to update its own state when got notifies.

The core elements of the way consists of:

1. A state object of the whole App
2. A dispatch function passed down to each component to instruct it dispatch actions instead of handle events by itself.
3. A reducer function(can be splitted into several) to caculate and return the newest state object based on the current object and action object.
4. App component notifys every component to sync their state each time the reducer returns the newest state.
4 changes: 2 additions & 2 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { draw, fill, rectangle, pick } from "./utils/drawHelpers";

import { EditorState, ActionObj } from "./types";
import Picture from "./models/picture";
import { historyUpdateState } from "./models/reducer";
import { reducer } from "./models/reducer";

const initialState: EditorState = {
currentTool: "draw",
Expand Down Expand Up @@ -44,7 +44,7 @@ function startPixelEditor({
tools,
controls,
dispatch(action: ActionObj) {
state = historyUpdateState(state, action);
state = reducer(state, action);
app.syncState(state);
},
});
Expand Down
5 changes: 4 additions & 1 deletion src/models/Picture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// model for a frame of canvas
// Model for a frame of canvas
// The width is logical, eg. 16 X 16, not actual width of canvas element, which
// in app is hard-coded 384px
// When export canvas to image, the width should be used as actual width of the exported image.
class Picture {
public width: number;
public height: number;
Expand Down
2 changes: 1 addition & 1 deletion src/models/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EditorState, ActionObj } from "../types";
import Picture from "./picture";

export function historyUpdateState(state: EditorState, action: ActionObj) {
export function reducer(state: EditorState, action: ActionObj) {
if (action.type == "undo") {
if (state.done.length == 0) return state;
return Object.assign({}, state, {
Expand Down

0 comments on commit 59a338d

Please sign in to comment.