summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/timeline_display_transform.html
blob: 49c10e291239b8abb23e7de5b4fb23056b18f8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/utils.html">

<script>
'use strict';

tr.exportTo('tr.ui', function() {
  function TimelineDisplayTransform(opt_that) {
    if (opt_that) {
      this.set(opt_that);
      return;
    }
    this.scaleX = 1;
    this.panX = 0;
    this.panY = 0;
  }

  TimelineDisplayTransform.prototype = {
    set: function(that) {
      this.scaleX = that.scaleX;
      this.panX = that.panX;
      this.panY = that.panY;
    },

    clone: function() {
      return new TimelineDisplayTransform(this);
    },

    equals: function(that) {
      var eq = true;
      if (that === undefined || that === null)
        return false;
      eq &= this.panX === that.panX;
      eq &= this.panY === that.panY;
      eq &= this.scaleX === that.scaleX;
      return !!eq;
    },

    almostEquals: function(that) {
      var eq = true;
      if (that === undefined || that === null)
        return false;
      eq &= Math.abs(this.panX - that.panX) < 0.001;
      eq &= Math.abs(this.panY - that.panY) < 0.001;
      eq &= Math.abs(this.scaleX - that.scaleX) < 0.001;
      return !!eq;
    },

    incrementPanXInViewUnits: function(xDeltaView) {
      this.panX += this.xViewVectorToWorld(xDeltaView);
    },

    xPanWorldPosToViewPos: function(worldX, viewX, viewWidth) {
      if (typeof viewX == 'string') {
        if (viewX === 'left') {
          viewX = 0;
        } else if (viewX === 'center') {
          viewX = viewWidth / 2;
        } else if (viewX === 'right') {
          viewX = viewWidth - 1;
        } else {
          throw new Error('viewX must be left|center|right or number.');
        }
      }
      this.panX = (viewX / this.scaleX) - worldX;
    },

    xPanWorldBoundsIntoView: function(worldMin, worldMax, viewWidth) {
      if (this.xWorldToView(worldMin) < 0)
        this.xPanWorldPosToViewPos(worldMin, 'left', viewWidth);
      else if (this.xWorldToView(worldMax) > viewWidth)
        this.xPanWorldPosToViewPos(worldMax, 'right', viewWidth);
    },

    xSetWorldBounds: function(worldMin, worldMax, viewWidth) {
      var worldWidth = worldMax - worldMin;
      var scaleX = viewWidth / worldWidth;
      var panX = -worldMin;
      this.setPanAndScale(panX, scaleX);
    },

    setPanAndScale: function(p, s) {
      this.scaleX = s;
      this.panX = p;
    },

    xWorldToView: function(x) {
      return (x + this.panX) * this.scaleX;
    },

    xWorldVectorToView: function(x) {
      return x * this.scaleX;
    },

    xViewToWorld: function(x) {
      return (x / this.scaleX) - this.panX;
    },

    xViewVectorToWorld: function(x) {
      return x / this.scaleX;
    },

    applyTransformToCanvas: function(ctx) {
      ctx.transform(this.scaleX, 0, 0, 1, this.panX * this.scaleX, 0);
    }
  };

  return {
    TimelineDisplayTransform: TimelineDisplayTransform
  };
});
</script>