A
A
andrei_pro2019-05-02 21:26:53
JavaScript
andrei_pro, 2019-05-02 21:26:53

Move an object along a path?

Hello.
The task is this: there is a block on the page with a circle next to it, you need to implement dragging the circle only around the block, that is, it travels exactly along the border + 5px.
5ccb37457cefc958449437.png
Actually:
1. Added events:

document.documentElement.addEventListener('mousemove', this.handleMove, true)
            document.documentElement.addEventListener('mousedown', this.deselect, true)
            document.documentElement.addEventListener('mouseup', this.handleUp, true)

2. Methods
deselect(e) {
                const target = e.target || e.srcElement

                if (target.tagName === 'circle') {
                    this.dragging = true
                }
            },
            handleMove(e) {
                e.stopPropagation()
                e.preventDefault()

                const isTouchMove = e.type.indexOf('touchmove') !== -1
                var mouseX = isTouchMove
                    ? e.touches[0].clientX
                    : e.clientX
                var mouseY = isTouchMove
                    ? e.touches[0].clientY
                    : e.clientY

                if (this.dragging) {
                    this.cords1.x = mouseX
                    this.cords1.y = mouseY
                }
            },
            handleUp(e) {
                this.dragging = false
            }

<circle :cx="cords1.x"
            :cy="cords1.y"
            r="4"
            stroke="red"
            fill="red"></circle>

On this, when I click on the circle, it is dragged across the page.
Further, to make conditions for movement along the trajectory through many experiments unsuccessfully.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2019-05-02
@andrei_pro

We draw a figure, we find its center.
When dragging, we draw a straight line between the mouse coordinates and the center of the shape (it is not necessary to draw it).
Place a circle at the point of intersection of this line and the figure.
Well, move it 5px along the same line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question