|
| 1 | +import React from "react"; |
| 2 | +import "./styles.css"; |
| 3 | + |
| 4 | +export default class Carousel extends React.Component { |
| 5 | + constructor(props) { |
| 6 | + super(props); |
| 7 | + |
| 8 | + this.draggableRef = React.createRef(); |
| 9 | + |
| 10 | + this.state = { |
| 11 | + dragging: false, |
| 12 | + dragStartX: 0, |
| 13 | + dragStartTime: 0, |
| 14 | + carouselPosition: 0, //This can be a value between 0 - 360, |
| 15 | + change: 0, |
| 16 | + width: 0, |
| 17 | + velocity: 0 |
| 18 | + }; |
| 19 | + |
| 20 | + this.onDragStartMouse = this.onDragStartMouse.bind(this); |
| 21 | + this.onDragStartTouch = this.onDragStartTouch.bind(this); |
| 22 | + this.onMouseMove = this.onMouseMove.bind(this); |
| 23 | + this.onTouchMove = this.onTouchMove.bind(this); |
| 24 | + this.updatePosition = this.updatePosition.bind(this); |
| 25 | + this.onDragEnd = this.onDragEnd.bind(this); |
| 26 | + this.onDragStart = this.onDragStart.bind(this); |
| 27 | + this.onDragEndMouse = this.onDragEndMouse.bind(this); |
| 28 | + this.onDragEndTouch = this.onDragEndTouch.bind(this); |
| 29 | + } |
| 30 | + |
| 31 | + componentDidMount() { |
| 32 | + window.addEventListener("mouseup", this.onDragEndMouse); |
| 33 | + window.addEventListener("touchend", this.onDragEndTouch); |
| 34 | + this.setState(() => ({ |
| 35 | + width: this.draggableRef.current.getBoundingClientRect().width |
| 36 | + })); |
| 37 | + } |
| 38 | + |
| 39 | + componentWillUnmount() { |
| 40 | + window.removeEventListener("mouseup", this.onDragEndMouse); |
| 41 | + window.removeEventListener("touchend", this.onDragEndTouch); |
| 42 | + } |
| 43 | + |
| 44 | + onDragStart(clientX) { |
| 45 | + this.setState(() => ({ dragStartX: clientX, dragging: true })); |
| 46 | + requestAnimationFrame(this.updatePosition); |
| 47 | + } |
| 48 | + |
| 49 | + onDragEnd() { |
| 50 | + if (this.state.dragging) { |
| 51 | + this.setState(() => ({ dragging: false, change: 0 })); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + updatePosition() { |
| 56 | + //When we start dragging updatePosition is called once, then it must keep |
| 57 | + //calling itself so we can continue to update the position of the current item, |
| 58 | + //thats why the following line is here, it will terminate when we mouseup and dragging becomes false |
| 59 | + if (this.state.dragging) requestAnimationFrame(this.updatePosition); |
| 60 | + |
| 61 | + const now = Date.now(); |
| 62 | + const elapsed = now - this.state.dragStartTime; |
| 63 | + |
| 64 | + if (this.state.dragging && elapsed > 20) { |
| 65 | + console.log("carouselPos: ", this.state.carouselPosition); |
| 66 | + this.draggableRef.current.style.transform = `rotate3d(0,1,0,${ |
| 67 | + this.state.carouselPosition |
| 68 | + }deg)`; |
| 69 | + |
| 70 | + this.setState(() => ({ dragStartTime: Date.now() })); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + onMouseMove(e) { |
| 75 | + const change = e.clientX - this.state.dragStartX; |
| 76 | + this.setState(prevState => ({ |
| 77 | + change: change, |
| 78 | + carouselPosition: this.calculateCarouselPosition( |
| 79 | + change - prevState.change, |
| 80 | + prevState.carouselPosition, |
| 81 | + prevState.width |
| 82 | + ) |
| 83 | + })); |
| 84 | + } |
| 85 | + |
| 86 | + onTouchMove(e) { |
| 87 | + const touch = e.targetTouches[0]; |
| 88 | + const change = touch.clientX - this.state.dragStartX; |
| 89 | + this.setState(prevState => ({ |
| 90 | + change: change, |
| 91 | + carouselPosition: this.calculateCarouselPosition( |
| 92 | + change - prevState.change, |
| 93 | + prevState.carouselPosition, |
| 94 | + prevState.width |
| 95 | + ) |
| 96 | + })); |
| 97 | + } |
| 98 | + |
| 99 | + onDragStartMouse(e) { |
| 100 | + this.onDragStart(e.clientX); |
| 101 | + window.addEventListener("mousemove", this.onMouseMove); |
| 102 | + } |
| 103 | + |
| 104 | + onDragStartTouch(e) { |
| 105 | + const touch = e.targetTouches[0]; |
| 106 | + this.onDragStart(touch.clientX); |
| 107 | + window.addEventListener("touchmove", this.onTouchMove); |
| 108 | + } |
| 109 | + |
| 110 | + onDragEndMouse(e) { |
| 111 | + window.removeEventListener("mousemove", this.onMouseMove); |
| 112 | + this.onDragEnd(); |
| 113 | + } |
| 114 | + |
| 115 | + onDragEndTouch(e) { |
| 116 | + window.removeEventListener("touchmove", this.onTouchMove); |
| 117 | + this.onDragEnd(); |
| 118 | + } |
| 119 | + |
| 120 | + calculateCarouselPosition(currentMovement, previousCarouselState, width) { |
| 121 | + //current movement is something like +40 or -20 or whatever, depending on left or right movement |
| 122 | + // previouCarousel is something between 1 & 360, so we need to map changes to that, we may need to factor in the |
| 123 | + // component width when mapping the currentMovement to the carousel change |
| 124 | + const cappedMovement = |
| 125 | + currentMovement > 0 |
| 126 | + ? Math.min(currentMovement, width) |
| 127 | + : Math.max(currentMovement, -width); |
| 128 | + const degreesMoved = (cappedMovement / width) * 360 + previousCarouselState; //may be -30 degrees or + 10 degrees? who knows |
| 129 | + //which percentage of the current div have we moved? and how many degrees is that for our 360 carousel |
| 130 | + //If mappedCarouselMovement + previousCarousel state is > 360, then we take the amount we go over by and instead add that to 0, |
| 131 | + //so we always have a number between 0 and 360. Same for < 0, if its -30 after the calculation, then we take that different and |
| 132 | + //subtract it from 360 |
| 133 | + const currentCarouselDegrees = |
| 134 | + degreesMoved > 360 |
| 135 | + ? degreesMoved - 360 |
| 136 | + : degreesMoved < 0 |
| 137 | + ? degreesMoved + 360 |
| 138 | + : degreesMoved; |
| 139 | + |
| 140 | + return currentCarouselDegrees; |
| 141 | + } |
| 142 | + |
| 143 | + render() { |
| 144 | + const { change, carouselPosition } = this.state; |
| 145 | + |
| 146 | + return ( |
| 147 | + <div> |
| 148 | + <h1>hey</h1> |
| 149 | + <div |
| 150 | + ref={this.draggableRef} |
| 151 | + onMouseDown={this.onDragStartMouse} |
| 152 | + onTouchStart={this.onDragStartTouch} |
| 153 | + className="spinText" |
| 154 | + > |
| 155 | + touch move me wherever necessary |
| 156 | + </div> |
| 157 | + {/* <Carousel360 imageNumber={carouselPosition} /> */} |
| 158 | + {carouselPosition} |
| 159 | + </div> |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments