|
| 1 | +import $ from 'dom7'; |
| 2 | + |
| 3 | +function svgWheelCircles() { |
| 4 | + const total = 256; |
| 5 | + let circles = ''; |
| 6 | + for (let i = total; i > 0; i -= 1) { |
| 7 | + const angle = i * Math.PI / (total / 2); |
| 8 | + const hue = 360 / total * i; |
| 9 | + circles += `<circle cx="${150 - Math.sin(angle) * 125}" cy="${150 - Math.cos(angle) * 125}" r="15" fill="hsl(${hue}, 100%, 50%)"></circle>`; |
| 10 | + } |
| 11 | + return circles; |
| 12 | +} |
| 13 | +export default { |
| 14 | + render() { |
| 15 | + return ` |
| 16 | + <div class="color-picker-module color-picker-module-wheel"> |
| 17 | + <div class="color-picker-wheel"> |
| 18 | + <svg viewBox="0 0 300 300" width="300" height="300">${svgWheelCircles()}</svg> |
| 19 | + <div class="color-picker-wheel-handle"></div> |
| 20 | + </div> |
| 21 | + </div> |
| 22 | + `; |
| 23 | + }, |
| 24 | + init(self) { |
| 25 | + const { app } = self; |
| 26 | + |
| 27 | + let isTouched; |
| 28 | + let isMoved; |
| 29 | + let touchStartX; |
| 30 | + let touchStartY; |
| 31 | + let touchCurrentX; |
| 32 | + let touchCurrentY; |
| 33 | + |
| 34 | + let wheelRect; |
| 35 | + let wheelIsTouched; |
| 36 | + let wheelHandleIsTouched; |
| 37 | + |
| 38 | + const { $el } = self; |
| 39 | + |
| 40 | + function setHueFromWheelCoords(x, y) { |
| 41 | + const wheelCenterX = wheelRect.left + wheelRect.width / 2; |
| 42 | + const wheelCenterY = wheelRect.top + wheelRect.height / 2; |
| 43 | + const angleRad = Math.atan2(y - wheelCenterY, x - wheelCenterX); |
| 44 | + let angleDeg = angleRad * 180 / Math.PI + 90; |
| 45 | + if (angleDeg < 0) angleDeg += 360; |
| 46 | + angleDeg = 360 - angleDeg; |
| 47 | + self.setValue({ hue: angleDeg }); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + function handleTouchStart(e) { |
| 52 | + if (isMoved || isTouched) return; |
| 53 | + touchStartX = e.type === 'touchstart' ? e.targetTouches[0].pageX : e.pageX; |
| 54 | + touchCurrentX = touchStartX; |
| 55 | + touchStartY = e.type === 'touchstart' ? e.targetTouches[0].pageY : e.pageY; |
| 56 | + touchCurrentY = touchStartY; |
| 57 | + const $targetEl = $(e.target); |
| 58 | + wheelHandleIsTouched = $targetEl.closest('.color-picker-wheel-handle').length > 0; |
| 59 | + wheelIsTouched = $targetEl.closest('circle').length > 0; |
| 60 | + if (wheelIsTouched) { |
| 61 | + wheelRect = $el.find('.color-picker-wheel')[0].getBoundingClientRect(); |
| 62 | + setHueFromWheelCoords(touchStartX, touchStartY); |
| 63 | + } |
| 64 | + } |
| 65 | + function handleTouchMove(e) { |
| 66 | + if (!(wheelIsTouched || wheelHandleIsTouched)) return; |
| 67 | + touchCurrentX = e.type === 'touchmove' ? e.targetTouches[0].pageX : e.pageX; |
| 68 | + touchCurrentY = e.type === 'touchmove' ? e.targetTouches[0].pageY : e.pageY; |
| 69 | + e.preventDefault(); |
| 70 | + if (!isMoved) { |
| 71 | + // First move |
| 72 | + isMoved = true; |
| 73 | + if (wheelHandleIsTouched) { |
| 74 | + wheelRect = $el.find('.color-picker-wheel')[0].getBoundingClientRect(); |
| 75 | + } |
| 76 | + } |
| 77 | + if (wheelIsTouched || wheelHandleIsTouched) { |
| 78 | + setHueFromWheelCoords(touchCurrentX, touchCurrentY); |
| 79 | + } |
| 80 | + } |
| 81 | + function handleTouchEnd() { |
| 82 | + isMoved = false; |
| 83 | + wheelIsTouched = false; |
| 84 | + wheelHandleIsTouched = false; |
| 85 | + } |
| 86 | + |
| 87 | + function handleResize() { |
| 88 | + self.modules.wheel.update(self); |
| 89 | + } |
| 90 | + |
| 91 | + const passiveListener = app.touchEvents.start === 'touchstart' && app.support.passiveListener ? { passive: true, capture: false } : false; |
| 92 | + |
| 93 | + self.$el.on(app.touchEvents.start, handleTouchStart, passiveListener); |
| 94 | + app.on('touchmove:active', handleTouchMove); |
| 95 | + app.on('touchend:passive', handleTouchEnd); |
| 96 | + app.on('resize', handleResize); |
| 97 | + |
| 98 | + self.destroyWheelEvents = function destroyWheelEvents() { |
| 99 | + self.$el.off(app.touchEvents.start, handleTouchStart, passiveListener); |
| 100 | + app.off('touchmove:active', handleTouchMove); |
| 101 | + app.off('touchend:passive', handleTouchEnd); |
| 102 | + app.off('resize', handleResize); |
| 103 | + }; |
| 104 | + }, |
| 105 | + update(self) { |
| 106 | + const { |
| 107 | + value, |
| 108 | + } = self; |
| 109 | + |
| 110 | + const { hsl, hsb } = value; |
| 111 | + |
| 112 | + const wheelSize = self.$el.find('.color-picker-wheel')[0].offsetWidth; |
| 113 | + const wheelHalfSize = wheelSize / 2; |
| 114 | + const angleRad = value.hue * Math.PI / 180; |
| 115 | + const handleSize = wheelSize / 6; |
| 116 | + const handleHalfSize = handleSize / 2; |
| 117 | + const tX = wheelHalfSize - Math.sin(angleRad) * (wheelHalfSize - handleHalfSize) - handleHalfSize; |
| 118 | + const tY = wheelHalfSize - Math.cos(angleRad) * (wheelHalfSize - handleHalfSize) - handleHalfSize; |
| 119 | + self.$el.find('.color-picker-wheel-handle') |
| 120 | + .css('background-color', `hsl(${hsl[0]}, 100%, 50%)`) |
| 121 | + .transform(`translate(${tX}px, ${tY}px)`); |
| 122 | + }, |
| 123 | + destroy(self) { |
| 124 | + if (self.destroyWheelEvents) self.destroyWheelEvents(); |
| 125 | + delete self.destroyWheelEvents; |
| 126 | + }, |
| 127 | +}; |
0 commit comments