| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,99 @@ | |||
| 1 | + window.onload = function () { | ||
| 2 | + tryLinkSketch(); | ||
| 3 | + } | ||
| 4 | + | ||
| 5 | + function tryLinkSketch() { | ||
| 6 | + var sketchId = "wordDroppings"; | ||
| 7 | + var sketch = Processing.getInstanceById(sketchId); | ||
| 8 | + if ( sketch == undefined ) | ||
| 9 | + return setTimeout(tryLinkSketch, 200); /*try again later*/ | ||
| 10 | + else { | ||
| 11 | + initDragDrop(sketch); | ||
| 12 | + } | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + function initDragDrop ( sketch ) { | ||
| 16 | + var target = sketch.externals.canvas; | ||
| 17 | + var targetPosition = getElementPosition(target); | ||
| 18 | + var messageType = 'text/plain'; | ||
| 19 | + | ||
| 20 | + var links = document.querySelectorAll('a'), el = null; | ||
| 21 | + for (var i = 0; i < links.length; i++) { | ||
| 22 | + el = links[i]; | ||
| 23 | + | ||
| 24 | + el.setAttribute('draggable', 'true'); | ||
| 25 | + | ||
| 26 | + addEvent(el, 'dragstart', function (e) { | ||
| 27 | + e.dataTransfer.effectAllowed = 'copy'; | ||
| 28 | + var message = this.textContent || this.innerHTML; | ||
| 29 | + e.dataTransfer.setData( messageType, message ); | ||
| 30 | + return false; | ||
| 31 | + }); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + addEvent( target, 'dragenter', function (e) { | ||
| 35 | + if (e.preventDefault) e.preventDefault(); | ||
| 36 | + sketch.dragEnter(); | ||
| 37 | + return false; | ||
| 38 | + }); | ||
| 39 | + | ||
| 40 | + addEvent( target, 'dragover', function (e) { | ||
| 41 | + if (e.preventDefault) e.preventDefault(); | ||
| 42 | + e.dataTransfer.dropEffect = 'copy'; | ||
| 43 | + sketch.dragOver( e.pageX-targetPosition.x, | ||
| 44 | + e.pageY-targetPosition.y ); | ||
| 45 | + return false; | ||
| 46 | + }); | ||
| 47 | + | ||
| 48 | + addEvent( target, 'dragleave', function () { | ||
| 49 | + sketch.dragLeave(); | ||
| 50 | + }); | ||
| 51 | + | ||
| 52 | + addEvent( target, 'drop', function (e) { | ||
| 53 | + if (e.stopPropagation) e.stopPropagation(); | ||
| 54 | + if (e.preventDefault) e.preventDefault(); | ||
| 55 | + sketch.dragDrop( e.dataTransfer.getData(messageType), | ||
| 56 | + e.pageX-targetPosition.x, | ||
| 57 | + e.pageY-targetPosition.y ); | ||
| 58 | + return false; | ||
| 59 | + }); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + // see: http://html5demos.com/drag | ||
| 63 | + var addEvent = (function () { | ||
| 64 | + if (document.addEventListener) { | ||
| 65 | + return function (el, type, fn) { | ||
| 66 | + if (el && el.nodeName || el === window) { | ||
| 67 | + el.addEventListener(type, fn, false); | ||
| 68 | + } else if (el && el.length) { | ||
| 69 | + for (var i = 0; i < el.length; i++) { | ||
| 70 | + addEvent(el[i], type, fn); | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + }; | ||
| 74 | + } else { | ||
| 75 | + return function (el, type, fn) { | ||
| 76 | + if (el && el.nodeName || el === window) { | ||
| 77 | + el.attachEvent('on' + type, function () { return fn.call(el, window.event); }); | ||
| 78 | + } else if (el && el.length) { | ||
| 79 | + for (var i = 0; i < el.length; i++) { | ||
| 80 | + addEvent(el[i], type, fn); | ||
| 81 | + } | ||
| 82 | + } | ||
| 83 | + }; | ||
| 84 | + } | ||
| 85 | + })(); | ||
| 86 | + | ||
| 87 | + // see: http://www.quirksmode.org/js/findpos.html | ||
| 88 | + function getElementPosition (obj) { | ||
| 89 | + var curleft = curtop = 0; | ||
| 90 | + if (obj.offsetParent) { | ||
| 91 | + do { | ||
| 92 | + curleft += obj.offsetLeft; | ||
| 93 | + curtop += obj.offsetTop; | ||
| 94 | + } while (obj = obj.offsetParent); | ||
| 95 | + return {x:curleft,y:curtop}; | ||
| 96 | + } | ||
| 97 | + return undefined; | ||
| 98 | + } | ||
| 99 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,80 @@ | |||
| 1 | + /** | ||
| 2 | + * Drag one of the links below into the sketch window! <br/> | ||
| 3 | + * (Opera currently not supported, sorry!) | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | + color normalColor; | ||
| 7 | + color dragColor; | ||
| 8 | + color currentColor; | ||
| 9 | + | ||
| 10 | + int nx = -10, ny = -10; | ||
| 11 | + int cx = -10, cy = -10; | ||
| 12 | + | ||
| 13 | + ArrayList trail; | ||
| 14 | + | ||
| 15 | + String message; | ||
| 16 | + | ||
| 17 | + void setup () | ||
| 18 | + { | ||
| 19 | + size( 300, 200 ); | ||
| 20 | + | ||
| 21 | + normalColor = color(100); | ||
| 22 | + dragColor = color(200); | ||
| 23 | + currentColor = normalColor; | ||
| 24 | + | ||
| 25 | + trail = new ArrayList(); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + void draw () | ||
| 29 | + { | ||
| 30 | + background(100); | ||
| 31 | + | ||
| 32 | + noFill(); | ||
| 33 | + stroke(currentColor); | ||
| 34 | + strokeWeight(5); | ||
| 35 | + rect(2.5,2.5,width-5,height-5); | ||
| 36 | + | ||
| 37 | + strokeWeight(1); | ||
| 38 | + if ( trail.size() > 2 ) | ||
| 39 | + { | ||
| 40 | + for ( int i = 1; i < trail.size(); i++ ) | ||
| 41 | + { | ||
| 42 | + stroke(map(i,1,trail.size(),100,0)); | ||
| 43 | + line( trail.get(i-1)[0], trail.get(i-1)[1], | ||
| 44 | + trail.get(i)[0], trail.get(i)[1] ); | ||
| 45 | + } | ||
| 46 | + trail.remove(trail.get(0)); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + noStroke(); | ||
| 50 | + fill(255); | ||
| 51 | + cx += (nx-cx) / 10.0; | ||
| 52 | + cy += (ny-cy) / 10.0; | ||
| 53 | + ellipse( cx, cy, 7, 7 ); | ||
| 54 | + text( message, cx+7.5, cy+2.5 ); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + void dragEnter () | ||
| 58 | + { | ||
| 59 | + currentColor = dragColor; | ||
| 60 | + trail = new ArrayList(); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + void dragOver ( int dragX, int dragY ) | ||
| 64 | + { | ||
| 65 | + trail.add(new int[]{dragX,dragY}); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + void dragLeave () | ||
| 69 | + { | ||
| 70 | + currentColor = normalColor; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + void dragDrop ( String dropMessage, int dropX, int dropY ) | ||
| 74 | + { | ||
| 75 | + message = dropMessage; | ||
| 76 | + nx = dropX; | ||
| 77 | + ny = dropY; | ||
| 78 | + | ||
| 79 | + currentColor = normalColor; | ||
| 80 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments