2
* Copyright (C) 2016 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
* From processing.js: https://raw.githubusercontent.com/processing-js/processing-js/v1.4.8/processing.js
22
* Re-map a number from one range to another. In the example above, the number
23
* '25' is converted from a value in the range 0..100 into a value that
24
* ranges from the left edge (0) to the right edge (width) of the screen.
25
* Numbers outside the range are not clamped to 0 and 1, because out-of-range
26
* values are often intentional and useful.
28
* @param {Number} value The incoming value to be converted
29
* @param {Number} istart Lower bound of the value's current range
30
* @param {Number} istop Upper bound of the value's current range
31
* @param {Number} ostart Lower bound of the value's target range
32
* @param {Number} ostop Upper bound of the value's target range
36
function map(value, istart, istop, ostart, ostop) {
37
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart))
41
* Return a value which is always between `min` and `max`
43
* @param {Number} value The current value
44
* @param {Number} min The minimum value
45
* @param {Number} max The maximum value
49
function clamp(value, min, max) {
50
if (value < min) return min
51
if (value > max) return max
55
// calculates the distance from the middle of one rect to middle of other rect
56
function rectDistance(rect1, rect2) {
57
return pointDistance(Qt.point(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2),
58
Qt.point(rect2.x + rect2.width / 2, rect2.y + rect2.height / 2))
61
// calculates the distance between two points
62
function pointDistance(point1, point2) {
63
return Math.sqrt(Math.pow(point1.x - point2.x, 2) +
64
Math.pow(point1.y - point2.y, 2)
68
// from http://stackoverflow.com/questions/14616829/java-method-to-find-the-rectangle-that-is-the-intersection-of-two-rectangles-usi
69
function intersectionRect(r1, r2) {
70
var xmin = Math.max(r1.x, r2.x);
71
var xmax1 = r1.x + r1.width;
72
var xmax2 = r2.x + r2.width;
73
var xmax = Math.min(xmax1, xmax2);
74
var out = {x:0, y:0, width:0, height:0}
76
var ymin = Math.max(r1.y, r2.y);
77
var ymax1 = r1.y + r1.height;
78
var ymax2 = r2.y + r2.height;
79
var ymax = Math.min(ymax1, ymax2);
83
out.width = xmax - xmin;
84
out.height = ymax - ymin;
90
function easeOutCubic(t) { return (--t)*t*t+1 }
92
function linearAnimation(startProgress, endProgress, startValue, endValue, progress) {
93
// progress : progressDiff = value : valueDiff => value = progress * valueDiff / progressDiff
94
return (progress - startProgress) * (endValue - startValue) / (endProgress - startProgress) + startValue;