javascript - KineticJS: Blob scaling -


i ask if it's possible scale - tween on height/width of blob object in canvas? trick redefine each points according initial position of each points? desired effect see blob expand/retract on factor value (ie. 10%). thank you.

code:

for(var i=0 ; i<shape_childs.length ; i++){ var child = shape_childs[i];  var height=child.getheight();  child.blobtween = new kinetic.tween({      node: child,      duration: 1,      height:height+(height*0.20),      easing: kinetic.easings.linear  });  } 

here fiddle :) jsfiddle.net/3vckj/1

as @ani says, welcome stackoverflow. it's traditional show code can assist you.

the blob object, objects in kineticjs, can scaled using myshape.setscale.

in case, can set desired scaling factor inside tween:

  var tween = new kinetic.tween({     node: blob,      duration: 1,     scalex: 1.1,     scaley: 1.1   }); 

[ changes given additional comments ]

it looks want blob remain in original x/y while scaling.

to that, you'll want tween x/y of blob while scaling:

    var tweenup = new kinetic.tween({       node: blob,        duration: 1,       scalex: 1.10,       scaley: 1.10,       x:blob.getx()-(blob.getwidth()*.10),       y:blob.gety()-(blob.getheight()*.10)     }); 

unfortunately, kineticjs blob.getwidth , blob.getheight not return value (just 0).

so need estimate width , height of blob.

you can try refine calculation accounting tension (not shown here).

    var blobpoints=[{x: x_r-60, y: y_r}, {x: x_r, y: y_r+35}, {x: x_r+60, y: y_r}, {x: x_r, y: y_r-55}]     var minx=1000000;     var miny=1000000;     var maxx=-100;     var maxy=-100;     for(var i=0;i<blobpoints.length;i++){         var pt=blobpoints[i];         console.log(pt.x+"/"+pt.y);         if(pt.x<minx){minx=pt.x;}         if(pt.x>maxx){maxx=pt.x;}         if(pt.y<miny){miny=pt.y;}         if(pt.y>maxy){maxy=pt.y;}     } 

and set blob width , height properties our calculated values:

    blob.setwidth(maxx-minx);     blob.setheight(maxy-miny); 

now tween should scale blob 1.10 while remaining in relatively same x/y.

here code , fiddle: http://jsfiddle.net/m1erickson/xwvsp/

<!doctype html> <html>   <head>     <meta charset="utf-8">     <title>prototype</title>     <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>     <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>  <style> #container{   border:solid 1px #ccc;   margin-top: 10px;   width:400px;   height:400px; } </style>         <script> $(function(){      var stage = new kinetic.stage({         container: 'container',         width: 400,         height: 400     });     var layer = new kinetic.layer();     stage.add(layer);      var sc_height = 300;     var sc_width = 300;      var x_r=(sc_width/2)-(40/2);     var y_r=(sc_height/2)-(40/2);      var blobpoints=[{x: x_r-60, y: y_r}, {x: x_r, y: y_r+35}, {x: x_r+60, y: y_r}, {x: x_r, y: y_r-55}]     var minx=1000000;     var miny=1000000;     var maxx=-100;     var maxy=-100;     for(var i=0;i<blobpoints.length;i++){         var pt=blobpoints[i];         console.log(pt.x+"/"+pt.y);         if(pt.x<minx){minx=pt.x;}         if(pt.x>maxx){maxx=pt.x;}         if(pt.y<miny){miny=pt.y;}         if(pt.y>maxy){maxy=pt.y;}     }      var blob = new kinetic.blob({       points: blobpoints,       stroke: 'red',       strokewidth: 10,       fill: '#faa',       tension: 1.2,       scale: 1,       x: 20,       y: 50     });     blob.setwidth(maxx-minx);     blob.setheight(maxy-miny);     layer.add(blob);     layer.draw();      var tweenup = new kinetic.tween({       node: blob,        duration: 1,       scalex: 1.10,       scaley: 1.10,       x:blob.getx()-(blob.getwidth()*.10),       y:blob.gety()-(blob.getheight()*.10)     });      $("#scaleup").click(function(){ tweenup.play(); });     $("#scaledown").click(function(){ tweenup.reverse(); });  }); // end $(function(){});  </script>        </head>  <body>     <div id="container"></div>     <button id="scaleup">tween scale up</button>     <button id="scaledown">tween scale down</button> </body> </html> 

Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

How to mention the localhost in android -

php - Calling a template part from a post -