UNPKG

@antv/g2

Version:

the Grammar of Graphics in Javascript

79 lines 3.5 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { isFunction } from '@antv/util'; import { addWave } from './wave'; import { LiquidShapesPath } from './shapes'; const getLiquidShape = (shape = 'circle') => LiquidShapesPath[shape] || LiquidShapesPath.circle; export const Liquid = (options, context) => { if (!context) return; const { coordinate } = context; const { liquidOptions, styleOptions } = options; const { liquidShape, percent } = liquidOptions; const { background: backgroundStyle, outline = {}, wave = {} } = styleOptions, attr = __rest(styleOptions, ["background", "outline", "wave"]); const { border = 2, distance = 0 } = outline, outlineStyle = __rest(outline, ["border", "distance"]); const { length = 192, count = 3 } = wave; return (points, cfg, defaultAttr) => { const { document } = context.canvas; const { color, fillOpacity } = defaultAttr; const attrs = Object.assign(Object.assign({ fill: color }, defaultAttr), attr); const g = document.createElement('g', {}); // Center x/y. const [centerX, centerY] = coordinate.getCenter(); // [width,height]. const size = coordinate.getSize(); const radius = Math.min(...size) / 2; // 1、Gets the path of the overall shape. const buildPath = isFunction(liquidShape) ? liquidShape : getLiquidShape(liquidShape); const shapePath = buildPath(centerX, centerY, radius, ...size); // 2、Background create. if (Object.keys(backgroundStyle).length) { const backgroundShape = document.createElement('path', { style: Object.assign({ d: shapePath, fill: '#fff' }, backgroundStyle), }); g.appendChild(backgroundShape); } // Percent > 0 Mapping water waves. if (percent > 0) { // 3. Clip create. const clipShape = document.createElement('path', { style: { d: shapePath, }, }); g.appendChild(clipShape); g.style.clipPath = clipShape; // 4. Wave create. addWave(centerX, centerY, 1 - percent, count, attrs, g, clipShape.getBBox().y, radius * 2, length, true, document); } // 5. Draw distance. const distanceShape = document.createElement('path', { style: { d: shapePath, fill: 'transparent', lineWidth: border + 2 * distance, stroke: '#fff', }, }); // 6. Draw border. const borderShape = document.createElement('path', { style: Object.assign(Object.assign(Object.assign({ d: shapePath, stroke: color, strokeOpacity: fillOpacity, lineWidth: border }, attrs), outlineStyle), { fill: 'transparent' }), }); g.appendChild(distanceShape); g.appendChild(borderShape); return g; }; }; Liquid.props = {}; //# sourceMappingURL=liquid.js.map