canvas - KineticJS - Multiple transforms applied serially -


i'm using kineticjs allow users manipulate images -- crop, rotate, scale, flip, etc.

when apply transform image, works great. when apply second transform acts upon original, untransformed image, not new one.

here's simplified jsfiddle: http://jsfiddle.net/sdwpj/ click scale button twice - should keep increasing size. click rotate button twice, should keep image onscreen.

i know why happening. it's because transforms aren't changing actual image, they're changing way see image. want change actual image. maybe can save image after each transform or flatten stage?

how can multiple transforms serially?

here's code fiddle:

html:

<button id="scale">scale</button> <button id="rotate">rotate photo</button> <div id="container"></div> 

javascript:

var photopath = "http://www.html5canvastutorials.com/demos/assets/yoda.jpg"; var stage; var photo; stage = new kinetic.stage({     container: 'container',     width: 0,     height: 0 }); var layer = new kinetic.layer({     id: "imagelayer" }); var originalimage = new image(); originalimage.src = photopath; originalimage.onload = function () {     photo = new kinetic.image({         id: "image",         image: originalimage     });     stage.setheight(originalimage.height);     stage.setwidth(originalimage.width);     layer.add(photo);     stage.add(layer); }; var scale = function () {     var image = stage.get('#image')[0];     var scale = 1.5;     image.setscale(scale, scale);     stage.draw(); }; var rotate = function () {     var angle = 90;     var image = stage.get('#image')[0];     image.rotatedeg(angle);     image.setx(image.getheight());     stage.draw(); }; document.getelementbyid('scale').addeventlistener('click', scale, false); document.getelementbyid('rotate').addeventlistener('click', rotate, false); 

[edited: simpler answer! ]

you can tranform kinetic element relative it’s current position (instead of beginning position)

here's how:

// translate current position rect.move(10,0);  layer.draw();  // rotate current angle rect.rotate(20*math.pi/180);  layer.draw();   // scale current size rect.setscalex(rect.getscalex()*1.1); rect.setscaley(rect.getscaley()*1.1); layer.draw(); 

here’s code , fiddle: http://jsfiddle.net/m1erickson/z6yg8/

<!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.1.min.js"></script>  <style> #container{   border:solid 1px #ccc;   margin-top: 10px;   width:300px; } </style>         <script> $(function(){      var stage = new kinetic.stage({         container: 'container',         width: 300,         height: 300     });     var layer = new kinetic.layer();     stage.add(layer);       var rect = new kinetic.rect({       name:"rect1",       x: 80,       y: 80,       width: 50,       height: 20,       offset:[50/2,20/2],       fill: "lightgray",       stroke: "skyblue",       strokewidth: 3     });     layer.add(rect);     stage.draw();       $("#translate").click(function(){          rect.move(10,0);          layer.draw();     });      $("#rotate").click(function(){          rect.rotate(20*math.pi/180);          layer.draw();      });      $("#scale").click(function(){         rect.setscalex(rect.getscalex()*1.1);         rect.setscaley(rect.getscaley()*1.1);         layer.draw();      });  }); // end $(function(){});  </script>        </head>  <body>     <div id="container"></div>     <button id="translate">translatex +25 </button>     <button id="rotate">rotate +5degrees</button>     <button id="scale">scale *1.10</button> </body> </html> 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -