Giter Site home page Giter Site logo

adtc / dndwithtouch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lenniea/dndwithtouch

0.0 0.0 0.0 64 KB

Drag and Drop with Touch API - now enhanced with two drop zones, and the ability to work correctly even when the page can be scrolled.

Home Page: https://adtc.github.io/DnDWithTouch/

HTML 53.08% CSS 2.92% JavaScript 44.00%
frontend javascript web

dndwithtouch's People

Contributors

adtc avatar deepakkadarivel avatar

dndwithtouch's Issues

Use elementFromPoint to detect the drop zone in a simpler, scalable way

So it turns out that the entire concept of calling getBoundingClientRect() on each drop zone one by one is unnecessary.

There's a better way to do this: document.elementFromPoint(touch.clientX, touch.clientY). Just get the element, check if it's a drop zone, and drop the element there.

We need to update this function:

DnDWithTouch/main.js

Lines 144 to 154 in 7e93c83

function detectDropOn(zone, x, y) {
var rect = zone.getBoundingClientRect();
var x1 = rect.left;
var y1 = rect.top;
var x2 = rect.right;
var y2 = rect.bottom;
//Very simple detection here
var returnvalue = false;
if (x1 <= x && y1 <= y && x <= x2 && y <= y2) {
returnvalue = true;
}

Use changedTouches in handleTouchEnd to find the last touch point

Instead of storing the last touch point in global variables, we can simply detect them locally in handleTouchEnd using changedTouches. It would look something like this:

let touch = e.changedTouches[0]
if (!touch.target.id /* try to check if it's current target */) { return }
let elem = document.elementFromPoint(touch.clientX, touch.clientY)

(Note: The last line corresponds to issue #1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.