6c32711d97c06670389cac6db50b25f2e417b1d64a1b06292165045ea68369d7a0692125f458e14cd52e6279e4d177213a30a0da616a2ab72bff9a2a56b404 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <div class="el-color-svpanel"
  3. :style="{
  4. backgroundColor: background
  5. }">
  6. <div class="el-color-svpanel__white"></div>
  7. <div class="el-color-svpanel__black"></div>
  8. <div class="el-color-svpanel__cursor"
  9. :style="{
  10. top: cursorTop + 'px',
  11. left: cursorLeft + 'px'
  12. }">
  13. <div></div>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. import draggable from '../draggable';
  19. export default {
  20. name: 'el-sl-panel',
  21. props: {
  22. color: {
  23. required: true
  24. }
  25. },
  26. computed: {
  27. colorValue() {
  28. const hue = this.color.get('hue');
  29. const value = this.color.get('value');
  30. return { hue, value };
  31. }
  32. },
  33. watch: {
  34. colorValue() {
  35. this.update();
  36. }
  37. },
  38. methods: {
  39. update() {
  40. const saturation = this.color.get('saturation');
  41. const value = this.color.get('value');
  42. const el = this.$el;
  43. let { clientWidth: width, clientHeight: height } = el;
  44. this.cursorLeft = saturation * width / 100;
  45. this.cursorTop = (100 - value) * height / 100;
  46. this.background = 'hsl(' + this.color.get('hue') + ', 100%, 50%)';
  47. },
  48. handleDrag(event) {
  49. const el = this.$el;
  50. const rect = el.getBoundingClientRect();
  51. let left = event.clientX - rect.left;
  52. let top = event.clientY - rect.top;
  53. left = Math.max(0, left);
  54. left = Math.min(left, rect.width);
  55. top = Math.max(0, top);
  56. top = Math.min(top, rect.height);
  57. this.cursorLeft = left;
  58. this.cursorTop = top;
  59. this.color.set({
  60. saturation: left / rect.width * 100,
  61. value: 100 - top / rect.height * 100
  62. });
  63. }
  64. },
  65. mounted() {
  66. draggable(this.$el, {
  67. drag: (event) => {
  68. this.handleDrag(event);
  69. },
  70. end: (event) => {
  71. this.handleDrag(event);
  72. }
  73. });
  74. this.update();
  75. },
  76. data() {
  77. return {
  78. cursorTop: 0,
  79. cursorLeft: 0,
  80. background: 'hsl(0, 100%, 50%)'
  81. };
  82. }
  83. };
  84. </script>