summaryrefslogtreecommitdiffstats
path: root/playgrounds/matrix/drag.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-10-18 11:29:49 +0200
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-10-18 11:29:49 +0200
commitfb34ed0894b0e4fc4c2a8488ad7375f357e02989 (patch)
tree129e790ba81a1a1cf9c0d6dbaae556a9ea6a3653 /playgrounds/matrix/drag.js
parent6175adf23ddd6587954b20dc2700458d27ccaf69 (diff)
parent8ca3341952c2979520b349ce754bc98d8bb5f1d3 (diff)
downloadsvg.js-fb34ed0894b0e4fc4c2a8488ad7375f357e02989.tar.gz
svg.js-fb34ed0894b0e4fc4c2a8488ad7375f357e02989.zip
Merge branch '776-new-fx' into 3.0.0
Diffstat (limited to 'playgrounds/matrix/drag.js')
-rw-r--r--playgrounds/matrix/drag.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/playgrounds/matrix/drag.js b/playgrounds/matrix/drag.js
new file mode 100644
index 0000000..2dd6cac
--- /dev/null
+++ b/playgrounds/matrix/drag.js
@@ -0,0 +1,68 @@
+function reactToDrag(element, onDrag, beforeDrag) {
+
+ let xStart, yStart
+ let startDrag = event => {
+
+ // Avoid the default events
+ event.preventDefault()
+
+ // Store the position where the drag started
+ xStart = event.pageX
+ yStart = event.pageY
+
+ // Fire the start drag event
+ if (beforeDrag) {
+ var { x, y } = parent.point(event.pageX, event.pageY)
+ beforeDrag(event, x, y)
+ }
+
+ // Register events to react to dragging and drag ends
+ SVG.on(window, ['mousemove.drag', 'touchmove.drag'], reactDrag)
+ SVG.on(window, ['mouseup.drag', 'touchend.drag'], stopDrag)
+ }
+
+ let reactDrag = event => {
+
+ // Convert screen coordinates to svg coordinates and use them
+ var { x, y } = parent.point(event.pageX, event.pageY)
+ if (onDrag)
+ onDrag(event, x, y)
+ }
+
+ let stopDrag = event => {
+ SVG.off(window, ['mousemove.drag', 'touchmove.drag'])
+ SVG.off(window, ['mouseup.drag', 'touchend.drag'])
+ }
+
+ // Bind the drag tracker to this element directly
+ let parent = element.doc()
+ let point = new SVG.Point()
+ element.mousedown(startDrag).touchstart(startDrag)
+}
+
+SVG.extend(SVG.Element, {
+ draggable: function (after) {
+
+ let sx, sy
+
+ reactToDrag(this, (e, x, y) => {
+
+ this.transform({
+ origin: [sx, sy],
+ position: [x, y],
+ })
+
+ if (after) {
+ after(this, x, y)
+ }
+
+ }, (e, x, y) => {
+
+ var toAbsolute = new SVG.Matrix(this).inverse()
+ var p = new SVG.Point(x, y).transform(toAbsolute)
+ sx = p.x
+ sy = p.y
+ })
+ return this
+ },
+})