四虎国产精品永久地址998_chinesexxx少妇露脸_日本丁香久久综合国产精品_一区二区久久久久_四虎av影视_久久久久国产一区二区三区不卡

中培偉業(yè)IT資訊頻道
您現(xiàn)在的位置:首頁 > IT資訊 > 軟件研發(fā) > HTML5教程——HTML 5 Canvas

HTML5教程——HTML 5 Canvas

2016-01-29 11:32:23 | 來源:中培企業(yè)IT培訓(xùn)網(wǎng)

canvas元素用于在網(wǎng)頁上繪制圖形。

什么是Canvas?

HTML5的canvas元素使用JavaScript在網(wǎng)頁上繪制圖像。

畫布是一個矩形區(qū)域,您可以控制其每一像素。

canvas擁有多種繪制路徑、矩形、圓形、字符以及添加圖像的方法。

創(chuàng)建Canvas元素

向HTML5頁面添加canvas元素。

規(guī)定元素的id、寬度和高度:

<canvas width="200"height="100"></canvas>

通過JavaScript來繪制

canvas元素本身是沒有繪圖能力的。所有的繪制工作必須在JavaScript內(nèi)部完成:

<script type="text/javascript">

var c=document.getElementById("myCanvas");

var cxt=c.getContext("2d");

cxt.fillStyle="#FF0000";

cxt.fillRect(0,0,150,75);

</script>

JavaScript使用id來尋找canvas元素:

var c=document.getElementById("myCanvas");

然后,創(chuàng)建context對象:

var cxt=c.getContext("2d");

getContext("2d")對象是內(nèi)建的HTML5對象,擁有多種繪制路徑、矩形、圓形、字符以及添加圖像的方法。

下面的兩行代碼繪制一個紅色的矩形:

cxt.fillStyle="#FF0000";

cxt.fillRect(0,0,150,75);

fillStyle方法將其染成紅色,fillRect方法規(guī)定了形狀、位置和尺寸。

理解坐標(biāo)

上面的fillRect方法擁有參數(shù)(0,0,150,75)。

意思是:在畫布上繪制150x75的矩形,從左上角開始(0,0)。

如下圖所示,畫布的X和Y坐標(biāo)用于在畫布上對繪畫進(jìn)行定位。

實例:把鼠標(biāo)懸停在矩形上可以看到坐標(biāo)

更多Canvas實例

下面的在canvas元素上進(jìn)行繪畫的更多實例:

實例-線條

通過指定從何處開始,在何處結(jié)束,來繪制一條線:

JavaScript代碼:

<script type="text/javascript">

var c=document.getElementById("myCanvas");

var cxt=c.getContext("2d");

cxt.moveTo(10,10);

cxt.lineTo(150,50);

cxt.lineTo(10,50);

cxt.stroke();

</script>

canvas元素:

<canvas width="200"height="100"style="border:1px solid #c3c3c3;">

Your browser does not support the canvas element.

</canvas>

親自試一試

實例-圓形

通過規(guī)定尺寸、顏色和位置,來繪制一個圓:

JavaScript代碼:

<script type="text/javascript">

var c=document.getElementById("myCanvas");

var cxt=c.getContext("2d");

cxt.fillStyle="#FF0000";

cxt.beginPath();

cxt.arc(70,18,15,0,Math.PI*2,true);

cxt.closePath();

cxt.fill();

</script>

canvas元素:

<canvas width="200"height="100"style="border:1px solid #c3c3c3;">

Your browser does not support the canvas element.

</canvas>

親自試一試

實例-漸變

使用您指定的顏色來繪制漸變背景:

JavaScript代碼:

<script type="text/javascript">

var c=document.getElementById("myCanvas");

var cxt=c.getContext("2d");

var grd=cxt.createLinearGradient(0,0,175,50);

grd.addColorStop(0,"#FF0000");

grd.addColorStop(1,"#00FF00");

cxt.fillStyle=grd;

cxt.fillRect(0,0,175,50);

</script>

canvas元素:

<canvas width="200"height="100"style="border:1px solid #c3c3c3;">

Your browser does not support the canvas element.

</canvas>

親自試一試

實例-圖像

把一幅圖像放置到畫布上:

JavaScript代碼:

<script type="text/javascript">

var c=document.getElementById("myCanvas");

var cxt=c.getContext("2d");

var img=new Image()

img.src="flower.png"

cxt.drawImage(img,0,0);

</script>

canvas元素:

<canvas width="200"height="100"style="border:1px solid #c3c3c3;">

Your browser does not support the canvas element.

</canvas>

標(biāo)簽: HTML5教程

相關(guān)閱讀

主站蜘蛛池模板: 施秉县| 方城县| 邢台市| 宜州市| 安宁市| 威信县| 德昌县| 灵台县| 宁安市| 佛学| 会东县| 祁东县| 原平市| 手游| 巴楚县| 柘荣县| 富蕴县| 确山县| 盐城市| 民丰县| 延吉市| 邵东县| 保定市| 手游| 台前县| 讷河市| 大理市| 淳安县| 湟中县| 康乐县| 大渡口区| 清新县| 汉沽区| 章丘市| 饶平县| 舒城县| 和林格尔县| 正定县| 济源市| 陆良县| 吉木乃县|