A Draggable Bezier Curve Editor for ECharts That Exports Reusable Configs
ECharts configurations are normally static JSON blobs; adding direct manipulation turns the library into a visual design tool. The export step closes the loop — a designer or developer can shape a curve by eye, then drop the exact math into production code without manual fitting or guesswork.
A single-file HTML tool wraps ECharts with a full direct-manipulation editor for cubic Bezier curves. Four draggable points — start, end, and two intermediate controls — plus two rotatable handles give fine-grained control over the curve shape. Every drag fires a re-render so the curve updates in real time.
Hit-testing on the canvas detects which element the cursor is over, and mouse events translate pixel coordinates back into data space. The tool clamps all coordinates to [0,1] and uses a 0.8 damping factor when dragging handles to keep movement stable.
Two export paths cover different workflows. “Export Code” produces a commented JavaScript snippet with the control-point coordinates, the cubic Bezier function, a curve generator, and an ECharts usage example. “Export Data” dumps a JSON object containing the coordinates, the expanded polynomial coefficients, and the parametric formulas for x(t) and y(t). Both can be copied to the clipboard or downloaded as a file.
ECharts is rarely used for interactive editing, but its pixel-to-data coordinate conversion API makes direct manipulation feasible without external geometry libraries.
The handle-dragging implementation uses a damping factor rather than a fixed-length constraint, which feels more forgiving but means the handle length changes during drag — a trade-off between mathematical purity and UX smoothness.
Exporting the expanded polynomial coefficients alongside the control points is a practical touch: it saves the consumer from re-deriving the math when they need to evaluate the curve outside a Bezier function.