• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > html5之canvas进阶的代码详解(图)

html5之canvas进阶的代码详解(图)

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含html5,canvas等相关知识,匿名希望在学习及工作中可以帮助到您
1)用路径绘图:

beginPath()——开始一条新路径;

closePath()——尝试闭合现有路径,方法是绘制一条线,连接最后那条线的终点与初始坐标;

fill()——填充用子路径描述的图形;

isPointInPath(x,y)——如果指定的点在当前路径所描述的图形之内则返回true;

lineTo(x,y)——绘制一条到指定坐标的子路径;

moveTo(x,y)——移动到指定坐标而不绘制子路径;

rect(x,y,w,h)——绘制一个矩形,其左上角位于(x,y),宽度是w,高度是h;

stroke()——给子路径描述的图形绘制轮廓线;

<style type="text/css">
        canvas{
            border:thin solid black;
            margin: 4px;
        }
        body > *{
            float: left;
        }
    </style>
<canvas id="canvas1" width="500" height="140">
        您的浏览器不支持<code>canvas</code>!
    </canvas>
    <script>
        //由直线创建路径
        var ctx=document.getElementById("canvas1").getContext("2d");
        ctx.fillStyle="#136455";
        ctx.strokeStyle="blue";
        ctx.lineWidth=4;

        ctx.beginPath();
        ctx.moveTo(10,10);
        ctx.lineTo(110,10);
        ctx.lineTo(110,120);
        ctx.closePath();
        ctx.fill();

        ctx.beginPath();
        ctx.moveTo(150,10);
        ctx.lineTo(200,10);
        ctx.lineTo(200,120);
        ctx.lineTo(190,120);
        ctx.fill();
        ctx.stroke();

        ctx.beginPath();
        ctx.moveTo(250,10);
        ctx.lineTo(250,120);
        ctx.stroke();
    </script>


lineCap——在绘制线条或闭合图形时设置线条末端的样式;

    <canvas id="canvas2" width="500" height="140">
        您的浏览器不支持<code>canvas</code>!
    </canvas>
    <script>
        //设置lineCap属性
        var ctx2=document.getElementById("canvas2").getContext("2d");
        ctx2.strokeStyle="red";
        ctx2.lineWidth=2;

        ctx2.beginPath();
        ctx2.moveTo(0,50);
        ctx2.lineTo(200,50);
        ctx2.stroke();

        ctx2.strokeStyle="black";
        ctx2.lineWidth=40;

        var xpos=50;
        var styles=["butt","round","square"];
        for(var i=0;i<styles.length;i++){
            ctx2.beginPath();
            ctx2.lineCap=styles[i];
            ctx2.moveTo(xpos,50);
            ctx2.lineTo(xpos,150);
            ctx2.stroke();
            xpos+=50;
        }
    </script>


    <canvas id="canvas3" width="500" height="140">
        您的浏览器不支持<code>canvas</code>!
    </canvas>
    <script>
        //用Rect方法绘制矩形
        var ctx3=document.getElementById("canvas3").getContext("2d");
        ctx3.fillStyle="yellow";
        ctx3.strokeStyle="black";
        ctx3.lineWidth=4;

        ctx3.beginPath();
        ctx3.moveTo(110,10);
        ctx3.lineTo(110,100);
        ctx3.lineTo(10,10);
        ctx3.closePath();

        ctx3.rect(110,10,100,90);
        ctx3.rect(110,100,130,30);
        ctx3.fill();
        ctx3.stroke();
    </script>


2)绘制圆弧:

arc(x,y,rad,startAngle,endAngle,direction)——绘制一段圆弧到(x,y),半径为rad,起始角度为 startAngle,结束角度为endAngle。可选参数direction指定了圆弧的方向;

arcTo(x1,y1,x2,y2,rad)——绘制一段半径为rad,经过(x1,y1),直到(x2,y2)的圆弧;

    <canvas id="canvas4" width="500" height="140">
        您的浏览器不支持<code>canvas</code>!
    </canvas>
    <script>
        //使用arcTo方法
        var ctx4=document.getElementById("canvas4").getContext("2d");
        var point1=[100,10];
        var point2=[200,10];
        var point3=[200,110];

        ctx4.fillStyle="yellow";
        ctx4.strokeStyle="black";
        ctx4.lineWidth=4;

        ctx4.beginPath();
        ctx4.moveTo(point1[0],point1[1]);
        ctx4.arcTo(point2[0],point2[1],point3[0],point3[1],100);
        ctx4.stroke();

        drawPoint(point1[0],point1[1]);
        drawPoint(point2[0],point2[1]);
        drawPoint(point3[0],point3[1]);

        ctx4.beginPath();
        ctx4.moveTo(point1[0],point1[1]);
        ctx4.lineTo(point2[0],point2[1]);
        ctx4.lineTo(point3[0],point3[1]);
        ctx4.stroke();

        function drawPoint(x,y){
            ctx4.lineWidth=1;
            ctx4.strokeStyle="red";
            ctx4.strokeRect(x-2,y-2,4,4);
        }
    </script>


    <canvas id="canvas5" width="500" height="140">
        您的浏览器不支持<code>canvas</code>!
    </canvas>
    <script>
        //响应鼠标移动绘制圆弧
        var canvasElem = document.getElementById("canvas5");
        var ctx5 = canvasElem.getContext("2d");

        var point1 = [100, 10];
        var point2 = [200, 10];
        var point3 = [200, 110];

        draw();

        canvasElem.onmousemove = function (e) {
            if (e.ctrlKey) {
                point1 = [e.clientX, e.clientY];
            } else if(e.shiftKey) {
                point2 = [e.clientX, e.clientY];
            } else {
                point3 = [e.clientX, e.clientY];
            }
            ctx5.clearRect(0, 0, 540, 140);
            draw();
        }

        function draw() {

            ctx5.fillStyle = "yellow";
            ctx5.strokeStyle = "black";
            ctx5.lineWidth = 4;

            ctx5.beginPath();
            ctx5.moveTo(point1[0], point1[1]);
            ctx5.arcTo(point2[0], point2[1], point3[0], point3[1], 50);
            ctx5.stroke();

            drawPoint(point1[0]
  


 

您可能想查找下面的文章:

  • HTML5知识点总结
  • HTML5的本地存储
  • HTML5本地存储之IndexedDB
  • Html5实现文件异步上传功能
  • Html5新标签datalist实现输入框与后台数据库数据的动态匹配
  • 详解HTML5 window.postMessage与跨域
  • HTML5拖放API实现拖放排序的实例代码
  • 解决html5中video标签无法播放mp4问题的办法
  • HTML5新特性 多线程(Worker SharedWorker)
  • Html5新增标签有哪些

相关文章

  • 2018-12-03HTML5 Web 存储详解
  • 2018-12-03怎样使用JS获取函数参数名称
  • 2018-12-03HTML5之SVG 2D入门5—颜色的表示及定义方式_html5教程技巧
  • 2018-12-03HTML5之SVG 2D入门12—SVG DOM及DOM操作介绍_html5教程技巧
  • 2017-08-06phonegap常用事件总结(必看篇)
  • 2018-12-03font-weight:blod的跳动问题怎样用CSS解决
  • 2018-12-03HTML5计时器小例子_html5教程技巧
  • 2018-12-03html5文字阴影效果text-shadow使用示例_html5教程技巧
  • 2018-12-03HTML5--多媒体标签详解
  • 2017-08-06HTML5中原生的右键菜单创建方法

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • NodeJS与HTML5相结合实现拖拽多个文件上传到服务器的实现方法
    • html/css应用的方法
    • 使用HTML5的链接预取功能(link prefetching)给网站提速_html5教程技巧
    • HTML5边玩边学(五)-图像、图案和字体
    • HTML5 Canvas画线技巧——实现绘制一个像素宽的细线
    • 关于 HTML5 的七个传说小结_html5教程技巧
    • 用HTML5制作一个简单的弹力球游戏
    • HTML5实践-灰色图片画廊实现的方法
    • html5 学习简单的拾色器 _html5教程技巧
    • HTML5 canvas 基本语法_html5教程技巧

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有