Tạo Gradient sử dụng HTML5 Canvas



HTML5 canvas cho phép chúng ta fill và stroke các hình bởi sử dụng linear và radial gradient, sử dụng các phương thức sau:

STTPhương thức và Miêu tả
1

addColorStop(offset, color)

Phương thức này thêm một màu dừng với màu đã cho tới gradient tại offset đã cho. Ở đây 0.0 là offset tại điểm cuối của gradient, 1.0 là offset tại điểm cuối khác.

2

createLinearGradient(x0, y0, x1, y1)

Phương thức này trả về một đối tượng CanvasGradient mà biểu diễn một linear gradient mà tô màu các đường đã cho bởi các tọa độ được biểu diễn bởi các tham số. Bốn tham số biểu diễn điểm bắt đầu (x1,y1) và điểm kết thúc (x2,y2) của gradient

3

createRadialGradient(x0, y0, r0, x1, y1, r1)

Phương thức này trả về một đối tượng CanvasGradient biểu diễn một radial gradient mà tô màu cone đã cho bởi các đường tròn được biểu diễn bởi các tham số. 3 tham số đầu tiên định nghĩa một đường tròn với tọa độ tâm (x1,y1) và bán kính r1; và tham số thứ hai là vòng tròn với tọa độ tâm (x2,y2) và bán kính r2.

Quảng cáo

Ví dụ Linear Gradient

Sau đây là ví dụ sử dụng các phương thức trên để tạo Linear Gradient:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function drawShape(){
  // get the canvas element using the DOM
  var canvas = document.getElementById('mycanvas');  // Make sure we don't execute when canvas isn't supported
  if (canvas.getContext){    // use getContext to use the canvas for drawing
    var ctx = canvas.getContext('2d');    // Create Linear Gradients
    var lingrad = ctx.createLinearGradient(0,0,0,150);
    lingrad.addColorStop(0, '#00ABEB');
    lingrad.addColorStop(0.5, '#fff');
    lingrad.addColorStop(0.5, '#66CC00');
    lingrad.addColorStop(1, '#fff');    var lingrad2 = ctx.createLinearGradient(0,50,0,95);
    lingrad2.addColorStop(0.5, '#000');
    lingrad2.addColorStop(1, 'rgba(0,0,0,0)');    // assign gradients to fill and stroke styles
    ctx.fillStyle = lingrad;
    ctx.strokeStyle = lingrad2;
    // draw shapes
    ctx.fillRect(10,10,130,130);
    ctx.strokeRect(50,50,50,50);
  } else {
    alert('You need Safari or Firefox 1.5+ to see this demo.');
  }
}
</script>
</head>
<body onload="drawShape();">
   <canvas id="mycanvas"></canvas>
</body>
</html>

Ví dụ trên sẽ cho kết quả sau:

Canvas Linear Gradient

Ví dụ Radial Gradient

Sau đây là ví dụ sử dụng các phương thức trên để tạo Radial Gradient:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function drawShape(){
  // get the canvas element using the DOM
  var canvas = document.getElementById('mycanvas');  // Make sure we don't execute when canvas isn't supported
  if (canvas.getContext){    // use getContext to use the canvas for drawing
    var ctx = canvas.getContext('2d');    // Create gradients
    var radgrad = ctx.createRadialGradient(45,45,10,52,50,30);
    radgrad.addColorStop(0, '#A7D30C');
    radgrad.addColorStop(0.9, '#019F62');
    radgrad.addColorStop(1, 'rgba(1,159,98,0)');
  
    var radgrad2 = ctx.createRadialGradient(105,105,20,112,120,50);
    radgrad2.addColorStop(0, '#FF5F98');
    radgrad2.addColorStop(0.75, '#FF0188');
    radgrad2.addColorStop(1, 'rgba(255,1,136,0)');    var radgrad3 = ctx.createRadialGradient(95,15,15,102,20,40);
    radgrad3.addColorStop(0, '#00C9FF');
    radgrad3.addColorStop(0.8, '#00B5E2');
    radgrad3.addColorStop(1, 'rgba(0,201,255,0)');    var radgrad4 = ctx.createRadialGradient(0,150,50,0,140,90);
    radgrad4.addColorStop(0, '#F4F201');
    radgrad4.addColorStop(0.8, '#E4C700');
    radgrad4.addColorStop(1, 'rgba(228,199,0,0)');
  
    // draw shapes
    ctx.fillStyle = radgrad4;
    ctx.fillRect(0,0,150,150);
    ctx.fillStyle = radgrad3;
    ctx.fillRect(0,0,150,150);
    ctx.fillStyle = radgrad2;
    ctx.fillRect(0,0,150,150);
    ctx.fillStyle = radgrad;
    ctx.fillRect(0,0,150,150);
  } else {
    alert('You need Safari or Firefox 1.5+ to see this demo.');
  }
}
</script>
</head>
<body onload="drawShape();">
   <canvas id="mycanvas"></canvas>
</body>
</html>
Quảng cáo

Ví dụ trên sẽ cho kết quả sau:

Canvas Radial Gradient

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Follow fanpage của team https://www.facebook.com/vietjackteam/ hoặc facebook cá nhân Nguyễn Thanh Tuyền https://www.facebook.com/tuyen.vietjack để tiếp tục theo dõi các loạt bài mới nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... mới nhất của chúng tôi.


canvas_trong_html5.jsp


Tài liệu giáo viên