[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
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바