🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #669

Re: ¿Como redimensionar (encoger) una imagen SVG en html?

#669
From: MD
Date: Thu, 05 Oct 2006 21:45
194 lines
7492 bytes
Bueno ya encontre algo ^_^.

Es un poco cochinete la solucion porque hay que tirar de javascript 
desde html y en el fichero SVG hay que meterle un peque�o script. Osea 
que no vale para cualquier fichero SVG encuentres por la red, porque 
primero tienes que destriparlo para meterle el script.

No se si se podra encontrar un metodo generico que valga para cualquier 
SVG sin destriparlo. Pero bueno este puede ser util para gente que tenga 
acceso a los SVG para meterles mano.

Bueno aqui teneis la informacion: 
http://digg.com/design/Scaling_SVG_Graphics


Pues eso un parche que funciona (en firefox he probado de momento). Y 
aplicarlo (teniendo acceso al codigo fuente de los ficheros SVG...porque 
sino estas vendido) es facil, se me ocurren miles de formas para 
aplicarlo por ejemplo que la imagen ocupe toda la anchura del navegador 
y cuando se redimensione se encoja (bueno mas o menos eso era mi 
objetivo inicial :P y creo que lo cumple).

Y como nose si se pueden anexar ficheros en la lista, pego un peque�o 
ejemplo de prueba:

------------------------HTML-----------------------------------------
<html>
	<head><title>svg scaling with javascript</title>
	<script type="text/javascript">
		var W3CDOM = (document.createElement && document.getElementsByTagName);
		window.onload 	= init;
		function init(evt) {
			SVGscale(0.5);
		}
		function SVGscale(scale) {
			window.SVGsetDimension(500*scale,500*scale);
			window.SVGsetScale(scale,scale);	
			        if (!W3CDOM) return;
			var box 	= document.getElementById('svgid');
			box.width  	= 500*scale;
			box.height 	= 500*scale;
		}
	</script>
</head>
<body bgcolor="#ffddff">
   <object id="svgid" data="prueba.svg" type="image/svg+xml" 
wmode="transparent" style="overflow: hidden;">
     <param name="src" value="prueba.svg" wmode="transparent" 
type="image/svg+xml">
   </object>
Choose here:
	<a href="#" onclick="SVGscale(0.1);">xsmall</a> or
	<a href="#" onclick="SVGscale(0.25);">small</a> or
	<a href="#" onclick="SVGscale(0.5);">medium</a> or
	<a href="#" onclick="SVGscale(1);">1:1</a> or
	<a href="#" onclick="SVGscale(1.5);">large</a> or ...
<h3>description</h3>
	<a href="http://www.w3.org/TR/SVG/">here more about SVG</a>:
this doc was my only source for this demonstration. especially the part
about scripting is very interesting. this example shows how to rescale
a svg more or less dynamically. there are also methods for scaling
(rotation, transformation) svg dynamically and as an animation. this
doc does the following:
<ol>
	<li>SVG doc is loaded embedded in the HTML, where an init function in 
the SVG file's ECMA-script-part ...</li>
	<li>...
registers functions in the top.* space. therefore, a script in this
HTML file can call those HTML2svg functions which execute something in
the SVG graphic. therefore you have to open the SVG file in a 
texteditor</li>
	<li>the init function of the HTML file resizes the svg to its initial 
dimension</li>
</ol>

<h3>relevant code</h3>
<ul>
<li><h4>SVG, in and right after the header ...</h4></li>
<pre>onload="RunScript(evt)"
...
...
&lt;script type="text/ecmascript"&gt;
&lt;![CDATA[
var g_element;
var SVGDoc;
var SVGRoot;
function RunScript(LoadEvent) {
	top.SVGsetDimension	= setDimension;
	top.SVGsetScale		= setScale;
	SVGDoc			= LoadEvent.target.ownerDocument;
	g_element		= SVGDoc.getElementById("layer1");
}

function setDimension(w,h) {
	SVGDoc.documentElement.setAttribute("width", w);
	SVGDoc.documentElement.setAttribute("height", h);
}

function setScale(sw, sh) {
	g_element.setAttribute("transform", "scale(" + sw + " " + sh +")");
}
]]&gt;
&lt;/script&gt;
</pre>
<li><h4>HTML</h4></li>
<pre>&lt;script type="text/javascript"&gt;
var W3CDOM = (document.createElement &amp;&amp; 
document.getElementsByTagName);
window.onload 	= init;
function init(evt) {
	SVGscale(0.5);
}
function SVGscale(scale) {
	window.SVGsetDimension(640*scale,480*scale);
	window.SVGsetScale(scale,scale);	
         if (!W3CDOM) return;
	var box 	= document.getElementById('svgid');
	box.width  	= 640*scale;
	box.height 	= 480*scale;
}
&lt;/script&gt;
</pre>
</ul>
</body>
---------------------------------------------------------------------


-------------------PRUEBA.SVG----------------------------------------
var SVGDoc;
var SVGRoot;
//var HTMLSVG;
function RunScript(LoadEvent) {
	top.SVGsetDimension	= setDimension;
	top.SVGsetScale		= setScale;
	SVGDoc			= LoadEvent.target.ownerDocument;
	//HTMLSVG			= top.document.getElementById("svgid");
	g_element		= SVGDoc.getElementById("layer1");
}

function setDimension(w,h) {
	SVGDoc.documentElement.setAttribute("width", w);
	SVGDoc.documentElement.setAttribute("height", h);
}

function setScale(sw, sh) {
	g_element.setAttribute("transform", "scale(" + sw + " " + sh +")");
}
]]>
    </script>
   <defs
      id="defs4" />
   <g
      id="layer1"
      transform="translate(0,0)"><!--Importante el Inkscape tiene un bug 
(uhmm hay que reportar) y mueve la capa aleatoriamente//-->
     <rect
        width="50"
        height="500"
        x="0"
        y="0"
 
style="fill:blue;fill-rule:evenodd;stroke:black;stroke-width:0.65028489px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
        id="rect1876" />
     <rect
        width="500"
        height="50"
        x="0"
        y="450"
 
style="fill:blue;fill-rule:evenodd;stroke:black;stroke-width:0.65028489px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
        id="rect1876" />
     <rect
        width="500"
        height="50"
        x="0"
        y="0"
 
style="fill:red;fill-rule:evenodd;stroke:black;stroke-width:0.65028489px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
        id="rect1876" />
     <rect
        width="50"
        height="500"
        x="450"
        y="0"
 
style="fill:red;fill-rule:evenodd;stroke:black;stroke-width:0.65028489px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
        id="rect1876" />
   </g>
</svg>
---------------------------------------------------------------------

P.D. Ahora el chiste para mejorar el negocio seria encontrar uno 
generico para no tener que toquetear un SVG y redimensionar cualquier 
SVG que enlaces. Pero este es de momento es util, es sin duda.
Technical Details
Message-ID: <C1fVg.23569$MA3.1461@news.ono.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newspeer1.de.telia.net!newspeer4.de.telia.net!de.telia.net!newsf01.ono.com!news.ono.com.POSTED!53ab2750!not-for-mail
References: <nljUg.23297$MA3.18995@news.ono.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f86c8,9dbe823aa1fd37a4
X-Google-Attributes: gidf86c8,public
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newspeer1.de.telia.net!newspeer4.de.telia.net!de.telia.net!newsf01.ono.com!news.ono.com.POSTED!53ab2750!not-for-mail
From: MD <mdcid@yahoo.es>
User-Agent: Thunderbird 1.5.0.4 (Windows/20060516)
MIME-Version: 1.0
Newsgroups: es.comp.os.linux.programacion
Subject: Re: ¿Como redimensionar (encoger) una imagen SVG en html?
References: <nljUg.23297$MA3.18995@news.ono.com>
In-Reply-To: <nljUg.23297$MA3.18995@news.ono.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 194
Message-ID: <C1fVg.23569$MA3.1461@news.ono.com>
Date: Thu, 05 Oct 2006 23:45:08 +0200
NNTP-Posting-Host: 81.172.86.167
X-Trace: news.ono.com 1160084706 81.172.86.167 (Thu, 05 Oct 2006 23:45:06 MEST)
NNTP-Posting-Date: Thu, 05 Oct 2006 23:45:06 MEST