[2022 하계 모각코] 3회차 회고

2022. 7. 17. 23:58·기타/모각코
728x90
반응형

목표

canvas api에 대한 지식을 얼추 얻었기 때문에

 

vertex와 edge에 대한 구조를 설계할 예정입니다.

 

회고

vanila.js 만으로는 설계의 한계가 있다고 판단했기 때문에 TypeScript를 활용하여 설계했습니다.

 

시간이 모자라 우선 Vertex만 구성한 후, 테스트 해봤습니다.

 

Vertex.ts

export class Vertex {
  name: string;
  x: number;
  y: number;
  radius: number;
  ctx: CanvasRenderingContext2D;
  targetVertex: Vertex[];

  constructor(ctx: CanvasRenderingContext2D, name: string) {
    this.name = name;
    this.x = this.y = 0;
    this.radius = 50;
    this.ctx = ctx;
    this.targetVertex = [];
  }

  draw() {
    this.ctx.strokeStyle = "black";
    this.ctx.lineWidth = 3;
    this.ctx.beginPath();
    this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
    this.ctx.stroke();
    this.ctx.font = "24px inferit";
    const textMeasure = this.ctx.measureText(this.name);
    const textWidth = textMeasure.width;
    this.ctx.fillText(
      this.name,
      this.x - textWidth / 2,
      this.y + textMeasure.fontBoundingBoxAscent / 2.5
    );
  }

  addTargetVertex(newVertex: Vertex) {
    this.targetVertex.push(newVertex);
  }

  // 그릴 공간에 다른 vertex가 있는지 검사 후 호출.
  move(newX: number, newY: number) {
    this.x = newX;
    this.y = newY;
    this.draw();
  }
}

App.ts

import { Vertex } from "./component/Vertex.js";

class App {
  canvas: HTMLCanvasElement;
  ctx: CanvasRenderingContext2D;
  stageWidth: number;
  stageHeight: number;
  num: number;
  vertexList: Vertex[];

  constructor() {
    // canvas 요소 생성
    this.canvas = document.createElement("canvas");
    // canvas 요소로부터 2d context 얻기
    this.ctx = this.canvas.getContext("2d")!;

    this.num = 0;

    // html에 생성한 canvas를 넣기
    document.body.appendChild(this.canvas);

    this.stageWidth = document.body.clientWidth;
    this.stageHeight = document.body.clientHeight;

    this.vertexList = [];

    // 화면 사이즈 설정
    window.addEventListener("resize", this.resize.bind(this), false);
    this.resize();
  }

  resize() {
    // 레티나 디스플레이에서 선명하게 보이기 위해 canvas의 크기를 body의 두배씩으로 잡는다.
    this.stageWidth = document.body.clientWidth;
    this.stageHeight = document.body.clientHeight;

    this.canvas.width = this.stageWidth * 2;
    this.canvas.height = this.stageHeight * 2;
    this.ctx.scale(2, 2);
  }
}

window.onload = () => {
  const app = new App();
};

이번주에는 입력 부분과 에지를 직접 그려보는 부분을 공부하고 적용해볼 생각입니다.

'기타 > 모각코' 카테고리의 다른 글

[2022 하계 모각코] 4회차 결과  (0) 2022.07.25
[2022 하계 모각코] 4회차 목표  (0) 2022.07.24
[2022 하계 모각코] 3회차 목표  (0) 2022.07.17
[2022 하계 모각코] 2회차 회고  (0) 2022.07.10
[2022 하계 모각코] 2회차 목표  (0) 2022.07.10
'기타/모각코' 카테고리의 다른 글
  • [2022 하계 모각코] 4회차 결과
  • [2022 하계 모각코] 4회차 목표
  • [2022 하계 모각코] 3회차 목표
  • [2022 하계 모각코] 2회차 회고
uinone
uinone
노는 게 제일 좋아😉
  • uinone
    ideaDummy
    uinone
  • 전체
    오늘
    어제
    • 분류 전체보기
      • CS
        • 확률과 통계
        • 자료구조
        • 논리회로
        • OS
        • 데이터 통신
        • 데이터 과학
        • 컴파일러
      • 알고리즘
        • 그리디
      • 컴퓨터 비전
      • 안드로이드
      • Web
        • CSS
        • TypeScript
        • React.js
      • 기타
        • 모각코
        • 메모장
        • 오류해결
        • 풍미박산 기절초풍 설치과정
      • DL
      • ML
      • 언어
        • C
        • Ocaml
      • Tensorflow
      • 8기 글로벌 SW*AI인재 프로그램
      • 논문 정리
        • 3D Object Detection
        • 3D Multi Object Tracking
      • CUDA
      • Dataset
        • NuScenes
  • 블로그 메뉴

    • LinkedIn
    • Github
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    NetworkFlow
    우선순위 큐
    백준
    정렬
    그리디 알고리즘
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
uinone
[2022 하계 모각코] 3회차 회고
상단으로

티스토리툴바