canvas addColorStop()
此方法規定漸變的位置與顏色,與createLinearGradient方法或createRadialGradient方法一起使用。
不對漸變物件使用該方法,漸變將不可見,為了獲得可見的漸變,需要建立至少一個色標。
語法結構:
[JavaScript] 純文字檢視 複製程式碼gradient.addColorStop(stop,color);
引數解析:
(1).stop:介於0.0與1.0之間的值,表示漸變中開始與結束之間的位置。
(2).color:在stop位置顯示的CSS顏色值。
瀏覽器相容:
(1).IE9+瀏覽器支援此方法。
(2).edge瀏覽器支援此方法。
(3).谷歌瀏覽器支援此方法。
(4).火狐瀏覽器支援此方法。
(5).Opera瀏覽器支援此方法。
(6).Safari瀏覽器支援此方法。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <style type="text/css"> canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); var gnt1 = cans.createLinearGradient(10,0,390,0); gnt1.addColorStop(0.4,'red'); gnt1.addColorStop(0.8,'blue'); cans.fillStyle = gnt1; cans.fillRect(10,10,380,180); } window.onload=function(){ pageLoad(); } </script> <body> <canvas id="can" width="400px" height="300px"></canvas> </body> </html>
第一個引數規定圖片的漸變區域,當然只有一個無法形成漸變,需要兩個才能形成一個漸變區域。
如下程式碼:
[JavaScript] 純文字檢視 複製程式碼gnt1.addColorStop(0.4,'red'); gnt1.addColorStop(0.8,'blue');
此段程式碼就是規定影像的40%-80%之間是漸變區域,圖示如下:
相關文章
- canvasCanvas
- canvas clearRect()Canvas
- canvas toBlob()Canvas
- canvas toDataURL()Canvas
- canvas stroke()Canvas
- canvas fill()Canvas
- canvas fillRect()Canvas
- canvas translate()Canvas
- canvas rotate()Canvas
- canvas scale()Canvas
- canvas fillText()Canvas
- canvas createPattern()Canvas
- canvas createImageData()Canvas
- canvas restore()CanvasREST
- canvas save()Canvas
- canvas putImageData()Canvas
- canvas drawImage()Canvas
- canvas clip()Canvas
- canvas arcTo()Canvas
- canvas isPointInPath()Canvas
- canvas arc()Canvas
- canvas getImageData()Canvas
- canvas fillStyleCanvas
- canvas closePath()Canvas
- canvas beginPath()Canvas
- canvas strokeRect()Canvas
- canvas strokeStyleCanvas
- canvas rect()Canvas
- canvas setTransform()CanvasORM
- canvas(三)Canvas
- 初探canvasCanvas
- Canvas 教程Canvas
- canvas transform()CanvasORM
- canvas apiCanvasAPI
- react canvasReactCanvas
- canvas translateCanvas
- One simple way to draw canvas, wxml2canvasCanvasXML
- WPF C# create canvas and draw ellipse in canvasC#Canvas