HTML5簡明教程-1.1.4.HTML5Canvas參考手冊 之 線性漸層 createLinearGradient,html5canvas

來源:互聯網
上載者:User

HTML5簡明教程-1.1.4.HTML5Canvas參考手冊 之 線性漸層 createLinearGradient,html5canvas

本文串連:http://blog.csdn.net/tomorrow13210073213/article/details/42453769

轉載請註明出處


HTML5的canvas為我們提供了類似於CSS3中linear-gradient屬性定義的漸層效果,用createLinearGradient()方法可以建立基於畫布的線性漸層,建立好的線性漸層可以用於填充路徑、定義筆觸等。同時,HTML5也為我們提供了createRadialGradient()方法用於建立放射漸層,我們將在另一篇博文中詳細介紹。


方法:createLinearGradient()

方法名:createLinearGradient()

方法描述:用於建立基於整個畫布的線性漸層,該漸層可以用於填充路徑或定義筆觸(作為屬性fillStyle或strokeStyle的值)

js文法:context.createLinearGradient(x0,y0,x1,y1);
       參數:x0:漸層開始點的 x 座標,
       參數:y0:漸層開始點的 y 座標,
       參數:x1:漸層結束點的 x 座標,
       參數:y1:漸層結束點的 y 座標

瀏覽器安全色:IE 9、Firefox、Opera、Chrome 以及 Safari 支援 createLinearGradient() 方法。

使用注意:(1)獲得漸層對象後,我們要使用 addColorStop() 方法規定不同的顏色,以及在漸層對象中的何處定位顏色。關於該方法,另有博文詳述。

(2)我們用createLinearGradient()方法定義的漸層是基於整個canvas的,僅僅通過定義該漸層是看不到的,將該漸層定義為屬性fillStyle或strokeStyle的值後,我們實際是將路徑定義的矩形(面或筆觸)、圓形(面或筆觸)部分的漸層顯示出來。


廢話不多講,下面是測試代碼,js注釋中有相關測試的描述:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Html5 Canvas createLinearGradient</title>    <style type="text/css">        body {            margin: 0px;            width: 440px;        }        canvas {            float: left;            margin-left: 10px;            margin-top: 10px;        }    </style></head><body>    <!--       createLinearGradient() 方法建立線性漸層對象。       漸層可用於填充矩形、圓形、線條、文本等等。       該對象一般作為 strokeStyle 或 fillStyle 屬性的值。       用方法 addColorStop() 規定不同的顏色,以及在 gradient 對象中的何處定位顏色。       文法:context.createLinearGradient(x0,y0,x1,y1);       參數:x0:漸層開始點的 x 座標,       參數:y0:漸層開始點的 y 座標,       參數:x1:漸層結束點的 x 座標,       參數:y1:漸層結束點的 y 座標   -->    <!--       addColorStop() 方法規定 gradient 對象中的顏色和位置。       addColorStop() 方法與 createLinearGradient() 或 createRadialGradient() 一起使用。       可以多次調用 addColorStop() 方法來改變漸層,理論上可以定義無限中間色。如果您不對 gradient 對象使用該方法,那麼漸層將不可見。為了獲得可見的漸層,您需要建立至少一個色標。       文法:gradient.addColorStop(stop,color);       參數:stop:介於 0.0 與 1.0 之間的值,表示漸層中開始與結束之間的位置。       參數:color:在結束位置顯示的 CSS 顏色值。   -->    <canvas id="can0" width="430" height="100"></canvas>    <canvas id="can" width="100" height="100"></canvas>    <canvas id="can2" width="100" height="100"></canvas>    <canvas id="can3" width="100" height="100"></canvas>    <canvas id="can4" width="100" height="100"></canvas>    <canvas id="can5" width="430" height="100"></canvas>    <canvas id="can6" width="430" height="100"></canvas>    <canvas id="can7" width="430" height="100"></canvas>    <canvas id="can8" width="430" height="100"></canvas></body><script type="text/javascript">    //可以用0-1之間的值定義中間顏色,理論上講可以定義無限中間顏色    var c0 = document.getElementById('can0');    var cC0 = c0.getContext('2d');    //參數:(漸層開始點的 x 座標,漸層開始點的 y 座標,漸層結束點的 x 座標,漸層結束點的 y 座標)    //該方法的參數需要注意,後兩個參數是座標而不是長度,這一點與繪製矩形的相關方法不同    var lg0 = cC0.createLinearGradient(0, 0, 430, 0);    lg0.addColorStop(0, '#823835');    lg0.addColorStop(0.1, '#8abeb2');    lg0.addColorStop(0.2, '#c9ba83');    lg0.addColorStop(0.3, '#ddd38c');    lg0.addColorStop(0.4, '#de9c52');    lg0.addColorStop(0.5, '#44b37e');    lg0.addColorStop(0.6, '#823835');    lg0.addColorStop(0.7, '#8abeb2');    lg0.addColorStop(0.8, '#c9ba83');    lg0.addColorStop(0.9, '#ddd38c');    lg0.addColorStop(1, '#de9c52');    cC0.fillStyle = lg0;    cC0.fillRect(0, 0, 430, 100);    //左上方到右下角的漸層    var c = document.getElementById('can');    var cC = c.getContext('2d');    var lg = cC.createLinearGradient(0, 0, 100, 100);    lg.addColorStop(0, 'red');    lg.addColorStop(0.5, 'yellow');    lg.addColorStop(1, 'green');    cC.fillStyle = lg;    cC.fillRect(0, 0, 100, 100);    //垂直漸層    var c2 = document.getElementById('can2');    var cC2 = c2.getContext('2d');    var lg2 = cC2.createLinearGradient(0, 0, 0, 100);    lg2.addColorStop(0, 'red');    lg2.addColorStop(0.5, 'yellow');    lg2.addColorStop(1, 'green');    cC2.fillStyle = lg2;    cC2.fillRect(0, 0, 100, 100);    //水平漸層    var c3 = document.getElementById('can3');    var cC3 = c3.getContext('2d');    var lg3 = cC3.createLinearGradient(0, 0, 100, 0);    lg3.addColorStop(0, 'red');    lg3.addColorStop(0.5, 'yellow');    lg3.addColorStop(1, 'green');    cC3.fillStyle = lg3;    cC3.fillRect(0, 0, 100, 100);    //左下角到右上方的漸層    var c4 = document.getElementById('can4');    var cC4 = c4.getContext('2d');    var lg4 = cC4.createLinearGradient(0, 100, 100, 0);    lg4.addColorStop(0, 'red');    lg4.addColorStop(0.5, 'yellow');    lg4.addColorStop(1, 'green');    cC4.fillStyle = lg4;    cC4.fillRect(0, 0, 100, 100);    //createLinearGradient()方法所定義的漸層是針對於整個canvas的漸層,    //當起點座標不是0時,起點之前的顏色將是起點顏色(addColorStop(0, 'red')),    //終點之後的顏色將是終點顏色(lg5.addColorStop(1, 'green'))    var c5 = document.getElementById('can5');    var cC5 = c5.getContext('2d');    var lg5 = cC5.createLinearGradient(100, 0, 300, 0);    lg5.addColorStop(0, 'red');    lg5.addColorStop(0.5, 'yellow');    lg5.addColorStop(1, 'green');    cC5.fillStyle = lg5;    cC5.fillRect(0, 0, 430, 100);    //正如前文所說,用createLinearGradient()方法定義漸層的漸層是針對於整個canvas的漸層,    //我們用該漸層作為填充或筆觸進行繪製時,實際是將我們繪製部分的漸層顏色顯示出來    var c6 = document.getElementById('can6');    var cC6 = c6.getContext('2d');    var lg6 = cC6.createLinearGradient(100, 0, 300, 0);    lg6.addColorStop(0, 'red');    lg6.addColorStop(0.5, 'yellow');    lg6.addColorStop(1, 'green');    cC6.fillStyle = lg6;    cC6.fillRect(0, 0, 100, 100);    cC6.fillRect(330, 0, 100, 100);    cC6.fillRect(150, 0, 100, 100);    //createLinearGradient()方法所定義的漸層是針對於整個canvas的漸層,    //當起點座標不是0時,起點之前的顏色將是起點顏色(addColorStop(0, 'red')),    //終點之後的顏色將是終點顏色(lg5.addColorStop(1, 'green'))    var c7 = document.getElementById('can7');    var cC7 = c7.getContext('2d');    var lg7 = cC7.createLinearGradient(0, 0, 0, 100);    lg7.addColorStop(0, 'red');    lg7.addColorStop(0.5, 'yellow');    lg7.addColorStop(1, 'green');    cC7.fillStyle = lg7;    cC7.fillRect(0, 0, 430, 100);    //正如前文所說,用createLinearGradient()方法定義漸層的漸層是針對於整個canvas的漸層,    //我們用該漸層作為填充或筆觸進行繪製時,實際是將我們繪製部分的漸層顏色顯示出來    var c8 = document.getElementById('can8');    var cC8 = c8.getContext('2d');    var lg7 = cC8.createLinearGradient(0, 0, 0, 100);    lg7.addColorStop(0, 'red');    lg7.addColorStop(0.5, 'yellow');    lg7.addColorStop(1, 'green');    cC8.fillStyle = lg7;    cC8.fillRect(0, 0, 100, 100);    cC8.fillRect(330, 0, 100, 100);    cC8.fillRect(165, 0, 100, 100);</script></html>

下面是測試:

(圖片傳不上來啊,csdn咋啦,大家自己測試吧)


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.