| 1 | export function Transform(k, x, y) {
|
|---|
| 2 | this.k = k;
|
|---|
| 3 | this.x = x;
|
|---|
| 4 | this.y = y;
|
|---|
| 5 | }
|
|---|
| 6 |
|
|---|
| 7 | Transform.prototype = {
|
|---|
| 8 | constructor: Transform,
|
|---|
| 9 | scale: function(k) {
|
|---|
| 10 | return k === 1 ? this : new Transform(this.k * k, this.x, this.y);
|
|---|
| 11 | },
|
|---|
| 12 | translate: function(x, y) {
|
|---|
| 13 | return x === 0 & y === 0 ? this : new Transform(this.k, this.x + this.k * x, this.y + this.k * y);
|
|---|
| 14 | },
|
|---|
| 15 | apply: function(point) {
|
|---|
| 16 | return [point[0] * this.k + this.x, point[1] * this.k + this.y];
|
|---|
| 17 | },
|
|---|
| 18 | applyX: function(x) {
|
|---|
| 19 | return x * this.k + this.x;
|
|---|
| 20 | },
|
|---|
| 21 | applyY: function(y) {
|
|---|
| 22 | return y * this.k + this.y;
|
|---|
| 23 | },
|
|---|
| 24 | invert: function(location) {
|
|---|
| 25 | return [(location[0] - this.x) / this.k, (location[1] - this.y) / this.k];
|
|---|
| 26 | },
|
|---|
| 27 | invertX: function(x) {
|
|---|
| 28 | return (x - this.x) / this.k;
|
|---|
| 29 | },
|
|---|
| 30 | invertY: function(y) {
|
|---|
| 31 | return (y - this.y) / this.k;
|
|---|
| 32 | },
|
|---|
| 33 | rescaleX: function(x) {
|
|---|
| 34 | return x.copy().domain(x.range().map(this.invertX, this).map(x.invert, x));
|
|---|
| 35 | },
|
|---|
| 36 | rescaleY: function(y) {
|
|---|
| 37 | return y.copy().domain(y.range().map(this.invertY, this).map(y.invert, y));
|
|---|
| 38 | },
|
|---|
| 39 | toString: function() {
|
|---|
| 40 | return "translate(" + this.x + "," + this.y + ") scale(" + this.k + ")";
|
|---|
| 41 | }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | export var identity = new Transform(1, 0, 0);
|
|---|
| 45 |
|
|---|
| 46 | transform.prototype = Transform.prototype;
|
|---|
| 47 |
|
|---|
| 48 | export default function transform(node) {
|
|---|
| 49 | while (!node.__zoom) if (!(node = node.parentNode)) return identity;
|
|---|
| 50 | return node.__zoom;
|
|---|
| 51 | }
|
|---|