Skip to content

Commit af4d057

Browse files
author
Darren Johnson
committed
added rubberbanding for circles and rectangles
1 parent 467c0c1 commit af4d057

File tree

2 files changed

+95
-46
lines changed

2 files changed

+95
-46
lines changed

test.coffee

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ shapeChoice =
3333
circle: false
3434
line: false
3535

36+
currentObj = ''
37+
shapeChoiceIsSet = false
3638

3739
meshMaterial = new THREE.MeshBasicMaterial(
3840
color: drawColor
@@ -97,24 +99,29 @@ onDocumentMouseMove = (event) ->
9799
event.preventDefault()
98100
mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1
99101
mouse2D.y = -((event.clientY )/ (window.innerHeight )) * 2 + 1
100-
# gotta figure out how to draw incramentally
101-
# if isMouseDown
102-
# intersects = ray.intersectScene(scene)
103-
# if intersects.length > 0
104-
# intersector = getRealIntersector(intersects)
105-
# if isCtrlDown
106-
# scene.removeObject intersector.object unless intersector.object == plane
107-
# else
108-
# intersector = getRealIntersector(intersects)
109-
# setSquarePosition intersector
110-
# square = new THREE.Mesh(cubeGeo, meshMaterial)
111-
# square.position.copy squarePosition
112-
# square.matrixAutoUpdate = false
113-
# square.updateMatrix()
114-
# scene.addObject square
102+
103+
temp = mouse2D.clone()
104+
projector.unprojectVector(temp, camera)
105+
106+
if isMouseDown && shapeChoiceIsSet() && ! shapeChoice.line
107+
108+
dX = ( Math.max(temp.x, dStart.x) - Math.min(temp.x, dStart.x) )
109+
dY = ( Math.max(temp.y, dStart.y) - Math.min(temp.y, dStart.y) )
110+
111+
if shapeChoice.rectangle
112+
xScale = dX / 5.0
113+
yScale = dY / 5.0
114+
else if shapeChoice.circle
115+
radius = Math.sqrt(dX * dX + dY * dY)
116+
xScale = yScale = radius / 14
117+
118+
currentObj.scale.set(xScale, yScale, 1)
119+
currentObj.updateMatrix()
120+
115121

116122
# onDocumentMouseDown(event)
117123
# find the current location of the click event by unprojecting
124+
# if a shapeChoice has been made then create the base object of small size (scale it as mouse moves while mouse down)
118125
onDocumentMouseDown = (event) ->
119126
event.preventDefault()
120127
isMouseDown = true
@@ -123,13 +130,23 @@ onDocumentMouseDown = (event) ->
123130
mouse2D.y = -((event.clientY )/ window.innerHeight) * 2 + 1
124131
mouse2D.z = 1
125132

126-
temp = mouse2D
133+
temp = mouse2D.clone()
127134
projector.unprojectVector( temp, camera)
128135

129136
dStart.x = temp.x
130137
dStart.y = temp.y
131138
dStart.z = 0
132139

140+
if shapeChoice.rectangle
141+
pX = dStart.x
142+
pY = dStart.y
143+
makeRectangle(10,10,pX,pY, drawColor)
144+
else if shapeChoice.circle
145+
makeCircle(10, 10, dStart.x, dStart.y, drawColor)
146+
147+
148+
149+
133150
# onDocumentMouseUp(event)
134151
# find the current location of the click event by unprojecting
135152
# create a shape object depending on the menu selections
@@ -144,7 +161,7 @@ onDocumentMouseUp = (event) ->
144161
mouse2D.y = -((event.clientY )/ window.innerHeight) * 2 + 1
145162
mouse2D.z = 1
146163

147-
dEnd = mouse2D
164+
dEnd = mouse2D.clone()
148165
dEnd = projector.unprojectVector( dEnd, camera)
149166

150167
# find the delta between the initial mouse down and the mouse up positions
@@ -153,14 +170,15 @@ onDocumentMouseUp = (event) ->
153170

154171

155172
# draw the shape
156-
if shapeChoice.rectangle
157-
# calculate the position of the rectangle (position is the center of the object so I have to offset by half the height and width)
158-
pX = dStart.x + .5 * dX
159-
pY = dStart.y - .5 * dY
160-
makeRectangle(dX,dY,pX,pY, drawColor)
161-
else if shapeChoice.circle
162-
makeCircle(dX, dY, dStart.x, dStart.y, drawColor)
163-
else if shapeChoice.line
173+
# if shapeChoice.rectangle
174+
# # calculate the position of the rectangle (position is the center of the object so I have to offset by half the height and width)
175+
# pX = dStart.x + .5 * dX
176+
# pY = dStart.y - .5 * dY
177+
# makeRectangle(dX,dY,pX,pY, drawColor)
178+
# else if shapeChoice.circle
179+
# makeCircle(dX, dY, dStart.x, dStart.y, drawColor)
180+
# else
181+
if shapeChoice.line
164182
makeLine(dStart.x, dStart.y, dEnd.x, dEnd.y, drawColor)
165183

166184
# onDocumentKeyDown(event)
@@ -255,6 +273,7 @@ makeRectangle = (dX, dY, pX, pY, color ) ->
255273
scene.addObject square
256274
# add the object to the object list for save/load purposes
257275
object_list[square.id] = ['rectangle', dX, dY, pX, pY, color]
276+
currentObj = square
258277

259278
# makeCircle(deltaX, deltaY, positionX, positionY)
260279
# create a circle from radius, and center point
@@ -273,14 +292,15 @@ makeCircle = (dX, dY, pX, pY, color) ->
273292
scene.addObject circle
274293
# add the object to the object list for save/load purposes
275294
object_list[circle.id] = ['circle', dX, dY, pX, pY, color]
295+
currentObj = circle
276296

277297
# makeLine(startX, startY, end, endY)
278298
# create a Line from start point and end point
279299
makeLine = (sX, sY, eX, eY, color) ->
280300
lineMat = new THREE.LineBasicMaterial(
281301
color: color
282-
opacity: 0.8
283-
linewidth: 1
302+
opacity: 1.0
303+
linewidth: 2
284304
)
285305

286306
lineGeo = new THREE.Geometry(0)
@@ -292,6 +312,7 @@ makeLine = (sX, sY, eX, eY, color) ->
292312
scene.addObject(line)
293313
# add the object to the object list for save/load purposes
294314
object_list[line.id] = ['line', sX, sY, eX, eY, color]
315+
currentObj = line
295316

296317
# open a prompt asking for the save/load name
297318
showPrompt = ->
@@ -333,6 +354,9 @@ setDrawColor = ->
333354

334355
######## UI Elements ################
335356

357+
shapeChoiceIsSet = ->
358+
return (shapeChoice.circle || shapeChoice.line || shapeChoice.rectangle)
359+
336360
# create the color control UI
337361
color_gui = new DAT.GUI({
338362
height: 3 * 32 -1

test.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var animate, camera, click2D, colorValues, color_gui, container, cube, cubeGeo, dEnd, dStart, deselect, drawColor, i, init, intersector, isCtrlDown, isMouseDown, isShiftDown, lineMat, load, loadObject, makeCircle, makeLine, makeRectangle, makeSquare, meshMaterial, mouse2D, mouse3D, object_list, onDocumentKeyDown, onDocumentKeyUp, onDocumentMouseDown, onDocumentMouseMove, onDocumentMouseUp, plane, print, projector, ray, render, renderer, rollOverMaterial, rollOverMesh, rollOveredFace, save, scene, setDrawColor, shapeChoice, shape_gui, showPrompt, squarePosition, square_size, startSquarePosX, startSquarePosY, stats, theta, tmpVec;
1+
var animate, camera, click2D, colorValues, color_gui, container, cube, cubeGeo, currentObj, dEnd, dStart, deselect, drawColor, i, init, intersector, isCtrlDown, isMouseDown, isShiftDown, lineMat, load, loadObject, makeCircle, makeLine, makeRectangle, makeSquare, meshMaterial, mouse2D, mouse3D, object_list, onDocumentKeyDown, onDocumentKeyUp, onDocumentMouseDown, onDocumentMouseMove, onDocumentMouseUp, plane, print, projector, ray, render, renderer, rollOverMaterial, rollOverMesh, rollOveredFace, save, scene, setDrawColor, shapeChoice, shapeChoiceIsSet, shape_gui, showPrompt, squarePosition, square_size, startSquarePosX, startSquarePosY, stats, theta, tmpVec;
22
if (!Detector.webgl) {
33
Detector.addGetWebGLMessage();
44
}
@@ -28,6 +28,8 @@ shapeChoice = {
2828
circle: false,
2929
line: false
3030
};
31+
currentObj = '';
32+
shapeChoiceIsSet = false;
3133
meshMaterial = new THREE.MeshBasicMaterial({
3234
color: drawColor,
3335
wireframe: false
@@ -61,42 +63,59 @@ init = function() {
6163
return document.addEventListener("keyup", onDocumentKeyUp, false);
6264
};
6365
onDocumentMouseMove = function(event) {
66+
var dX, dY, radius, temp, xScale, yScale;
6467
event.preventDefault();
6568
mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
66-
return mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
69+
mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
70+
temp = mouse2D.clone();
71+
projector.unprojectVector(temp, camera);
72+
if (isMouseDown && shapeChoiceIsSet() && !shapeChoice.line) {
73+
dX = Math.max(temp.x, dStart.x) - Math.min(temp.x, dStart.x);
74+
dY = Math.max(temp.y, dStart.y) - Math.min(temp.y, dStart.y);
75+
if (shapeChoice.rectangle) {
76+
xScale = dX / 5.0;
77+
yScale = dY / 5.0;
78+
} else if (shapeChoice.circle) {
79+
radius = Math.sqrt(dX * dX + dY * dY);
80+
xScale = yScale = radius / 14;
81+
}
82+
currentObj.scale.set(xScale, yScale, 1);
83+
return currentObj.updateMatrix();
84+
}
6785
};
6886
onDocumentMouseDown = function(event) {
69-
var temp;
87+
var pX, pY, temp;
7088
event.preventDefault();
7189
isMouseDown = true;
7290
mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
7391
mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
7492
mouse2D.z = 1;
75-
temp = mouse2D;
93+
temp = mouse2D.clone();
7694
projector.unprojectVector(temp, camera);
7795
dStart.x = temp.x;
7896
dStart.y = temp.y;
79-
return dStart.z = 0;
97+
dStart.z = 0;
98+
if (shapeChoice.rectangle) {
99+
pX = dStart.x;
100+
pY = dStart.y;
101+
return makeRectangle(10, 10, pX, pY, drawColor);
102+
} else if (shapeChoice.circle) {
103+
return makeCircle(10, 10, dStart.x, dStart.y, drawColor);
104+
}
80105
};
81106
onDocumentMouseUp = function(event) {
82-
var dX, dY, pX, pY;
107+
var dX, dY;
83108
event.preventDefault();
84109
isMouseDown = false;
85110
setDrawColor();
86111
mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
87112
mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
88113
mouse2D.z = 1;
89-
dEnd = mouse2D;
114+
dEnd = mouse2D.clone();
90115
dEnd = projector.unprojectVector(dEnd, camera);
91116
dX = Math.max(dEnd.x, dStart.x) - Math.min(dEnd.x, dStart.x);
92117
dY = Math.max(dEnd.y, dStart.y) - Math.min(dEnd.y, dStart.y);
93-
if (shapeChoice.rectangle) {
94-
pX = dStart.x + .5 * dX;
95-
pY = dStart.y - .5 * dY;
96-
return makeRectangle(dX, dY, pX, pY, drawColor);
97-
} else if (shapeChoice.circle) {
98-
return makeCircle(dX, dY, dStart.x, dStart.y, drawColor);
99-
} else if (shapeChoice.line) {
118+
if (shapeChoice.line) {
100119
return makeLine(dStart.x, dStart.y, dEnd.x, dEnd.y, drawColor);
101120
}
102121
};
@@ -167,7 +186,8 @@ makeRectangle = function(dX, dY, pX, pY, color) {
167186
square.matrixAutoUpdate = false;
168187
square.updateMatrix();
169188
scene.addObject(square);
170-
return object_list[square.id] = ['rectangle', dX, dY, pX, pY, color];
189+
object_list[square.id] = ['rectangle', dX, dY, pX, pY, color];
190+
return currentObj = square;
171191
};
172192
makeCircle = function(dX, dY, pX, pY, color) {
173193
var circle, circleGeo, radius;
@@ -182,15 +202,16 @@ makeCircle = function(dX, dY, pX, pY, color) {
182202
circle.matrixAutoUpdate = false;
183203
circle.updateMatrix();
184204
scene.addObject(circle);
185-
return object_list[circle.id] = ['circle', dX, dY, pX, pY, color];
205+
object_list[circle.id] = ['circle', dX, dY, pX, pY, color];
206+
return currentObj = circle;
186207
}
187208
};
188209
makeLine = function(sX, sY, eX, eY, color) {
189210
var line, lineGeo, p1, p2;
190211
lineMat = new THREE.LineBasicMaterial({
191212
color: color,
192-
opacity: 0.8,
193-
linewidth: 1
213+
opacity: 1.0,
214+
linewidth: 2
194215
});
195216
lineGeo = new THREE.Geometry(0);
196217
p1 = new THREE.Vector3(sX, sY, 0);
@@ -199,7 +220,8 @@ makeLine = function(sX, sY, eX, eY, color) {
199220
lineGeo.vertices.push(new THREE.Vertex(p2));
200221
line = new THREE.Line(lineGeo, lineMat);
201222
scene.addObject(line);
202-
return object_list[line.id] = ['line', sX, sY, eX, eY, color];
223+
object_list[line.id] = ['line', sX, sY, eX, eY, color];
224+
return currentObj = line;
203225
};
204226
showPrompt = function() {
205227
var name;
@@ -236,6 +258,9 @@ setDrawColor = function() {
236258
drawColor += g;
237259
return drawColor += b;
238260
};
261+
shapeChoiceIsSet = function() {
262+
return shapeChoice.circle || shapeChoice.line || shapeChoice.rectangle;
263+
};
239264
color_gui = new DAT.GUI({
240265
height: 3 * 32 - 1
241266
});

0 commit comments

Comments
 (0)