javascript - Keeping shape and animation of ball consistance over period of time -


i new two.js. trying basic experiments rubber ball example reposition ball on every second per random input instead of mouse movement.

so, have written below code, removing rubber ball effect after iteration. don't know going wrong.

second problem, after iteration, rubber ball changing shape circle oval kind of shape.

jsfiddle: http://jsfiddle.net/2v93n/ tried many times, not working live jsfiddle.

<body> <script>       var 2 = new two({         fullscreen: true,         autostart: true       }).appendto(document.body);        two.resoultion = 32;        var delta = new two.vector();       var mouse = new two.vector();       var drag = 0.1;       var radius = 25;       var shadow = two.makecircle(two.width / 2, two.height / 2, radius);       var ball = two.makecircle(two.width / 2, two.height / 2, radius);       ball.nostroke().fill = 'green';shadow.nostroke().fill = 'rgba(0, 0, 0, 0.2)';          shadow.scale = 0.85;      function moverubberball() {        shadow.offset = new two.vector(- radius / 2, radius * 2);        _.each(ball.vertices, function(v) {         v.origin = new two.vector().copy(v);       });        mouse.x = math.floor((math.random() * 1000) + 1);       mouse.y = math.floor((math.random() * 600) + 1);       shadow.offset.x = 5 * radius * (mouse.x - two.width / 2) / two.width;       shadow.offset.y = 5 * radius * (mouse.y - two.height / 2) / two.height;        two.bind('update', function() {          delta.copy(mouse).subself(ball.translation);          _.each(ball.vertices, function(v, i) {            var dist = v.origin.distanceto(delta);           var pct = dist / radius;            var x = delta.x * pct;           var y = delta.y * pct;            var destx = v.origin.x - x;           var desty = v.origin.y - y;            v.x += (destx - v.x) * drag;           v.y += (desty - v.y) * drag;            shadow.vertices[i].copy(v);          });         ball.translation.addself(delta);         shadow.translation.copy(ball.translation);         shadow.translation.addself(shadow.offset);        });   }   var auto_refresh = setinterval("moverubberball()", 1000); </script> </body> 

please somebody.

the main issue two.bind('update'...) within moverubberball(). when bind update means function called on requestanimationframe, 60fps. when event bound every time moverubberball called — once second — adds another callback. after 5 seconds there 5 updates called 60fps. updated fiddle import two.js correctly , fix error outlined:

http://jsfiddle.net/2v93n/1/


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -