<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Animated Drop Targets</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<a href="#toc" class="jump">Jump to Table of Contents</a>
<h1>Example: Animated Drop Targets</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><div class="intro">
<p>This example will show you how to make an animated node a Drop target.</p>
</div>
<div class="example newwindow">
<a href="anim-drop-example.html" target="_blank" class="button">
View Example in New Window
</a>
</div>
<h3 id="setting-up-the-html">Setting up the HTML</h3>
<p>First we will create our HTML.</p>
<pre class="code prettyprint"><div id="dock"></div>
<div id="drag">Drag #1</div>
<div id="anim1" class="anim">Anim #1</div>
<div id="anim2" class="anim">Anim #2</div>
<div id="anim3" class="anim">Anim #3</div>
<div id="anim4" class="anim">Anim #4</div>
<div id="anim5" class="anim">Anim #5</div></pre>
<p>Now we give that HTML some CSS to make it visible.</p>
<pre class="code prettyprint">.anim {
position: relative;
height: 50px;
width: 100px;
border: 1px solid black;
background-color: #00B8BF;
top: 100px;
}
#drag {
height: 50px;
width: 50px;
border: 1px solid black;
background-color: #004C6D;
color: white;
cursor: move;
z-index: 5;
}
#dock {
height: 600px;
width: 75px;
background-color: #D00050;
border: 1px solid black;
position: absolute;
top: 5px;
right: 0px;
}
.anim.yui3-dd-drop-over {
background-color: #EDFF9F;
}
.anim.done {
background-color: white;
}
#drag1.yui3-dd-drag-over {
opacity: .5;
filter: alpha(opacity=50);
}</pre>
<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
<p>Now we need to create our YUI instance and tell it to load the <code>dd-drop</code>, <code>dd-plugin</code>, <code>dd-drop-plugin</code> and <code>anim</code> modules.</p>
<pre class="code prettyprint">YUI().use('dd-drop', 'anim', 'dd-plugin', 'dd-drop-plugin');</pre>
<h3 id="making-the-node-draggable">Making the Node draggable</h3>
<p>Now that we have a YUI instance with the modules loaded, we need to instantiate the <code>Drag</code> instance on this Node.</p>
<p>In this example we will be using Node plugins to accomplish our tasks.</p>
<pre class="code prettyprint">YUI().use('dd-drop', 'anim', 'dd-plugin', 'dd-drop-plugin', function(Y) {
//Get the node #drag
var d = Y.one('#drag');
d.plug(Y.Plugin.Drag, { dragMode: 'intersect' });
});</pre>
<h3 id="animating-the-nodes">Animating the Nodes</h3>
<p>Now we will set up the Animation sequence that we want to run.</p>
<pre class="code prettyprint">//Get all the div's with the class anim
var anims = Y.Node.all('div.anim');
var counter = 0;
anims.each(function(v, k, items) {
//Get a reference to the Node instance
var a = v;
counter++;
//Add the FX plugin
a.plug(Y.Plugin.NodeFX);
//Add the Drop plugin
a.plug(Y.Plugin.Drop);
//Set the attributes on the animation
a.fx.setAttrs({
from: {
left: 0
},
to: {
curve: function() {
var points = [],
n = 10;
for (var i = 0; i < n; ++i) {
points.push([
Math.floor(Math.random()*Y.DOM.winWidth() - 60 - a.get('offsetWidth')),
Math.floor(Math.random()*Y.DOM.winHeight() - a.get('offsetHeight'))
]);
}
return points;
}
},
//Do the animation 20 times
iterations: 20,
//Alternate it so it "bounces" across the screen
direction: 'alternate',
//Give all of them a different duration so we get different speeds.
duration: ((counter * 1.75) + 1)
});
});</pre>
<h3 id="making-the-node-a-target">Making the Node a Target</h3>
<p>Using the <code>dd-drop-plugin</code>, we now need to make this animated Node a Drop Target.</p>
<p>To do that, we need to add the plugin after the fx plugin.</p>
<pre class="code prettyprint">//Add the FX plugin
a.plug(Y.Plugin.NodeFX);
//Add the Drop plugin
a.plug(Y.Plugin.Drop);</pre>
<h3 id="syncing-the-target-with-the-animation">Syncing the Target with the Animation</h3>
<p>The Drop Target needs to be in sync with the animation, since we are changing the height, width, top and left style.</p>
<p>We do this by adding a listener to the <code>tween</code> event on the animation instance.</p>
<pre class="code prettyprint">//on tween of the original anim, we need to sync the drop's shim.
a.fx.on('tween', function() {
//Do we have an active Drag?
if (Y.DD.DDM.activeDrag) {
//Size this shim
this.drop.sizeShim();
//Force an over target check since we might not be moving the mouse.
Y.Lang.later(0, a, function() {
this.drop._handleTargetOver();
});
}
}, a);</pre>
<h3 id="full-example-source">Full example source</h3>
<pre class="code prettyprint">YUI().use('dd', 'dd-plugin', 'dd-drop-plugin', 'anim', function(Y) {
//Get the node #drag
var d = Y.one('#drag');
d.plug(Y.Plugin.Drag, { dragMode: 'intersect' });
//Get all the divs with the class anim
var anims = Y.Node.all('div.anim');
var counter = 0;
anims.each(function(v, k) {
//Get a reference to the Node instance
var a = v;
counter++;
//Add the FX plugin
a.plug(Y.Plugin.NodeFX);
//Add the Drop plugin
a.plug(Y.Plugin.Drop);
//Set the attributes on the animation
a.fx.setAttrs({
from: {
left: 0
},
to: {
curve: function() {
var points = [],
n = 10;
for (var i = 0; i < n; ++i) {
points.push([
Math.floor(Math.random()*Y.DOM.winWidth() - 60 - a.get('offsetWidth')),
Math.floor(Math.random()*Y.DOM.winHeight() - a.get('offsetHeight'))
]);
}
return points;
}
},
//Do the animation 20 times
iterations: 20,
//Alternate it so it "bounces" across the screen
direction: 'alternate',
//Give all of them a different duration so we get different speeds.
duration: ((counter * 1.75) + 1)
});
//When this drop is entered, pause the fx
a.drop.on('drop:enter', function() {
this.fx.pause();
}, a);
//When the drop is exited, run the fx again
a.drop.on('drop:exit', function() {
this.fx.run();
}, a);
//When a drop is on the node, do something special
a.drop.on('drop:hit', function(e) {
//Stop the animation
this.fx.stop();
//remove the tween listener
this.fx.unsubscribeAll('tween');
//move it to the dock
this.fx.setAttrs({
from: {
opacity: 1
},
to: {
height: 50,
width: 50,
left: function() {
var dW = Y.one('body').get('viewportRegion').right;
return ((dW - 60)); //Minus 60 for the dock
},
top: 15,
opacity: .5
},
direction: 'normal',
iterations: 1,
duration: .5,
//We are using reverse above for the "bouncing", reset it here.
reverse: false
});
//On end, add a class and destroy the target
this.fx.on('end', function() {
this.drop.get('node').addClass('done');
this.drop.destroy();
}, this);
//Run this animation
this.fx.run();
//others that were dropped on.
Y.each(e.others, function(v, k) {
var node = v.get('node');
node.fx.run();
});
}, a);
//on tween of the original anim, we need to sync the drop's shim.
a.fx.on('tween', function() {
//Do we have an active Drag?
if (Y.DD.DDM.activeDrag) {
//Size this shim
this.drop.sizeShim();
//Force an over target check since we might not be moving the mouse.
Y.Lang.later(0, a, function() {
this.drop._handleTargetOver();
});
}
}, a);
a.fx.run();
});
});</pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div id="toc" class="sidebox">
<div class="hd">
<h2 class="no-toc">Table of Contents</h2>
</div>
<div class="bd">
<ul class="toc">
<li>
<a href="#setting-up-the-html">Setting up the HTML</a>
</li>
<li>
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
</li>
<li>
<a href="#making-the-node-draggable">Making the Node draggable</a>
</li>
<li>
<a href="#animating-the-nodes">Animating the Nodes</a>
</li>
<li>
<a href="#making-the-node-a-target">Making the Node a Target</a>
</li>
<li>
<a href="#syncing-the-target-with-the-animation">Syncing the Target with the Animation</a>
</li>
<li>
<a href="#full-example-source">Full example source</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="A simple drag interaction that doesn't require a drop interaction.">
<a href="simple-drag.html">Simple Drag</a>
</li>
<li data-description="How to apply the Drag Plugin to a node.">
<a href="drag-plugin.html">Drag - Node plugin</a>
</li>
<li data-description="A simple proxy drag interaction that doesn't require a drop interaction.">
<a href="proxy-drag.html">Drag - Proxy</a>
</li>
<li data-description="How to constrain a draggable Node to another Node's region.">
<a href="constrained-drag.html">Drag - Constrained to a Region</a>
</li>
<li data-description="Using interaction groups, this example demonstrates how to tie into the Drag & Drop Utility's interesting moments to provide visual affordances for the current drag operation.">
<a href="groups-drag.html">Drag - Interaction Groups</a>
</li>
<li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
<a href="shim-drag.html">Using the Drag Shim</a>
</li>
<li data-description="How to use the Drop Target events to code your application.">
<a href="drop-code.html">Using Drop Based Coding</a>
</li>
<li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
<a href="winscroll.html">Window Scrolling</a>
</li>
<li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
<a href="delegate.html">Drag Delegation</a>
</li>
<li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
<a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
</li>
<li data-description="How to use Drag plugins with a DD Delegate based solution.">
<a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
</li>
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
<a href="list-drag.html">List Reorder w/Bubbling</a>
</li>
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
<a href="scroll-list.html">List Reorder w/Scrolling</a>
</li>
<li data-description="How to make an animated node a Drop target.">
<a href="anim-drop.html">Animated Drop Targets</a>
</li>
<li data-description="Example Photo Browser application.">
<a href="photo-browser.html">Photo Browser</a>
</li>
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
<a href="portal-drag.html">Portal Style Example</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Working with multiple YUI instances.">
<a href="../yui/yui-multi.html">Multiple Instances</a>
</li>
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/dd',
name: 'anim-drop',
title: 'Animated Drop Targets',
newWindow: 'true',
auto: false
};
YUI.Env.Tests.examples.push('simple-drag');
YUI.Env.Tests.examples.push('drag-plugin');
YUI.Env.Tests.examples.push('proxy-drag');
YUI.Env.Tests.examples.push('constrained-drag');
YUI.Env.Tests.examples.push('groups-drag');
YUI.Env.Tests.examples.push('shim-drag');
YUI.Env.Tests.examples.push('drop-code');
YUI.Env.Tests.examples.push('winscroll');
YUI.Env.Tests.examples.push('delegate');
YUI.Env.Tests.examples.push('delegate-drop');
YUI.Env.Tests.examples.push('delegate-plugins');
YUI.Env.Tests.examples.push('list-drag');
YUI.Env.Tests.examples.push('scroll-list');
YUI.Env.Tests.examples.push('anim-drop');
YUI.Env.Tests.examples.push('photo-browser');
YUI.Env.Tests.examples.push('portal-drag');
YUI.Env.Tests.examples.push('yui-multi');
YUI.Env.Tests.examples.push('stylesheet-theme');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>