[Canvas] 보일러 플레이트 작성 (캔버스 클래스)

2024. 5. 24. 15:23·Canvas
728x90

index.js

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./style.css">
  <title>Document</title>
</head>
<body>
  <canvas></canvas>
</body>
<script type="module" src="./index.js"></script>
</html>

 

CanvasOption.js

export default class canvasOption {
  constructor() {
    this.canvas = document.querySelector('canvas')
    this.ctx = this.canvas.getContext('2d')
    this.dpr = window.devicePixelRatio
    this.fps = 60
    this.interval = 1000 / this.fps
    this.canvasWidth = innerWidth
    this.canvasHeight = innerHeight
  }
}

 

728x90

 

index.js

import canvasOption from "./CanvasOption.js"

class Canvas extends canvasOption {
  constructor() {
    super()
  }

  init() {
    this.canvasWidth = innerWidth
    this.canvasHeight = innerHeight
    this.canvas.width = this.canvasWidth * this.dpr
    this.canvas.height = this.canvasHeight * this.dpr
    this.ctx.scale(this.dpr, this.dpr)

    this.canvas.style.width = this.canvasWidth + 'px'
    this.canvas.style.height = this.canvasHeight + 'px'
  }

  render() {
    let now, delta
    let then = Date.now()

    const frame = () => {
      requestAnimationFrame(frame)
      now = Date.now()
      delta = now - then
      if (delta < this.interval) return

      // code write here

      then = now - (delta % this.interval)
    }
    requestAnimationFrame(frame)
  }
}

const canvas = new Canvas()

window.addEventListener('load', () => {
  canvas.init()
  canvas.render()
})

window.addEventListener('resize', () => {
  canvas.init()
})

 

style.css

html, body { 
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
728x90

'Canvas' 카테고리의 다른 글

[Canvas] 불꽃놀이 만들기  (1) 2024.06.10
[Canvas] 캔버스 resize  (0) 2024.05.22
[Canvas] dat.GUI 사용하기  (0) 2024.05.22
[Canvas] svg 필터 입히기(feGaussianBlur, feColorMatrix) | Gooey Effect  (0) 2024.05.21
[Canvas] 파티클 그리기 | 애니메이션 추가(requestAnimationFrame)  (1) 2024.05.20
'Canvas' 카테고리의 다른 글
  • [Canvas] 불꽃놀이 만들기
  • [Canvas] 캔버스 resize
  • [Canvas] dat.GUI 사용하기
  • [Canvas] svg 필터 입히기(feGaussianBlur, feColorMatrix) | Gooey Effect
gamzaggang7
gamzaggang7
    250x250
  • gamzaggang7
    abcdefghklpqrstuvwxyz
    gamzaggang7
  • 전체
    오늘
    어제
    • 분류 전체보기
      • CS
        • OS
        • 자료구조_알고리즘
      • Java
      • Javascript
      • Node.js
      • React
      • Vue.js
      • 코딩테스트
        • 백준-Java
        • 프로그래머스-JS
      • Canvas
      • HTML, CSS
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    vue.js
    스택
    css
    vue animation
    자바
    vue modal
    fecolormatrix
    자바공부
    Node.js
    큐
    오즈코딩스쿨
    React
    dat.gui
    라우팅
    정렬
    2차원배열
    hashchange
    서버구축
    npm
    해시
    BFS
    til
    자바백준풀이
    fegaussianblur
    백준풀이
    canvas
    props
    Next.js
    프로그래머스
    자바스크립트
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
gamzaggang7
[Canvas] 보일러 플레이트 작성 (캔버스 클래스)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.