用javascript在canvs上绘制…

你好,我看了你写的这个是基于html5做的,我想问一下有没有基于纯javascript写的算法。
转自: http://jo2.org/html5-canvas-round-rect/

源码:

    //圆角矩形
    CanvasRenderingContext2D .prototype.roundRect = function (x, y, w, h, r) {
        //if (w < 2 * r) r = w / 2;
        //if (h < 2 * r) r = h / 2;
        this.beginPath();
        this.moveTo(x+r, y);
        this.arcTo(x+w, y, x+w, y+h, r);
        this.arcTo(x+w, y+h, x, y+h, r);
        this.arcTo(x, y+h, x, y, r);
        this.arcTo(x, y, x+w, y, r);
        // this.arcTo(x+r, y);
        this.closePath();
        return this;
    }

使用:

var c=document.getElementByIdx_x('myCanvas');  
var ctx=c.getContext('2d');
ctx. roundRect ( 200 , 300 , 200 , 120 , 20 ) . stroke ( ) ;


这种方法可以扩展 ContextRenderingContext2D原型
那么什么是ContextRenderingContext2D呢? W3C网站上有详细的说明: http://www.w3.org/html/ig/zh/wiki/2dcontext#canvasrenderingcontext2d

扩展 ContextRenderingContext2D原型的好处是,可以将扩展函数写在独立的.js文件中,然后在html中加入该脚本文件就可以了,如:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值