-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplane.js
More file actions
167 lines (142 loc) · 5.58 KB
/
plane.js
File metadata and controls
167 lines (142 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
************************
** By **
** Nicola Klemenc **
** github.com/nicklem **
************************
*/
var PLANE = ( function() {
var overallZoom = 1 ;
var getOverallZoom = function() { return overallZoom ; } ;
var updateOverallZoomFactor = function() { overallZoom *= OPT.getZoom() } ;
var xCenter , yCenter ;
var updateCPlaneCenter = function() {
xCenter = xMin + ( xPixCenter / xPixWidth ) * xWidth ;
yCenter = yMin + ( yPixCenter / yPixWidth ) * yWidth ;
} ;
var xMin = -2 , xMax = 1 , yMin = -1.5 , yMax = 1.5 ;
// var xMin = -2 , xMax = 2 , yMin = -1.5 , yMax = 1.5 ;
var xWidth = xMax - xMin ;
var yWidth = yMax - yMin ;
var zoomCPlaneBounds = function() {
var z = OPT.getZoom() ;
xMin = xCenter - ( xWidth / ( 2 * z ) ) ;
xMax = xCenter + ( xWidth / ( 2 * z ) ) ;
yMin = yCenter - ( yWidth / ( 2 * z ) ) ;
yMax = yCenter + ( yWidth / ( 2 * z ) ) ;
} ;
var updateCPlaneWidths = function() {
xWidth = xMax - xMin ;
yWidth = yMax - yMin ;
} ;
var deltaX , deltaY ;
var setDeltasPerPixel = function() {
deltaX = ( xMax - xMin ) / xPixWidth ;
deltaY = ( yMax - yMin ) / yPixWidth ;
return this ;
} ;
var xPixWidth ;
// var geTXPIXWidth = function() { return xPixWidth ; } ;
var getXPixWidth = function() { return xPixWidth ; } ;
var getExtendedXPixWidth = function() { return xPixWidth + xRotBoundExtension ; } ;
var setXPixWidth = function( newVal ) { xPixWidth = newVal ; } ;
var yPixWidth ;
var getYPixWidth = function() { return yPixWidth ; } ;
var getExtendedYPixWidth = function() { return yPixWidth + yRotBoundExtension ; } ;
var setYPixWidth = function( newVal ) { yPixWidth = newVal ; } ;
var xPixCenter ;
// var getXPixCenter = function() { return xPixCenter ; } ;
var setXPixCenter = function( newVal ) { xPixCenter = newVal - CANVAS.getOffsetLeft() ; } ;
var yPixCenter ;
// var getYPixCenter = function() { return yPixCenter ; } ;
var setYPixCenter = function( newVal ) { yPixCenter = newVal - CANVAS.getOffsetTop() ; } ;
var xRotBoundOffset = 0 ;
var getXRotBoundOffset = function() { return xRotBoundOffset ; } ;
var yRotBoundOffset = 0 ;
var getYRotBoundOffset = function() { return yRotBoundOffset ; } ;
var xRotBoundExtension = 0 ;
// var getXRotBoundExtension = function() { return xRotBoundExtension ; } ;
var yRotBoundExtension = 0 ;
// var getYRotBoundExtension = function() { return yRotBoundExtension ; } ;
var setRotBounds = function() {
// Naive approach, but works fine
if( OPT.isPolar() ) {
var l = xPixWidth / 2 , h = yPixWidth / 2 ;
var diag = Math.sqrt( l * l + h * h ) ;
xRotBoundOffset = yRotBoundOffset = Math.ceil( diag - ( l < h ? l : h ) ) ;
xRotBoundExtension = 2 * xRotBoundOffset ;
yRotBoundExtension = 2 * xRotBoundOffset ;
} else {
xRotBoundOffset = yRotBoundOffset = xRotBoundExtension = yRotBoundExtension = 0 ;
}
} ;
var initHeight = function() {
var idealX = yWidth * ( xPixWidth / yPixWidth ) ;
xMin = -3/5 * idealX ; xMax = 2/5 * idealX ;
updateCPlaneWidths() ;
} ;
var initWidth = function() {
var idealY = xWidth * ( yPixWidth / xPixWidth ) ;
yMin = - idealY / 2 ; yMax = idealY / 2 ;
updateCPlaneWidths() ;
} ;
var setPixWidths = function( newWidth , newHeigth ) {
if( xPixWidth !== window.innerWidth || yPixWidth !== window.innerHeight ) {
setXPixWidth( newWidth ) ;
setYPixWidth( newHeigth ) ;
( xPixWidth > yPixWidth ? initHeight : initWidth )() ;
}
return this ;
} ;
var toReZ , toImZ ;
var setPToCArrays = function() {
var i ;
toReZ = new Float64Array( xPixWidth + xRotBoundExtension ) ;
toImZ = new Float64Array( yPixWidth + yRotBoundExtension ) ;
// TODO: improve total width detection
for( i = 0 ; i < ( xPixWidth + xRotBoundExtension ) ; i++ ) { toReZ[i] = xMin + ( deltaX * ( i - xRotBoundOffset ) ) ; }
for( i = 0 ; i < ( yPixWidth + yRotBoundExtension ) ; i++ ) { toImZ[i] = yMin + ( deltaY * ( i - yRotBoundOffset ) ) ; }
return this;
} ;
var getToReZ = function() { return toReZ ; } ;
var getToImZ = function() { return toImZ ; } ;
var getTotalPixelNumber = function() {
return toImZ.length * toReZ.length ;
} ;
var remapPolarClick = function() {
var remappedX = xPixCenter - xPixWidth / 2 ;
var remappedY = yPixCenter - yPixWidth / 2 ;
var rot = MATH.toRad( OPT.getRot() ) ;
var rotX = MATH.rotComplex( [ remappedX , remappedY ] , rot ) ;
setXPixCenter( rotX[ 0 ] + xPixWidth / 2 ) ;
setYPixCenter( rotX[ 1 ] + yPixWidth / 2 ) ;
} ;
var updateDrawParams = function( ev ) {
updateOverallZoomFactor() ;
setXPixCenter( ev.layerX ) ;
setYPixCenter( ev.layerY ) ;
if( OPT.isPolar() ) { remapPolarClick() ; }
updateCPlaneCenter() ;
zoomCPlaneBounds() ;
updateCPlaneWidths() ;
return this ;
} ;
var API = {
"setPToCArrays" : setPToCArrays ,
"setPixWidths" : setPixWidths ,
"setDeltasPerPixel" : setDeltasPerPixel ,
"getOverallZoom" : getOverallZoom ,
"getTotalPixelNumber" : getTotalPixelNumber ,
"getToReZ" : getToReZ ,
"getToImZ" : getToImZ ,
"updateDrawParams" : updateDrawParams ,
"getYPixWidth" : getYPixWidth ,
"getXPixWidth" : getXPixWidth ,
"setRotBounds" : setRotBounds ,
"getXRotBoundOffset" : getXRotBoundOffset ,
"getYRotBoundOffset" : getYRotBoundOffset ,
"getExtendedYPixWidth": getExtendedYPixWidth ,
"getExtendedXPixWidth": getExtendedXPixWidth
} ;
return API ;
} () ) ; // END PLANE