Canvas基礎知識

<!DOCTYPE html />
<html>
    <head>
        <title>Learning the basics of canvas</title>
        <meta charset="utf-8" />
        <script type="text/javascript" src="jquery-1.6.4.js"></script>
        <script type="text/javascript">
            $().ready(function () {
                //獲取canvas對象
                var canvas = $("#myCanvas");
                //得到2D渲染上下文,以便開始繪製圖形
                var context = canvas.get(0).getContext("2d");

                //繪製矩形
                context.fillRect(40, 40, 100, 100);
                //繪製矩形邊框
                context.strokeRect(200, 200, 100, 100);

                //繪製線條(實際稱為路徑)
                //開始路徑
                context.beginPath();
                //設置路徑原點
                context.moveTo(150, 150);
                //設置路徑終點
                context.lineTo(300, 300);
                //結束路徑
                context.closePath();
                //繪製路徑
                context.stroke();

                //繪製圓形(canvas并無繪製圓形的方法,只有繪製弧形的方法)
                //開始路徑
                context.beginPath();
                //繪製一個圓形
                context.arc(230, 90, 50, 0, Math.PI * 2, false);
                context.arc(350, 90, 50, 0, Math.PI, false);
                //結束路徑
                context.closePath();
                //context.stroke();
                context.fill();

                //樣式
                context.fillStyle = "rgb(255,0,0)";
                context.fillRect(0, 1, 20, 20);
                context.fillStyle = "rgb(0,255,0)";
                context.fillRect(25, 1, 20, 20);

                context.strokeStyle = "rgb(255,0,0)";
                context.strokeRect(50, 1, 20, 20);
                context.strokeStyle = "rgb(0,255,0)";
                context.strokeRect(75, 1, 20, 20);

                context.strokeStyle = "rgb(0,0,255)";
                context.beginPath();
                context.moveTo(100, 10);
                context.lineTo(300, 10);
                context.closePath();
                context.stroke();

                //修改線條寬度
                context.lineWidth = 5;
                context.strokeStyle = "rgb(255,0,0)";
                context.beginPath();
                context.moveTo(100, 20);
                context.lineTo(300, 20);
                context.closePath();
                context.stroke();

                context.lineWidth = 10;
                context.strokeStyle = "orange";
                context.strokeRect(350, 200, 100, 100);

                //繪製文本(使用canvas繪製文本並非好方法)
                var text = "Hello World!";
                context.fillStyle = "pink";
                context.font = "italic 50px serif";
                context.fillText(text, 10, 400);
                //繪製描邊文本
                context.strokeStyle = "pink";
                context.font = "50px serif";
                context.strokeText(text, 300, 400);

                //擦除Canvas
                //context.clearRect(0, 0, canvas.width(), canvas.height());

                //寬度/高度技巧
                context.fillStyle = "yellow";
                //重設置canvas高度寬度將完全重置canvas上所有內容為初始狀態
                canvas.attr("width", canvas.width());
                canvas.attr("height", canvas.height());
                //將繪製黑色矩形
                context.fillRect(40, 40, 100, 100);
            });
        </script>
    </head>
    <body>
        <!-- 創建一個canvas元素 -->
        <canvas id="myCanvas" width="700" height="500"></canvas>
    </body>
</html>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值