OLD | NEW |
| (Empty) |
1 /* Copyright (c) 2012 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.range'); | |
8 base.require('ui.event_target'); | |
9 | |
10 base.exportTo('ccfv', function() { | |
11 function QuadViewViewport(bbox, | |
12 opt_scale, | |
13 opt_dontPadBbox, opt_devicePixelRatio) { | |
14 ui.EventTarget.call(this); | |
15 if (bbox.isEmpty) | |
16 throw new Error('Cannot initialize a viewport with an empty bbox'); | |
17 | |
18 this.setWorldBBox(bbox, opt_dontPadBbox); | |
19 | |
20 var devicePixelRatio; | |
21 if (opt_devicePixelRatio) | |
22 devicePixelRatio = opt_devicePixelRatio; | |
23 else | |
24 devicePixelRatio = window.devicePixelRatio || 1; | |
25 | |
26 var scale; | |
27 if (opt_scale) { | |
28 scale = opt_scale; | |
29 } else { | |
30 if (devicePixelRatio > 1) | |
31 scale = 0.25; | |
32 else | |
33 scale = 0.125; | |
34 } | |
35 this.scale_ = scale; | |
36 this.updateScale_(); | |
37 } | |
38 | |
39 QuadViewViewport.prototype = { | |
40 | |
41 __proto__: ui.EventTarget.prototype, | |
42 | |
43 set scale(scale) { | |
44 this.scale_ = scale; | |
45 this.updateScale_(); | |
46 this.didChange_(); | |
47 }, | |
48 | |
49 get scale() { | |
50 return this.scale_; | |
51 }, | |
52 | |
53 updateScale_: function() { | |
54 this.worldPixelsPerDevicePixel_ = this.scale_; | |
55 this.devicePixelsPerLayoutPixel_ = 1 / devicePixelRatio; | |
56 | |
57 this.deviceWidth = | |
58 this.worldRect.width * this.worldPixelsPerDevicePixel_; | |
59 this.deviceHeight = | |
60 this.worldRect.height * this.worldPixelsPerDevicePixel_; | |
61 | |
62 this.layoutWidth = this.deviceWidth * this.devicePixelsPerLayoutPixel_; | |
63 this.layoutHeight = this.deviceHeight * this.devicePixelsPerLayoutPixel_; | |
64 | |
65 this.transformWorldToDevicePixels_ = mat2d.create(); | |
66 this.transformDevicePixelsToWorld_ = mat2d.create(); | |
67 this.updateTransform_(); | |
68 }, | |
69 | |
70 setWorldBBox: function(bbox, opt_dontPadBbox) { | |
71 var world_rect = bbox.asRect(); | |
72 var world_pad; | |
73 if (opt_dontPadBbox) { | |
74 world_pad = 0; | |
75 } else { | |
76 world_pad = Math.min(world_rect.width, | |
77 world_rect.height) * 0.10; | |
78 } | |
79 | |
80 world_rect.enlarge(world_pad); | |
81 this.worldRect = world_rect; | |
82 this.updateScale_(); | |
83 this.updateTransform_(); | |
84 this.didChange_(); | |
85 }, | |
86 | |
87 updateTransform_: function() { | |
88 if (!this.transformWorldToDevicePixels_) | |
89 return; | |
90 | |
91 mat2d.identity(this.transformWorldToDevicePixels_); | |
92 mat2d.translateInplaceXY( | |
93 this.transformWorldToDevicePixels_, | |
94 -this.worldRect.left, -this.worldRect.top); | |
95 mat2d.scaleInplaceXY(this.transformWorldToDevicePixels_, | |
96 this.worldPixelsPerDevicePixel_, | |
97 this.worldPixelsPerDevicePixel_); | |
98 | |
99 mat2d.invert(this.transformDevicePixelsToWorld_, | |
100 this.transformWorldToDevicePixels_); | |
101 }, | |
102 | |
103 layoutPixelsToWorldPixels2: function(v) { | |
104 var tmp = this.layoutPixelsToDevicePixels2(v); | |
105 return this.devicePixelsToWorldPixels2(tmp); | |
106 }, | |
107 | |
108 layoutPixelsToDevicePixels2: function(v) { | |
109 var res = vec2.create(); | |
110 return vec2.scale(res, v, 1 / this.devicePixelsPerLayoutPixel_); | |
111 }, | |
112 | |
113 devicePixelsToWorldPixels2: function(v) { | |
114 var res = vec2.create(); | |
115 vec2.transformMat2d(res, v, this.transformDevicePixelsToWorld_); | |
116 return res; | |
117 }, | |
118 | |
119 getWorldToDevicePixelTransform: function() { | |
120 return this.transformDevicePixelsToWorld_; | |
121 }, | |
122 | |
123 getDeviceLineWidthAssumingTransformIsApplied: function( | |
124 desired_device_line_width) { | |
125 return desired_device_line_width / this.worldPixelsPerDevicePixel_; | |
126 }, | |
127 | |
128 applyTransformToContext: function(ctx) { | |
129 var transform = this.transformWorldToDevicePixels_; | |
130 ctx.transform(transform[0], transform[1], transform[2], | |
131 transform[3], transform[4], transform[5]); | |
132 }, | |
133 | |
134 forceRedrawAll: function() { | |
135 this.didChange_(); | |
136 }, | |
137 | |
138 didChange_: function() { | |
139 base.dispatchSimpleEvent(this, 'change', false, false); | |
140 } | |
141 }; | |
142 | |
143 return { | |
144 QuadViewViewport: QuadViewViewport, | |
145 } | |
146 }); | |
147 | |
OLD | NEW |