1
1
import Matrix from 'node-matrices' ;
2
2
import { projectPoint } from '../utils' ;
3
3
import { fillQuadTex , fillTriTex } from './canvasUtils' ;
4
- import WhiskerFactory from './whisker' ;
4
+ import Whisker from './whisker' ;
5
5
6
6
( function ( workerContext ) {
7
7
if ( workerContext . paper ) return ;
@@ -12,15 +12,13 @@ import WhiskerFactory from './whisker';
12
12
messageCallbacks [ event . data . messageId ] ( event . data . receiveData ) ;
13
13
} ) ;
14
14
15
- const whiskerFactory = new WhiskerFactory ( workerContext ) ;
16
-
17
15
workerContext . paper = {
18
16
get ( name , data , callback ) {
19
17
if ( name === 'whisker' ) {
20
- const whisker = whiskerFactory . createWhisker ( data ) ;
18
+ const whisker = new Whisker ( data ) ;
21
19
22
20
if ( callback ) {
23
- whisker . then ( callback ) ;
21
+ callback ( whisker ) ;
24
22
return ;
25
23
}
26
24
return whisker ;
@@ -136,104 +134,20 @@ import WhiskerFactory from './whisker';
136
134
callback,
137
135
whiskerPointCallback,
138
136
} = { } ) => {
139
- // eslint-disable-next-line no-console
140
- console . warn ( 'paper.whenPointsAt is deprecated, use the new whisker api instead' ) ;
141
-
142
- whiskerLength = whiskerLength || 0.7 ;
143
- paperNumber = paperNumber || ( await workerContext . paper . get ( 'number' ) ) ;
144
- requiredData = requiredData || [ ] ;
145
- const supporterCanvas = await workerContext . paper . get ( 'supporterCanvas' , { id : 'whisker' } ) ;
146
- const supporterCtx = supporterCanvas . getContext ( '2d' ) ;
147
- let pointAtData = null ;
148
- let lastWhiskerEnd = null ;
149
-
150
- // Adapted from https://stackoverflow.com/questions/9043805/test-if-two-lines-intersect-javascript-function
151
- function intersects ( v1 , v2 , v3 , v4 ) {
152
- const det = ( v2 . x - v1 . x ) * ( v4 . y - v3 . y ) - ( v4 . x - v3 . x ) * ( v2 . y - v1 . y ) ;
153
- if ( det === 0 ) {
154
- return false ;
155
- } else {
156
- const lambda = ( ( v4 . y - v3 . y ) * ( v4 . x - v1 . x ) + ( v3 . x - v4 . x ) * ( v4 . y - v1 . y ) ) / det ;
157
- const gamma = ( ( v1 . y - v2 . y ) * ( v4 . x - v1 . x ) + ( v2 . x - v1 . x ) * ( v4 . y - v1 . y ) ) / det ;
158
- return 0 < lambda && lambda < 1 && ( 0 < gamma && gamma < 1 ) ;
159
- }
137
+ const whisker = new Whisker ( {
138
+ direction,
139
+ whiskerLength,
140
+ paperNumber,
141
+ requiredData,
142
+ } ) ;
143
+
144
+ if ( callback ) {
145
+ whisker . on ( 'paperAdded' , callback ) ;
146
+ whisker . on ( 'paperRemoved' , ( ) => callback ( null ) ) ;
160
147
}
161
- function intersectsPaper ( whiskerStart , whiskerEnd , paper ) {
162
- return (
163
- ( intersects ( whiskerStart , whiskerEnd , paper . points . topLeft , paper . points . topRight ) ||
164
- intersects ( whiskerStart , whiskerEnd , paper . points . topRight , paper . points . bottomRight ) ||
165
- intersects ( whiskerStart , whiskerEnd , paper . points . bottomRight , paper . points . bottomLeft ) ||
166
- intersects ( whiskerStart , whiskerEnd , paper . points . bottomLeft , paper . points . topLeft ) ) &&
167
- requiredData . every ( name => paper . data [ name ] !== undefined )
168
- ) ;
169
- }
170
-
171
- setInterval ( async ( ) => {
172
- const papers = await workerContext . paper . get ( 'papers' ) ;
173
- const points = papers [ paperNumber ] . points ;
174
-
175
- let segment = [ points . topLeft , points . topRight ] ;
176
- if ( direction === 'right' ) segment = [ points . topRight , points . bottomRight ] ;
177
- if ( direction === 'down' ) segment = [ points . bottomRight , points . bottomLeft ] ;
178
- if ( direction === 'left' ) segment = [ points . bottomLeft , points . topLeft ] ;
179
-
180
- const whiskerStart = {
181
- x : ( segment [ 0 ] . x + segment [ 1 ] . x ) / 2 ,
182
- y : ( segment [ 0 ] . y + segment [ 1 ] . y ) / 2 ,
183
- } ;
184
- const whiskerEnd = {
185
- x : whiskerStart . x + ( segment [ 1 ] . y - segment [ 0 ] . y ) * whiskerLength ,
186
- y : whiskerStart . y - ( segment [ 1 ] . x - segment [ 0 ] . x ) * whiskerLength ,
187
- } ;
188
-
189
- if (
190
- ! pointAtData ||
191
- ! papers [ pointAtData . paperNumber ] ||
192
- // Try keeping `pointAtData` stable if possible.
193
- ! intersectsPaper ( whiskerStart , whiskerEnd , papers [ pointAtData . paperNumber ] )
194
- ) {
195
- let newPointAtData = null ;
196
- Object . keys ( papers ) . forEach ( otherPaperNumber => {
197
- if ( otherPaperNumber === paperNumber ) return ;
198
- if ( intersectsPaper ( whiskerStart , whiskerEnd , papers [ otherPaperNumber ] ) ) {
199
- newPointAtData = { paperNumber : otherPaperNumber , paper : papers [ otherPaperNumber ] } ;
200
- }
201
- } ) ;
202
- if ( newPointAtData !== pointAtData ) {
203
- pointAtData = newPointAtData ;
204
- if ( callback ) callback ( pointAtData ) ;
205
- }
206
- }
207
148
208
- supporterCtx . clearRect ( 0 , 0 , supporterCanvas . width , supporterCanvas . height ) ;
209
- supporterCtx . fillStyle = supporterCtx . strokeStyle = pointAtData
210
- ? 'rgb(0, 255, 0)'
211
- : 'rgb(255, 0, 0)' ;
212
- supporterCtx . beginPath ( ) ;
213
- supporterCtx . moveTo ( whiskerStart . x , whiskerStart . y ) ;
214
- supporterCtx . lineTo ( whiskerEnd . x , whiskerEnd . y ) ;
215
- supporterCtx . stroke ( ) ;
216
-
217
- const dotFraction = ( Date . now ( ) / 600 ) % 1 ;
218
- supporterCtx . beginPath ( ) ;
219
- supporterCtx . arc (
220
- whiskerEnd . x * dotFraction + whiskerStart . x * ( 1 - dotFraction ) ,
221
- whiskerEnd . y * dotFraction + whiskerStart . y * ( 1 - dotFraction ) ,
222
- 2 ,
223
- 0 ,
224
- 2 * Math . PI
225
- ) ;
226
- supporterCtx . fill ( ) ;
227
- supporterCtx . commit ( ) ;
228
-
229
- if (
230
- ! lastWhiskerEnd ||
231
- lastWhiskerEnd . x !== whiskerEnd . x ||
232
- lastWhiskerEnd . y !== whiskerEnd . y
233
- ) {
234
- lastWhiskerEnd = whiskerEnd ;
235
- if ( whiskerPointCallback ) whiskerPointCallback ( whiskerEnd . x , whiskerEnd . y ) ;
236
- }
237
- } , 10 ) ;
149
+ if ( whiskerPointCallback ) {
150
+ whisker . on ( 'whiskerMoved' , whiskerPointCallback ) ;
151
+ }
238
152
} ;
239
153
} ) ( self ) ;
0 commit comments