[ Launch: Drag practice ] 6192464 by tarvaina
-
-
Save tarvaina/6192464 to your computer and use it in GitHub Desktop.
Drag practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"description":"Drag practice","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/GyWsjMW.png"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var svg = d3.select("svg"); | |
| var bg = svg.append("rect") | |
| .attr({ | |
| x: 0, | |
| y: 0, | |
| width: "100%", | |
| height: "100%" | |
| }); | |
| var crosshairX = 422; | |
| var crosshairY = 352; | |
| var crosshairWidth = 10; | |
| var barColor = "#ABCCBF"; | |
| var lightUpDuration = 1000; | |
| var lightDownDuration = 1000; | |
| var lightUpColor = "#355548"; | |
| var ybar = svg.append("rect").attr({ | |
| x: crosshairX, | |
| y: crosshairY, | |
| width: crosshairWidth, | |
| height: 0, | |
| fill: "#000" | |
| }); | |
| var xbar = svg.append("rect").attr({ | |
| x: crosshairX, | |
| y: crosshairY, | |
| width: 0, | |
| height: crosshairWidth, | |
| fill: "#000" | |
| }); | |
| var drag = d3.behavior.drag().on("drag", function() { | |
| var dx = d3.event.dx; | |
| var dy = d3.event.dy; | |
| if (dy >= 0) { | |
| ybar.attr({ | |
| y: crosshairY + crosshairWidth, | |
| height: dy | |
| }); | |
| } else { | |
| ybar.attr({ | |
| y: crosshairY + dy, | |
| height: -dy | |
| }); | |
| } | |
| if (dx >= 0) { | |
| xbar.attr({ | |
| x: crosshairX + crosshairWidth, | |
| width: dx | |
| }); | |
| } else { | |
| xbar.attr({ | |
| x: crosshairX + dx, | |
| width: -dx | |
| }); | |
| } | |
| }).on("dragstart", function() { | |
| bg.transition() | |
| .duration(lightUpDuration) | |
| .attr({fill: lightUpColor}); | |
| xbar.transition() | |
| .duration(lightUpDuration) | |
| .attr({fill: barColor}); | |
| ybar.transition() | |
| .duration(lightUpDuration) | |
| .attr({fill: barColor}); | |
| }).on("dragend", function() { | |
| ybar | |
| .attr({height: 0}) | |
| .transition() | |
| .duration(lightDownDuration) | |
| .attr({fill: "#000"}); | |
| xbar | |
| .attr({width: 0}) | |
| .transition() | |
| .duration(lightDownDuration) | |
| .attr({fill: "#000"}); | |
| bg | |
| .transition() | |
| .duration(lightDownDuration) | |
| .attr({fill: "#000"}); | |
| }); | |
| bg.call(drag); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment