OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015 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 * @unrestricted | |
6 */ | |
7 UI.BezierUI = class { | |
8 /** | |
9 * @param {number} width | |
10 * @param {number} height | |
11 * @param {number} marginTop | |
12 * @param {number} controlPointRadius | |
13 * @param {boolean} linearLine | |
14 */ | |
15 constructor(width, height, marginTop, controlPointRadius, linearLine) { | |
16 this.width = width; | |
17 this.height = height; | |
18 this.marginTop = marginTop; | |
19 this.radius = controlPointRadius; | |
20 this.linearLine = linearLine; | |
21 } | |
22 | |
23 /** | |
24 * @param {!Common.Geometry.CubicBezier} bezier | |
25 * @param {!Element} path | |
26 * @param {number} width | |
27 */ | |
28 static drawVelocityChart(bezier, path, width) { | |
29 var height = UI.BezierUI.Height; | |
30 var pathBuilder = ['M', 0, height]; | |
31 /** @const */ var sampleSize = 1 / 40; | |
32 | |
33 var prev = bezier.evaluateAt(0); | |
34 for (var t = sampleSize; t < 1 + sampleSize; t += sampleSize) { | |
35 var current = bezier.evaluateAt(t); | |
36 var slope = (current.y - prev.y) / (current.x - prev.x); | |
37 var weightedX = prev.x * (1 - t) + current.x * t; | |
38 slope = Math.tanh(slope / 1.5); // Normalise slope | |
39 pathBuilder = pathBuilder.concat(['L', (weightedX * width).toFixed(2), (he
ight - slope * height).toFixed(2)]); | |
40 prev = current; | |
41 } | |
42 pathBuilder = pathBuilder.concat(['L', width.toFixed(2), height, 'Z']); | |
43 path.setAttribute('d', pathBuilder.join(' ')); | |
44 } | |
45 | |
46 /** | |
47 * @return {number} | |
48 */ | |
49 curveWidth() { | |
50 return this.width - this.radius * 2; | |
51 } | |
52 | |
53 /** | |
54 * @return {number} | |
55 */ | |
56 curveHeight() { | |
57 return this.height - this.radius * 2 - this.marginTop * 2; | |
58 } | |
59 | |
60 /** | |
61 * @param {!Element} parentElement | |
62 * @param {string} className | |
63 * @param {number} x1 | |
64 * @param {number} y1 | |
65 * @param {number} x2 | |
66 * @param {number} y2 | |
67 */ | |
68 _drawLine(parentElement, className, x1, y1, x2, y2) { | |
69 var line = parentElement.createSVGChild('line', className); | |
70 line.setAttribute('x1', x1 + this.radius); | |
71 line.setAttribute('y1', y1 + this.radius + this.marginTop); | |
72 line.setAttribute('x2', x2 + this.radius); | |
73 line.setAttribute('y2', y2 + this.radius + this.marginTop); | |
74 } | |
75 | |
76 /** | |
77 * @param {!Element} parentElement | |
78 * @param {number} startX | |
79 * @param {number} startY | |
80 * @param {number} controlX | |
81 * @param {number} controlY | |
82 */ | |
83 _drawControlPoints(parentElement, startX, startY, controlX, controlY) { | |
84 this._drawLine(parentElement, 'bezier-control-line', startX, startY, control
X, controlY); | |
85 var circle = parentElement.createSVGChild('circle', 'bezier-control-circle')
; | |
86 circle.setAttribute('cx', controlX + this.radius); | |
87 circle.setAttribute('cy', controlY + this.radius + this.marginTop); | |
88 circle.setAttribute('r', this.radius); | |
89 } | |
90 | |
91 /** | |
92 * @param {?Common.Geometry.CubicBezier} bezier | |
93 * @param {!Element} svg | |
94 */ | |
95 drawCurve(bezier, svg) { | |
96 if (!bezier) | |
97 return; | |
98 var width = this.curveWidth(); | |
99 var height = this.curveHeight(); | |
100 svg.setAttribute('width', this.width); | |
101 svg.setAttribute('height', this.height); | |
102 svg.removeChildren(); | |
103 var group = svg.createSVGChild('g'); | |
104 | |
105 if (this.linearLine) | |
106 this._drawLine(group, 'linear-line', 0, height, width, 0); | |
107 | |
108 var curve = group.createSVGChild('path', 'bezier-path'); | |
109 var curvePoints = [ | |
110 new Common.Geometry.Point( | |
111 bezier.controlPoints[0].x * width + this.radius, | |
112 (1 - bezier.controlPoints[0].y) * height + this.radius + this.marginTo
p), | |
113 new Common.Geometry.Point( | |
114 bezier.controlPoints[1].x * width + this.radius, | |
115 (1 - bezier.controlPoints[1].y) * height + this.radius + this.marginTo
p), | |
116 new Common.Geometry.Point(width + this.radius, this.marginTop + this.radiu
s) | |
117 ]; | |
118 curve.setAttribute( | |
119 'd', 'M' + this.radius + ',' + (height + this.radius + this.marginTop) +
' C' + curvePoints.join(' ')); | |
120 | |
121 this._drawControlPoints( | |
122 group, 0, height, bezier.controlPoints[0].x * width, (1 - bezier.control
Points[0].y) * height); | |
123 this._drawControlPoints( | |
124 group, width, 0, bezier.controlPoints[1].x * width, (1 - bezier.controlP
oints[1].y) * height); | |
125 } | |
126 }; | |
127 | |
128 UI.BezierUI.Height = 26; | |
OLD | NEW |