跪拜 Guibai
← All articles
Frontend · Vue.js · Data Visualization

One Vue 3 Component Handles Solid Colors, Gradients, and Canvas Eyedropper Picking

By 雨夜寻晴天 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Design tools, low-code builders, and data visualization dashboards frequently need color picking beyond the native input, and this library collapses three separate libraries and their state management into one coherent package. Its screenshot-based eyedropper and multi-user history isolation solve real integration pain points that Western developers building similar web apps often hit.

Summary

The library, @you-want/color-picker-lib, ships three components: ColorPicker for solid and gradient palettes, EyeDropper for screenshot-based canvas sampling, and SliderColorSetting for gradient stop editing. It avoids the browser's native EyeDropper API by using snapdom to capture the target area and sample through Canvas, making it work wherever Canvas is available.

The package separates overall transparency from per-stop alpha to prevent data pollution, always writes back new array references for gradient stops to satisfy canvas redraw triggers like Konva, and provides a session-commit event that fires only once per edit session. It supports themeing via five CSS variables, requires only Vue 3.5+ and Element Plus 2.6+, and comes with 196 automated tests covering parsing, coordinates, SSR, and keyboard interaction.

The build is 71.81 kB JS (20.92 kB gzip) plus 12.80 kB CSS (3.62 kB gzip), excluding Element Plus. Known gaps include no support for var(), calc(), radial gradients, or CSS Color 4 spaces, and real-browser verification is currently Chrome-centric.

Takeaways
— The library provides three Vue 3 components: ColorPicker, EyeDropper, and SliderColorSetting.
— It handles solid colors and linear gradients in one panel, with preset palette replacement, recent colors, and a custom trigger slot.
— Gradient editing supports adding/deleting stops, dragging, angle input, 90° rotation, and flipping color order.
— The canvas eyedropper uses snapdom for screenshot sampling instead of the native EyeDropper API, so it works in any Canvas-capable environment.
— Overall transparency (fill-opacity) is stored separately from per-stop alpha to avoid recolor pollution when opacity is toggled.
— Gradient stops are always written back as new arrays, satisfying canvas libraries like Konva that trigger redraws on reference changes.
— Recently used colors are stored per user (via a getUserId callback) and only persisted when the user is logged in.
— Minimum peer dependencies are vue ^3.5.0 and element-plus >=2.6.0.
— The library supports SSR and doesn't bind to Pinia or any store; getUserId is passed in by the host.
— The build is 71.81 kB JS (20.92 kB gzip) and 12.80 kB CSS (3.62 kB gzip), excluding Element Plus.
— Current limitations include no var(), calc(), radial gradients, or CSS Color 4 color spaces; browser testing is mainly Chrome.
— A utility, horizontalGradientBorderStyle, generates rectangle gradient borders with proper corner handling.
Conclusions

The screenshot-based eyedropper is a deliberate workaround for native API availability, trading a dependency on snapdom for broader reach across browsers and environments.

Separating raw color from effective color prevents a common bug where users accidentally multiply alpha twice and end up with faded colors in design tools.

The library's decision to force new array references for gradient stops is a pragmatic fix for canvas redraw mechanisms, but it imposes a specific update pattern on host code.

By requiring Element Plus as a peer dependency, the library leverages existing UI infrastructure but limits adoption in projects that don't already use Element Plus.

The explicit list of unsupported color formats (var(), calc(), radial gradients) is a refreshingly honest contract; it prevents silent misinterpretation of CSS values.

Concepts & terms
Effective color vs. raw color
In this library, raw color stores the user's original per-stop alpha values, while effective color is the final rendered value that multiplies overall transparency (fill-opacity) into the color. The two are kept separate so toggling overall opacity doesn't destroy the original alpha data.
snapdom
A screenshot utility that captures a DOM element and renders it into a canvas or image. Here it's used to screenshot the target area for the eyedropper, allowing pixel sampling without depending on the browser's native EyeDropper API.
SSR (Server-Side Rendering) safety
The library avoids direct access to `window` during render, so it can be rendered on the server without throwing errors. This is a common requirement for Vue/Nuxt applications.
Color stop array reference
Canvas libraries like Konva detect changes by checking whether an array reference has changed. Returning a new array on every edit forces a redraw, whereas mutating the same array in place won't trigger any update.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗