亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

HTML5 畫布

? ?<canvas></canvas>是HTML5出現(xiàn)的新標(biāo)籤,像所有的dom物件一樣它有自己本身的屬性、方法和事件,其中就有繪圖的方法,js能夠呼叫它來進(jìn)行繪圖。

什麼是Canvas?

HTML5 <canvas> 元素用於圖形的繪製,透過腳本(通常是JavaScript)來完成.

##<canvas> 標(biāo)籤只是圖形容器,您必須使用腳本來繪製圖形。

你可以透過多種方法使用Canva繪製路徑,盒、圓、字元以及新增影像。

基本上所有的瀏覽器都支援<canvas> 元素除了IE8及以前的版本


canvas元素繪製圖像的時候有兩種方法,分別是

??

????? context.fill()//填充

??????? context.stroke()//繪製邊框

style:在進(jìn)行圖形繪製前,要設(shè)定好繪圖的樣式

????

??? context.fillStyle//填入的樣式

?????. strokeStyle//邊框樣式

? ? ? ?

?context.lineWidth//圖形邊框?qū)挾?span style="line-height: 1.76em;">

#顏色的表示方式:

???????? 直接使用顏色名稱:"red" "green" "blue"

???????? 十六進(jìn)位色彩值?1-255,1-255)

?????????rgba(1-255,1-255,1-255,透明度)


使用Canvasvas繪製直線:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>php.cn</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');
            cans.moveTo(20,30);//第一個起點(diǎn)
            cans.lineTo(120,90);//第二個點(diǎn)
            cans.lineTo(220,60);//第三個點(diǎn)(以第二個點(diǎn)為起點(diǎn))
            cans.lineWidth=3;
            cans.strokeStyle = 'red';
            cans.stroke();
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>
Canvas 座標(biāo)

canvas 是一個二維網(wǎng)格。

canvas 的左上角座標(biāo)為 (0,0)

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
    </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');
            cans.fillStyle = 'yellow';
            cans.fillRect(30,30,340,240);
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

注意:這裡使用了一個方法-fillRect()從字面意思也能知道這個就是填滿一個矩形,這裡的參數(shù)值得說明一下fillRect(X,Y,Width,Height) ,這個和數(shù)學(xué)裡的座標(biāo)是不一樣的,這裡的X,Y是相對Canvas左上角的起點(diǎn)開始的,謹(jǐn)記! !


圓形

#
<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
    </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');
            cans.beginPath();
            cans.arc(200,150,100,0,Math.PI*2,false);
            cans.closePath();
            cans.lineWidth = 5;
            cans.strokeStyle = 'red';
            cans.stroke();
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

注意: 這裡的arc方法的用法是arc(X,Y,Radius,startAngle,endAngle,anticlockwise),意思是(圓心X座標(biāo),圓心Y座標(biāo),半徑,開始角度(弧度),結(jié)束角度弧度,是否按照順時針畫);


#Canvas - 漸層

漸層可以填滿在矩形, 圓形,線條, 文字等等, 各種形狀可以自己定義不同的顏色。

以下有兩種不同的方式來設(shè)定Canvas漸層:

createLinearGradient(x,y,x1,y1) - 建立線條漸層

createRadialGradient(x,y, r,x1,y1,r1) - 建立一個徑向/圓漸層

當(dāng)我們使用漸層對象,必須使用兩種或兩種以上的停止顏色。

addColorStop()方法指定顏色停止,參數(shù)使用座標(biāo)來描述,可以是0至1.

使用漸變,設(shè)定fillStyle或strokeStyle的值為漸變,然後繪製形狀,如矩形,文本,或一條線。

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
    </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,'red');
            gnt1.addColorStop(0.5,'green');
            gnt1.addColorStop(1,'blue');
            cans.fillStyle = gnt1;
            cans.fillRect(10,10,380,280);
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

Canvas - 文字

? ?text:要繪製的文字

???? x:文字起點(diǎn)的x座標(biāo)軸

#???? y:文字起點(diǎn)的y座標(biāo)軸

???? context.font:設(shè)定字體樣式

???? context.textAlign:水平對準(zhǔn)方式

? ? ? ? ? start、end、right、center

???? context.textBaseline: 垂直對準(zhǔn)方式#hang),?????ideographic、bottom

<!DOCTYPE html>
<html>
 <head>
    <meta charset="utf-8">
    <title>Canvas</title>
 </head>
 <style type="text/css">
    body{margin:20px auto; padding:0; width:800px; }
    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');
        cans.font = 'bold 144px consolas';
        cans.textAlign = 'left';
        cans.textBaseline = 'top';
        cans.strokeStyle = '#DF5326';
        cans.strokeText('Hello', 100, 100);
        cans.font = 'bold 144px arial';
        cans.fillStyle = 'red';
        cans.fillText('World', 300,300);
    }
    function img_click(){
        var can = $$('can');
        var cans = can.getContext('2d');
        cans.clearRect(0,0,800,600);
    }
 </script>
<body onload="pageLoad();">
    <canvas id="can" width="800px" height="600px" onclick="img_click(this);"></canvas>
</body>
</html>

#

繼續(xù)學(xué)習(xí)
||
<!doctype html> <html> <head> <meta charset="UTF-8"> </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'); cans.moveTo(0,0); cans.lineTo(400,300); var gnt1 = cans.createLinearGradient(0,0,400,300);//線性漸變的起止坐標(biāo) gnt1.addColorStop(0,'red');//創(chuàng)建漸變的開始顏色,0表示偏移量,個人理解為直線上的相對位置,最大為1,一個漸變中可以寫任意個漸變顏色 gnt1.addColorStop(1,'yellow'); cans.lineWidth=3; cans.strokeStyle = gnt1; cans.stroke(); } </script> <body onload="pageLoad();"> <canvas id="can" width="400px" height="300px">4</canvas> </body> </html>
提交重置程式碼