HTML 캔버스 createLinearGradient() 메서드

❮ HTML 캔버스 참조

예시

사각형의 채우기 스타일로 검은색에서 흰색으로 가는 그라디언트(왼쪽에서 오른쪽으로)를 정의합니다.

브라우저는 HTML5캔버스태그를 지원하지 않습니다.

자바스크립트:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var grd = ctx.createLinearGradient(0, 0, 170, 0);
grd.addColorStop(0, "black");
grd.addColorStop(1, "white");

ctx.fillStyle = grd;
ctx.fillRect(20, 20, 150, 100);

브라우저 지원

표의 숫자는 이 방법을 완전히 지원하는 첫 번째 브라우저 버전을 지정합니다.

Method
createLinearGradient() Yes 9.0 Yes Yes Yes

정의 및 사용

createLinearGradient() 메서드는 선형 그래디언트 객체를 만듭니다.

그라디언트는 사각형, 원, 선, 텍스트 등을 채우는 데 사용할 수 있습니다.

팁: 이 객체를 strokeStyle 또는 fillStyle 속성에 대한 값으로 사용하십시오.

팁: addColorStop() 메서드를 사용 하여 다양한 색상을 지정하고 그라디언트 객체에서 색상을 배치할 위치를 지정합니다.

자바스크립트 구문: 컨텍스트 .createLinearGradient( x0,y0,x1,y1 );

매개변수 값

Parameter Description
x0 The x-coordinate of the start point of the gradient
y0 The y-coordinate of the start point of the gradient
x1 The x-coordinate of the end point of the gradient
y1 The y-coordinate of the end point of the gradient

더 많은 예

예시

사각형의 채우기 스타일로 그라디언트(위에서 아래로)를 정의합니다.

귀하의 브라우저는 캔버스 태그를 지원하지 않습니다.

자바스크립트:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var my_gradient = ctx.createLinearGradient(0, 0, 0, 170);
my_gradient.addColorStop(0, "black");
my_gradient.addColorStop(1, "white");
ctx.fillStyle = my_gradient;
ctx.fillRect(20, 20, 150, 100);

예시

사각형의 채우기 스타일로 검은색에서 빨간색, 흰색으로 가는 그라디언트를 정의합니다.

귀하의 브라우저는 캔버스 태그를 지원하지 않습니다.

자바스크립트:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var my_gradient = ctx.createLinearGradient(0, 0, 170, 0);
my_gradient.addColorStop(0, "black");
my_gradient.addColorStop(0.5 ,"red");
my_gradient.addColorStop(1, "white");
ctx.fillStyle = my_gradient;
ctx.fillRect(20, 20, 150, 100);

❮ HTML 캔버스 참조