Canvas tutorial #6 (Fill Styles)

2020. 4. 6. 20:00·개발/Canvas
728x90

※ 이 글은 예전 블로그에서 퍼왔으며 2020.3.4.에 작성된 글입니다.

※ 이 글은 해당사이트를 번역한 것입니다.


Color Fill(색상 채우기)

단색(solid color)으로 모양을 채우기 위해선,

fillStyle( ) 속성을 사용하여 RBG, HEX 등의 색상을 설정하고 fill( )을 사용하여 도형을 채울 수 있다.

특별한 명시가 없는 경우 HTML5Canvas 도형의 기본 채우기색은 검은색이다.

context.fillStyle = "color";
context.fill();

Linear Gradient(선형 그라데이션)

Linear gradient을 구현하기 위해선 createLinearGradation( )을 사용할 수 있다.

Linear gradient는 gradient의 방향을 정의하는 가상의 선으로 정의된다.

gradation을 만들었다면 addColorStop 속성을 사용하여 색상을 부여할 수 있다.

Color stops은 가상의 라인을 따라 배치 되며, 0은 시작점에 1은 끝점에 배치된다.

const grd = context.createLinearGradient(startX, startY, endX, endY);

grd.addColorStop(0, "color");
grd.addColorStop(1, "color");

context.fillStyle = grd";
context.fill();

Radial Gradient(원형 그라데이션)

Radial gradient을 구현하기 위해선 createLinearGradation( )을 사용할 수 있다.

Radial gradient은 두 개의 가상 원으로 정의되며, gradient는 시작 원에서 시작하여 끝 원으로 이동한다.

const grd = context.createRadialGradient(x1, y1, r1, x2, y2, r2);

grd.addColorStop(0, "color");
grd.addColorStop(1, "color");

context.fillStyle = grd;
context.fill();

/*
x1: 시작 원의 중점(x값)
y1: 시작 원의 중점(y값)
r1: 시작 원의 반지름
x2: 끝 원의 중점(x값)
y2: 끝 원의 중점(y값)
r2: 끝 원의 반지름
*/

Pattern(패턴)

pattern을 구현하기 위해선 패턴 객체를 반환하는 canvas context의 createPattern( )를 사용하고 fillStyle속성을 패턴 객체로 설정한 다음 fill( )을 사용하여 모양을 채울 수 있다.

createPattern( )는 이미지 객체와 repeat option를 필요로 한다.

특별한 명시가 없는 경우 repeat option의 default값은 repeat이다.

​

· repeat option 종류

repeat, repeat-x, repeat-y, no-repeat

const imageObj = new Image();

imageObj.onload = function() {
const pattern = context.createPattern(imageObj, 'repeat');

context.rect(0, 0, canvas.width, canvas.height);
context.fillStyle = pattern;
context.fill();
};
imageObj.src = 'https://www.html5canvastutorials.com/demos/assets/wood-pattern.png';
저작자표시 (새창열림)

'개발 > Canvas' 카테고리의 다른 글

HTML5 Canvas Tutorial for Beginners 필기  (0) 2020.04.06
Canvas tutorial #7 (Images)  (1) 2020.04.06
Canvas tutorial #5 (Shapes)  (0) 2020.04.06
Canvas tutorial #4 (Paths)  (0) 2020.04.06
Canvas tutorial #3 (Curves)  (0) 2020.04.06
'개발/Canvas' 카테고리의 다른 글
  • HTML5 Canvas Tutorial for Beginners 필기
  • Canvas tutorial #7 (Images)
  • Canvas tutorial #5 (Shapes)
  • Canvas tutorial #4 (Paths)
기짜낭
기짜낭
생각이 많지만, 지금 내가 해야할 것을 하자.
  • 기짜낭
    So tired
    기짜낭
    • 분류 전체보기 (203) N
      • 개발 (13)
        • Javascript (19)
        • Typescript (5)
        • Canvas (8)
        • React (13)
        • C (3)
      • 활동 (63)
        • 개인 프로젝트 (33)
        • 나눔 서포터즈 3기 (9)
        • 멋쟁이 사자처럼 (7)
        • 0&1 C++ 자료구조 스터디 (0)
        • 제 9회 창업 아이디어톤 (3)
        • Poom (ZeroWasteShop) (3)
        • 해커톤 (2)
        • 우테코 프리코스 7기 (6)
      • 알고리즘 (27)
        • 알고리즘 정리 (5)
        • 백준 (18)
        • 프로그래머스 (4)
      • 강연 (7)
      • 독서 (19)
      • 회고 & 생각 (17) N
        • 연간회고 (5) N
      • 기타 (5)
  • 블로그 메뉴

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

  • 공지사항

    • ※ 예전 블로그
  • 인기 글

  • 태그

    우테코
    독서
    1주에 1권씩
    CSS
    ES6
    알고리즘
    대학
    독후감
    HTML
    TypeScript
    react
    프로그래밍
    프로젝트
    HTML5
    개발자
    Erica
    canvas
    tutorial
    1주 1권
    타입스크립트
    에리카
    프론트엔드
    개념
    군대
    백준
    한양대학교
    Javascript
    개발
    3기
    디자인
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
기짜낭
Canvas tutorial #6 (Fill Styles)
상단으로

티스토리툴바