raphael js, resize canvas then setViewBox to show all elements

余生颓废 提交于 2019-12-13 17:31:42

问题


I have a problem with my canvas.

My canvas in first width = 1300, height = 500

Then I resize it to width = 800px, height = 500

I try setViewBox to zoom it. But mouse not fix with element when I drag them.

@canvas.resize(800, 500)
@canvas.setViewBox(0,0, ??, ??)

how to calculate it???

Thank for your help. :)


回答1:


You can calculate the necessary dimensions using an approach like this:

function recalculateViewBox( canvas )
{
    var max_x = 0, max_y = 0;
    canvas.forEach( function( el )
        {
            var box = el.getBBox();
            max_x = Math.max( max_x, box.x2 );
            max_y = Math.max( max_y, box.y2 );
        } );
    if ( max_x && max_y )
        canvas.setViewBox( 0, 0, max_x, max_y );
}

Essentially, you simply walk the contents of the canvas and construct a meta-bounding box, then adjust the viewBox to it.

If you wanted to get a little fancy, you could always animate the viewbox so that it transitions fluidly to it's new size. Not functionally important, but moderately sexy...



来源:https://stackoverflow.com/questions/14054595/raphael-js-resize-canvas-then-setviewbox-to-show-all-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!