2018-12-11 14:10:59 +08:00
|
|
|
import { filterNull } from './methodsExtend'
|
|
|
|
|
2018-12-10 18:23:02 +08:00
|
|
|
export function drawLine (ctx, lineBegin, lineEnd, lineWidth = 2, lineColor = '#000', dashArray = [10, 0]) {
|
2018-12-07 15:50:10 +08:00
|
|
|
if (!ctx || !lineBegin || !lineEnd) return
|
|
|
|
|
|
|
|
ctx.beginPath()
|
|
|
|
|
|
|
|
ctx.moveTo(...lineBegin)
|
|
|
|
ctx.lineTo(...lineEnd)
|
|
|
|
|
|
|
|
ctx.closePath()
|
|
|
|
|
|
|
|
ctx.lineWidth = lineWidth
|
|
|
|
ctx.strokeStyle = lineColor
|
2018-12-08 16:05:19 +08:00
|
|
|
ctx.setLineDash(dashArray)
|
|
|
|
|
|
|
|
ctx.stroke()
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function drawPolylinePath (ctx, points, close = false, newPath = false) {
|
2018-12-08 16:05:19 +08:00
|
|
|
if (!ctx || !points.length) return
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
newPath && ctx.beginPath()
|
|
|
|
|
2018-12-10 18:23:02 +08:00
|
|
|
points.forEach((point, i) =>
|
|
|
|
point && (i === 0 ? ctx.moveTo(...point) : ctx.lineTo(...point)))
|
2018-12-08 16:05:19 +08:00
|
|
|
|
|
|
|
close && ctx.lineTo(...points[0])
|
2018-12-10 18:23:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function drawPolyline (ctx, points, lineWidth = 2, lineColor = '#000', close = false, dashArray = [10, 0], newPath = false) {
|
2018-12-10 18:23:02 +08:00
|
|
|
if (!ctx || !points.length) return
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
drawPolylinePath(ctx, points, close, newPath)
|
2018-12-08 16:05:19 +08:00
|
|
|
|
|
|
|
ctx.lineWidth = lineWidth
|
|
|
|
ctx.strokeStyle = lineColor
|
|
|
|
ctx.setLineDash(dashArray)
|
2018-12-07 15:50:10 +08:00
|
|
|
|
|
|
|
ctx.stroke()
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function drawSmoothlinePath (ctx, points, close = false, newPath = false, moveTo = false) {
|
|
|
|
const canDrawPoints = filterNull(points)
|
2018-12-10 18:23:02 +08:00
|
|
|
|
2018-12-13 18:48:12 +08:00
|
|
|
if (!ctx || canDrawPoints.length < 2) return
|
2018-12-10 18:23:02 +08:00
|
|
|
|
2018-12-13 18:48:12 +08:00
|
|
|
close && canDrawPoints.push(canDrawPoints[0])
|
2018-12-10 18:23:02 +08:00
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
newPath && ctx.beginPath()
|
2018-12-13 18:48:12 +08:00
|
|
|
|
|
|
|
if (canDrawPoints.length === 2) {
|
|
|
|
ctx.moveTo(...canDrawPoints[0])
|
|
|
|
ctx.lineTo(...canDrawPoints[1])
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastPointIndex = canDrawPoints.length - 1
|
2018-12-10 18:23:02 +08:00
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
moveTo && ctx.moveTo(...canDrawPoints[0])
|
|
|
|
|
|
|
|
canDrawPoints.forEach((t, i) =>
|
|
|
|
(i !== lastPointIndex) && drawBezierCurveLinePath(ctx,
|
|
|
|
...getBezierCurveLineControlPoints(canDrawPoints, i, false),
|
|
|
|
canDrawPoints[i + 1]))
|
2018-12-10 18:23:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function getBezierCurveLineControlPoints (points, index, close = false, offsetA = 0.25, offsetB = 0.25) {
|
|
|
|
const pointNum = points.length
|
|
|
|
|
|
|
|
if (pointNum < 3 || index >= pointNum) return
|
|
|
|
|
|
|
|
let beforePointIndex = index - 1
|
|
|
|
beforePointIndex < 0 && (beforePointIndex = (close ? pointNum + beforePointIndex : 0))
|
|
|
|
|
|
|
|
let afterPointIndex = index + 1
|
|
|
|
afterPointIndex >= pointNum && (afterPointIndex = (close ? afterPointIndex - pointNum : pointNum - 1))
|
|
|
|
|
|
|
|
let afterNextPointIndex = index + 2
|
|
|
|
afterNextPointIndex >= pointNum && (afterNextPointIndex = (close ? afterNextPointIndex - pointNum : pointNum - 1))
|
|
|
|
|
|
|
|
const pointBefore = points[beforePointIndex]
|
|
|
|
const pointMiddle = points[index]
|
|
|
|
const pointAfter = points[afterPointIndex]
|
|
|
|
const pointAfterNext = points[afterNextPointIndex]
|
2018-12-10 18:23:02 +08:00
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
2018-12-11 14:10:59 +08:00
|
|
|
pointMiddle[0] + offsetA * (pointAfter[0] - pointBefore[0]),
|
|
|
|
pointMiddle[1] + offsetA * (pointAfter[1] - pointBefore[1])
|
2018-12-10 18:23:02 +08:00
|
|
|
],
|
|
|
|
[
|
2018-12-11 14:10:59 +08:00
|
|
|
pointAfter[0] - offsetB * (pointAfterNext[0] - pointMiddle[0]),
|
|
|
|
pointAfter[1] - offsetB * (pointAfterNext[1] - pointMiddle[1])
|
2018-12-10 18:23:02 +08:00
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function drawSmoothline (ctx, points, lineWidth = 2, lineColor = '#000', close = false, dashArray = [10, 0], newPath = false, moveTo = false) {
|
|
|
|
if (!ctx || points.length < 3) return
|
2018-12-10 18:23:02 +08:00
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
drawSmoothlinePath(ctx, points, close, newPath, moveTo)
|
|
|
|
|
|
|
|
ctx.lineWidth = lineWidth
|
|
|
|
ctx.strokeStyle = lineColor
|
|
|
|
ctx.setLineDash(dashArray)
|
|
|
|
|
|
|
|
ctx.stroke()
|
2018-12-10 18:23:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function drawBezierCurveLinePath (ctx, ctlBefore, ctlAfter, end, newPath = false) {
|
|
|
|
if (!ctx || !ctlBefore || !ctlAfter || !end) return
|
2018-12-10 18:23:02 +08:00
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
newPath && ctx.beginPath()
|
|
|
|
|
|
|
|
ctx.bezierCurveTo(...ctlBefore, ...ctlAfter, ...end)
|
2018-12-10 18:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function drawPoints (ctx, points, radius = 10, color = '#000') {
|
|
|
|
points.forEach(point => {
|
|
|
|
if (!point) return
|
|
|
|
|
|
|
|
ctx.beginPath()
|
|
|
|
|
|
|
|
ctx.arc(...point, radius, 0, Math.PI * 2)
|
|
|
|
|
|
|
|
ctx.fillStyle = color
|
|
|
|
|
|
|
|
ctx.fill()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:10:59 +08:00
|
|
|
export function getLinearGradientColor (ctx, begin, end, color) {
|
|
|
|
if (!ctx || !begin || !end || !color.length) return
|
|
|
|
|
|
|
|
let colors = color
|
|
|
|
|
|
|
|
typeof colors === 'string' && (colors = [color, color])
|
|
|
|
|
|
|
|
const linearGradientColor = ctx.createLinearGradient(...begin, ...end)
|
|
|
|
|
|
|
|
const colorGap = 1 / (colors.length - 1)
|
|
|
|
|
|
|
|
colors.forEach((c, i) => linearGradientColor.addColorStop(colorGap * i, c))
|
|
|
|
|
|
|
|
return linearGradientColor
|
|
|
|
}
|
|
|
|
|
2018-12-15 19:16:31 +08:00
|
|
|
export function getRadialGradientColor (ctx, origin, begin = 0, end = 100, color) {
|
|
|
|
if (!ctx || !origin || !color.length) return
|
|
|
|
|
|
|
|
let colors = color
|
|
|
|
|
|
|
|
typeof colors === 'string' && (colors = [color, color])
|
|
|
|
|
|
|
|
const radialGradientColor = ctx.createRadialGradient(...origin, begin, ...origin, end)
|
|
|
|
|
|
|
|
const colorGap = 1 / (colors.length - 1)
|
|
|
|
|
|
|
|
colors.forEach((c, i) => radialGradientColor.addColorStop(colorGap * i, c))
|
|
|
|
|
|
|
|
return radialGradientColor
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:47:47 +08:00
|
|
|
export function getCircleRadianPoint (x, y, radius, radian) {
|
|
|
|
const { sin, cos } = Math
|
|
|
|
|
|
|
|
return [x + cos(radian) * radius, y + sin(radian) * radius]
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:25:49 +08:00
|
|
|
export function getTextsWidth (ctx, texts) {
|
|
|
|
if (!ctx || !texts) return
|
|
|
|
|
|
|
|
return texts.map(text => ctx.measureText(text).width)
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:50:10 +08:00
|
|
|
const canvas = {
|
2018-12-08 16:05:19 +08:00
|
|
|
drawLine,
|
2018-12-10 18:23:02 +08:00
|
|
|
drawPolylinePath,
|
|
|
|
drawPolyline,
|
2018-12-11 14:10:59 +08:00
|
|
|
getBezierCurveLineControlPoints,
|
2018-12-10 18:23:02 +08:00
|
|
|
drawSmoothlinePath,
|
|
|
|
drawSmoothline,
|
2018-12-11 14:10:59 +08:00
|
|
|
drawBezierCurveLinePath,
|
|
|
|
drawPoints,
|
2018-12-12 18:47:47 +08:00
|
|
|
getLinearGradientColor,
|
2018-12-15 19:16:31 +08:00
|
|
|
getRadialGradientColor,
|
2018-12-20 18:25:49 +08:00
|
|
|
getCircleRadianPoint,
|
|
|
|
getTextsWidth
|
2018-12-07 15:50:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function (Vue) {
|
|
|
|
Vue.prototype.canvas = canvas
|
|
|
|
}
|