標籤

印刷 (1) 行動裝置 (2) 批次 (1) 表單 (2) 套件 (4) 素材 (1) 動畫 (1) 部落格 (1) 電子書 (1) 導覽列 (6) 線上工具 (5) 檔案管理 (1) 瀏覽器 (2) animation (3) App (1) Bootstrap (3) canvas (2) CSS (20) excel (1) flash (2) html (3) html5 (2) IE (1) il (3) jQuery (5) js (5) loading (1) office (3) pdf (2) php (4) png (1) script (1) UI (3) windows (1) youtube (1)

2014年3月7日 星期五

canvas 基礎語法

認識 canvas
http://www.w3schools.com/html/html5_canvas.asp

canvas 的屬性和方法
http://www.w3schools.com/tags/ref_canvas.asp




<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas 基礎語法</title>

</head>

<body>
<canvas id="myCanvas" width="500" height="500" style="border:1px solid #996600;">Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
/*======================= 著色 =======================*/
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75); /* (X,Y,寬度,高度) */

/*======================= 畫線 =======================*/
ctx.moveTo(0,0); /* (X,Y)限定的行的起點 */
ctx.lineTo(500,200); /* (X,Y)限定的線的終點 */
ctx.stroke();

/*======================= 畫圓 =======================*/
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI); /* (圓心X,圓心Y,直徑,不知道,不知道) */
ctx.stroke();

/*======================= 畫文字(填滿) =======================*/
ctx.font = "30px Arial"; /* 級數  字型 */
ctx.fillText("Hello World",10,100); /* fillText("內容",X,Y) */

/*======================= 畫文字(線框) =======================*/
ctx.font = "30px Arial"; /* 級數  字型 */
ctx.strokeText("Hello World",10,200); /* strokeText("內容",X,Y) */

/*======================= 線性漸層 =======================*/
//建立建層
var grd = ctx.createLinearGradient(0,0,200,0); /* (X,Y,X1,Y1) */
grd.addColorStop(0,"red"); /* 介於0到1的任意位置,"顏色" */
grd.addColorStop(0.6,"white"); /* 介於0到1的任意位置,"顏色" */
grd.addColorStop(1,"red"); /* 介於0到1的任意位置,"顏色" */

//漸層填色
ctx.fillStyle = grd;
ctx.fillRect(0,100,150,80); /* (X,Y,寬度,高度) */

/*======================= 圓形漸層 =======================*/
//建立建層
var grd = ctx.createRadialGradient(0,310,5,100,390,150); /* (圓心X,圓心Y,最小直徑R,X1,Y1,最大直徑R1) 依物件位置而定 */
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");

//漸層填色
ctx.fillStyle = grd;
ctx.fillRect(0,300,150,80);/* (X,Y,寬度,高度) */

/*======================= 插入圖片 失敗!! =======================*/
var img=document.getElementById("scream"); /* 宣告圖片ID */
ctx.drawImage(img,10,10); /**/
</script>
</body>
</html>

沒有留言 :

張貼留言