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.
7 base.require('base.utils');
9 base.exportTo('tracing', function() {
10 function TimelineDisplayTransform(opt_that) {
20 TimelineDisplayTransform.prototype = {
22 this.scaleX = that.scaleX;
23 this.panX = that.panX;
24 this.panY = that.panY;
28 return new TimelineDisplayTransform(this);
31 equals: function(that) {
33 if (that === undefined || that === null)
35 eq &= this.panX === that.panX;
36 eq &= this.panY === that.panY;
37 eq &= this.scaleX === that.scaleX;
41 almostEquals: function(that) {
43 if (that === undefined || that === null)
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;
51 incrementPanXInViewUnits: function(xDeltaView) {
52 this.panX += this.xViewVectorToWorld(xDeltaView);
55 xPanWorldPosToViewPos: function(worldX, viewX, viewWidth) {
56 if (typeof viewX == 'string') {
57 if (viewX === 'left') {
59 } else if (viewX === 'center') {
60 viewX = viewWidth / 2;
61 } else if (viewX === 'right') {
62 viewX = viewWidth - 1;
64 throw new Error('viewX must be left|center|right or number.');
67 this.panX = (viewX / this.scaleX) - worldX;
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);
77 xSetWorldBounds: function(worldMin, worldMax, viewWidth) {
78 var worldWidth = worldMax - worldMin;
79 var scaleX = viewWidth / worldWidth;
81 this.setPanAndScale(panX, scaleX);
84 setPanAndScale: function(p, s) {
89 xWorldToView: function(x) {
90 return (x + this.panX) * this.scaleX;
93 xWorldVectorToView: function(x) {
94 return x * this.scaleX;
97 xViewToWorld: function(x) {
98 return (x / this.scaleX) - this.panX;
101 xViewVectorToWorld: function(x) {
102 return x / this.scaleX;
105 applyTransformToCanvas: function(ctx) {
106 ctx.transform(this.scaleX, 0, 0, 1, this.panX * this.scaleX, 0);
111 TimelineDisplayTransform: TimelineDisplayTransform