-
Notifications
You must be signed in to change notification settings - Fork 14
/
IKRS.BezierCanvasHandler.js~
369 lines (275 loc) · 12.7 KB
/
IKRS.BezierCanvasHandler.js~
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* @author Ikaros Kappler
* @date 2013-08-14
* @version 1.0.0
**/
IKRS.BezierCanvasHandler = function() {
IKRS.Object.call( this );
// These are MouseEvent locations
var latestMouseDownPosition;
var latestMouseDragPosition;
var currentDragPoint;
var canvas_width = 512;
var canvas_height = 768;
this.bezierCurve = null;
this.canvas = document.getElementById("bezier_canvas");
this.context = this.canvas.getContext( "2d" );
/*
var renderer = new THREE.WebGLRenderer( { "canvas" : bezier_canvas } );
if( !renderer.context )
window.alert( "ERR" );
// This is a THREE.Vector3 rotation (euklid)
var currentRotation;
var mesh; // will be initialized later
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75,
canvas_width/canvas_height,
0.1,
1000
);
// create a point light
var pointLight =
new THREE.PointLight(0xFFFFFF);
// set its position
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
// add to the scene
scene.add( pointLight );
*/
this.canvas.onmousedown = this.mouseDownHandler;
this.canvas.onmouseup = this.mouseUpHandler;
this.canvas.onmousemove = this.mouseMoveHandler;
/*
this.canvas.onmousedown = function( e ) {
// window.alert( "mouse down. Event: " + e + ", e.pageX=" + e.pageX + ", e.pageY=" + e.pageY );
this.latestMouseDownPosition = new THREE.Vector2( e.pageX, e.pageY );
this.latestMouseDragPosition = new THREE.Vector2( e.pageX, e.pageY );
window.alert( getBezierCurve() );
// Find drag point?
if( this.pointIsNearPosition(this.getBezierCurve().getStartPoint(), e.pageX, e.pageY, 5) ) {
this.currentDragPoint = this.getBezierCurve().getStartPoint();
} else if( this.pointIsNearPosition(this.getBezierCurve().getEndPoint(), e.pageX, e.pageY, 5) ) {
this.currentDragPoint = this.getBezierCurve().getEndPoint();
} else if( this.pointIsNearPosition(this.getBezierCurve().getStartControlPoint(), e.pageX, e.pageY, 5) ) {
this.currentDragPoint = this.getBezierCurve().getStartControlPoint();
} else if( this.pointIsNearPosition(this.getBezierCurve().getEndControlPoint(), e.pageX, e.pageY, 5) ) {
this.currentDragPoint = this.getBezierCurve().getEndControlPoint();
}
}
this.canvas.onmouseup = function( e ) {
// window.alert( "mouse up. Event: " + e + ", e.pageX=" + e.pageX + ", e.pageY=" + e.pageY );
// Clear mouse down position
this.latestMouseDownPosition = null;
this.latestMouseDragPosition = null;
this.currentDragPoint = null;
}
this.canvas.onmousemove = function( e ) {
// window.alert( "mouse move. Event: " + e + ", e.pageX=" + e.pageX + ", e.pageY=" + e.pageY );
if( this.latestMouseDownPosition ) {
if( this.currentDragPoint ) {
// Update dragges point's position
var moveX = this.latestMouseDragPosition.x - e.pageX;
var moveY = this.latestMouseDragPosition.y - e.pageY;
this.currentDragPoint.add( new THREE.Vector2(moveX,moveY) );
// Re-calculate the segment cache
this.getBezierCurve().updateArcLengths();
// And repaint the curve
this.drawBezierCurve( this.context,
this.bezierCurve,
this.drawOffset
);
}
this.latestMouseDownPosition.set( e.pageX, e.pageY );
this.latestMouseDragPosition.set( e.pageX, e.pageY );
}
}
*/
/*
this.pointIsNearPosition = function( point,
x,
y,
tolerance ) {
return (Math.sqrt( Math.pow(point.x-x,2) + Math.pow(point.y-y,2) ) <= tolerance);
}
*/
/*
//window.alert( "preview_canvas.width=" + preview_canvas.width + ", preview_canvas.height=" + preview_canvas.height );
renderer.setSize( canvas_width, // preview_canvas.width, // canvasWidth, // window.innerWidth,
canvas_height // preview_canvas.height // canvasHeight // window.innerHeight
);
document.body.appendChild( renderer.domElement );
*/
//window.alert( "BB" );
var bezierWidth = 400;
var bezierHeight = 500;
this.bezierCurve = new IKRS.CubicBezierCurve( new THREE.Vector2( bezierWidth/8, 5*bezierHeight/8 ), // startPoint
new THREE.Vector2( 7*bezierWidth/8, 5*bezierHeight/8 ), // endPoint
new THREE.Vector2( 3*bezierWidth/8, bezierHeight/4, 0), // startControlPoint
new THREE.Vector2( 5*bezierWidth/8, bezierHeight/4, 0) // endControlPoint
);
// Store a reverse reference inside the handler so the mousehandlers can access this object
this.canvas.bezierCanvasHandler = this;
// Draw the bezier curve
// (the dirty way: access the segment cache directly)
//this.drawOffset = new THREE.Vector2( 100, 0 );
this.drawBezierCurve( this.context,
this.bezierCurve,
this.drawOffset,
true, // drawStartPoint
true, // drawEndPoint
true, // drawStartControlPoint
true // drawEndControlPoint
);
}
IKRS.BezierCanvasHandler.prototype = new IKRS.Object();
IKRS.BezierCanvasHandler.prototype.constructor = IKRS.BezierCanvasHandler;
IKRS.BezierCanvasHandler.prototype.drawOffset = new THREE.Vector2( 100, 0 );
// 0: start point
// 1: start control point
// 2: end control point
// 3: end point
IKRS.BezierCanvasHandler.prototype.draggedPointID = -1;
IKRS.BezierCanvasHandler.prototype.drawBezierCurve = function( context,
bezierCurve,
drawOffset,
drawStartPoint,
drawEndPoint,
drawStartControlPoint,
drawEndControlPoint ) {
// Clear screen?
if( document.forms["bezier_form"].elements["clear_on_repaint"].checked ) {
context.fillStyle = "#FFFFFF";
context.fillRect( 0, 0, 512, 768 );
}
context.lineWidth = 1;
context.beginPath();
// window.alert( "Beginning bezier path at: (" + bezierCurve.segmentCache[0].x + ", " + bezierCurve.segmentCache[0].y + ")" );
context.moveTo( bezierCurve.segmentCache[0].x + drawOffset.x,
bezierCurve.segmentCache[0].y + drawOffset.y
);
for( var i = 1; i < bezierCurve.segmentCache.length; i++ ) {
context.lineTo( bezierCurve.segmentCache[i].x + drawOffset.x,
bezierCurve.segmentCache[i].y + drawOffset.y
);
}
context.stroke();
// Draw the end points
if( drawStartPoint || drawEndPoint ) {
context.fillStyle = "#B400FF";
// Start point?
if( drawStartPoint ) {
context.fillRect( bezierCurve.getStartPoint().x - 1 + drawOffset.x,
bezierCurve.getStartPoint().y - 1 + drawOffset.y,
3, 3 );
}
// End point?
if( drawEndPoint ) {
context.fillRect( bezierCurve.getEndPoint().x - 1 + drawOffset.x,
bezierCurve.getEndPoint().y - 1 + drawOffset.y,
3, 3 );
}
}
// Draw the control points?
if( drawStartControlPoint || drawEndControlPoint ) {
context.fillStyle = "#B8D438";
// Start control point?
if( drawStartControlPoint ) {
context.fillRect( bezierCurve.getStartControlPoint().x - 1 + drawOffset.x,
bezierCurve.getStartControlPoint().y - 1 + drawOffset.y,
3, 3 );
}
// End control point?
if( drawEndControlPoint ) {
context.fillRect( bezierCurve.getEndControlPoint().x - 1 + drawOffset.x,
bezierCurve.getEndControlPoint().y - 1 + drawOffset.y,
3, 3 );
}
}
}
IKRS.BezierCanvasHandler.prototype.getBezierCurve = function() {
return this.bezierCurve;
}
IKRS.BezierCanvasHandler.prototype.pointIsNearPosition = function( point,
x,
y,
tolerance ) {
var distance = Math.sqrt( Math.pow(point.x-x,2) + Math.pow(point.y-y,2) );
//window.alert( "point=(" + point.x + ", " + point.y + "), x=" + x + ", y=" + y + ", tolerance=" + tolerance + ", distance=" + distance );
return ( distance <= tolerance );
}
IKRS.BezierCanvasHandler.prototype.mouseDownHandler = function( e ) {
// window.alert( "mouse down. Event: " + e + ", e.pageX=" + e.pageX + ", e.pageY=" + e.pageY );
this.bezierCanvasHandler.latestMouseDownPosition = new THREE.Vector2( e.pageX, e.pageY );
this.bezierCanvasHandler.latestMouseDragPosition = new THREE.Vector2( e.pageX, e.pageY );
//window.alert( this.bezierCanvasHandler.getBezierCurve() );
// Find relative coordinates
var rect = this.getBoundingClientRect();
var left = e.clientX - rect.left - this.clientLeft + this.scrollLeft;
var top = e.clientY - rect.top - this.clientTop + this.scrollTop;
//window.alert( "left=" + left + ", top=" + top );
// Add draw offset :)
left -= this.bezierCanvasHandler.drawOffset.x;
top -= this.bezierCanvasHandler.drawOffset.y;
var clickTolerance = 50; // px
// Find drag point?
if( this.bezierCanvasHandler.pointIsNearPosition(this.bezierCanvasHandler.getBezierCurve().getStartPoint(), left, top, clickTolerance) ) {
this.bezierCanvasHandler.currentDragPoint = this.bezierCanvasHandler.getBezierCurve().getStartPoint();
this.bezierCanvasHandler.draggedPointID = 0;
} else if( this.bezierCanvasHandler.pointIsNearPosition(this.bezierCanvasHandler.getBezierCurve().getEndPoint(), left, top, clickTolerance) ) {
this.bezierCanvasHandler.currentDragPoint = this.bezierCanvasHandler.getBezierCurve().getEndPoint();
this.bezierCanvasHandler.draggedPointID = 3;
} else if( this.bezierCanvasHandler.pointIsNearPosition(this.bezierCanvasHandler.getBezierCurve().getStartControlPoint(), left, top, clickTolerance) ) {
this.bezierCanvasHandler.currentDragPoint = this.bezierCanvasHandler.getBezierCurve().getStartControlPoint();
this.bezierCanvasHandler.draggedPointID = 1;
} else if( this.bezierCanvasHandler.pointIsNearPosition(this.bezierCanvasHandler.getBezierCurve().getEndControlPoint(), left, top, clickTolerance) ) {
this.bezierCanvasHandler.currentDragPoint = this.bezierCanvasHandler.getBezierCurve().getEndControlPoint();
this.bezierCanvasHandler.draggedPointID = 2;
} else {
this.bezierCanvasHandler.draggedPointID = -1;
}
}
IKRS.BezierCanvasHandler.prototype.mouseUpHandler = function( e ) {
// window.alert( "mouse up. Event: " + e + ", e.pageX=" + e.pageX + ", e.pageY=" + e.pageY );
// Clear mouse down position
this.bezierCanvasHandler.latestMouseDownPosition = null;
this.bezierCanvasHandler.latestMouseDragPosition = null;
this.bezierCanvasHandler.currentDragPoint = null;
this.bezierCanvasHandler.draggedPointID = -1;
// And repaint the curve (to make the eventually hidden drag points to disappear)
this.bezierCanvasHandler.drawBezierCurve( this.bezierCanvasHandler.context,
this.bezierCanvasHandler.bezierCurve,
this.bezierCanvasHandler.drawOffset,
(this.bezierCanvasHandler.draggedPointID != 0), // drawStartPoint
(this.bezierCanvasHandler.draggedPointID != 3), // drawEndPoint
(this.bezierCanvasHandler.draggedPointID != 1), // drawStartControlPoint
(this.bezierCanvasHandler.draggedPointID != 2) // drawEndControlPoint
);
}
IKRS.BezierCanvasHandler.prototype.mouseMoveHandler = function( e ) {
//window.alert( "mouse move. Event: " + e + ", e.pageX=" + e.pageX + ", e.pageY=" + e.pageY + ", lastMouseDownPosition=" + this.bezierCanvasHandler.latestMouseDownPosition + ", currentDragPoint=" + this.bezierCanvasHandler.currentDragPoint );
if( this.bezierCanvasHandler.latestMouseDownPosition ) {
if( this.bezierCanvasHandler.currentDragPoint ) {
// Update dragges point's position
var moveX = this.bezierCanvasHandler.latestMouseDragPosition.x - e.pageX;
var moveY = this.bezierCanvasHandler.latestMouseDragPosition.y - e.pageY;
this.bezierCanvasHandler.currentDragPoint.add( new THREE.Vector2(-moveX,-moveY) );
// window.alert( "Point moved by (" + moveX + ", " + moveY + ")" );
// Re-calculate the segment cache
this.bezierCanvasHandler.getBezierCurve().updateArcLengths();
// And repaint the curve
this.bezierCanvasHandler.drawBezierCurve( this.bezierCanvasHandler.context,
this.bezierCanvasHandler.bezierCurve,
this.bezierCanvasHandler.drawOffset,
(this.bezierCanvasHandler.draggedPointID != 0), // drawStartPoint
(this.bezierCanvasHandler.draggedPointID != 3), // drawEndPoint
(this.bezierCanvasHandler.draggedPointID != 1), // drawStartControlPoint
(this.bezierCanvasHandler.draggedPointID != 2) // drawEndControlPoint
);
}
this.bezierCanvasHandler.latestMouseDownPosition.set( e.pageX, e.pageY );
this.bezierCanvasHandler.latestMouseDragPosition.set( e.pageX, e.pageY );
}
}
//window.alert( "IKRS.BezierCanvasHandler=" + IKRS.BezierCanvasHandler );
//window.alert( "IKRS.BezierCanvasHandler.prototype=" + IKRS.BezierCanvasHandler.prototype );