Last active
December 28, 2015 23:09
-
-
Save chnn/7576614 to your computer and use it in GitHub Desktop.
Brush-like scrolling control
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
| <!doctype html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <link rel="stylesheet" type="text/css" href="screen.css"> | |
| </head> | |
| <body> | |
| <div class="box"> | |
| <div class="sheet"></div> | |
| </div> | |
| <div class="brusher"> | |
| </div> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> | |
| <script src="scroll.js"></script> | |
| </body> | |
| </html> |
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
| body { | |
| position: relative; | |
| shape-rendering: crispEdges; | |
| } | |
| .box { | |
| width: 700px; | |
| height: 500px; | |
| border: 1px solid gray; | |
| margin: 10px 0 0 10px; | |
| overflow: scroll; | |
| } | |
| .sheet, .brusher { | |
| background-image: -webkit-radial-gradient(45px 45px, farthest-corner, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%); | |
| } | |
| .sheet { | |
| width: 5000px; | |
| height: 5000px; | |
| } | |
| .brusher { | |
| position: absolute; | |
| left: 730px; | |
| width: 150px; | |
| height: 150px; | |
| border: 1px solid gray; | |
| margin-top: 10px; | |
| } | |
| .brush { | |
| fill: gray; | |
| stroke: black; | |
| stroke-width: 1px; | |
| opacity: 0.5; | |
| } |
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 BOX_W = 700, | |
| BOX_H = 500, | |
| BRUSHER_W = 150, | |
| BRUSHER_H = 150, | |
| SHEET_W = 5000, | |
| SHEET_H = 5000, | |
| BRUSH_W = BOX_W * BRUSHER_W / SHEET_W, | |
| BRUSH_H = BOX_H * BRUSHER_H / SHEET_H; | |
| var node = d3.select('.box').node(); | |
| var drag = d3.behavior.drag() | |
| .on('drag', function (d) { | |
| var el = d3.select(this), | |
| x = +el.attr('x') + d3.event.dx, | |
| y = +el.attr('y') + d3.event.dy; | |
| // Don't scroll beyond bounds of brusher | |
| if (x < 0) { x = 0; } | |
| if (y < 0) { y = 0; } | |
| if (x > BRUSHER_W - BRUSH_W) { x = BRUSHER_W - BRUSH_W; } | |
| if (y > BRUSHER_H - BRUSH_H) { y = BRUSHER_H - BRUSH_H; } | |
| // Update rect position | |
| el.attr({'x': x, 'y': y}); | |
| // Update box scroll position | |
| node.scrollLeft = x / BRUSHER_W * SHEET_W; | |
| node.scrollTop = y / BRUSHER_H * SHEET_H; | |
| }); | |
| var rect = d3.select('.brusher').append('svg') | |
| .append('rect') | |
| .classed('brush', true) | |
| .attr('width', BRUSH_W) | |
| .attr('height', BRUSH_H) | |
| .call(drag); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment