I'm trying to use an SVG library like Two.js or SVG.js and alter (on-the-fly) their dynamically generated SVG elements in order to make them responsive. I have a basic example here, showing how a predefined, inline SVG can be responsive.
It involves wrapping the SVG element in a container, assigning some CSS values, adding viewBox and preserveAspectRatio attributes. This appears to be the common way to do responsive SVG elements:
http://jsfiddle.net/N4PK4/
<div class='container'>
<div class='svg-container'>
<svg version="1.1" viewBox="0 0 500 500" preserveAspectRatio="xMinYMin meet" class="svg-content">
<circle fill="#F7941E" stroke="#231F20" stroke-width="10" cx="250" cy="250" r="200" opacity="1" />
</svg>
</div>
</div>
.svg-container {
display: inline-block;
position: relative;
width: 100%;
outline: 1px solid red;
padding-bottom: 100%;
vertical-align: middle;
overflow: hidden;
}
.svg-content {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}
.container {
max-width: 400px;
}
However, when I dynamically generate an SVG element with Two.js, and then I try to use jQuery to edit the DOM and make the SVG element responsive (similar to the above example), the SVG element is no longer visible:
http://jsfiddle.net/g5WZj/5/
You can see that I have two SVGs in that example. The first one is the one that doesn't show up. The second, is the inline, static SVG markup that I copied from what was generated in the first example. So obviously, it is not the markup that is the problem.
Also, here is a similar example using svg.js. Same result:
http://jsfiddle.net/3Luaw/5/
So basically, the problem lies in the fact that if you create a static SVG element in your HTML, then it's easy to make it responsive. But if you try to dynamically create an SVG element, its difficult (or impossible I dunno?) to make it responsive.
I know that the SVG DOM is kind of different than the normal HTML DOM, so there can be unexpected results when trying to make changes to it dynamically.
How do I overcome this hurdle?
Sure, the SVG DOM is kind of different than the normal HTML DOM. So you'd better to use special-purpose svg lib to handle that. Use SVG.js or two.js to modify SVG node, not jQuery.
If you want to your app will be compatible with old bjust start your work, you can try Raphealjs too.
In your example http://jsfiddle.net/g5WZj/5/ the reason why your "dynamic" svg doesn't show up is because you haven't set a size on it. If you rightclick the svg part and inspect the svg element you'll find that its computed layout size in the page is 0x0.
You can set the width and height attributes on the <svg> element, or you can add something similar to your css stylesheet, e.g svg { width: 500px; height 500px; }
Related
I have a problem with SVG's foreignObjects elements on Safari. I am working with SVG elements and what I am using d3.js library.
What I did is to append on canvas a foreignObject element because differently from rect or other SVG elements didn't allow to put html elements inside. Now, after some days of development I noticed that it seems that Safari doesn't allow to put html elements inside a foreignObject. Better, it allows to do and all seems to work according to the inspector, but the I put inside the foreignObject has a background set.
The result is that I can correctly drag the foreignObject and the div inside inside the canvas but differently from Chrome or Firefox, the div's background stay on the canvas top left corner not following the div.
Here below I paste the portion of the style and foreign object code I append to the canvas
SVG
<foreignObject transform="translate(304,215)rotate(-174.86597769360367)" height="180" width="40" class="dragging">
<div class="new-rect" style="background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAAC0CAYAAABYIPRNAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAERJJREFUeNrsnWtwXOV5x//ntverpNWuJMuyZF1tbNkIg7kkJsQ0DLfEJlDaiWmY0qSXD23TGWinaWcgnU7L0KRJ2xQw01BCEnBDYyg0QLCpDXbCRQLbwndb99tqtdr79ew5fd6zkiBf+nnZPj+PbFnaPZr1+zvP+3/e9+yxZJomGEbmfwKGRWBYBIZFYFgEhkVg/k/Uenoxp0ZHqy9Krb4st9uN4ZERfHXffXj+Jy/ilttuxNmR+b3x6dhDiqLYZUVSJQkaddAaPVwzKhWHommj4a7IHe3dvszPfvIK7v+jr+BzN34OjzzyCEKhEHK5HGKxJZTLZVx/3bUswqei3MkyxDpJKp2CbhSRXMQDFz7K7ZckN2RVfM9YeaRU/cME9FLlxmQ2+WH7Rt9NBsqTyeUEdL1iHYsrwqeUdCYDSZJ+6+t/8PXp1mBXT3yquH99uwuKqliCGDTwkvTJZ0iWDYVceeP0qdwHnev6ur/5139V7Orq+o6qad/P5/MnpF9/Qt0g1dPK4ienhmKphKDf/2+Ly9H7G92t8JQaMT03BYfTAb8jCJkG1DCoIlBVMFcUkGUFpUoJy+klZFIlBAL+ibxj6airUdsXaYpgbn7+N+12+4FlqhIlOj5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ag3ujhng8hl8dGznf0NC4e2Cgfwp1SN3dcFOUcBEUvV7fsydOfDhXMfMv9q679qA7HLy6KdzQoywlIaXSKIu2UVarYXFlQ1mvGKhICuUEG9Z1tuJy/PKzk/Pn1OjobHOpVLm1u7u7aH7iwhcWocYRK4v+gAh08qHH/3n/xrvvyS3s7P/yuqnZ2TPRyXlPPJ6AXdxa7xMdw2pmLOWLaGttw5F33jk3t3x533JxCol4Ev0Dm37t4lUW4VOzpqBB3GcxmUgulIs6zf/69MJibKhpXeMvM8vRhkomb00jpmFaK4tiajDKFdjEO6W8ZmJ6fvwGcTMuRVKtW/bWswR1LYKoCqt3aRdSiHexpxKp8+GW0NDAVVueXpiJBsRDaKxVWVXEHdpVo6Tbgi0N2ZSevSPgDcTcPjtSs+P4/4DE/8sbU3frCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsMiMCwCwyIwLALDIjAsAsOwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwLAIDIvAsAgMi8CwCAyLwNQ+/yvAAM/MO22Kfsg/AAAAAElFTkSuQmCC); left:-45px;">
</div>
</foreignObject>
Style
foreignObject{
cursor:pointer;
position:relative;
}
foreignObject div.new-rect{
position: absolute;
top: -8px;
background-repeat: no-repeat;
background-size: auto;
height: 180px;
width: 130px;
}
That's all. I think I am doing something that Safari doesn't like. But question is .. what ?
Setting position: "fixed" on the div inside foreignObject fixed the foreignObject position for me in Safari (as well as on Chrome on iOS). It doesn't seem to scale the foreignObject according to the scaling of the SVG though
I've found that you need to insert a <body> element to get it work in Safari, like this:
<foreignObject width="170" height="28">
<body xmlns="http://www.w3.org/1999/xhtml">
<div>my content</div>
</body>
</foreignObject>
Try adding in an inline width and height for the foreignObject. This seems to work on Safari.
<foreignObject style="overflow: visible;" y="0" x="0" width="240" height="160">
<div class="info-panel" xmlns="http://www.w3.org/2000/xmlns/" >
<div class="panel-text" xmlns="http://www.w3.org/2000/xmlns/">
<h1>ForeignObject</h1>
<p>For this to work in Safari try adding in a width and height inline on the foreignObject element. The background will also expand to the more text you add.</p>
</div>
</div>
</foreignObject>
Demo
https://codepen.io/chrisgannon/pen/2107d092ec56025bba036934d680f2bc
You can use <image> element in SVG instead of <foreignObject>. This can solve the scale and position issue easily to avoid <foreignObject> bug on Safari
Remember to change the src attribute to href.
Putting display:contents on the html tag inside puts it into place as of Safari 15.4. But you won't be able to interact much with it since the wrapping div is now nuked from the DOM. Defeating the whole purpose of foreignObject. Sing with me Safari is the new IE
Try wrapping your foreignObjects in group elements. I found that trying to style foreignObjects with transforms doesn't work well in Safari. Also most SVG's don't work in canvas elements in Firefox.
<g transform="translate(304,215)rotate(-174.86597769360367)">
<foreignObject>
<div> whatever in here </div>
<foreignObject>
</g>
I found a weird bug with iOS Safari regarding foreignObject. When I set the opacity in CSS for the HTML content within the foreignObject (it was a button in my case) it would position it absolutely over the SVG. But it positioned as expected when I removed the opacity. Took me hours to debug this issue.
A bit old, but I hope this could help:
As #do-ic suggested adding position: fixed to the element inside foreignObject solves the positioning, but not the scale.
To solve the scale I added this:
d3.zoom().on('zoom', event => {
// zoom stuff...
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (isSafari) {
d3.selectAll(NODE_SELECTOR).style('transform', `scale(${transform.k})`);
}
});
Also the element inside foreignObject should have: transform-origin: 0px 0px
Hi i solved this behavior without viewbox and set a #svg height and width dimension
New solutions:
Do not use the following styles in Safari + foreignobjects:
position
webkit-transform-style
webkit-backface-visibility
transition
transform
calc (The calculation result may not conform to the expectation)
opacity
You can avoid style problems without using them
old:
I find #Quinn answer, Add some information, First , you need get your SVG transform information, like is <g class = "main-tree" transform="translate(875) scale(1.09)"></g> . and You can use d3.zoomTransform to get these data, let transform = d3.zoomTransform(d3.select("#treesvg").node());
The following content is the same as that of #Quinn ,You need to select the root element under the foreignObject,Do not execute the code in other browsers
I need to reduce the opacity of image in site and I did it by decreasing opacity value in below code. But when I tried to search code: opacity: 0.3, I didn't get that code anywhere in the site files. I tried fgrep -r "opacity : 0.3", and still I didn't find it. Opacity code is coming from 3rd party plugin we are using.
Is there any way to override that opacity of the image [ Means Image which is displaying outside the Mug in link by adding new class?
Image :
Overriding style can be done with
style="opacity:1"
at the element.
Or create a new class in your css:
.aitraph>svg>image{
opacity:1 !important;
}
and give your element this ccs class.
The only way you could override it is by using the !important tag after you CSS property in your stylsheet. If you do not use the important then than the inline style will always take precedent to any style sheets.
Example:
.whateverClass {
color: 000 !important;
}
Edit: Now knowing that you do not have the ability to add a class to the element, you can use css child selectors as outlined below as I can see an ID on the svg parent object that you could target:
https://jsfiddle.net/sLtgsqyk/
html example:
<div id="test">
<svg width="391" height="391" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">
<image href="https://i.stack.imgur.com/KUQYl.png" x="0" y="0" height="1276" width="2000"/>
</svg>
</div>
CSS:
#test > svg > image
{
height: 1000px !important;
width: 1500px !important;
}
You can add new class to image (e.g. .opacity_img) and set style opacity:0.3!important
Don't like !important but it can help you
I have a svg file. I'm adding it through the <object data = "img/img.svg" type = "image/svg + xml">...
Svg file contains tag "path" with attribute "fill".
This svg file needs to be used several times, and use a different color for "fill".
it is necessary something like div.red #pathId {fill: red} and div.green #pathId {fill: green}
Css property do not apply to this svg file, as on the page it is similar to the iframe way, and => css not apply.
I know that we can in SVG file to specify the path to the CSS file. But then all the styles of this CSS file is used only for the SVG file. (.red and .green not available in SVG and => it does not apply)
Is there any solution without using JS?
(With JS we can get the contents of SVG through .contentDocument and set the fill attribute)
You can only access to the SVG if the SVG is an element of the HTML-DOM like this:
<div class="svg">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="566.93px" height="1133.858px" viewBox="0 0 566.93 1133.858" enable-background="new 0 0 566.93 1133.858" xml:space="preserve">...</svg>
</div>
It's a html snippet from one of my projects. In the global css there are basic some styles:
.svg {
height: 400px;
width: 400px;
}
svg {
height: 100%;
width: 100%;
}
To include the SVG I use the following mechanism:
HTML:
<div class="svg" data-svgpath="img/img.svg"></div>
JS:
function loadSvg(container) {
if (container.dataset.svgpath) {
$(container).load(container.dataset.svgpath, function(resp, status, xhr) {
container.classList.add('is-loaded');
});
}
}
Load SVG and inject into the DOM:
$('.svg').each(function() {
loadSvg(this);
});
This is a short summary of what I do in the project and it works like a charm. I have access through the global css and js, because I manipulate the visibility of groups in the SVG too.
The browser support of inline svg is quite well: http://caniuse.com/#feat=svg-html5
EDIT:
Ah my mistake: you are looking for a solution without JS. Hm, sorry but may it's still helpful for you or someone other.
Ciao
Ralf
I was wondering if there is a way using css or javascript that allows for a semi-transparent div element to appear visually on top of all other elements, but hyperlinks and other interactive elements under it can still be clicked? Just a whimsical idea to give my website the ability to darken or lighten the look of the website.
EDIT
I know that z-index moves objects from the background to the foreground but they also block interactive objects...
Such behavior possible via poiner-events:none (non-standard and not supported by IE).
Originally being an SVG feature, it's supported for any SVG element (IE 9+):
<svg poiner-events="none">
<rect width="100%" height="100%" fill="black" fill-opacity="0.5" />
</svg>
But for the effect you mentioned I would recommend to use opacity on body and some background on html:
body {
opacity: 0.5;
}
html {
background: black;
}
create another div element on top of existing div, and have it's z-index more than the existing one. Hope this should help.
Hey.
Let's say that somewhere on my page I have SVG graphics. There is one group that I would like to re-scale when some event is triggered. How do I do that?
example code:
<svg onresize="getDivSize(evt)">
<g transform=scale(13)>
<rect id="Square" class="chart"
width="80" height="20"
fill="black"
stroke-width="0px"
rx="8" ry="8" />
<text id="TextElement" x="13" y="15" fill="green">Text</text>
</g>
</svg>
I want to change scale(13) argument, to do that what should be in function getScreenSize(evt) {...}?
Or how achieve similar effect in different way?
edit
As for general idea I want to resize graphic without specifying fixed values anywhere. So my divs sizes are percentage based, now I just want my graphic to exactly fit my div regardless of its size. That's why I thought of JS changing scale() argument whenever event is fired (div resize). Function would put into scale argument computation of DivSize/rectBaseSize (x or y).
Add an id attribute to the <g> and then try this:
document.getElementById("id_of_g_element").transform.baseVal.getItem(0).setScale(new_scalex,newscale_y);
or alternatively:
document.getElementById("id_of_g_element").setAttribute("transform", "scale(" + new_scalex + "," + new_scaley + ")");
On the other hand, why don't you just use a viewBox to have the svg contents rescaled automatically? Or are there specific constraints on what should be shown? It's also possible to do some of that with non-scripted solutions based on CSS3 media-queries, see http://www.youtube.com/watch?v=YAK5el8Uvrg (don't forget to check the description for links to the demo files shown in that presentation).
If you 'just' want an SVG to scale to the size of a DIV, you can do that with CSS
#stretched-image {
margin: 0;
position:fixed;
top:0; left:0; right:0; bottom:0;
height:100%;
background-size:cover;
display: block;
background-position:50% 50%;
background-image: url(../img/pic.svg);
}