Javascript var not working in -webkit-transform - javascript

Here is my code:
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<style>
body {
text-align: center;
width: 100%;}
#container {
transform: rotate(30deg);
transform-origin: bottom center;
-ms-transform: rotate(30deg); /* IE 9 */
-ms-transform-origin: bottom center;
-webkit-transform: rotate 0deg; /* Safari and Chrome */
-webkit-transform-origin:bottom center;
border: 1px solid black;
width: 152px;
margin: 40px auto;
height: 244px;
overflow: visible;
}
</style>
<script>
var meteo = (function () {
var meteo = null;
$.ajax({
'async': false,
'global': false,
'url': "http://api.openweathermap.org/data/2.5/weather?q=Leicester&units=metric",
'dataType': "json",
'success': function (data) {
meteo = data;
}
});
return meteo;
})();
var vento_forza = meteo.wind.speed;
var vento_direzione = meteo.wind.deg+"deg";
</script>
</head>
<body>
<div id="container">
<svg version="1.1" id="indicator" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="152px" height="244px" viewBox="0 0 152 244" enable-background="new 0 0 152 244" xml:space="preserve">
<polygon fill="#84FFFB" points="0,244 76,207 152,244 "/>
<polygon fill="#D6F4FF" points="76,0 76,207 152,244 "/>
<polygon fill="#00A5EC" points="76,0 76,207 0,244 "/>
</svg>
</div>
<script>
function direzione(){
document.getElementById("container").style.webkitTransform = "rotate(vento_direzione)"
}
</script>
<input type="button" onclick="direzione()" value="Change link">
</body>
</html>
Basically I want the rotation of the container div to vary accordingly to the var vento_direzione (which it is a number obtained from the api request to witch I have appended a "deg" string) whenever I click the button; sadly, that doesn't seem to happen. If I just pass a value inside the function instead of the var, everything works fine. What am I doing wrong?
Please help! Cheers!

you want
document.getElementById("container").style.webkitTransform = "rotate("+vento_direzione+")";
As it is you are just trying to pass it the literal string "rotate(vento_direzione)", variables do not get replaced with their values when they are in strings like that.
So what you need to do is concatenate the actual string parts "rotate(" and ")" with the vento_direzione variable: "rotate("+vento_direzione+")" is string concatenation

Related

Click svg path and change option value on a select component

Currently I have some code that when selecting a value from a dropdown list will paint a specific path of a svg image.
var paths1 = {
"https://www.google.com": {
color: "#eb8c00",
selector: "#path1"
},
"https://www.yahoo.com": {
color: "#eb8c00",
selector: "#path2"
},
}
$('#path').on("change", function() {
$('#path1, #path2').css({
fill: "#FFFFFF"
});
var value = $(this).val()
if (!value) {
return;
}
var {
color,
selector
} = paths1[value]
$(selector).css({
fill: color
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div name="svg">
<svg id="Layer_1" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="500px" height="500px"
viewBox="0 0 500 500"
enable-background="new 0 0 500 500"
xml:space="preserve">
<path id="path1"
fill="#FFFFFF"
stroke="#231F20"
stroke-miterlimit="10"
d="M291.451,51.919v202.54c0,0,164.521,119.846,140.146,0 C407.227,134.613,291.451,51.919,291.451,51.919z"/>
<path id="path2"
fill="#FFFFFF"
stroke="#231F20"
stroke-miterlimit="10"
d="M169.595,150.844c0,0-76.24,69.615-40.606,128.309 c35.634,58.695,155.798-51.867,151.654-85.993C276.498,159.034,177.054,89.42,169.595,150.844z"/>
</svg>
</div>
<select id="path" name="path" class="pathSelector">
<option value="">Select Path</option>
<option value="https://www.google.com">Google</option>
<option value="https://www.yahoo.com">Yahoo</option>
</select>
<div>
<input
type="button"
class="selectbutton"
value="Select"
onClick="window.open(path.value,'newtab'+path.value)">
</div>
It works fine, as the button will also redirect to a different page.Now I need to be able to the reverse. I want to click on a path and the dropdown selector will also change. Clicking on path1 will select Google and clicking on path2 will select Yahoo from my dropdown list.
How can I achieve this?
To achieve this, you could define a click event handler:
$('#Layer_1 path').on("click", function() {...});
and use a for in loop to compare the selector values in the paths1 array with the ID of the clicked path:
for (key in paths1) {
if (paths1[key].selector == '#' + this.id) {...}
}
If you found a match, you can use the key to select the option tag and assign the selected property to it:
$('option[value="' + key + '"]').prop('selected', true);
All together:
$('#Layer_1 path').on("click", function() {
for (key in paths1) {
if (paths1[key].selector == '#' + this.id) {
$('option[value="' + key + '"]').prop('selected', true);
}
}
});
You could also change the colors similar to the other event handler:
$('#Layer_1 path').on("click", function() {
for (key in paths1) {
if (paths1[key].selector == '#' + this.id) {
$('option[value="' + key + '"]').prop('selected', true);
$('#path1, #path2').css({
fill: "#FFFFFF"
});
$(this).css({fill: paths1[key].color});
}
}
});
Working example:
var paths1 = {
"https://www.google.com": {
color: "#eb8c00",
selector: "#path1"
},
"https://www.yahoo.com": {
color: "#eb8c00",
selector: "#path2"
},
}
$('#path').on("change", function() {
$('#path1, #path2').css({
fill: "#FFFFFF"
});
var value = $(this).val()
if (!value) {
return;
}
var {
color,
selector
} = paths1[value]
$(selector).css({
fill: color
});
});
$('#Layer_1 path').on("click", function() {
for (key in paths1) {
if (paths1[key].selector == '#' + this.id) {
$('option[value="' + key + '"]').prop('selected', true);
$('#path1, #path2').css({
fill: "#FFFFFF"
});
$(this).css({fill: paths1[key].color});
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div name="svg">
<svg id="Layer_1" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="500px" height="500px"
viewBox="0 0 500 500"
enable-background="new 0 0 500 500"
xml:space="preserve">
<path id="path1"
fill="#FFFFFF"
stroke="#231F20"
stroke-miterlimit="10"
d="M291.451,51.919v202.54c0,0,164.521,119.846,140.146,0 C407.227,134.613,291.451,51.919,291.451,51.919z"/>
<path id="path2"
fill="#FFFFFF"
stroke="#231F20"
stroke-miterlimit="10"
d="M169.595,150.844c0,0-76.24,69.615-40.606,128.309 c35.634,58.695,155.798-51.867,151.654-85.993C276.498,159.034,177.054,89.42,169.595,150.844z"/>
</svg>
</div>
<select id="path" name="path" class="pathSelector">
<option value="">Select Path</option>
<option value="https://www.google.com">Google</option>
<option value="https://www.yahoo.com">Yahoo</option>
</select>
<div>
<input
type="button"
class="selectbutton"
value="Select"
onClick="window.open(path.value,'newtab'+path.value)">
</div>
here is a code that you can check out as an example. It must help. Good luck.
function SelectADropdownItem(id, val) {
var d = document.getElementById(id);
for (var i = 0; i < d.length; i++) {
if (d[i].value == val) {
d[i].selected = true;
} else {
d[i].selected = false;
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<img
onclick="SelectADropdownItem('design-dropdown','Fleur')"
src="//www.willmaster.com/library/images/ImageClickSelects/flower.jpg"
style="
border: none;
height: 120px;
width: 94px;
cursor: pointer;
margin: 0 5px 0 5px;
"
alt="Flower design"
/>
<img
onclick="SelectADropdownItem('design-dropdown','Nine Patch')"
src="//www.willmaster.com/library/images/ImageClickSelects/ninepatch.jpg"
style="
border: none;
height: 120px;
width: 94px;
cursor: pointer;
margin: 0 5px 0 5px;
"
alt="Nine patch design"
/>
<img
onclick="SelectADropdownItem('design-dropdown','Pink Gate')"
src="//www.willmaster.com/library/images/ImageClickSelects/pinkgate.jpg"
style="
border: none;
height: 120px;
width: 94px;
cursor: pointer;
margin: 0 5px 0 5px;
"
alt="Pink gate design"
/>
<br />
<img
onclick="SelectADropdownItem('design-dropdown','Sand Dollar')"
src="//www.willmaster.com/library/images/ImageClickSelects/sanddollar.jpg"
style="
border: none;
height: 120px;
width: 94px;
cursor: pointer;
margin: 0 5px 0 5px;
"
alt="Sand dollar design"
/>
<img
onclick="SelectADropdownItem('design-dropdown','Sandria')"
src="//www.willmaster.com/library/images/ImageClickSelects/sandria.jpg"
style="
border: none;
height: 120px;
width: 94px;
cursor: pointer;
margin: 0 5px 0 5px;
"
alt="Sandria design"
/>
<img
onclick="SelectADropdownItem('design-dropdown','Blue Wheel')"
src="//www.willmaster.com/library/images/ImageClickSelects/wheel.jpg"
style="
border: none;
height: 120px;
width: 94px;
cursor: pointer;
margin: 0 5px 0 5px;
"
alt="Blue wheel design"
/>
<select id="design-dropdown">
<option>Select here or click a pattern above.</option>
<option value="Fleur">Fleur</option>
<option value="Nine Patch">Nine Patch</option>
<option value="Pink Gate">Pink Gate</option>
<option value="Sand Dollar">Sand Dollar</option>
<option value="Sandria">Sandria</option>
<option value="Blue Wheel">Blue Wheel</option>
</select>
</body>
</html>

Mousemove and Hover Jquery effect to Pure Javascript or VueJS event

I have a VueJS component which is a svg map image of the united states. I previously used jquery to create hover effects to display the information bubble. I am trying to convert this to a pure Javascript solution. It seems a click event is the easiest to implement given vuejs mousemove seems to be less reliable. I have added the click event in method and I am trying to capture the SVG path location and steal the top and left position to enable the info box there. I have tried this.offsettop and this.offsetleft but they return undefined.
Jquery code I am trying to convert:
$("path, circle").hover(function(e) {
$('#info-box').css('display','block');
$('#info-box').html($(this).data('info'));
});
$("path, circle").mouseleave(function(e) {
$('#info-box').css('display','none');
});
$(document).mousemove(function(e) {
$('#info-box').css('top',e.pageY-$('#info-box').height()-30);
$('#info-box').css('left',e.pageX-($('#info-box').width())/2);
}).mouseover();
var ios = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if(ios) {
$('a').on('click touchend', function() {
var link = $(this).attr('href');
window.open(link,'_blank');
return false;
});
}
VueJS Component
<template id="geomap-assignments">
<v-card class="pa-4 ma-3 rounded-lg" min-height="500px">
<div class="card-title-docs">
<h4 class="card-header">Assignments</h4>
<h5 class="card-sub-header"></h5>
</div>
<div class="map-container">
<div id="info-box"></div>
<?xml version="1.0" encoding="utf-8" ?>
<svg xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="us-map" preserveAspectRatio="xMinYMin meet" sodipodi:docname="Republican_Party_presidential_primaries_results,_2016.svg" inkscape:version="0.91 r13725" x="0px" y="0px" width="959px" height="593px" viewBox="174 100 959 593" enable-background="new 174 100 959 593" xml:space="preserve">
<sodipodi:namedview bordercolor="#666666" objecttolerance="10" pagecolor="#ffffff" borderopacity="1" gridtolerance="10" guidetolerance="10" inkscape:cx="509.19152" inkscape:cy="282.2353" inkscape:zoom="1.2137643" showgrid="false" id="namedview71" inkscape:current-layer="g5" inkscape:window-maximized="1" inkscape:window-y="-8" inkscape:window-x="-8" inkscape:pageopacity="0" inkscape:window-height="1017" inkscape:window-width="1920" inkscape:pageshadow="2">
</sodipodi:namedview>
<g id="g5">
<path id="HI" data-info="<div>State: Hawaii</div><div>Capital: Honolulu</div>" fill="#D3D3D3" d="M407.1,619.3l1.9-3.6l2.3-0.3l0.3,0.8l-2.1,3.1H407.1z M417.3,615.6l6.1,2.6l2.1-0.3l1.6-3.9 l-0.6-3.4l-4.2-0.5l-4,1.8L417.3,615.6z M448,625.6l3.7,5.5l2.4-0.3l1.1-0.5l1.5,1.3l3.7-0.2l1-1.5l-2.9-1.8l-1.9-3.7l-2.1-3.6 l-5.8,2.9L448,625.6z M468.2,634.5l1.3-1.9l4.7,1l0.6-0.5l6.1,0.6l-0.3,1.3l-2.6,1.5l-4.4-0.3L468.2,634.5z M473.5,639.7l1.9,3.9 l3.1-1.1l0.3-1.6l-1.6-2.1l-3.7-0.3V639.7z M480.5,638.5l2.3-2.9l4.7,2.4l4.4,1.1l4.4,2.7v1.9l-3.6,1.8l-4.8,1l-2.4-1.5 L480.5,638.5z M497.1,654.1l1.6-1.3l3.4,1.6l7.6,3.6l3.4,2.1l1.6,2.4l1.9,4.4l4,2.6l-0.3,1.3l-3.9,3.2l-4.2,1.5l-1.5-0.6l-3.1,1.8 l-2.4,3.2l-2.3,2.9l-1.8-0.2l-3.6-2.6l-0.3-4.5l0.6-2.4l-1.6-5.7l-2.1-1.8l-0.2-2.6l2.3-1l2.1-3.1l0.5-1l-1.6-1.8L497.1,654.1z" />
<path id="OK" data-info="<div>State: Oklahoma</div><div>Capital: Oklahoma City</div>" fill="#D3D3D3" d="M549.3,422.6l-10.7-0.5l-6.4-0.5l0.3,0.2l-0.7,10.4l22,1.4l32.1,1.3l-2.3,24.4l-0.5,17.8l0.2,1.6 l4.3,3.7l2.1,1.1l0.7-0.2l0.7-2.1l1.4,1.8h2.1v-1.4l2.7,1.4l-0.5,3.9l4.1,0.2l2.5,1.1l4.1,0.7l2.5,1.8l2.3-2.1l3.4,0.7l2.5,3.4h0.9 v2.3l2.3,0.7l2.3-2.3l1.8,0.7h2.5l0.9,2.5l4.8,1.8l1.4-0.7l1.8-4.1h1.1l1.1,2.1l4.1,0.7l3.7,1.4l3,0.9l1.8-0.9l0.7-2.5h4.3l2.1,0.9 l2.7-2.1h1.1l0.7,1.6h4.1l1.6-2.1l1.8,0.5l2.1,2.5l3.2,1.8l3.2,0.9l1.9,1.1l-0.4-37.2l-1.4-11l-0.2-8.9l-1.4-6.5l-0.8-7.2l-0.1-3.8 l-12.1,0.3l-46.4-0.5l-45-2.1L549.3,422.6z" />
<path id="KS" data-info="<div>State: Kansas</div><div>Capital: Topeka</div>" fill="#D3D3D3" d="M677.4,425.1l-12.6,0.2l-46.1-0.5l-44.6-2.1l-24.6-1.3l4.1-64.7l21.8,0.8l40.5,1.4l44.1,0.5h5.1 l3.2,3.2l2.8,0.2l0.9,1.1v2l-1.8,1.6l-0.5,2.6l2.2,3.6l2.5,3.1l2.5,2l1.1,11.2L677.4,425.1z" />
<path id="LA" data-info="<div>State: Louisiana</div><div>Capital: Baton Rouge</div>" fill="#D3D3D3" d="M776.2,573l-1-2.6l-1.1-3.1l-3.3-3.5l0.9-6.8l-0.1-1.1l-1.3,0.3l-8.2,0.9l-25,0.5l-0.7-2.4l0.9-8.5 l3.3-5.9l5-8.7l-0.6-2.4l1.3-0.7l0.5-2l-2.3-2.1l-0.1-1.9l-1.8-4.3l-0.5-5.9l-9.7,0.1l-19.2,0.9l-22.2,0l0,9.6l0.7,9.4l0.7,3.9 l2.5,4.1l0.9,5l4.3,5.5l0.2,3.2l0.7,0.7l-0.7,8.5l-3,5l1.6,2.1l-0.7,2.5l-0.7,7.3l-1.4,3.2l0.1,3.6l4.7-1.5l8.1-0.3l10.3,3.6 l6.5,1.1l3.7-1.5l3.2,1.1l3.2,1l0.8-2.1l-3.2-1.1l-2.6,0.5l-2.7-1.6c0,0,0.2-1.3,0.8-1.5c0.6-0.2,3.1-1,3.1-1l1.8,1.5l1.8-1 l3.2,0.6l1.5,2.4l0.3,2.3l4.5,0.3l1.8,1.8l-0.8,1.6l-1.3,0.8l1.6,1.6l8.4,3.6l3.6-1.3l1-2.4l2.6-0.6l1.8-1.5l1.3,1l0.8,2.9 l-2.3,0.8l0.6,0.6l3.4-1.3l2.3-3.4l0.8-0.5l-2.1-0.3l0.8-1.6l-0.2-1.5l2.1-0.5l1.1-1.3l0.6,0.8c0,0-0.2,3.1,0.6,3.1 c0.8,0,4.2,0.6,4.2,0.6l4,1.9l1,1.5h2.9l1.1,1l2.3-3.1v-1.5h-1.3l-3.4-2.7l-5.8-0.8l-3.2-2.3l1.1-2.7l2.3,0.3l0.2-0.6l-1.8-1v-0.5 h3.2l1.8-3.1l-1.3-1.9l-0.3-2.7l-1.5,0.2l-1.9,2.1l-0.6,2.6l-3.1-0.6l-1-1.8l1.8-1.9l2-1.8L776.2,573z" />
<path id="VA" data-info="<div>State: Virginia</div><div>Capital: Richmond</div>" fill="#D3D3D3" d="M1002.9,369.2l-0.1-1.9l6.5-2.5l-0.8,3.2l-2.9,3.8l-0.4,4.6l0.5,3.4l-1.8,5l-2.2,1.9l-1.5-4.6 l0.4-5.4l1.6-4.2L1002.9,369.2z M1005.2,397.5L947,410.1l-37.4,5.3l-6.7-0.4l-2.6,1.9l-7.3,0.2l-8.4,1l-8.9,1l8.5-4.9l0-2.1 l1.5-2.1l10.6-11.5l3.9,4.5l3.8,1l2.5-1.1l2.2-1.3l2.5,1.3l3.9-1.4l1.9-4.6l2.6,0.5l2.9-2.1l1.8,0.5l2.8-3.7l0.3-2.1l-1-1.3l1-1.9 l5.3-12.3l0.6-5.7l1.2-0.5l2.2,2.4l3.9-0.3l1.9-7.6l2.8-0.6l1-2.7l2.6-2.3l1.3-2.3l1.5-3.4l0.1-5.1l9.8,3.8 c0.7,0.3,0.7-4.8,0.7-4.8l4.1,1.4l-0.5,2.6l8.2,2.9l1.3,1.8l-0.9,3.7l-1.3,1.3l-0.5,1.7l0.5,2.4l2,1.3l3.9,1.4l2.9,1l4.9,0.9 l2.2,2.1l3.2,0.4l0.9,1.2l-0.4,4.7l1.4,1.1l-0.5,1.9l1.2,0.8l-0.2,1.4l-2.7-0.1l0.1,1.6l2.3,1.5l0.1,1.4l1.8,1.8l0.5,2.5l-2.6,1.4 l1.6,1.5l5.8-1.7L1005.2,397.5z" />
<g id="DC">
<path id="path58" fill="#D3D3D3" d="M975.8,353.8l-1.1-1.6l-1-0.8l1.1-1.6l2.2,1.5L975.8,353.8z" />
<circle id="circle60" data-info="<div>Washington DC</div>" fill="#D3D3D3" stroke="#FFFFFF" stroke-width="1.5" cx="975.3" cy="351.8" r="5" />
</g>
</g>
<path id="path67" fill="none" stroke="#A9A9A9" stroke-width="2" d="M385,593v55l36,45 M174,525h144l67,68h86l53,54v46" />
</svg>
</div>
</v-card>
</template>
<script>
Vue.component('geomap-assignments', {
template: '#geomap-assignments',
methods: {
activateCaption: function (event) {
// `event` is the native DOM event
if (event) {
const left = this.offsetTop
const top = this.offsetTop
alert("LEFT" + left + "RIGHT" + top);
}
}
},
data() {
return {
documents: [
]
}
}
})
</script>
<style scoped>
.card-title-docs {
margin: 20px 20px;
}
.card-header {
font-size: 1rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 1px;
color: #515151;
}
.card-sub-header {
font-size: .8rem;
font-weight: 500;
text-transform: uppercase;
letter-spacing: .7px;
color: #7f818d;
}
.map-container{
position:relative;
width: 100%;
height: 100%;
}
#us-map {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
path:hover, circle:hover {
stroke: #002868 !important;
stroke-width: 2px;
stroke-linejoin: round;
fill: #002868 !important;
cursor: pointer;
}
#path67 {
fill: none !important;
stroke: #A9A9A9 !important;
cursor: default;
}
#info-box {
display: none;
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
background-color: #ffffff;
border: 2px solid #BF0A30;
border-radius: 5px;
padding: 5px;
font-family: arial;
}
</style>
'''
The solution was found in the comments on the question.
In summary, the issue was understanding how to position the #info-box element. Using the getBoundingClientRect() function, we can get the position of any element relative to the entire document. To get the position of the clicked path element, simply subtract the top and left of the #us-map from the top and left of the path. This will give the position for the top left corner of the path, relative to the containing element. We can then use that to position the #info-box.

How can i move SVG Polygon element by mouse?

I want to move this polygon by mouse. How can i do this?
I think i should use some like onMouseDown and onMouseMove get new position and transform="translate(x,y) but how can i do this by JS?
You can use draggable from jquery UI. Here is your edited code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#Layer_1" ).draggable();
} );
</script>
</head>
<body>
<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" viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve">
<polygon class="st0" points=" 0,5 10,0 20,5 10,10" transform="translate(90,95) rotate(0 0 0)" stroke="none" fill="red"/>
</svg>
</body>
</html>
Take the reference from below, it's working:
HTML:
<svg id="pointer" height="50" width="50">
<polygon points="1,1 49,10 20,20 10,49">
<!-- coordinates are in x,y format -->
<!-- points go clockwise around the polygon -->
</svg>
bbc
CSS:
#pointer {
overflow: hidden;
position: fixed;
top: 200px;
left: 200px;
position: relative;
}
html {
cursor: none;
}
a {
font-size: 40px;
}
a:hover {
cursor: pointer;
}
Javascript:
var mouseX;
var mouseY;
window.onmousemove = handleMouseMove;
function handleMouseMove(event) {
event = event || window.event;
document.getElementById('pointer').style.top=event.clientY + "px";
document.getElementById('pointer').style.left=event.clientX + "px";
}

Vertically centering part of svg content

I've been trying to center a collection of svg paths/text vertically in CSS / JS for quite a while with no luck. None of the solutions I've found here have worked so far. I have multiple < text > , < circle >, < rect >, although I don't think the type of content really matters.
The height is variable, so I have thought of 2 methods:
1) Transform via 50%. This is not working because you cannot transform a sub-set of content from a svg, from what I have found so far. The < g > tag was promising but I had no luck.
2) My second idea was to adjust the mask down via javascript once content is loaded. This, likewise, is not working.
All code is on code-pen and copied here as well: https://codepen.io/anon/pen/MmdWXB
HTML:
<div class="mask_group" id="mask_container">
<div class="text">
<svg>
<defs>
<mask id="mask" x="0" y="0" width="100%" height="100%" >
<rect class="cutout label" x="0" y="0" width="100%" height="100%"/>
<text class="label normal_font_weight" y="1em" dy="0em" x="50%">A</text>
<text class="label normal_font_weight" y="3em" dy="0em" x="50%">B</text>
<line class="label" x1="47%" y1="4.6em" x2="53%" y2="4.6em" style="stroke:rgb(255,0,0);stroke-width:1.5" />
<text class="label" y="7em" dy="0em" x="50%">C</text>
<text class="label" y="9em" id="final_text" dy="0em" x="50%">D</text>
</mask>
</defs>
<rect id="base" x="0" y="0" width="100%" height="100%"/>
</svg>
</div>
</div>
<div style="background-color:blue; width: 100%; height: 100%">
</div>
CSS:
html,body {
height: 100%;
}
.text {
position: relative;
/*top: 0;
left: 0;*/
width: 100%;
height: 100%;
margin-left: 0%;
/*z-index: 11; */
}
.mask_group {
position: fixed;
width: 100%;
height: 100%;
z-index: 10;
}
svg {
position: absolute;
width: 100%;
height: inherit;
}
svg text {
text-anchor: middle;
}
.cutout {
fill: white;
}
svg #base {
fill: #051E2A; /*#1F5773;*/
-webkit-mask: url(#mask);
mask: url(#mask);
}
/*Unused*/
.vert_center {
position: relative;
margin-top: 50%;
transform: translateY(-50%);
}
JS:
function body_loaded() {
var final_text = document.getElementById("final_text");
var position = final_text.getBoundingClientRect();
var bottom = position.bottom;
var mask_container = document.getElementById("mask_container");
var mask_position = mask_container.getBoundingClientRect();
var mask_height = mask_position.height;
var origin_y = (mask_height - bottom) / 2;
alert(origin_y);
var mask = document.getElementById("mask");
mask.style.y = origin_y;
}
Thanks ahead of time!
I'm not sure what you are attempting to do with the <mask>, so I am going to ignore it for now.
To vertically centre the SVG, just give it a fixed height and use the standard translate(-50%) trick. The only wrinkle is that you can't put the CSS transform on the <svg> element because transform has a different behavior in SVGs than it does in CSS. SVG transforms pre-date CSS transforms. So you just need to wrap the SVG in an HTML container and apply the transform to that instead.
I've set the height of the <svg> to 10em. Obviously you'll need to adjust that if you have more text. You could do that with JS using
svgElem.setAttribute("height", "10em");
But take it out of the CSS first obviously :)
html,body {
height: 100%;
}
.svg-wrapper {
position: absolute;
width: 100%;
top: 50%;
transform: translate(0, -50%);
}
.mask_group {
position: fixed;
width: 100%;
height: 100%;
z-index: 10;
}
svg {
width: 100%;
height: 10em;
fill: blue; /* default fill colour for contents */
}
svg text {
text-anchor: middle;
}
svg #base {
fill: #051E2A; /*#1F5773;*/
}
<div class="mask_group" id="mask_container">
<div class="svg-wrapper">
<svg>
<rect id="base" x="0" y="0" width="100%" height="100%"/>
<text class="label normal_font_weight" y="1em" dy="0em" x="50%">A</text>
<text class="label normal_font_weight" y="3em" dy="0em" x="50%">B</text>
<line class="label" x1="47%" y1="4.6em" x2="53%" y2="4.6em" style="stroke:rgb(255,0,0);stroke-width:1.5" />
<text class="label" y="7em" dy="0em" x="50%">C</text>
<text class="label" y="9em" id="final_text" dy="0em" x="50%">D</text>
</svg>
</div>
</div>
<div style="background-color:blue; width: 100%; height: 100%">
</div>

How to reset the timer of SVG animation when using beginElementAt()?

I use elt.beginElementAt(-5) on SVG animation with dur="10s", it's worked in 5s, normal.
BUT if at the end of an animation (i.e. at 5 seconds after launch elt.beginElementAt(-5) cmd) I want to trigger it again, It doesn't work!
I have to wait for the "truth" duration (10s in this example), to be able to restart the animation. Nothing happens if I try between 5s and 10s after my first elt.beginElementAt(-5) command...
So, how to reset the timer? How to ignore the "normal" duration?
Thanks.
To have SVG start and reset functions work together, set the fill attribute.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<title>Scalable Vector Graphics (SVG) - SVG Animations</title>
<!-- Scaffold -->
<style>
/* <![CDATA[ */
body {
margin-left: 100px;
}
svg {
border: 1px solid black;
width: 600px;
height: 400px;
}
svg * {
fill: none;
stroke: #000000;
stroke-width: 1px;
}
/* ]]> */
</style>
<script>
/* <![CDATA[ */
function go() {
var elements = document.getElementsByTagName("animate");
for (var i = 0; i < elements.length; i++) {
elements[i].setAttribute("fill", "freeze");
elements[i].beginElement();
}
}
function reset() {
var elements = document.getElementsByTagName("animate");
for (var i = 0; i < elements.length; i++) {
elements[i].setAttribute("fill", "remove");
elements[i].endElement();
}
}
/* ]]> */
</script>
</head>
<body>
<h1>Scalable Vector Graphics (SVG) - SVG Animations</h1>
<svg xmlns="http://www.w3.org/2000/svg" width="600px" height="400px">
<rect x="10" y="10" width="100" height="100">
<!-- begin="indefinite" allows us to start the animation from script -->
<animate attributeName="x" begin="indefinite" dur="1s" from="10" to="300" fill="freeze" />
</rect>
</svg>
<button onclick="go();" type="button">Go</button>
<button onclick="reset();" type="reset">Reset</button>
</body>
</html>

Categories