v-coupon 一款基于 vue 的卡券组件
介绍
你是否在心里暗骂设计师设计出这种有缺口的卡券?
你是否试了多种 CSS 方案都无法完美实现?
你是否在为不得不用 jpg/png...等格式而内心挣扎?
v-coupon 就是为了解决以上痛点而诞生的。
先看看效果

如果上图你有看到白色背景,那不是 bug, 是我截图的时候页面的背景就是白色的
使用
npm i v-coupon
import coupon from 'v-coupon'
Vue.use(coupon)
<v-coupon config="yourConfig">
<div class="content">
你要展示的内容
</div>
</v-coupon>
当然,你也许不需要全局引入,可以直接在需要使用的页面注册 v-coupon/src/coupon.vue 组件
import vCoupon from 'v-coupon/src/coupon.vue'
components: {
'v-coupon': vCoupon
}
详细说明
<v-coupon>...</v-coupon> 之间包含的内容,是一个 slot, 这意味这 v-coupon 只提供一个裁切模版,其他内容你可以任意定制,给予你最大的发挥空间,但同时为了避免各种复杂的问题,只能有一个slot, 以下代码的第二个 div 不会被显示出来
<v-coupon config="yourConfig">
<div class="content">
第一个 div 的内容,会正常显示
</div>
<div>
第二个 div 的内容,不会显示出来
</div>
</v-coupon>
- 配置, 上例的
yourConfig 就是我们要传给 v-coupon 的配置,具体的配置项如下
{
width: 200,
height: 300,
borderRadius: 15,
borderColor: '#33cc44',
borderWidth: 0.5,
borderOpcity: 1,
borderDash: null,
showLine: true,
lineColor: '#33cc44',
lineWidth: 0.4,
lineOpcity: 1,
lineDash: [3, 5],
lineOffset: 5,
cutPosition: 200,
cutRadius: 10,
cutSlope: 1.5,
background: '#ffffff'
}
以上单位, 除了裁切口弧度 cutSlope 外,其他数值的单位均为 px, 暂未对其他单位做支持.
上面的配置的值, 其实就是 v-coupon 的默认值, 采用 Object.assign 的方式合并用户配置跟默认值, 也就是说, 你不必传所有的配置,没有传的会采用默认值
边框虚线数组 和 分割线虚线数组 有不明白的, 可以参考 MDN:SGV的stroke-dasharray属性
兼容
v-coupon 的核心内容是 SVG 的 foreignObject, 具体的兼容请参考 Can I Use
其他
有任何 bug, 需求, 意见, 建议, 吐槽, 欢迎走 issue 通道, 同时也欢迎 pr.