9.22.2020

HTML Canvas Practice

 

HTML Canvas Practice


Recently I learned how to use html canvas tag. After reading a bit of MDN, I built a simple position detect page. User can draw a line by clicking and when user click the start point, the line turns into red line and user no longer can draw line until user click the reset button.
Below is the JavaScript code for this small project and preview of the project.





const canvas = document.querySelector("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext("2d");

const resetButton = document.querySelector("button");

ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);

let coordinates = [];
let line = new Path2D();
ctx.lineWidth = 5;
ctx.strokeStyle = "white";


const appendNewLine = (x, y) => {
  if (coordinates.length <= 0) {
    return line.moveTo(x, y);
  }
  const {x:prevX, y:prevY} = coordinates[coordinates.length - 1];
  const newLine = new Path2D();
  newLine.moveTo(prevX, prevY);
  newLine.lineTo(x, y);
  line.addPath(newLine);
  ctx.stroke(line);
};

const addDot = (x, y) => {
  const newDot = new Path2D();
  newDot.arc(x, y, 5, 0, Math.PI * 2, true);
  ctx.fillStyle = "white";
  ctx.fill(newDot);
  appendNewLine(x, y);
  coordinates.push({
    x,
    y,
  });
}

const isReturnToStart = (x, y) => {
  if (coordinates.length > 0) {
    const {x:startX, y:startY} = coordinates[0];
    if (x <= startX + 5 && x >= startX - 5) {
      if (y <= startY + 5 && y >= startY - 5) return true;
    }
  }
  return false;
}

const drawLine = (e) => {
  if (isReturnToStart(e.offsetX, e.offsetY)) {
    appendNewLine(coordinates[0].x, coordinates[0].y);
    ctx.strokeStyle = "red";
    ctx.stroke(line);
    console.log(coordinates);
    return e.target.removeEventListener("click", drawLine);
  }
  addDot(e.offsetX, e.offsetY);
}

const resetCanvas = () => {
  ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
  ctx.fillStyle = "#000000";
  ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
  line = new Path2D();
  ctx.lineWidth = 5;
  ctx.strokeStyle = "white";
  coordinates = [];
  canvas.addEventListener("click", drawLine);  
}

resetButton.addEventListener("click", resetCanvas);

canvas.addEventListener("click", drawLine);


You can see a Demo in here :D

Share:

9.17.2020

Toy project with React + TypeScript + Firebase - LinkBook

 


LinkBook



User Experience


  • User can login or sign up with email and password.
  • User can login via Google or Github account.
  • User can create, read, update, delete links.
  • User can assign title and related tags(keywords) when creating or updating link items.
  • User can sort links by created(or updated)time by clicking arrow button.
  • User can filter their links by a tag keyword.

Preview





P.S.


  • Skills that I used
    • React (create-react-app)
    • TypeScript
    • Firebase
    • gh-pages
  • If you find any bug on this website, please let me know.
  • Contact Info
    • swimmingkiim@gmail.com
Share:

9.11.2020

How to start your React Native project


 


What is React Native?


React Native is an open-source mobile application framework created by Facebook, Inc. It is used to develop applications for Android, IOS, Web and UWP by enabling developers to use React along with native platform capabilities. - wikipedia

First, you need these on your labtop


  • node(higher than v.12)
  • npm(higher than v.6)
  • If you want to run a simulator on your labtop,
    • Xcode(if you're using Mac)
    • Android studio(If you're using Windows or Linux)
  • An Iphone or android phone(you need install Expo application, I recommend this method)
  • npm install -g expo-cli

Two ways to make React Native project


  1. Expo
    • It's like a create-react-app for React Native version. Basic environment settings come along with itself.
    • PROS
      • convenient
      • no need to do all those complex settings
      • you can text on Iphone(If you have the actual Iphone) without buying a Mac.
    • CONS
      • It's hard to use files and packages made with native languages. (you have to eject if you want to use them)
  2. React Native CLI
    • If you use a React Native CLI method, you can customize your project setting.
    • PROS
      • You can use packages that using native files.
      • It's pretty much up to you, so you can optimize your environment settings as you like.
    • CONS
      • It is very very complicate to setup things right.
      • If you don't have Mac, it is difficult to run your app in an IOS environment.

P.S. If your app don't need to use native files, I recommend you to use Expo over React Native CLI. It will save so much of your time.

Make a project file using Expo


  1. First, you need to sign up on an Expo website.
  2. Then, open a terminal, go to your directory that you want to create a Expo project.
  3. Type expo init [new project name] into your terminal.
  4. Expo will require you to select things such as "choose between blank or minimal or TypeScript version etc...", so make your choice.
  5. Now, It depends on what device you want to run a test.
    • IOS DEVICE
      • Go into your project directory by cd [new project name].
      • expo login and type your username and password.
      • Then, npm start or expo start .
      • Get your IOS device and go to expo application and login.
      • If your IOS device and your laptop are using the same wifi, then your new project name will come up to your IOS device.
    • ANDROID DEVICE
      • Go to your project directory like IOS DEVICE did.
      • Type npm start or expo start into your terminal.
      • Then, your expo web page will be automatically open on your default browser.
      • On the left-bottom, there will be a QR code.
      • Get your ANDROID DEVICE and go to expo application, login, and finally scan the QR code with your device.
    • SIMULATOR
      • Like ANDROID DEVICE, just type npm start or expo start.
      • Then, it will open a browser, you can find button for SIMULATOR above the QR code.
      • Like I said at first, you need to install Xcode or Android studio and make sure that your simulator is working before you do the expo.


If there's any wrong information, please let me know : D I will fix it.
Share:

9.10.2020

TypeScript 기본 개념 정리




아래 정리는 노마드코더의 TypeScript 강의를 듣고 복습으로 작성한 것입니다.

TypeScript란


  • TypeScript는 JavaScript를 기초로 해서 만들어졌다.
  • 말랑한 언어인 JavaScript에 엄격하게 타입을 구분해서 사용하는 것이 TypeScript이다.
  • TypeScript는 컴파일 되어서 JavaScript로 사용된다.
  • 사소한 실수를 하더라도 TypeScript는 컴파일 과정에서 걸러지기 때문에 버그를 줄일 수 있다.

설치


  • npm install typescript --save-dev (개별 프로젝트에 설치시)
  • npm install g typescript (컴퓨터 전체에서 사용시)

tsconfig.json


  • TypeScript가 어떻게 JavaScript로 변환될지 옵션을 설정해주는 파일이다.
  • 기본 예시


{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES2015",
    "sourceMap": true,
    "outDir": "dist"
  },
  "include": ["index.ts"],
  "exclude": ["node_modules"]
}


tsc



  • tsc 를 터미널에 입력 후 실행하면 tsconfig.json의 설정에 따라 TypeScript 파일이 JavaScript 파일로 바뀐다.
  • package.json에서 start 명령어를 node (실행할 파일경로) , prestart 명령어를 tsc 로 해놓으면 다음과 같은 순서로 실행된다.
    1. tsc 를 실행하여 JavaScript 파일 생성
    2. node 명령어로 생성된 파일 실행

Types



P.S. VS Code를 사용한다면 TS Lint 익스텐션을 설치할 것. TypeScript Error를 나타내준다.
P.S. tsc-watch 모듈을 dev모드로 설치 후, package.json의 script에서 "start": "tsc-watch --onSuccess (백슬래쉬)" node (파일경로) (백슬래쉬)" 로 지정해주면 TypeScript 파일이 업데이트 될 때마다 자동으로 tsc 후 node 명령어를 실행시켜준다.

아래는 타입을 어떻게 지정하는지 보여주는 예시 코드이다.
const sayHi = (name:string, age:number):string => "someValue";

Interface


  • interface 로 선언한 타입은 TypeScript의 타입 지정할 때 사용될 수 있다.

interface Human {
  name: string;
  age:number;
  gender: string;
}

const person = {
  name: "me",
  age: 100,
  gender: "female"
}

const sampleFunction = (person:Human) : string => "someValue";

Class


  • interface 는 JavaScript 코드로 컴파일 되지 않는다.
  • 그런데 경우에 따라서는 이와 같은 구조가 JavaScript 코드로 컴파일 되어야 할 수 있다.
  • 그럴 때는 class 를 사용한다.

class Human {
  public name: string;
  public age: number;
  public gender: string;
  constructor(name: string, age: number, gender?: string) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  }
}

const me: Human = new Human("me", 100, "female");

P.S. 파일 끝에 export {}; 코드를 넣어주어야 TypeScript Error가 나지 않는다.

P.S.
변수명 뒤에 ? 를 붙여주면 함수를 호출할 때 선택적으로 기입할 수 있게 된다.
P.S. const me:Human[] 라고 적어주면 me라는 변수는 Human이란 타입 요소들의 array가 된다.
Share:

I made a new portfolio website with React

thumbnail


I made new portfolio website


About a while ago, I built a personal portfolio website. I already had one, but it was just not look nice to me. SO, I decided to build another one. Again, I kept carried on with the concept of "Swimming Pool". But this time I worked differently.

  • First, I built chunks of vanilla JavaScript code on repl.it, which later became the React components of this project.
    • This process helped me a lot. I learned so much from making thing with vanilla JS.
  • Then, when I was working on React project, I divided the previous code into components , templates (layout of the each section), and pages (combines components and templates).
    • First, I thought this structuring is annoying, but it really did its job when I was refactoring this project. This big structure made me easy to find where I need to fix.


I Just LOVE web page that has its own concept


Yes I do. So I spent a lot of time building sea wave, bubbles and swimming rainbow fish. It can be look like little bit old fashion to someone, but whatever, I love it and that's it : D

here is the preview of my portfolio website.





What I've learned from this project



  1. how to convert vanilla JavaScript code(mostly DOM) to React states and hooks etc.
    • and realize how difficult that can be
  2. how to make CSS animation and JavaScript animation
  3. learned how to use svg tag(I used this skill to make a sea wave)
  4. learned how to use color palette from pinterest properly(since I'm suck with colors)
  5. A importance of mobile view
    • learned that I need to test a lot with different device width, and adjust font-size and ratio etc.


Share: