Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML5 drag & drop & how to detect cursor position in js #34

Open
xgqfrms opened this issue Sep 7, 2018 · 17 comments
Open

HTML5 drag & drop & how to detect cursor position in js #34

xgqfrms opened this issue Sep 7, 2018 · 17 comments
Labels
document.elementFromPoint document.elementFromPoint(x, y); element.getBoundingClientRect(); element.getBoundingClientRect(); Face Detection API Face Detection API HTML5 drag & drop HTML5 drag & drop import jquery & ES6 syntax How to import jquery using ES6 syntax?

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Sep 7, 2018

HTML5 drag & drop

how to detect cursor position in js?

https://javascript.info/mouse-drag-and-drop

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

How to import jquery using ES6 syntax?

https://stackoverflow.com/questions/34338411/how-to-import-jquery-using-es6-syntax

import $ from "jquery";

// export for others scripts to use
window.$ = $;
import jQuery from "jquery";

// export for others scripts to use
window.jQuery = jQuery;

Repository owner locked and limited conversation to collaborators Sep 7, 2018
@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

Face Detection API

console.error("Your browser isn't supported `Face Detection API`!");

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

document.elementFromPoint(x, y);

image

https://en.js.cx/task/drag-heroes/solution/

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

timestamp

const getTimestamp = (debug = false) => {
            let timestamp = `timestamp-`;
            timestamp += new Date().getTime();
            if (debug) {
                console.log(`时间戳: ${timestamp}`);
            }
            try {
                if (timestamp !== "timestamp-") {
                    return timestamp;
                }
            } catch (err) {
                console.error(`时间戳 err: ${err}`);
                return `timestamp-${Math.random() * 1024 ** 5}`;
            }
        };

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

js & delete

js delete object property

image

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

function

image


delete & Object

true

    function test() {
        let x = `123`,
            obj = {
                x
            };
        console.log(`obj: `, obj);
        delete obj.x;
        console.log(`obj: `, obj);
    }

delete & var/let/const

false

    function test() {
        let x = `123`;
        console.log(`x: `, x);
        let result = delete x;
            console.log(`x: `, x);
        console.log(`result: `, result);
    };

Object & delete

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Description

https://stackoverflow.com/questions/742623/deleting-objects-in-javascript

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

drop-position

根据光标的位置,前后动态插入模块

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 7, 2018

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 *
 * @description getModulePositionGenerator
 * @augments
 * @example
 *
 */

const getModulePositionGenerator = (datas = [], debug = false) => {
    let result = {
        getTimestamp: (debug = false) => {
            let timestamp = `timestamp-`;
            timestamp += new Date().getTime();
            if (debug) {
                console.log(`时间戳: ${timestamp}`);
            }
            try {
                if (timestamp !== "timestamp-") {
                    return timestamp;
                }
            } catch (err) {
                console.error(`时间戳 err: ${err}`);
                return `timestamp-${Math.random() * 1024 ** 5}`;
            }
        },
        destoryPosition: (debug = false) => {
            let result = delete window.ModulePosition;
            if (debug) {
                console.log(`时间戳: ${timestamp}`);
            }
        },
        getInsertPosition: (selector = `[class="h5-dnd-container"]`, debug = false) => {
            let boxes = [...document.querySelectorAll(selector)];
            boxes.forEach(
                (box, i) => {
                    if (box && !box.dataset.positionFlag) {
                        box.dataset.positionFlag = true;
                        box.addEventListener("dragover", function(e) {
                            e.preventDefault();
                            let position = ``,
                                uid = ``,
                                size = box.getBoundingClientRect();
                            let boxY = size.top,
                                dragY = e.pageY;
                            uid = getTimestamp();
                            position = (dragY - boxY > (size.height / 2)) ? `after` : `before`;
                            if (uid && position) {
                                window.ModulePosition = {
                                    uid,
                                    position,
                                };
                            }
                        }, false);
                        box.addEventListener("drop", function(e) {
                            e.preventDefault();
                            e.stopPropagation();
                            // test
                            if (debug) {
                                console.log(`window.ModulePosition = \n`, JSON.stringify(window.ModulePosition, null, 4));
                            }
                        }, false);
                    }
                }
            );
        },
    };
    return result;
};




export default getModulePositionGenerator;

export {
    getModulePositionGenerator,
};

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 10, 2018

web worker

xgqfrms/ES6#9

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 11, 2018

drag and drop

https://www.webydo.com/blog/web-design/resources/11-amazing-html5-drag-and-drop-examples/

/*

<div draggable="true" ondragstart="event.dataTransfer.setData('text/plain', 'You can drag this text')">
    You can<strong></strong>this text.
</div>

function onDrop(event) {
    var data = event.dataTransfer.getData("text/plain");
    // event.target.textContent
    event.target.textContent = data;
    event.preventDefault();
}

*/

drag feedback image

https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Drag_operations#Drag_Effects

drag images

event.dataTransfer.setDragImage(image, xOffset, yOffset);

SVG / Canvas

function dragWithCustomImage(event) {
  var canvas = document.createElementNS("http://www.w3.org/1999/xhtml","canvas");
  canvas.width = canvas.height = 50;

  var ctx = canvas.getContext("2d");
  ctx.lineWidth = 4;
  ctx.moveTo(0, 0);
  ctx.lineTo(50, 50);
  ctx.moveTo(0, 50);
  ctx.lineTo(50, 0);
  ctx.stroke();

  var dt = event.dataTransfer;
  dt.setData('text/plain', 'Data to Drag');
  dt.setDragImage(canvas, 25, 25);
}

XUL panel

MDN ??? div ??? images

<panel id="panel" style="opacity: 0.6">
  <description id="pb">Drag Me</description>
</panel>

<vbox align="start" style="border: 1px solid black;" ondragstart="startDrag(event)">
  <description>Drag Me</description>
</vbox>
function startDrag(event) {
  event.dataTransfer.setData("text/plain", "<strong>Body</strong>");
  // setDragImage
  event.dataTransfer.setDragImage(document.getElementById("panel"), 20, 20);
}

Drag Effects

event.dataTransfer.effectAllowed = "copy";

https://en.wikipedia.org/wiki/Drag_and_drop

demo

https://www.sitepoint.com/a-drag-and-drop-planning-board-with-html5/

https://github.com/alexisgo/DragNDrop_PlanningBoard
https://www.sitepoint.com/examples/planning-board-with-html5/

https://www.html5rocks.com/en/tutorials/dnd/basics/

Modernizr

Modernizr tells you what HTML, CSS and JavaScript features the user’s browser has to offer.

https://modernizr.com/

HTML5 & Drag and Drop & API

https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 11, 2018

CORB

https://jakearchibald.com/2018/i-discovered-a-browser-bug/

Cross-origin read blocking, better known as CORB, is an algorithm which identifies dubious cross-origin resource fetches (e.g., fetches that would fail anyway like attempts to render JSON inside an img element) and blocks them before they reach a web page. CORB reduces the risk of leaking sensitive data by keeping it further from cross-origin web pages.

https://fetch.spec.whatwg.org/#corb

@xgqfrms xgqfrms added HTML5 drag & drop HTML5 drag & drop import jquery & ES6 syntax How to import jquery using ES6 syntax? Face Detection API Face Detection API document.elementFromPoint document.elementFromPoint(x, y); element.getBoundingClientRect(); element.getBoundingClientRect(); labels Sep 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
document.elementFromPoint document.elementFromPoint(x, y); element.getBoundingClientRect(); element.getBoundingClientRect(); Face Detection API Face Detection API HTML5 drag & drop HTML5 drag & drop import jquery & ES6 syntax How to import jquery using ES6 syntax?
Projects
None yet
Development

No branches or pull requests

1 participant