From f45f7f742d4d78e73b41a000b40b22e734ff7235 Mon Sep 17 00:00:00 2001 From: jiaming <743192023@qq.com> Date: Sat, 15 Dec 2018 19:16:31 +0800 Subject: [PATCH] add radial gradient color fun --- src/plugins/canvasExtend.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/plugins/canvasExtend.js b/src/plugins/canvasExtend.js index f5a93ce..4f27fad 100644 --- a/src/plugins/canvasExtend.js +++ b/src/plugins/canvasExtend.js @@ -147,6 +147,22 @@ export function getLinearGradientColor (ctx, begin, end, color) { return linearGradientColor } +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 +} + export function getCircleRadianPoint (x, y, radius, radian) { const { sin, cos } = Math @@ -163,6 +179,7 @@ const canvas = { drawBezierCurveLinePath, drawPoints, getLinearGradientColor, + getRadialGradientColor, getCircleRadianPoint }