网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 HTML5 Canvas绘制圆点虚线实例
内容
    HTML5 Canvas 提供了很多图形绘制的函数,但是很可惜,Canvas API只提供了画实线的函数(lineTo),却并未提供画虚线的方法。这样的设计有时会带来很大的不便,《JavaScript权威指南》的作者David Flanagan就认为这样的决定是有问题的,尤其是在标准的修改和实现都比较简单的情况下 (“…something that is so trivial to add to the specification and so trivial to implement… I really think you’re making a mistake here” —)。
    在Stack Overflow上,Phrogz提供了一个自己的画虚线的实现(),严格的说,这是一个点划线的实现(p.s. 我认为该页面上Rod MacDougall的简化版更好)。那么,如果需要画圆点虚线(如下图所示),应该怎么办呢?
    名单
    以下是我自己的实现,只支持画水平的和垂直的圆点虚线,可以参考Phrogz与Rod MacDougall的方法来添加斜线描画的功能。
    代码如下:
    var canvasPrototype = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
    canvasPrototype.dottedLine = function(x1, y1, x2, y2, interval) {
    if (!interval) {
    interval = 5;
    }
    var isHorizontal=true;
    if (x1==x2){
    isHorizontal=false;
    }
    var len = isHorizontal ? x2-x1 : y2-y1;
    this.moveTo(x1, y1);
    var progress=0;
    while (len > progress) {
    progress += interval;
    if (progress > len) {
    progress = len;
    }
    if (isHorizontal) {
    this.moveTo(x1+progress,y1);
    this.arc(x1+progress,y1,1,0,2*Math.PI,true);
    this.fill();
    } else {
    this.moveTo(x1,y1+progress);
    this.arc(x1,y1+progress,1,0,2*Math.PI,true);
    this.fill();
    }
    }
    }
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/18 12:23:16