|
28
|
1 |
// .................................................................... |
|
|
2 |
// Main Visual Sedimentation Code |
|
|
3 |
// .................................................................... |
|
|
4 |
|
|
|
5 |
// TODO : |
|
|
6 |
// - callback on rollOut |
|
|
7 |
|
|
|
8 |
(function($){ |
|
|
9 |
|
|
|
10 |
// Name Space Plug in Jquery Objects |
|
|
11 |
$.fn.vs = function (){} |
|
|
12 |
$.fn._vs={} |
|
|
13 |
|
|
|
14 |
// All this objects are define in correspondant .js files |
|
|
15 |
$.fn._vs.token = {} |
|
|
16 |
$.fn._vs.draw = {} |
|
|
17 |
$.fn._vs.stream = {} |
|
|
18 |
$.fn._vs.chart = {} |
|
|
19 |
$.fn._vs.phy = {} |
|
|
20 |
$.fn._vs.decay = {} |
|
|
21 |
$.fn._vs.flocculate = {} |
|
|
22 |
$.fn._vs.strata = {} |
|
|
23 |
$.fn._vs.aggregate = {} |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
// Core Classe |
|
|
27 |
var VisualSedimentation = function(element,options){ |
|
|
28 |
|
|
|
29 |
// Attach objects |
|
|
30 |
this.token = $.fn._vs.token |
|
|
31 |
this.draw = $.fn._vs.draw |
|
|
32 |
this.stream = $.fn._vs.stream |
|
|
33 |
this.chart = $.fn._vs.chart |
|
|
34 |
this.phy = $.fn._vs.phy |
|
|
35 |
this.decay = $.fn._vs.decay |
|
|
36 |
this.flocculate = $.fn._vs.flocculate |
|
|
37 |
this.strata = $.fn._vs.strata |
|
|
38 |
// this.aggregate = $.fn._vs.aggregate |
|
|
39 |
this.requestAnimFrame; |
|
|
40 |
|
|
|
41 |
|
|
|
42 |
// Mouse object have to be refactor |
|
|
43 |
this.mouse ={} |
|
|
44 |
this.mouse.x = 0 |
|
|
45 |
this.mouse.y = 0 |
|
|
46 |
this.mouse.isMouseDragging = false |
|
|
47 |
this.mouse.isMouseDown = false |
|
|
48 |
this.mouse.selectedBody = null |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
// Variables |
|
|
52 |
this.dataFlow = []; |
|
|
53 |
this.chartPhySetup= {} |
|
|
54 |
this.tokens = []; |
|
|
55 |
this.world = null; |
|
|
56 |
this.ctx = null; |
|
|
57 |
var elem = $(element); |
|
|
58 |
var self = this; |
|
|
59 |
var tokens = []; |
|
|
60 |
var B2D; |
|
|
61 |
var canvas; |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
// Default Settings |
|
|
65 |
var defaultSettings = { |
|
|
66 |
x:0, |
|
|
67 |
y:0, |
|
|
68 |
width:290.5, |
|
|
69 |
height:300.5, |
|
|
70 |
DOMelement:null, |
|
|
71 |
|
|
|
72 |
chart:{ |
|
|
73 |
x:undefined, |
|
|
74 |
y:undefined, |
|
|
75 |
width:undefined, |
|
|
76 |
height:undefined, |
|
|
77 |
colorRange:d3.scale.category10(), |
|
|
78 |
scale:d3.scale, |
|
|
79 |
type:'StackedAreaChart', |
|
|
80 |
/* |
|
|
81 |
name are based on prefuse tollokit layout : |
|
|
82 |
- CircleLayout, |
|
|
83 |
- StackedAreaChart, |
|
|
84 |
//- bubbleAreaChart, |
|
|
85 |
x AxisLabelLayout, |
|
|
86 |
x AxisLayout, |
|
|
87 |
x CollapsedStackLayout, |
|
|
88 |
x GridLayout, |
|
|
89 |
*/ |
|
|
90 |
spacer:5, |
|
|
91 |
//treeLayout:false, |
|
|
92 |
column:3, |
|
|
93 |
wallColor:"rgba(230,230,230,0)", |
|
|
94 |
label:true, |
|
|
95 |
radius:10 // for CircleLayout |
|
|
96 |
}, |
|
|
97 |
data:{ |
|
|
98 |
model:[ |
|
|
99 |
{label:"Column A"}, |
|
|
100 |
{label:"Column B"}, |
|
|
101 |
{label:"Column C"}, |
|
|
102 |
], |
|
|
103 |
strata:[ |
|
|
104 |
[ |
|
|
105 |
{initValue: 100, label: "Strata 1 col A"} |
|
|
106 |
],[ |
|
|
107 |
{initValue: 20, label: "Strata 1 col B"} |
|
|
108 |
],[ |
|
|
109 |
{initValue: 175, label: "Strata 2 col C"} |
|
|
110 |
] |
|
|
111 |
], |
|
|
112 |
token:[ |
|
|
113 |
{ |
|
|
114 |
timestamp:1, |
|
|
115 |
category:1, |
|
|
116 |
value: 1, |
|
|
117 |
userdata:{}, |
|
|
118 |
callback:{} |
|
|
119 |
} |
|
|
120 |
], |
|
|
121 |
tokenPast:0, |
|
|
122 |
stream:{ |
|
|
123 |
provider:'generator', |
|
|
124 |
refresh:10000/8, |
|
|
125 |
now:0 |
|
|
126 |
}, |
|
|
127 |
} |
|
|
128 |
, |
|
|
129 |
sedimentation:{ |
|
|
130 |
token:{size:{original:4 |
|
|
131 |
,minimum:2} |
|
|
132 |
,visible:true}, // fill color, shape, |
|
|
133 |
incoming:{ |
|
|
134 |
strategy:1, // 1 = one element by one, more = by Groupe |
|
|
135 |
point:[{x:50,y:0}, |
|
|
136 |
{x:100,y:0}, |
|
|
137 |
{x:150,y:0}], |
|
|
138 |
|
|
|
139 |
target:[{x:50,y:0}, |
|
|
140 |
{x:100,y:0}, |
|
|
141 |
{x:150,y:0}] |
|
|
142 |
}, |
|
|
143 |
granulate:{visible:false}, |
|
|
144 |
flocculate:{ |
|
|
145 |
number:1, // 1 = one element by one, more = by groupe of n |
|
|
146 |
action:"buffer", // [buffer,continue] |
|
|
147 |
strategy:"Size", // [BufferSize, Time, AcummulationAreaHeight, Fps, Manual] |
|
|
148 |
bufferSize:5, // number of token to make floculation |
|
|
149 |
bufferTime:1000, // time buffer to make flocullation |
|
|
150 |
bufferHeight:50, // height (pixel) to make floculation |
|
|
151 |
bufferFrameRate:25, // if the computer is to slow floculate |
|
|
152 |
buffer:[] |
|
|
153 |
}, |
|
|
154 |
suspension:{ |
|
|
155 |
height:null, // pourcent,adaptative |
|
|
156 |
incomming:'top', |
|
|
157 |
decay:{power:1.001}, // null |
|
|
158 |
refresh:200 |
|
|
159 |
}, |
|
|
160 |
accumulation:{height:null}, // pourcent ,adaptative |
|
|
161 |
aggregation:{height:0, maxData:0, invertStrata:false}, // pourcent ,adaptative |
|
|
162 |
}, |
|
|
163 |
options:{ |
|
|
164 |
refresh:1000/25, |
|
|
165 |
panel:false, |
|
|
166 |
scale:30, |
|
|
167 |
layout:false, |
|
|
168 |
canvasFirst:true |
|
|
169 |
} |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
this.now = function(){ |
|
|
174 |
return(new Date().getTime()) |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
// get Box2d World |
|
|
178 |
this.globalDecay = function (value){ |
|
|
179 |
if(typeof(value)=='undefined'){ |
|
|
180 |
return this.settings.sedimentation.suspension.decay.power |
|
|
181 |
}else{ |
|
|
182 |
return this.settings.sedimentation.suspension.decay.power=value |
|
|
183 |
} |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
// get Box2d World |
|
|
187 |
this.getWorld = function (){ |
|
|
188 |
return this.world; |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
this.chartUpdate = function (cat,y){ |
|
|
192 |
var options = {cat:cat,y:y} |
|
|
193 |
this.chart[this.settings.chart.type](self,'update',options) |
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
// Todo ...... |
|
|
197 |
this.flocculateTokens = function (number){ |
|
|
198 |
return this.flocculate.update(self,number) |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
// TODO DESTROY ALL TOKENS |
|
|
202 |
this.flocculateAll = function(){ |
|
|
203 |
return this.flocculate.all(self) |
|
|
204 |
} |
|
|
205 |
|
|
|
206 |
// Add token function |
|
|
207 |
this.addToken = function (element){ |
|
|
208 |
//var token = this.token.addToken(self,element) |
|
|
209 |
return this.token.addToken(self,element); |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
// Select token fonction |
|
|
213 |
this.selectAll = function (key,value){ |
|
|
214 |
return this.token.selectAll(self,key,value); |
|
|
215 |
} |
|
|
216 |
|
|
|
217 |
// Select token fonction |
|
|
218 |
this.select = function (key,value){ |
|
|
219 |
return this.token.select(self,key,value); |
|
|
220 |
} |
|
|
221 |
|
|
|
222 |
// update a categoryr in the chart |
|
|
223 |
this.updateAll = function (values){ |
|
|
224 |
var tokens = this.chart.updateAll(self,key,value) |
|
|
225 |
return tokens; |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
// update a category in the chart |
|
|
229 |
this.update = function (key,value){ |
|
|
230 |
var tokens = this.chart.update(self,key,value) |
|
|
231 |
return tokens; |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
/// Settings without |
|
|
236 |
|
|
|
237 |
|
|
|
238 |
//////////////////////////////////////////////////////// TO CLEAN |
|
|
239 |
// SAM @ROM1 : are you sure you need that ? extend doing it well normally |
|
|
240 |
// Merge options with defaults |
|
|
241 |
// http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically |
|
|
242 |
|
|
|
243 |
//console.log("////////") |
|
|
244 |
//options.model = modelToStrata(options.data.model) |
|
|
245 |
|
|
|
246 |
function merge_options(obj1,obj2){ |
|
|
247 |
var obj3 = {}; |
|
|
248 |
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } |
|
|
249 |
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; } |
|
|
250 |
return obj3; |
|
|
251 |
} |
|
|
252 |
|
|
|
253 |
merge_options(defaultSettings, options); |
|
|
254 |
if(options.data!=undefined) |
|
|
255 |
defaultSettings.data = options.data; |
|
|
256 |
|
|
|
257 |
//////////////////////////////////////////////////////////////////////////////////// |
|
|
258 |
// Merge option and add the DOMelement to setting |
|
|
259 |
//this.settings = $.extend(defaultSettings, options || {}); |
|
|
260 |
this.settings = $.extend(true,defaultSettings, options); |
|
|
261 |
this.settings.DOMelement = element |
|
|
262 |
//console.log('settings after extend',this.settings) |
|
|
263 |
|
|
|
264 |
// ----------------------------------------------- |
|
|
265 |
// SIMPLE DEFAULT SETTING FOR RETRO COMPATIBILITY |
|
|
266 |
// |
|
|
267 |
if(typeof(this.settings.chart.width) =="undefined"){this.settings.chart.width = this.settings.width} |
|
|
268 |
if(typeof(this.settings.chart.x) =="undefined")this.settings.chart.x=0 |
|
|
269 |
if(typeof(this.settings.chart.y) =="undefined")this.settings.chart.y=0 |
|
|
270 |
if(typeof(this.settings.chart.height)=="undefined")this.settings.chart.height=this.settings.height |
|
|
271 |
if(typeof(this.settings.stream) =="undefined"){this.settings.stream={}} |
|
|
272 |
if(typeof(this.settings.stream.now) =="undefined"){this.settings.stream.now=0} |
|
|
273 |
if(typeof(this.settings.stream.provider)=="undefined"){this.settings.stream.provider='generator'} |
|
|
274 |
if(typeof(this.settings.stream.refresh)=="undefined"){this.settings.stream.refresh=1000} |
|
|
275 |
if(typeof(this.settings.data.tokenPast)=="undefined"){this.settings.data.tokenPast=0} |
|
|
276 |
if(typeof(this.settings.data.tokens)=="undefined"){this.settings.data.tokens=[]} |
|
|
277 |
|
|
|
278 |
// FOR ROM1 setting by default aggregation : |
|
|
279 |
if(typeof(this.settings.data.strata) !="undefined" && this.settings.data.strata.length!=0){ |
|
|
280 |
if(typeof(this.settings.sedimentation.aggregation) =="undefined"){ |
|
|
281 |
this.settings.sedimentation.aggregation = {} |
|
|
282 |
} |
|
|
283 |
if(typeof(this.settings.sedimentation.aggregation.height) =="undefined"){ |
|
|
284 |
this.settings.sedimentation.aggregation.height = this.settings.chart.height/2 |
|
|
285 |
} |
|
|
286 |
if(typeof(this.settings.sedimentation.aggregation.maxData) =="undefined"){ |
|
|
287 |
this.settings.sedimentation.aggregation.maxData = 10 |
|
|
288 |
} |
|
|
289 |
} |
|
|
290 |
// END |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
// Initialisation - Private method |
|
|
294 |
this.init = function(){ |
|
|
295 |
// requestAnim shim layer by Paul Irish |
|
|
296 |
// not use yet, to add |
|
|
297 |
this.requestAnimFrame = (function(){ |
|
|
298 |
return window.requestAnimationFrame || |
|
|
299 |
window.webkitRequestAnimationFrame || |
|
|
300 |
window.mozRequestAnimationFrame || |
|
|
301 |
window.oRequestAnimationFrame || |
|
|
302 |
window.msRequestAnimationFrame || |
|
|
303 |
function(/* function */ callback, /* DOMElement */ element){ |
|
|
304 |
window.setTimeout(callback, 1000 / 60); |
|
|
305 |
}; |
|
|
306 |
})(); |
|
|
307 |
|
|
|
308 |
//console.log(this.settings) |
|
|
309 |
//console.log('Initialisation'); |
|
|
310 |
|
|
|
311 |
// Create the physical simulation |
|
|
312 |
this.world = new this.phy.b2World( |
|
|
313 |
new this.phy.b2Vec2(0, 0) //gravity |
|
|
314 |
, true //allow sleep |
|
|
315 |
); |
|
|
316 |
|
|
|
317 |
// Create container and canvas for physical simulation drawing |
|
|
318 |
var container = element.appendChild(document.createElement("div")); |
|
|
319 |
container.id = "box_sediviz_"+GUID() |
|
|
320 |
container.width = this.settings.width; // TOFIX |
|
|
321 |
container.height = this.settings.height; |
|
|
322 |
|
|
|
323 |
//console.log(container.id) |
|
|
324 |
// Allocate the new Element |
|
|
325 |
this.settings.DOMelement = container |
|
|
326 |
|
|
|
327 |
canvas = container.appendChild(document.createElement("canvas")); |
|
|
328 |
canvas.id = "canvas"; |
|
|
329 |
canvas.width = this.settings.width; // TOFIX |
|
|
330 |
canvas.height = this.settings.height; |
|
|
331 |
canvas.style.position = "absolute" |
|
|
332 |
|
|
|
333 |
//console.log(this.settings.width,this.settings.height) |
|
|
334 |
this.ctx = canvas.getContext("2d"); |
|
|
335 |
|
|
|
336 |
// Construct the Chart |
|
|
337 |
this.chart[this.settings.chart.type](self,'init') |
|
|
338 |
|
|
|
339 |
|
|
|
340 |
// Draw d3 |
|
|
341 |
//if(typeof(this.settings.options.debugaggregate)=="undefined"){ |
|
|
342 |
// this.aggregate.init(self); |
|
|
343 |
//} |
|
|
344 |
// Initiatlise stream |
|
|
345 |
this.stream.init(self) |
|
|
346 |
// Initiatlise decay |
|
|
347 |
this.flocculate.init(self) |
|
|
348 |
// Update stream |
|
|
349 |
this.stream.update(self); |
|
|
350 |
|
|
|
351 |
// Initiatlise tokens |
|
|
352 |
this.token.init(self) |
|
|
353 |
|
|
|
354 |
//FORCE UPDATE CHART due to (bug positionnement ) @rom1 |
|
|
355 |
this.strata.init(this) |
|
|
356 |
|
|
|
357 |
// Update the physical simulation |
|
|
358 |
window.setInterval( |
|
|
359 |
function(){self.update(self);}, |
|
|
360 |
self.settings.options.refresh/2 |
|
|
361 |
); |
|
|
362 |
// Refresh canvas drawings |
|
|
363 |
window.setInterval( |
|
|
364 |
function(){self.draw.update(self);}, |
|
|
365 |
self.settings.options.refresh |
|
|
366 |
); |
|
|
367 |
// Update Decay |
|
|
368 |
window.setInterval( |
|
|
369 |
function(){self.decay.update(self);}, |
|
|
370 |
self.settings.sedimentation.suspension.refresh |
|
|
371 |
); |
|
|
372 |
//this.decay.update(self); |
|
|
373 |
|
|
|
374 |
self.strata.update(self) |
|
|
375 |
|
|
|
376 |
|
|
|
377 |
// MOUSE PART |
|
|
378 |
// inspired by box2d stuffs, have to clean and finish this ! |
|
|
379 |
// http://www.emanueleferonato.com/2008/11/20/dragging-objects-with-box2d-flash/ |
|
|
380 |
// -------------------------- |
|
|
381 |
this.getBodyAtMouse=function (_this) { |
|
|
382 |
|
|
|
383 |
var x = _this.mouse.x/_this.settings.options.scale |
|
|
384 |
var y =_this.mouse.y/_this.settings.options.scale |
|
|
385 |
var mousePVec = new _this.phy.b2Vec2(x,y); |
|
|
386 |
var aabb = new _this.phy.b2AABB(); |
|
|
387 |
var area = 0.001 |
|
|
388 |
|
|
|
389 |
//console.log(_this.mouse.x,_this.mouse.y) |
|
|
390 |
aabb.lowerBound.Set(x - area, y - area); |
|
|
391 |
aabb.upperBound.Set(x + area, y + area); |
|
|
392 |
|
|
|
393 |
// Query the world for overlapping shapes. |
|
|
394 |
_this.mouse.selectedToken = null; |
|
|
395 |
|
|
|
396 |
// MERCI JULIEN POUR LE CLOSURE |
|
|
397 |
//selectedBody |
|
|
398 |
_this.world.QueryAABB(function(fixture){ |
|
|
399 |
return getBodyCB(fixture,_this,mousePVec) |
|
|
400 |
}, aabb); |
|
|
401 |
|
|
|
402 |
return _this.mouse.selectedToken; |
|
|
403 |
} |
|
|
404 |
//http://stackoverflow.com/questions/11674200/how-to-send-prototype-method-as-a-callback-in-javascript |
|
|
405 |
// pb here |
|
|
406 |
function getBodyCB(fixture,_this,mousePVec) { |
|
|
407 |
//console.log("phy",phy) |
|
|
408 |
//console.log("fixture",fixture.m_userData.type,fixture) |
|
|
409 |
//_this.mouse.elementpoi = fixture.GetBody() |
|
|
410 |
_this.mouse.selectedToken = fixture; |
|
|
411 |
|
|
|
412 |
if(fixture.GetBody().GetType() != _this.phy.b2Body.b2_staticBody) { |
|
|
413 |
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), mousePVec)) { |
|
|
414 |
_this.mouse.selectedToken = fixture; |
|
|
415 |
return false; |
|
|
416 |
} |
|
|
417 |
} |
|
|
418 |
return true; |
|
|
419 |
} |
|
|
420 |
|
|
|
421 |
this.handleMouseMove = function(e,_this) { |
|
|
422 |
canvasPosition = DOMabsOffset(_this.settings.DOMelement) |
|
|
423 |
_this.mouse.x = (e.clientX - (canvasPosition.offsetLeft- this.getScrollPosition()[0])); |
|
|
424 |
_this.mouse.y = (e.clientY - (canvasPosition.offsetTop- this.getScrollPosition()[1])); |
|
|
425 |
//if( _this.mouse.isMouseDown){ console.log(_this.mouse.y,canvasPosition.y)} |
|
|
426 |
//console.log("mouse",e.clientX,e.clientY ) |
|
|
427 |
//console.log("mouse",canvasPosition.x,canvasPosition.y ) |
|
|
428 |
//console.log("=",_this.mouse.x,_this.mouse.y) |
|
|
429 |
}; |
|
|
430 |
// from |
|
|
431 |
this.getScrollPosition= function(){ |
|
|
432 |
return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop); |
|
|
433 |
} |
|
|
434 |
|
|
|
435 |
document.addEventListener("mousemove", function (e){onDocumentMouseMove(e,self)}); |
|
|
436 |
document.addEventListener("mouseup", function (e){onDocumentMouseUp(e,self)}); |
|
|
437 |
document.addEventListener("mousedown", function (e){onDocumentMouseDown(e,self)}); |
|
|
438 |
|
|
|
439 |
|
|
|
440 |
|
|
|
441 |
function onDocumentMouseOver(e,_this) { |
|
|
442 |
|
|
|
443 |
var s = _this.getBodyAtMouse(_this); |
|
|
444 |
if(s!=null){ |
|
|
445 |
if(typeof(s.m_userData)!="undefined"){ |
|
|
446 |
if(typeof(s.m_userData.callback)!="undefined"){ |
|
|
447 |
if(typeof(s.m_userData.callback.mouseover)=="function"){ |
|
|
448 |
var t = _this.select('ID',s.m_userData.ID) |
|
|
449 |
s.m_userData.callback.mouseover(t) |
|
|
450 |
} |
|
|
451 |
|
|
|
452 |
if(typeof(s.m_userData.callback.mouseout)=="function"){ |
|
|
453 |
//console.log("mouseout exist") |
|
|
454 |
var t = _this.select('ID',s.m_userData.ID) |
|
|
455 |
var mouseoutTrigger |
|
|
456 |
var rollOut = function(){ |
|
|
457 |
var mt = mouseoutTrigger |
|
|
458 |
var tt = t |
|
|
459 |
var ici = _this |
|
|
460 |
var ss = s |
|
|
461 |
return function(){ |
|
|
462 |
var s = ici.getBodyAtMouse(ici); |
|
|
463 |
var mo = false; |
|
|
464 |
if(s!=null){ |
|
|
465 |
if(typeof(s.m_userData)!="undefined"){ |
|
|
466 |
if(s.m_userData.ID==tt.attr('ID')){ |
|
|
467 |
mo=false |
|
|
468 |
}else{ |
|
|
469 |
mo=true |
|
|
470 |
} |
|
|
471 |
}else{ |
|
|
472 |
mo=true |
|
|
473 |
} |
|
|
474 |
}else{ |
|
|
475 |
mo=true; |
|
|
476 |
} |
|
|
477 |
if(mo){ |
|
|
478 |
ss.m_userData.callback.mouseout(tt) |
|
|
479 |
clearInterval(mouseoutTrigger) |
|
|
480 |
} |
|
|
481 |
} |
|
|
482 |
}; |
|
|
483 |
mouseoutTrigger = window.setInterval(rollOut(),100) |
|
|
484 |
} |
|
|
485 |
} |
|
|
486 |
} |
|
|
487 |
} |
|
|
488 |
} |
|
|
489 |
|
|
|
490 |
function onDocumentMouseDown(e,_this) { |
|
|
491 |
//console.log("onDocumentMouseDown") |
|
|
492 |
_this.mouse.isMouseDown = true; |
|
|
493 |
// return false; |
|
|
494 |
_this.handleMouseMove(e,_this); |
|
|
495 |
var s = _this.getBodyAtMouse(_this); |
|
|
496 |
if(s!=null){ |
|
|
497 |
if(typeof(s.m_userData)!="undefined"){ |
|
|
498 |
if(typeof(s.m_userData.callback)!="undefined"){ |
|
|
499 |
if(typeof(s.m_userData.callback.onclick)=="function"){ |
|
|
500 |
var t = _this.select('ID',s.m_userData.ID) |
|
|
501 |
s.m_userData.callback.onclick(t) |
|
|
502 |
} |
|
|
503 |
} |
|
|
504 |
} |
|
|
505 |
} |
|
|
506 |
} |
|
|
507 |
|
|
|
508 |
function onDocumentMouseUp(e,_this) { |
|
|
509 |
_this.mouse.isMouseDown = false; |
|
|
510 |
// isMouseDown = false; |
|
|
511 |
// return false; |
|
|
512 |
//console.log("onDocumentMouseUp") |
|
|
513 |
} |
|
|
514 |
function onDocumentMouseMove( e,_this ) { |
|
|
515 |
|
|
|
516 |
if(_this.mouse.isMouseDown){ |
|
|
517 |
_this.mouse.isMouseDragging = true; |
|
|
518 |
_this.mouse.x = e.clientX; |
|
|
519 |
_this.mouse.y = e.clientY; |
|
|
520 |
|
|
|
521 |
}else{ |
|
|
522 |
_this.handleMouseMove(e,_this); |
|
|
523 |
onDocumentMouseOver("move",_this) |
|
|
524 |
} |
|
|
525 |
//console.log("m",_this) |
|
|
526 |
} |
|
|
527 |
} |
|
|
528 |
|
|
|
529 |
|
|
|
530 |
this.mouse.update = function (s) { |
|
|
531 |
if(isMouseDown && (!mouseJoint)) { |
|
|
532 |
var body = getBodyAtMouse(); |
|
|
533 |
if(body) { |
|
|
534 |
var md = new b2MouseJointDef(); |
|
|
535 |
md.bodyA = world.GetGroundBody(); |
|
|
536 |
md.bodyB = body; |
|
|
537 |
md.target.Set(mouseX, mouseY); |
|
|
538 |
md.collideConnected = true; |
|
|
539 |
md.maxForce = 300.0 * body.GetMass(); |
|
|
540 |
mouseJoint = world.CreateJoint(md); |
|
|
541 |
body.SetAwake(true); |
|
|
542 |
} |
|
|
543 |
} |
|
|
544 |
|
|
|
545 |
if(mouseJoint) { |
|
|
546 |
if(isMouseDown) { |
|
|
547 |
mouseJoint.SetTarget(new b2Vec2(mouseX, mouseY)); |
|
|
548 |
} else { |
|
|
549 |
world.DestroyJoint(mouseJoint); |
|
|
550 |
mouseJoint = null; |
|
|
551 |
} |
|
|
552 |
} |
|
|
553 |
|
|
|
554 |
}; |
|
|
555 |
|
|
|
556 |
|
|
|
557 |
|
|
|
558 |
// MOUSE END |
|
|
559 |
// -------------------------- |
|
|
560 |
|
|
|
561 |
this.update = function (s) { |
|
|
562 |
this.world.Step(1 / 60, 10, 10); |
|
|
563 |
this.world.DrawDebugData(); |
|
|
564 |
this.world.ClearForces(); |
|
|
565 |
//console.log('u') |
|
|
566 |
} |
|
|
567 |
|
|
|
568 |
var drawInit = function(){ |
|
|
569 |
ctx.fillStyle = "rgb(200,0,0)"; |
|
|
570 |
this.ctx.font = "14pt Calibri,Geneva,Arial"; |
|
|
571 |
this.ctx.fillText("Canvas ready for Visual Sedimentation ", 10, 20); |
|
|
572 |
window.setInterval( |
|
|
573 |
$.fn.vs.draw.refresh(ctx,world,this.settings) |
|
|
574 |
, this.settings.options.refresh); |
|
|
575 |
console.log("draw Init ") |
|
|
576 |
} |
|
|
577 |
|
|
|
578 |
|
|
|
579 |
var DOMabsOffset = function(target){ |
|
|
580 |
var top = target.offsetTop; |
|
|
581 |
var left = target.offsetLeft; |
|
|
582 |
|
|
|
583 |
while(target = target.offsetParent) { |
|
|
584 |
top += target.offsetTop; |
|
|
585 |
left += target.offsetLeft; |
|
|
586 |
} |
|
|
587 |
|
|
|
588 |
return {offsetLeft:left, offsetTop:top}; |
|
|
589 |
}; |
|
|
590 |
|
|
|
591 |
// GUID generator from : |
|
|
592 |
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript |
|
|
593 |
var GUID = function(){ |
|
|
594 |
var S4 = function () |
|
|
595 |
{ |
|
|
596 |
return Math.floor( |
|
|
597 |
Math.random() * 0x10000 /* 65536 */ |
|
|
598 |
).toString(16); |
|
|
599 |
}; |
|
|
600 |
|
|
|
601 |
return ( |
|
|
602 |
S4() + S4() + "-" + |
|
|
603 |
S4() + "-" + |
|
|
604 |
S4() + "-" + |
|
|
605 |
S4() + "-" + |
|
|
606 |
S4() + S4() + S4() |
|
|
607 |
); |
|
|
608 |
} |
|
|
609 |
|
|
|
610 |
// clone object |
|
|
611 |
// http://stackoverflow.com/questions/728360/copying-an-object-in-javascript |
|
|
612 |
function clone(obj) { |
|
|
613 |
if (null == obj || "object" != typeof obj) return obj; |
|
|
614 |
var copy = obj.constructor(); |
|
|
615 |
for (var attr in obj) { |
|
|
616 |
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; |
|
|
617 |
} |
|
|
618 |
return copy; |
|
|
619 |
} |
|
|
620 |
this.utile = {} |
|
|
621 |
this.utile.GUID = GUID |
|
|
622 |
this.utile.clone = clone |
|
|
623 |
|
|
|
624 |
this.settings = $.extend(this.settings, {} || {}); |
|
|
625 |
//console.log("ici",this.settings) |
|
|
626 |
this.init(); |
|
|
627 |
|
|
|
628 |
}; |
|
|
629 |
|
|
|
630 |
$.fn.vs = function(options){ |
|
|
631 |
if (!arguments.length){var options={}} |
|
|
632 |
//console.log('$.fn.vs settings',options) |
|
|
633 |
return this.each(function(){ |
|
|
634 |
var element = $(this); |
|
|
635 |
// Return early if this element already has a plugin instance |
|
|
636 |
if (element.data('VisualSedimentation')) return; |
|
|
637 |
var visualSedimentation = new VisualSedimentation(this,options); |
|
|
638 |
// Store plugin object in this element's data |
|
|
639 |
element.data('visualSedimentation', visualSedimentation); |
|
|
640 |
//visualSedimentation.test(); |
|
|
641 |
}); |
|
|
642 |
}; |
|
|
643 |
|
|
|
644 |
})(jQuery); |
|
|
645 |
|
|
|
646 |
|
|
|
647 |
|
|
|
648 |
|
|
|
649 |
|