- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / tracing / timeline_display_transform.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 base.require('base.utils');
8
9 base.exportTo('tracing', function() {
10   function TimelineDisplayTransform(opt_that) {
11     if (opt_that) {
12       this.set(opt_that);
13       return;
14     }
15     this.scaleX = 1;
16     this.panX = 0;
17     this.panY = 0;
18   }
19
20   TimelineDisplayTransform.prototype = {
21     set: function(that) {
22       this.scaleX = that.scaleX;
23       this.panX = that.panX;
24       this.panY = that.panY;
25     },
26
27     clone: function() {
28       return new TimelineDisplayTransform(this);
29     },
30
31     equals: function(that) {
32       var eq = true;
33       if (that === undefined || that === null)
34         return false;
35       eq &= this.panX === that.panX;
36       eq &= this.panY === that.panY;
37       eq &= this.scaleX === that.scaleX;
38       return !!eq;
39     },
40
41     almostEquals: function(that) {
42       var eq = true;
43       if (that === undefined || that === null)
44         return false;
45       eq &= Math.abs(this.panX - that.panX) < 0.001;
46       eq &= Math.abs(this.panY - that.panY) < 0.001;
47       eq &= Math.abs(this.scaleX - that.scaleX) < 0.001;
48       return !!eq;
49     },
50
51     incrementPanXInViewUnits: function(xDeltaView) {
52       this.panX += this.xViewVectorToWorld(xDeltaView);
53     },
54
55     xPanWorldPosToViewPos: function(worldX, viewX, viewWidth) {
56       if (typeof viewX == 'string') {
57         if (viewX === 'left') {
58           viewX = 0;
59         } else if (viewX === 'center') {
60           viewX = viewWidth / 2;
61         } else if (viewX === 'right') {
62           viewX = viewWidth - 1;
63         } else {
64           throw new Error('viewX must be left|center|right or number.');
65         }
66       }
67       this.panX = (viewX / this.scaleX) - worldX;
68     },
69
70     xPanWorldBoundsIntoView: function(worldMin, worldMax, viewWidth) {
71       if (this.xWorldToView(worldMin) < 0)
72         this.xPanWorldPosToViewPos(worldMin, 'left', viewWidth);
73       else if (this.xWorldToView(worldMax) > viewWidth)
74         this.xPanWorldPosToViewPos(worldMax, 'right', viewWidth);
75     },
76
77     xSetWorldBounds: function(worldMin, worldMax, viewWidth) {
78       var worldWidth = worldMax - worldMin;
79       var scaleX = viewWidth / worldWidth;
80       var panX = -worldMin;
81       this.setPanAndScale(panX, scaleX);
82     },
83
84     setPanAndScale: function(p, s) {
85       this.scaleX = s;
86       this.panX = p;
87     },
88
89     xWorldToView: function(x) {
90       return (x + this.panX) * this.scaleX;
91     },
92
93     xWorldVectorToView: function(x) {
94       return x * this.scaleX;
95     },
96
97     xViewToWorld: function(x) {
98       return (x / this.scaleX) - this.panX;
99     },
100
101     xViewVectorToWorld: function(x) {
102       return x / this.scaleX;
103     },
104
105     applyTransformToCanvas: function(ctx) {
106       ctx.transform(this.scaleX, 0, 0, 1, this.panX * this.scaleX, 0);
107     }
108   };
109
110   return {
111     TimelineDisplayTransform: TimelineDisplayTransform
112   };
113 });