• HTML5 canvas入门


    HTML5 Canvas入门


    <canvas> 标签定义图形,比如图表和其他图像,您必须使用脚本来绘制图形。
    在画布上(Canvas)画一个红色矩形,渐变矩形,彩色矩形,和一些彩色的文字。

    什么是 Canvas?
    HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.
    <canvas> 标签只是图形容器,您必须使用脚本来绘制图形。
    你可以通过多种方法使用Canva绘制路径,盒、圆、字符以及添加图像。

    浏览器支持
    Internet Explorer 9+, Firefox, Opera, Chrome, 和 Safari 支持 <canvas> 元素.
    注意: Internet Explorer 8 及更早 IE 版本的浏览器不支持 <canvas> 元素.

    创建一个画布(Canvas)

    一个画布在网页中是一个矩形框,通过canvas> 元素来绘制.

    注意: 默认情况下 <canvas> 元素没有边框和内容。
    <canvas>简单实例如下:
    <canvas id="myCanvas" width="200" height="100"></canvas>
    注意: 标签通常需要指定一个id属性 (脚本中经常引用), width 和 height 属性定义的画布的大小.
    提示:你可以在HTML页面中使用多个 <canvas> 元素.
    使用 style 属性来添加边框:
    实例
    <canvas id="myCanvas" width="200" height="100"
    style="border:1px solid #000000;">
    </canvas>

    使用 JavaScript 来绘制图像

    canvas 元素本身是没有绘图能力的。所有的绘制工作必须在 JavaScript 内部完成:
    实例
    <script>
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.fillStyle="#FF0000";
    ctx.fillRect(0,0,150,75);
    </script>

    实例解析:
    首先,找到 <canvas> 元素:
    var c=document.getElementById("myCanvas");
    然后,创建 context 对象:
    var ctx=c.getContext("2d");
    getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
    下面的两行代码绘制一个红色的矩形:
    ctx.fillStyle="#FF0000";
    ctx.fillRect(0,0,150,75);
    设置fillStyle属性可以是CSS颜色,渐变,或图案。fillStyle 默认设置是#000000(黑色)。
    fillRect(x,y,width,height) 方法定义了矩形当前的填充方式。

    例子:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <meta name="keywords" content="脚本小子_小贝_HTML5_canvas"/>
    <meta name="description" content="脚本小子_小贝_HTML5_canvas"/>
    <meta name="" content="脚本小子小贝 xiaobei qq:2801616735"/>
    <title>HTML5_canvas</title>
    <style>
    p{
        border:1px dashed red;
        padding:5px;
    }
    canvas{
        border:1px solid yellow;
    }
    </style>
    </head>
    
    <body>
        <h2>HTML5_canvas</h2>
        <ul style="list-style:none">
            <li>1、创建canvas</li>
            <li>2、创建矩形</li>
            <li>3、创建线条</li>
            <li>4、创建文本</li>
            <li>5、创建圆形</li>
            <li>6、创建图像</li>
        </ul>
        <hr/>
        <h3>1、创建canvas</h3>
        <p>
            <canvas id="canvas">亲,你的浏览器不支持canvas.^_^</canvas>
        </p>
        <hr/>
        <h3>2、创建矩形</h3>
        <p>
            <canvas id="canvas1">亲,你的浏览器不支持canvas.^_^</canvas>
            <button onclick="func(1);">创建矩形</button>
        </p>
        <hr/>
        <h3>3、创建线条</h3>
        <p>
            <canvas id="canvas2">亲,你的浏览器不支持canvas.^_^</canvas>
            <button onclick="func(2);">创建线条</button>
        </p>
        <hr/>
        <h3>4、创建文本</h3>
        <p>
            <canvas id="canvas3">亲,你的浏览器不支持canvas.^_^</canvas>
            <button onclick="func(3);">创建文本</button>
        </p>
        <hr/>
        <h3>5、创建圆形</h3>
        <p>
            <canvas id="canvas4">亲,你的浏览器不支持canvas.^_^</canvas>
            <button onclick="func(4);">创建圆形</button>
        </p>
        <hr/>
        <h3>6、创建图像</h3>
        <p>
            <img src="canvas.png" id="img"/>
            <canvas id="canvas5">亲,你的浏览器不支持canvas.^_^</canvas>
            <button onclick="func(5);">创建图形</button>
            
        </p>
    </body>
    <script>
        function func(id)
        {
            var c = document.getElementById('canvas'+id);
            var ctx = c.getContext('2d');
            switch(id)
            {
                case 1:
                    ctx.fillStyle = "#FF0000";
                    ctx.fillRect(10,10,100,50);
                break;
                case 2:
                    ctx.beginPath();
                    ctx.moveTo(10,10);
                    ctx.lineTo(100,100);
                    ctx.stroke();
                break;
                case 3:
                    ctx.font="20px Arial";
                    ctx.fillText("脚本小子_小贝_HTML5_canvas",10,50);
                break;
                case 4:
                    ctx.beginPath();
                    ctx.arc(95,50,40,0,2*Math.PI);
                    ctx.stroke();
                break;
                case 5:
                    var img=document.getElementById("img");
                    c.width=img.width;
                    c.height=img.height;
                    ctx.drawImage(img,10,10);
                break;
            }
            
        }
    </script>
    </html>

    浏览:

    准备知识:http://www.cnblogs.com/2801616735kzw/p/3608927.html

    易错点:http://www.cnblogs.com/2801616735kzw/p/3614941.html

  • 相关阅读:
    文字编码
    各个地区的编码
    Android中调用系统所装的软件打开文件
    Android中检测手机制式和移动网络类型
    Android使用ContentProvide(内容提供者)向其他应用程序共享数据
    windows Phone Push Notification
    解决Android加载图片时内存溢出的问题
    Android 正则表达式
    淡定
    Android简单数据存储类SharedPreferences详解及实例
  • 原文地址:https://www.cnblogs.com/2801616735kzw/p/4093776.html
Copyright © 2020-2023  润新知