javascript - svg.js strange interaction of scale and move/center -


i'm not familiar working svgs in js here strange. i'm having arrow , path supposed fill arrow extend. looks this: image 1 aim able scale white part should still stay inside arrowimage 2. weird thing cannot figure out how move white part right place. i've tried different attempts. here current code (it works scalefactor 1 not other):

var draw = svg('arrow').size(500, 500);     var arrowpath=draw.polyline('10,243.495 202.918,15.482 397.199,245.107').fill('none').stroke({ width: 20 });      var arrow=draw.group();     arrow.add(arrowpath.clone());      var scalefactor=0.5;      var fillarrow=draw.path('m357.669,198.387c-41.747,35.357-95.759,56.678-154.751,56.678c-58.991,0-113.003-21.32-154.75-56.676l154.75-182.907  l357.669,198.387z');     fillarrow.fill('#ffffee');     var movex=(arrowpath.width()/2-fillarrow.width()/2)/scalefactor+9.5;     console.log(movex);     fillarrow.center(arrowpath.cx(), arrowpath.cy()).scale(scalefactor); //another attempt fillarrow.move(movex,0); 

when scaling, rotating , translating in svg doing coordinate transforms. is, not changing object drawing coordinate system drawing object in. think of pixel has size on screen, , if svgobject.scale(0.5) pixel becomes half size.

so if draw square path('m10,10 l20,10 l20,20 l10,20 z') , apply scale(0.5) have drawn path looks path('m5,5 l10,5 l10,10 l5,10 z')

this might sound strange @ first but, alot of geometrical calculations becomes simpler when can this.

you want scale around tip of arrow (make sure not move). should place point in origo (0,0) , draw object around point. in group. because can translate group coordinate system correct position.

var draw = svg('arrow').size(600, 600);  // create group var arrow = draw.group();  // draw arrow path in group // have placed "tip" of arrow in (0,0) var arrowpath = arrow.polyline('-250,250 0,0 250,250').fill('none').stroke({     width: 20 });  // draw fill arrow in group. again, point  // scale around placed @ (0,0) var fillarrow = arrow.path('m0,0l150,150,-150,150z').fill('#ffffee');  // move group position display in svg arrow.move(260, 20);  var scalefactor = 0.5 fillarrow.scale(scalefactor); 

and here working example can test , change scale factor. http://jsfiddle.net/zmgqk/1/

and here explanation on how translate, rotate , scale works. http://commons.oreilly.com/wiki/index.php/svg_essentials/transforming_the_coordinate_system


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -