canvas 画圆角图片

canvas 画圆角图片

const util = require('./util.js');


      let params ={
        left:left, top:top, width:width, height:height, borderRadius:10}
      ctx.save()
      util.toDrawRadiusRect(params,ctx) 
      ctx.strokeStyle = 'rgba(255,255,255,0)'
      ctx.stroke()
      ctx.clip()
      ctx.drawImage(Imagepath, left, top, imgwidth, imgheight);
      ctx.restore()
const toDrawRect = (params, ctx) => {
  const {
    left, top, width, height, borderRadius,
    borderTopLeftRadius, borderTopRightRadius, borderBottomRightRadius, borderBottomLeftRadius
  } = params
  ctx.beginPath()
  if (borderRadius) {
    const br = borderRadius / 2
    ctx.moveTo(left + br, top) 
    ctx.lineTo(left + width - br, top) 
    ctx.arcTo(left + width, top, left + width, top + br, br) 
    ctx.lineTo(left + width, top + height - br) 
    ctx.arcTo(left + width, top + height, left + width - br, top + height, br) 
    ctx.lineTo(left + br, top + height)
    ctx.arcTo(left, top + height, left, top + height - br, br)
    ctx.lineTo(left, top + br) 
    ctx.arcTo(left, top, left + br, top, br)
  } else {
    const topLeftBr = borderTopLeftRadius ? borderTopLeftRadius / 2 : 0
    const topRightBr = borderTopRightRadius ? borderTopRightRadius / 2 : 0
    const bottomRightBr = borderBottomRightRadius ? borderBottomRightRadius / 2 : 0
    const bottomLeftBr = borderBottomLeftRadius ? borderBottomLeftRadius / 2 : 0
    ctx.moveTo(left + topLeftBr, top)
    ctx.lineTo(left + width - topRightBr, top)
    if (topRightBr) {
      ctx.arcTo(left + width, top, left + width, top + topRightBr, topRightBr)
    }
    ctx.lineTo(left + width, top + height - bottomRightBr)
    if (bottomRightBr) {
      ctx.arcTo(left + width, top + height,
        left + width - bottomRightBr, top + height, bottomRightBr)
    }
    ctx.lineTo(left + bottomLeftBr, top + height)
    if (bottomLeftBr) {
      ctx.arcTo(left, top + height, left, top + height - bottomLeftBr, bottomLeftBr)
    }
    ctx.lineTo(left, top + topLeftBr)
    if (topLeftBr) {
      ctx.arcTo(left, top, left + topLeftBr, top, topLeftBr)
    }
  }
}
module.exports = {
  toDrawRect :toDrawRect 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值