Scaling SVG (Raphael.js) like an SWF

后端 未结 5 777
[愿得一人]
[愿得一人] 2020-12-12 16:43

I started using Raphael.js a few days ago and I\'m really enjoying it. The only thing I haven\'t been able to figure out is how to get the \"paper\" or svg/vml tag to fill t

5条回答
  •  隐瞒了意图╮
    2020-12-12 17:31

    Well hello Zévan,

    I found a nice answer, made by a guy called Zevan. However, I found the code overcomplicated for my liking (Required jquery, did wierd stuff like "$(this)" etc).

    So I simplified it. It's now short enough to fit into stackoverflow ;):

    var paper;
    
    window.ScaleRaphael = function(container, width, height) {
        var wrapper = document.getElementById(container);
        wrapper.style.width = width + "px";
        wrapper.style.height = height + "px";
        wrapper.style.overflow = "hidden";
    
        wrapper.innerHTML = "
    <\/div>"; var nestedWrapper = document.getElementById("svggroup"); paper = new Raphael(nestedWrapper, width, height); paper.w = width; paper.h = height; paper.canvas.setAttribute("viewBox", "0 0 "+width+" "+height); paper.changeSize = function() { var w = window.innerWidth var h = window.innerHeight var ratioW = w / width; var ratioH = h / height; var scale = ratioW < ratioH ? ratioW : ratioH; var newHeight = Math.floor(height * scale); var newWidth = Math.floor(width * scale); wrapper.style.width = newWidth + "px"; wrapper.style.height = newHeight + "px"; paper.setSize(newWidth, newHeight); } window.onresize = function() { paper.changeSize(); } paper.changeSize(); return paper; }

    The only drawback from your version is that it requires SVG, it doesn't do VML. Is this a problem?

    I'm using it with a simplified version of your demo page:

     
     
    
    ScaleRaphaël Demo 1
     
    
    
    
    
    
    
    
    
    

提交回复
热议问题