Equivalent SVG transform for new coordinate system - javascript

SVG is embedded in an object tag which has some dimensions (w1,h1). The user can zoom and pan to an arbitrary region in the SVG and wants to retain that view.
I have the transform of the SVG with old dimensions. How to compute the transformation for the SVG to retain the previous view for the new dimensions(w2, h2)?
Here is an example HTML, the SVG is embedded in the object tag
In this example, (w1,h1) are (100%,100%) and (w2,h2) can be (50% ,50%)
<html>
<body>
<object class="auto-capital-svg-content" data="1.svg" width="100%" height="100%" type="image/svg+xml">
</object>
</body>
</html>
Here is a sample SVG
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" height="150" width="400" style="" >
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"></stop>
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"></stop>
</linearGradient>
</defs>
<g id="viewport" transform="matrix(6,0,0,6,200,200)">
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)"></ellipse>
<text fill="#ffffff" font-size="45" font-family="Verdana" x="150" y="86">SVG</text>
</g>
</svg>
This is what I thought : calculate the scale based on the new/old dimensions such that aspect ratio is preserved and scale the transform. But this did not take care of the translation (tx,ty) in the matrix {a,b,c,d,tx,ty}.What should be done here?

Related

How to add text inside an SVG element

I would like to add text inside an element such that my intro is behind a rectangular, transparent element.
I tried adding text inside an SVG like this
<svg height="150" width="500">
<text x="100" y="30" fill="red">I love SVG!</text>
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
However I can't Send the object backwards. Is this even possible?
Is there a better approach I can use to meet this objective rather than the solution I'm trying to use?
Are you just talking about whether the text is behind the oval, or on top of it?
If so, just changing the order of elements fixes that:
<svg height="150" width="500">
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
<text x="100" y="30" fill="red">I love SVG!</text>
</svg>
Or is there something else you're trying to do? (Please note, even with the text on top of the oval, the contrast isn't that great, and it's still hard to read.)

How to fill gradient svg with solid color on hover

Is it possible to do that with just css? I've got my svg with predefined gradient that I would like to fill with a color on hover in css. What I tried was embedding the svg tag in html structure and then adding a class to path that I would then style in css with fill property. This didn't work though, how should I approach this issue?
SVG tag for reference:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<defs>
<style>
.be51c222-80b4-489f-b2dd-486e23c9eef8{fill:url(#a89527f1-b302-44b4-9a05-0e6cac241929);}
</style>
<linearGradient id="a89527f1-b302-44b4-9a05-0e6cac241929" x1="12.05" x2="11.97" y1="23.94" y2="4.41" gradientUnits="userSpaceOnUse">
<stop offset="0" stop-color="#c41230"/>
<stop offset="1" stop-color="#f62028"/>
</linearGradient>
</defs>
<title>
slide-down
</title>
<g id="a50b190a-9952-4444-9199-5e8e5ee16a51" data-name="Warstwa 2">
<g id="bf15b3ba-4a89-4930-a912-ff1c5832ba6d" data-name="Warstwa 1">
<path d="M24,12A12,12,0,1,0,12,24,12,12,0,0,0,24,12ZM7,13h4V5h2v8h4l-5,6Z" class="be51c222-80b4-489f-b2dd-486e23c9eef8 icon-slide"/>
</g>
</g>
</svg>
Just use the :hover pseudo selector.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<defs>
<style>
.be51c222-80b4-489f-b2dd-486e23c9eef8{fill:url(#a89527f1-b302-44b4-9a05-0e6cac241929);}
.be51c222-80b4-489f-b2dd-486e23c9eef8:hover{fill:blue;}
</style>
<linearGradient id="a89527f1-b302-44b4-9a05-0e6cac241929" x1="12.05" x2="11.97" y1="23.94" y2="4.41" gradientUnits="userSpaceOnUse">
<stop offset="0" stop-color="#c41230"/>
<stop offset="1" stop-color="#f62028"/>
</linearGradient>
</defs>
<title>
slide-down
</title>
<g id="a50b190a-9952-4444-9199-5e8e5ee16a51" data-name="Warstwa 2">
<g id="bf15b3ba-4a89-4930-a912-ff1c5832ba6d" data-name="Warstwa 1">
<path d="M24,12A12,12,0,1,0,12,24,12,12,0,0,0,24,12ZM7,13h4V5h2v8h4l-5,6Z" class="be51c222-80b4-489f-b2dd-486e23c9eef8 icon-slide"/>
</g>
</g>
</svg>

directed gradient in svg's path element

I'm writing a simple webpage that displays a graph and shows dependencies. I found an unexpected behavior in how path elements are rendered within svg.
Here's the full HTML of the example:
<html>
<body>
<svg id="svgConnections" xmlns="http://www.w3.org/2000/svg" style="width: 300px; height: 120px">
<defs>
<linearGradient id="grad1" >
<stop offset="0%" style="stop-color:yellow;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<path d="M40,40 L100,100 Z" stroke="url(#grad1)" strokeWidth="1px" />
<path d="M200,100 L140,40 Z" stroke="url(#grad1)" strokeWidth="1px" />
</svg>
</body>
</html>
The same example is on https://jsfiddle.net/4fLjm0e2/
What bugs me is that the first line, which goes from top left to bottom right corner, looks exactly like the second line, which goes "in reverse": from bottom right corner to the top left.
How do I make the path always start with yellow and end with red?
This is not a bug. This is problem in understanding.
The default behavior of a linear gradient is to transition along a horizontal line from the left side of an object to its right side. It doesn't matter if you draw a path from left to right or from right to left. In both cases gradient will appear as from left to right as per default settings.
Consider the demo below:
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" gradientUnits="userSpaceOnUse">
<stop offset="0%" style="stop-color:yellow;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<g stroke-width="2">
<path d="M10,40 L110,40 Z" stroke="url(#grad1)" />
<path d="M110,70 L10,70 Z" stroke="url(#grad1)" />
</g>
</svg>
If you wants the transition of colors to occur across a vertical line or a line at an angle, you must specify the line's starting point with the x1 and
y1 attributes and its ending points with the x2 and y2 attributes.
Rather than duplicate the stops into each <linearGradient> element, we'll use the xlink:href attribute to refer to the original gradient. The stops will be inherited, but the x- and y-coordinates will be overridden by each individual gradient.
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="grad2" xlink:href="#grad1" x1="1" y1="1" x2="0" y2="0"></linearGradient>
Extending the above example:
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" gradientUnits="userSpaceOnUse">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="grad2" xlink:href="#grad1" x1="120" y1="0" x2="0" y2="0"></linearGradient>
</defs>
<g stroke-width="2">
<path d="M10,40 L110,40" stroke="url(#grad1)" />
<path d="M110,70 L10,70 Z" stroke="url(#grad2)" />
</g>
</svg>
As in your example, you are using diagonal paths so we need to override x1, y1, x2 and y2 attribute of both <linearGradient> elements.
These values on the first <linearGradient> element will override the default left to right settings to produce a diagonal gradient from top left to the right bottom.
While on the <linearGradient> element these values will change the direction of gradient i.e from bottom to top.
Now we can apply these gradients to the respective paths.
<svg width="300" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="grad2" xlink:href="#grad1" x1="1" y1="1" x2="0" y2="0"></linearGradient>
</defs>
<g stroke-width="2">
<path d="M40,40 L100,100 Z" stroke="url(#grad1)" />
<path d="M200,100 L140,40 Z" stroke="url(#grad2)" />
</g>
</svg>
Note: This Question can be useful in context with the current problem.

Changing SVG image attributes by button click using javascript/jquery

I have created one svg door image using Adobe illustrator.The image has 3 components
Door body
Glass part in center of the door
Door handle. >>
I want change the this 3 attributes by clicking buttons like changing handle, color, body material etc.
I have open this svg file in dreamweaver and it's looking weird with long image links and codes.
I don't know where to start with.
Thanks
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="612px" height="792px" viewBox="0 0 612 792" enable-background="new 0 0 612 792" xml:space="preserve">
<rect x="146.889" y="134.444" fill="#6B4520" stroke="#000000" stroke- miterlimit="10" width="328.889" height="487.777"/>
<path fill="#BDE5F1" stroke="#000000" stroke-miterlimit="10" d="M372.998,351.667c0,38.2-25.577,67-66.998,67
c-41.421,0-71.002-28.8-71.002-67c0-38.2,27.579-64.667,69- 64.667C345.419,287,372.998,313.467,372.998,351.667z"/>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="155.665" y1="352.25" x2="174.25" y2="352.25">
<stop offset="0" style="stop-color:#FFFFFF"/>
<stop offset="0" style="stop-color:#603913"/>
<stop offset="0" style="stop-color:#000000"/>
<stop offset="0.0161" style="stop-color:#000000"/>
<stop offset="0.3333" style="stop-color:#D9CDC0"/>
<stop offset="0.5161" style="stop-color:#000000"/>
<stop offset="0.8763" style="stop-color:#FFFFFF"/>
</linearGradient>
<polygon fill="url(#SVGID_1_)" stroke="#000000" stroke-miterlimit="10" points="171.333,430.556 164.958,434 159.111,430.556
155.665,344.667 159.111,272.778 164.958,270.5 171.333,272.778 174.25,344.25 "/>
</svg>
It's reasonably simple, the most important part is you include the svg within the html not simply as an image src attribute. The following is not dreamweaver specific.
For example:
<body>
<!-- this won't work well -->
<img src="/images/door.svg>
...
<!-- this will work very well -->
<svg>
<path></path>
<path></path>
</svg>
...
</body>
Now just like any other element you can give the svg and path elements id's and classes
<svg id="door">
<path id="handle"></path>
<path id="window"></path>
</svg>
From here you can use event listeners to those specific elements for example (with jQuery) to run off other functions for the customization of these svg elements.
$('#handle').on('click', function(){
$(this).css('fill', 'blue') // change path fill to blue
})
path's and other svg elements can more or less be styled with css which makes this a lot easier.
This isn't a copy and paste answer, but I hope it helps you!

JavaScript based SVG gradient manipulation on IE9

I have an SVG file which specifies a gradient and a circle like below. The embedded script toggles the orientation of the gradient onClick(), which works in all current browsers except IE9. My suspicion is that IE does not redraw the gradient. I tried a few things, such as setting the fill to a solid colour first and thenm reassign the (altered) gradient, to trigger a redraw but no dice so far. My question is, does anybody how I can work around that issue or, even better, solve it. Thanks.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
<![CDATA[
function flipGrad(evt) {
var g=document.getElementById('grad1');
var y1 = g.getAttribute('y1');
var y2 = g.getAttribute('y2');
g.setAttribute('y2', y1);
g.setAttribute('y1', y2);
}
]]>
</script>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<circle cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#grad1)" onclick="flipGrad(evt)" />
</svg>
EDIT:
Editing the stops help, so that might become workable. Truth is though, my actual file looks more like the following, which is Inkscape svg output, where gradients are split into colour sections and geometry sections and only the geometry is linked to from and object but the colour is reused in multiple other gradients. The approach to swap the stops would effect all objects linking to that color gradient:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
<![CDATA[
function flipGrad(evt) {
// var g=document.getElementById('gradGeometry');
// var y1 = g.getAttribute('y1');
// var y2 = g.getAttribute('y2');
// g.setAttribute('y2', y1);
// g.setAttribute('y1', y2);\
var s1=document.getElementById('stop1');
var s2=document.getElementById('stop2');
var s1s = s1.getAttribute('style');
var s2s = s2.getAttribute('style');
s1.setAttribute('style', s2s);
s2.setAttribute('style', s1s);
}
]]>
</script>
<defs>
<linearGradient id="gradColour">
<stop id="stop1" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop id="stop2" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="gradGeometry1" x1="0%" y1="0%" x2="0%" y2="100%" xlink:href="#gradColour" />
<linearGradient id="gradGeometry2" x1="0%" y1="0%" x2="100%" y2="0%" xlink:href="#gradColour" />
</defs>
<circle cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#gradGeometry1)" onclick="flipGrad(evt)" />
<circle cx="90" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#gradGeometry2)" onclick="flipGrad(evt)" />
</svg>
After testing a bit on IE9, it seems that the gradient vector is immutable once defined in IE. Your only choice is to use id'd gradient stops, which are mutable.
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
<![CDATA[
function flipGrad(evt) {
var s1=document.getElementById('stop1');
var s2=document.getElementById('stop2');
var s1s = s1.getAttribute('style');
var s2s = s2.getAttribute('style');
s1.setAttribute('style', s2s);
s2.setAttribute('style', s1s);
}
]]>
</script>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop id="stop1" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop id="stop2" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<circle id="bubble" cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#grad1)" onclick="flipGrad(evt)" />
</svg>

Categories