svg dom manipulation changing color stroke - svg

.circular-chart {
display: block;
max-width: 80%;
max-height: 40px;
}
.circle {
display: flex;
justify-content: center;
animation: progress 1s ease-out forwards;
}
.circle-red {
stroke: red;
fill: white;
stroke-width: 4;
stroke-linecap: round;
}
.circle-darkorange {
stroke: darkorange;
fill: white;
stroke-width: 4;
stroke-linecap: round;
}
.circle-lime {
stroke: lime;
fill: white;
stroke-width: 4;
stroke-linecap: round;
}
#keyframes progress {
0% {
stroke-dasharray: 0 100;
}
}
.movie-rating {
position: absolute;
margin-left: 10px;
}
const inputSearch = document.getElementById("input-search")
const searchForm = document.getElementById("search-form")
const movies = document.getElementById("movies")
let dataArray = []
let tooltipModal
let circleStrokeRate
// render html data in DOM
function renderHtmlData() {
let html = ""
dataArray.forEach(movie => {
let urlImg = "https://www.themoviedb.org/t/p/w440_and_h660_face/"
if(movie.poster_path === null) {
urlImg = "img/no_image.jpeg"
} else {
urlImg = `https://www.themoviedb.org/t/p/w440_and_h660_face/${movie.poster_path}`
}
html += `
<article class="movie-container">
<div class="poster-container">
<img
src="${urlImg}"
class="video-img"
>
<div class="tooltip-disable swing-in-right-fwd" data-modalid="${movie.id}">
<p class="add-watchlist">
<i class="fa-regular fa-eye"></i>
Watchlist
</p>
<p class="add-favorites">
<i class="fa-regular fa-star"></i>
Favorites
</p>
</div>
<button id="more-btn" data-btnid="${movie.id}">...</button>
</div>
<div class="movie-details">
<div class="col col-rating">
<span class="movie-rating">${movie.vote_average.toFixed(1)}</span>
<svg viewBox="0 0 36 36" class="circular-chart">
<path class="circle"
stroke-dasharray="${movie.vote_average * 10}, 100"
d="M18 2.0845
a 15.9155 15.9155 0 0 1 0 31.831
a 15.9155 15.9155 0 0 1 0 -31.831"
/>
</svg>
</div>
<div class="col col2">
<h3>${movie.title}</h3>
<p class="movie-date">${movie.release_date}</p>
</div>
</div>
</article>
`
movies.innerHTML = html
tooltipModal = document.querySelectorAll(".tooltip-disable")
circleStrokeRate = document.querySelectorAll(".circle")
setStrokeColor(movie.vote_average * 10)
})
}
// search for movies or series
searchForm.addEventListener("submit", async function (e) {
e.preventDefault()
const res = await fetch(`
https://api.themoviedb.org/3/search/movie?api_key="APIKEY&
page=1&include_adult=true&query=${inputSearch.value}
`)
const data = await res.json()
dataArray = data.results
renderHtmlData()
})
// handle circle stroke color rating movie
function setStrokeColor(percent) {
circleStrokeRate.forEach(circle => {
if (percent < 40) {
console.log("red")
circle.classList.add("circle-red")
} else if (percent < 70) {
console.log("orange")
circle.classList.add("circle-darkorange")
} else {
console.log("lime")
circle.classList.add("circle-lime")
}
})
}
hi guys hope someone could help me on this:
so the problem is when i try to check the percentage if is bigger or smaller and give it a stroke to the SVG it always returns me the color of the first svg element and it passes to all the others, and i cant see why is that, what im doing wrong.
Any help on this??

When you call setStrokeColor you set the percentage to all elements with the class "circle" not just the one you just added. You call the function when you build the original html.
<svg viewBox="0 0 36 36" class="circular-chart">
<path class="circle ${setStrokeColor(moive.vote_average)}"
stroke-dasharray="${movie.vote_average * 10}, 100"
d="M18 2.0845
a 15.9155 15.9155 0 0 1 0 31.831
a 15.9155 15.9155 0 0 1 0 -31.831"
/>
</svg>
function setStrokeColor(percent) {
circleStrokeRate.forEach(circle => {
if (percent < 40) {
return "circle-red";
} else if (percent < 70) {
return "circle-darkorange";
} else {
return "circle-lime";
}
});
}

Related

svg tooltip on mouse hover position

I'm working on a svg, a tooltip must appear on hover, but the problem is that tooltips appear far from the desired location.
The svg is a map and that's why the tooltip's location must be precise. First intention that the svg itself is the problem but after checking with another one which works perfectly I didn't find the source of the problem.
the actual result:
the desired one:
the yellow part is where the hover happen in the map.
Part of the svg code:
<svg id="Layer_0" data-name="Layer 0" xmlns="http://www.w3.org/2000/svg" width="529.017" height="491.509" viewBox="0 0 529.017 491.509">
<defs>
<style>
.cls-5898b1 {
fill: #5898b1 ;
}
.cls-f9f9f9 {
fill: #f9f9f9 ;
}
.cls-061d53 {
fill: #061d53 ;
}
.cls-5898b1, .cls-f9f9f9, .cls-061d53, .cls-IDF {
stroke: #061d53;
stroke-width: 0.5px;
}
.cls-f9f9f9-nostroke {
fill: #f9f9f9;
}
.cls-IDF {
fill: #bee0e0;
}
/* .mapImage, .mapImageMobile { */
/* float: left; */
/* width: 100%; */
/* } */
.mapImage path, .mapImageMobile path {
stroke-width: 1px;
}
.mapImage polygon, .mapImageMobile polygon {
stroke-width: 1px;
}
.mapImage path:hover, .mapImage polygon:hover, .mapImageMobile path:hover,
.mapImageMobile polygon:hover {
fill: #ffcb00;
}
.cls-1, .cls-4 {
stroke: #C4C4C4;
}
.st0 {
fill: #000000;
}
.cls-1, .cls-3, .cls-2 {
fill: transparent;
}
<g id="Groupe_1355" data-name="Groupe 1355" transform="translate(-129.507 -2290.08)">
<g id="Groupe_1190" data-name="Groupe 1190" transform="translate(-35.507 2042.682)">
<a id="centre" style="stroke:#061D53;">
<path id="FR-F" class="cls-f9f9f9-nostroke" data-name="Tracé 1042" d="M2822.574,2031.032a.545.545,0,0,1-.094-.146,10,10,0,0,1,0-3.668c.084-.661.556-1.926.667-2.583.064-.382-.071-1.209.125-1.542.248-.421,1.289-.729,1.625-1.083a7.25,7.25,0,0,0,1.292-2.167,19.775,19.775,0,0,0,.083-2.125,20.276,20.276,0,0,1,.75-3c.172-.478.755-1.34.875-1.834.161-.668-.172-2.111.083-2.75a3.738,3.738,0,0,1,1.375-1.542,3.789,3.789,0,0,1,1.709,0c.388.08,1.067.562,1.458.625a1.778,1.778,0,0,0,1-.083c.406-.3.38-1.531.75-1.875a4.486,4.486,0,0,1,1.834-.583,12.812,12.812,0,0,0,1.292,0c.418-.125,1.04-.834,1.458-.958.469-.14,1.515.206,1.959,0a2.524,2.524,0,0,0,.917-1.084c.147-.4-.118-1.3,0-1.708a3.525,3.525,0,0,1,.959-1.542c.441-.287,1.609-.064,2.084-.292a4.236,4.236,0,0,0,1.375-1.292c.323-.575.277-1.98.542-2.584.18-.41.952-1.019,1.042-1.458.107-.523-.7-1.609-.459-2.084s1.631-.473,2-.875a4.753,4.753,0,0,0,.75-2.208c.024-.371-.3-1.087-.292-1.458.019-.614.86-1.808.625-2.376-.166-.4-1.393-.454-1.5-.875s.6-1.135.875-1.459c.212-.248.875-.533,1-.833.141-.338-.328-1.154-.125-1.459s1.171-.178,1.417-.458a1.528,1.528,0,0,0,.208-1.125,1.348,1.348,0,0,0-.667-.5c-.238-.079-.775.108-1,0a2.837,2.837,0,0,1-.853-.986c-.059-.1-.109-.189-.147-.264a4.525,4.525,0,0,1-.5-1.833c.038-.362.614-.933.667-1.292a3.093,3.093,0,0,0-.292-1.542c-.137-.221-.663-.429-.833-.625-.281-.323-.875-1.074-.833-1.5a2.776,2.776,0,0,1,1.333-1.584c.5-.206,1.633.313,2.167.208a4.927,4.927,0,0,0,1.667-.875,7.99,7.99,0,0,0,1.208-1.667,10.572,10.572,0,0,0,1.083-2.625,5.313,5.313,0,0,0-.25-2.5c-.141-.254-.753-.469-.917-.708a12.664,12.664,0,0,1-.583-2,5.6,5.6,0,0,0-.958-1.917c-.219-.172-.842-.113-1.083-.25a4.216,4.216,0,0,1-1.25-1.292,4.653,4.653,0,0,1-.375-2.084,3.471,3.471,0,0,1,.367-.953,4.334,4.334,0,0,1,.592-.881,5.386,5.386,0,0,1,1.709-1c.657-.211,2.113.016,2.75-.25.553-.23,1.324-1.227,1.834-1.542a7.255,7.255,0,0,1,1.542-.667,16.14,16.14,0,0,1,1.667-.125c.732-.074,2.221-.613,2.917-.375.321.109.633.867.958.958a2.907,2.907,0,0,0,1.375-.333,7.579,7.579,0,0,0,2.125-1.375,6.794,6.794,0,0,0,.625-1.375,6.764,6.764,0,0,1,.75-1.792c.329-.314,1.339-.359,1.709-.625a2.534,2.534,0,0,0,.833-1.042c.079-.368-.428-1.086-.375-1.458a2.924,2.924,0,0,1,.667-1.125,7.51,7.51,0,0,1,1.258-.661,5.364,5.364,0,0,1,.77,1.23c.089.322-.11,1.019,0,1.333.177.5,1.107,1.185,1.334,1.667a4.512,4.512,0,0,1,.279,1.334,16.093,16.093,0,0,0-.279,1.834,8.07,8.07,0,0,0,.667,1.167,7.467,7.467,0,0,1,.667,2.112c.007.594-.847,1.713-.667,2.278.1.32.862.559,1.056.833.272.383.315,1.413.611,1.778.153.189.643.351.833.5a5.356,5.356,0,0,1,1.223,1.223c.2.4.027,1.405.278,1.778.271.4,1.27.72,1.667,1,.25.177.836.492.945.779.18.472-.364,1.5-.306,2a3.339,3.339,0,0,0,.583,1.444,3.058,3.058,0,0,0,1.195.556c.278.061.873-.1,1.139,0,.376.145.836.9,1.2,1.083.31.157,1.094.057,1.362.278a5.025,5.025,0,0,1,.944,2.278c.094.47-.179,1.473,0,1.917.153.381.985.8,1.139,1.181a3.008,3.008,0,0,1,0,1.32c-.057.255-.444.684-.445.945a1.046,1.046,0,0,0,.306.653c.3.194,1.063.066,1.417.042a2.332,2.332,0,0,0,1-.167c.175-.141.149-.705.334-.833.282-.2,1.031-.037,1.375-.042a4.759,4.759,0,0,0,1.25,0,1.625,1.625,0,0,0,.667-.625c.1-.229-.153-.8,0-1a1.527,1.527,0,0,1,1.125-.292c.414.086,1.013.766,1.333,1.042.262.227.654.938,1,.958.416.025,1.09-.662,1.333-1,.116-.16.09-.634.25-.75a1.446,1.446,0,0,1,1.125,0c.284.162.383.964.667,1.125.417.237,1.516-.263,1.917,0,.371.242.617,1.191.708,1.625.085.407-.25,1.333,0,1.667s1.285.173,1.584.458a4.559,4.559,0,0,1,.708,2.084,3.276,3.276,0,0,1-.083,1.458c-.172.329-1,.583-1.167.917a1.689,1.689,0,0,0,0,1.167,1.655,1.655,0,0,0,1,.542c.35.018.968-.409,1.292-.542a7.726,7.726,0,0,1,1.458-.583,7.4,7.4,0,0,1,1.931.292,9.191,9.191,0,0,0,1.584.556,4.1,4.1,0,0,0,1.889-.695c.236-.194.39-.864.639-1.042a2.652,2.652,0,0,1,1.167-.263,3.458,3.458,0,0,1,1.167.263c.279.142.634.781.944.82a2.181,2.181,0,0,0,1.223-.612c.21-.2.3-.879.556-1.028.193-.115.682.033,1.06.1a1.526,1.526,0,0,0,.176.564,2.168,2.168,0,0,0,.916.583c.24.07.759-.063,1,0a2.084,2.084,0,0,1,1.042.583c.218.392-.189,1.356-.083,1.792a4.048,4.048,0,0,0,.917,1.584,9.343,9.343,0,0,0,1.375.542c.224.12.716.357.834.583a4.751,4.751,0,0,1,0,2.334,5.606,5.606,0,0,1-.792,1.833c-.208.255-.915.468-1.084.75-.188.315.061,1.17-.167,1.459-.322.408-1.659.282-1.958.708a2.309,2.309,0,0,0,0,1.458c.087.288.6.707.667,1a4.265,4.265,0,0,1-.25,1.625,4.537,4.537,0,0,1-.583,1.167c-.133.16-.541.334-.667.5-.175.231-.2.893-.417,1.083a2.6,2.6,0,0,1-1.459.333c-.2-.02-.545-.32-.75-.333a2.863,2.863,0,0,0-1.542.458,2.456,2.456,0,0,0-.625,1.417,3.426,3.426,0,0,0,.917,1.667c.344.321,1.411.427,1.709.792a5.322,5.322,0,0,1,.5,2.125c.049.424-.169,1.316,0,1.708.113.264.7.538.833.792a2.246,2.246,0,0,1,.209,1.25,2.986,2.986,0,0,1-1.083,1.125c-.26.156-.964.108-1.167.333a1.617,1.617,0,0,0-.208,1.167c.1.279.755.522.875.792.182.409-.132,1.363,0,1.792.1.337.692.828.792,1.167a4.344,4.344,0,0,1-.042,1.667,6.105,6.105,0,0,1-.75,1.542c-.179.252-.757.589-.875.875a3.652,3.652,0,0,0,0,1.625,4.967,4.967,0,0,0,.875,1.875c.3.3,1.22.432,1.542.709a4.581,4.581,0,0,1,.875,1.333c.265.648.18,2.15.542,2.75.18.3.917.569,1.084.875a5.666,5.666,0,0,1,.25,2.292c-.028.48-.412,1.395-.431,1.875a4.436,4.436,0,0,0,.278,1.861c.245.4,1.218.755,1.528,1.112a4.5,4.5,0,0,1,.861,1.611,8.536,8.536,0,0,1-.444,2.695c-.126.454-.618,1.286-.694,1.75a6.316,6.316,0,0,0,.083,1.7c.084.427.589,1.2.583,1.639a6.057,6.057,0,0,1-.7,1.806,9.875,9.875,0,0,0-.944,1.612,2.345,2.345,0,0,0,.043.756c-.3-.048-.6-.121-.766-.145-.43-.063-1.244-.607-1.667-.5-.3.077-.629.711-.889.889-.426.292-1.539.454-1.889.833-.229.25-.243,1.006-.444,1.279a2.911,2.911,0,0,1-1.056.777c-.554.182-2-.481-2.334,0-.152.222.54.779.389,1s-.792,0-1.055,0a7.7,7.7,0,0,0-1.557,0c-.36.1-.917.677-1.277.778-.307.087-1.079-.25-1.278,0s.278,1.223,0,1.333a4.9,4.9,0,0,0-1.445.612,1.693,1.693,0,0,0-.333,1c.02.148.32.35.333.5.02.21-.349.567-.333.778.021.292.5.73.612,1,.177.422.726,1.417.444,1.778-.086.11-.43-.059-.556,0-.339.162-.616.986-.944,1.167a5.688,5.688,0,0,1-2.834.333c-.373-.071-.954-.729-1.334-.722-.355.007-.924.531-1.223.722-.227.145-.662.465-.888.612-.724.466-2.522,1.029-2.946,1.778-.177.314.182,1.133,0,1.444-.194.33-1.075.453-1.358.744a4.049,4.049,0,0,0-1.753.062c-.307.12-.469.912-.75,1.084a6.319,6.319,0,0,1-3.834.5c-.273-.089-.567-.685-.833-.792-.62-.248-2.011.12-2.667,0-.548-.1-1.535-.691-2.083-.792a6.887,6.887,0,0,0-2,0c-.469.1-1.276.874-1.75.792s-.959-1.189-1.417-1.375c-.328-.133-1.2-.278-1.417,0-.346.435.566,1.615.5,2.167-.033.278-.223.967-.5,1-.407.049-.668-1.142-1.064-1.25a2.39,2.39,0,0,0-1.853.639c-.149.209.182.846,0,1.028a1.85,1.85,0,0,1-1.834,0c-.316-.317.268-1.433,0-1.792-.195-.262-.927-.328-1.25-.375a5.1,5.1,0,0,0-1.917,0,3.008,3.008,0,0,0-1.25,1.139c-.1.235.133.808,0,1.028-.28.462-1.421.784-1.917,1-.344.15-1.079.663-1.417.5-.4-.191-.3-1.329-.583-1.667a2.894,2.894,0,0,0-1.584-.875,1.829,1.829,0,0,0-.917.542c-.2.222-.252.918-.5,1.083a3.261,3.261,0,0,1-2.084.083,4.657,4.657,0,0,1-1.028-.833c-.149-.128-.45-.3-.678-.489a.745.745,0,0,1-.294-.387c-.078-.485.989-1.218.973-1.709a1.705,1.705,0,0,0-.556-.833c-.348-.3-1.469-.338-1.667-.75-.252-.524.6-1.669.583-2.25a2.908,2.908,0,0,0-.583-1.75c-.338-.243-1.256-.068-1.667,0-.51.084-1.479.86-1.959.667-.674-.271-.809-2.08-1.333-2.583-.464-.445-1.854-.656-2.334-1.084a5.866,5.866,0,0,1-1.584-2.584c-.067-.529.546-1.514.625-2.042a6.676,6.676,0,0,0,0-2.129,7.393,7.393,0,0,0-1.292-2.038c-.411-.464-1.6-1.04-1.917-1.57-.395-.649-.208-2.339-.625-2.973-.358-.543-1.72-.991-2.125-1.5a9.785,9.785,0,0,1-.75-1.833c-.306-.732-.675-2.371-1.25-2.917-.282-.269-1.134-.326-1.458-.542-.405-.269-.87-1.272-1.333-1.417a1.485,1.485,0,0,0-1.167.25c-.3.365.164,1.437,0,1.876a1.525,1.525,0,0,1-.542.625c-.442.213-1.529-.151-1.959.083-.269.147-.436.859-.708,1a2.222,2.222,0,0,1-.958,0c-.649-.078-1.888-.635-2.542-.625-.313,0-.912.435-1.209.333a3.476,3.476,0,0,1-1.458-1.959c-.112-.486.372-1.46.375-1.959a4.754,4.754,0,0,0-.375-2.083,2.5,2.5,0,0,0-1.209-.708c-.3-.055-.933.344-1.208.208-.4-.2-.5-1.29-.833-1.583-.368-.321-1.459-.355-1.833-.667-.521-.433-.969-1.792-1.375-2.334C2823,2031.41,2822.728,2031.213,2822.574,2031.032Z" transform="translate(-2481.446 -1580.518)">
<title transform="translate(-2481.446 -1580.518)">Centre Val de Loire</title>
</path>
</a>
It appear that the bug appears just on large size desktops, in medium size the tooltip works perfectly.
You need to add closing tags </style>,</defs> </g> </svg>
Move <title> tag from path inside<a>link tags
Remove from <title> transform = "translate (-2481.446 -1580.518)
<svg id="Layer_0" data-name="Layer 0" xmlns="http://www.w3.org/2000/svg" width="529.017" height="491.509" viewBox="0 0 529.017 491.509">
<defs>
<style>
.cls-5898b1 {
fill: #5898b1 ;
}
.cls-f9f9f9 {
fill: #f9f9f9 ;
}
.cls-061d53 {
fill: #061d53 ;
}
.cls-5898b1, .cls-f9f9f9, .cls-061d53, .cls-IDF {
stroke: #061d53;
stroke-width: 0.5px;
}
.cls-f9f9f9-nostroke {
fill: #f9f9f9;
}
.cls-IDF {
fill: #bee0e0;
}
/* .mapImage, .mapImageMobile { */
/* float: left; */
/* width: 100%; */
/* } */
.mapImage path, .mapImageMobile path {
stroke-width: 1px;
}
.mapImage polygon, .mapImageMobile polygon {
stroke-width: 1px;
}
.mapImage path:hover, .mapImage polygon:hover, .mapImageMobile path:hover,
.mapImageMobile polygon:hover, path:hover {
fill: #ffcb00;
}
.cls-1, .cls-4 {
stroke: #C4C4C4;
}
.st0 {
fill: #000000;
}
.cls-1, .cls-3, .cls-2 {
fill: transparent;
}
</style>
</defs>
<g id="Groupe_1355" data-name="Groupe 1355" transform="translate(-129.507 -2290.08)">
<g id="Groupe_1190" data-name="Groupe 1190" transform="translate(-35.507 2042.682)">
<a id="centre" style="stroke:#061D53;">
<title transform="translate(-2481.446 -1580.518)">Centre Val de Loire</title>
<path id="FR-F" class="cls-f9f9f9-nostroke" data-name="Tracé 1042" d="M2822.574,2031.032a.545.545,0,0,1-.094-.146,10,10,0,0,1,0-3.668c.084-.661.556-1.926.667-2.583.064-.382-.071-1.209.125-1.542.248-.421,1.289-.729,1.625-1.083a7.25,7.25,0,0,0,1.292-2.167,19.775,19.775,0,0,0,.083-2.125,20.276,20.276,0,0,1,.75-3c.172-.478.755-1.34.875-1.834.161-.668-.172-2.111.083-2.75a3.738,3.738,0,0,1,1.375-1.542,3.789,3.789,0,0,1,1.709,0c.388.08,1.067.562,1.458.625a1.778,1.778,0,0,0,1-.083c.406-.3.38-1.531.75-1.875a4.486,4.486,0,0,1,1.834-.583,12.812,12.812,0,0,0,1.292,0c.418-.125,1.04-.834,1.458-.958.469-.14,1.515.206,1.959,0a2.524,2.524,0,0,0,.917-1.084c.147-.4-.118-1.3,0-1.708a3.525,3.525,0,0,1,.959-1.542c.441-.287,1.609-.064,2.084-.292a4.236,4.236,0,0,0,1.375-1.292c.323-.575.277-1.98.542-2.584.18-.41.952-1.019,1.042-1.458.107-.523-.7-1.609-.459-2.084s1.631-.473,2-.875a4.753,4.753,0,0,0,.75-2.208c.024-.371-.3-1.087-.292-1.458.019-.614.86-1.808.625-2.376-.166-.4-1.393-.454-1.5-.875s.6-1.135.875-1.459c.212-.248.875-.533,1-.833.141-.338-.328-1.154-.125-1.459s1.171-.178,1.417-.458a1.528,1.528,0,0,0,.208-1.125,1.348,1.348,0,0,0-.667-.5c-.238-.079-.775.108-1,0a2.837,2.837,0,0,1-.853-.986c-.059-.1-.109-.189-.147-.264a4.525,4.525,0,0,1-.5-1.833c.038-.362.614-.933.667-1.292a3.093,3.093,0,0,0-.292-1.542c-.137-.221-.663-.429-.833-.625-.281-.323-.875-1.074-.833-1.5a2.776,2.776,0,0,1,1.333-1.584c.5-.206,1.633.313,2.167.208a4.927,4.927,0,0,0,1.667-.875,7.99,7.99,0,0,0,1.208-1.667,10.572,10.572,0,0,0,1.083-2.625,5.313,5.313,0,0,0-.25-2.5c-.141-.254-.753-.469-.917-.708a12.664,12.664,0,0,1-.583-2,5.6,5.6,0,0,0-.958-1.917c-.219-.172-.842-.113-1.083-.25a4.216,4.216,0,0,1-1.25-1.292,4.653,4.653,0,0,1-.375-2.084,3.471,3.471,0,0,1,.367-.953,4.334,4.334,0,0,1,.592-.881,5.386,5.386,0,0,1,1.709-1c.657-.211,2.113.016,2.75-.25.553-.23,1.324-1.227,1.834-1.542a7.255,7.255,0,0,1,1.542-.667,16.14,16.14,0,0,1,1.667-.125c.732-.074,2.221-.613,2.917-.375.321.109.633.867.958.958a2.907,2.907,0,0,0,1.375-.333,7.579,7.579,0,0,0,2.125-1.375,6.794,6.794,0,0,0,.625-1.375,6.764,6.764,0,0,1,.75-1.792c.329-.314,1.339-.359,1.709-.625a2.534,2.534,0,0,0,.833-1.042c.079-.368-.428-1.086-.375-1.458a2.924,2.924,0,0,1,.667-1.125,7.51,7.51,0,0,1,1.258-.661,5.364,5.364,0,0,1,.77,1.23c.089.322-.11,1.019,0,1.333.177.5,1.107,1.185,1.334,1.667a4.512,4.512,0,0,1,.279,1.334,16.093,16.093,0,0,0-.279,1.834,8.07,8.07,0,0,0,.667,1.167,7.467,7.467,0,0,1,.667,2.112c.007.594-.847,1.713-.667,2.278.1.32.862.559,1.056.833.272.383.315,1.413.611,1.778.153.189.643.351.833.5a5.356,5.356,0,0,1,1.223,1.223c.2.4.027,1.405.278,1.778.271.4,1.27.72,1.667,1,.25.177.836.492.945.779.18.472-.364,1.5-.306,2a3.339,3.339,0,0,0,.583,1.444,3.058,3.058,0,0,0,1.195.556c.278.061.873-.1,1.139,0,.376.145.836.9,1.2,1.083.31.157,1.094.057,1.362.278a5.025,5.025,0,0,1,.944,2.278c.094.47-.179,1.473,0,1.917.153.381.985.8,1.139,1.181a3.008,3.008,0,0,1,0,1.32c-.057.255-.444.684-.445.945a1.046,1.046,0,0,0,.306.653c.3.194,1.063.066,1.417.042a2.332,2.332,0,0,0,1-.167c.175-.141.149-.705.334-.833.282-.2,1.031-.037,1.375-.042a4.759,4.759,0,0,0,1.25,0,1.625,1.625,0,0,0,.667-.625c.1-.229-.153-.8,0-1a1.527,1.527,0,0,1,1.125-.292c.414.086,1.013.766,1.333,1.042.262.227.654.938,1,.958.416.025,1.09-.662,1.333-1,.116-.16.09-.634.25-.75a1.446,1.446,0,0,1,1.125,0c.284.162.383.964.667,1.125.417.237,1.516-.263,1.917,0,.371.242.617,1.191.708,1.625.085.407-.25,1.333,0,1.667s1.285.173,1.584.458a4.559,4.559,0,0,1,.708,2.084,3.276,3.276,0,0,1-.083,1.458c-.172.329-1,.583-1.167.917a1.689,1.689,0,0,0,0,1.167,1.655,1.655,0,0,0,1,.542c.35.018.968-.409,1.292-.542a7.726,7.726,0,0,1,1.458-.583,7.4,7.4,0,0,1,1.931.292,9.191,9.191,0,0,0,1.584.556,4.1,4.1,0,0,0,1.889-.695c.236-.194.39-.864.639-1.042a2.652,2.652,0,0,1,1.167-.263,3.458,3.458,0,0,1,1.167.263c.279.142.634.781.944.82a2.181,2.181,0,0,0,1.223-.612c.21-.2.3-.879.556-1.028.193-.115.682.033,1.06.1a1.526,1.526,0,0,0,.176.564,2.168,2.168,0,0,0,.916.583c.24.07.759-.063,1,0a2.084,2.084,0,0,1,1.042.583c.218.392-.189,1.356-.083,1.792a4.048,4.048,0,0,0,.917,1.584,9.343,9.343,0,0,0,1.375.542c.224.12.716.357.834.583a4.751,4.751,0,0,1,0,2.334,5.606,5.606,0,0,1-.792,1.833c-.208.255-.915.468-1.084.75-.188.315.061,1.17-.167,1.459-.322.408-1.659.282-1.958.708a2.309,2.309,0,0,0,0,1.458c.087.288.6.707.667,1a4.265,4.265,0,0,1-.25,1.625,4.537,4.537,0,0,1-.583,1.167c-.133.16-.541.334-.667.5-.175.231-.2.893-.417,1.083a2.6,2.6,0,0,1-1.459.333c-.2-.02-.545-.32-.75-.333a2.863,2.863,0,0,0-1.542.458,2.456,2.456,0,0,0-.625,1.417,3.426,3.426,0,0,0,.917,1.667c.344.321,1.411.427,1.709.792a5.322,5.322,0,0,1,.5,2.125c.049.424-.169,1.316,0,1.708.113.264.7.538.833.792a2.246,2.246,0,0,1,.209,1.25,2.986,2.986,0,0,1-1.083,1.125c-.26.156-.964.108-1.167.333a1.617,1.617,0,0,0-.208,1.167c.1.279.755.522.875.792.182.409-.132,1.363,0,1.792.1.337.692.828.792,1.167a4.344,4.344,0,0,1-.042,1.667,6.105,6.105,0,0,1-.75,1.542c-.179.252-.757.589-.875.875a3.652,3.652,0,0,0,0,1.625,4.967,4.967,0,0,0,.875,1.875c.3.3,1.22.432,1.542.709a4.581,4.581,0,0,1,.875,1.333c.265.648.18,2.15.542,2.75.18.3.917.569,1.084.875a5.666,5.666,0,0,1,.25,2.292c-.028.48-.412,1.395-.431,1.875a4.436,4.436,0,0,0,.278,1.861c.245.4,1.218.755,1.528,1.112a4.5,4.5,0,0,1,.861,1.611,8.536,8.536,0,0,1-.444,2.695c-.126.454-.618,1.286-.694,1.75a6.316,6.316,0,0,0,.083,1.7c.084.427.589,1.2.583,1.639a6.057,6.057,0,0,1-.7,1.806,9.875,9.875,0,0,0-.944,1.612,2.345,2.345,0,0,0,.043.756c-.3-.048-.6-.121-.766-.145-.43-.063-1.244-.607-1.667-.5-.3.077-.629.711-.889.889-.426.292-1.539.454-1.889.833-.229.25-.243,1.006-.444,1.279a2.911,2.911,0,0,1-1.056.777c-.554.182-2-.481-2.334,0-.152.222.54.779.389,1s-.792,0-1.055,0a7.7,7.7,0,0,0-1.557,0c-.36.1-.917.677-1.277.778-.307.087-1.079-.25-1.278,0s.278,1.223,0,1.333a4.9,4.9,0,0,0-1.445.612,1.693,1.693,0,0,0-.333,1c.02.148.32.35.333.5.02.21-.349.567-.333.778.021.292.5.73.612,1,.177.422.726,1.417.444,1.778-.086.11-.43-.059-.556,0-.339.162-.616.986-.944,1.167a5.688,5.688,0,0,1-2.834.333c-.373-.071-.954-.729-1.334-.722-.355.007-.924.531-1.223.722-.227.145-.662.465-.888.612-.724.466-2.522,1.029-2.946,1.778-.177.314.182,1.133,0,1.444-.194.33-1.075.453-1.358.744a4.049,4.049,0,0,0-1.753.062c-.307.12-.469.912-.75,1.084a6.319,6.319,0,0,1-3.834.5c-.273-.089-.567-.685-.833-.792-.62-.248-2.011.12-2.667,0-.548-.1-1.535-.691-2.083-.792a6.887,6.887,0,0,0-2,0c-.469.1-1.276.874-1.75.792s-.959-1.189-1.417-1.375c-.328-.133-1.2-.278-1.417,0-.346.435.566,1.615.5,2.167-.033.278-.223.967-.5,1-.407.049-.668-1.142-1.064-1.25a2.39,2.39,0,0,0-1.853.639c-.149.209.182.846,0,1.028a1.85,1.85,0,0,1-1.834,0c-.316-.317.268-1.433,0-1.792-.195-.262-.927-.328-1.25-.375a5.1,5.1,0,0,0-1.917,0,3.008,3.008,0,0,0-1.25,1.139c-.1.235.133.808,0,1.028-.28.462-1.421.784-1.917,1-.344.15-1.079.663-1.417.5-.4-.191-.3-1.329-.583-1.667a2.894,2.894,0,0,0-1.584-.875,1.829,1.829,0,0,0-.917.542c-.2.222-.252.918-.5,1.083a3.261,3.261,0,0,1-2.084.083,4.657,4.657,0,0,1-1.028-.833c-.149-.128-.45-.3-.678-.489a.745.745,0,0,1-.294-.387c-.078-.485.989-1.218.973-1.709a1.705,1.705,0,0,0-.556-.833c-.348-.3-1.469-.338-1.667-.75-.252-.524.6-1.669.583-2.25a2.908,2.908,0,0,0-.583-1.75c-.338-.243-1.256-.068-1.667,0-.51.084-1.479.86-1.959.667-.674-.271-.809-2.08-1.333-2.583-.464-.445-1.854-.656-2.334-1.084a5.866,5.866,0,0,1-1.584-2.584c-.067-.529.546-1.514.625-2.042a6.676,6.676,0,0,0,0-2.129,7.393,7.393,0,0,0-1.292-2.038c-.411-.464-1.6-1.04-1.917-1.57-.395-.649-.208-2.339-.625-2.973-.358-.543-1.72-.991-2.125-1.5a9.785,9.785,0,0,1-.75-1.833c-.306-.732-.675-2.371-1.25-2.917-.282-.269-1.134-.326-1.458-.542-.405-.269-.87-1.272-1.333-1.417a1.485,1.485,0,0,0-1.167.25c-.3.365.164,1.437,0,1.876a1.525,1.525,0,0,1-.542.625c-.442.213-1.529-.151-1.959.083-.269.147-.436.859-.708,1a2.222,2.222,0,0,1-.958,0c-.649-.078-1.888-.635-2.542-.625-.313,0-.912.435-1.209.333a3.476,3.476,0,0,1-1.458-1.959c-.112-.486.372-1.46.375-1.959a4.754,4.754,0,0,0-.375-2.083,2.5,2.5,0,0,0-1.209-.708c-.3-.055-.933.344-1.208.208-.4-.2-.5-1.29-.833-1.583-.368-.321-1.459-.355-1.833-.667-.521-.433-.969-1.792-1.375-2.334C2823,2031.41,2822.728,2031.213,2822.574,2031.032Z" transform="translate(-2481.446 -1580.518)">
</path>
</a>
</g>
</g>
</svg>

Pure svg pie chart, text align center

How to place the value text inside each pie chart without framework d3.js.
i have tried using some javascript to get the width. It is the default
getBBox()); // get the SVG width.
I use stroke-dasharray to expand the pie space.
Which way i can get the correct stroke-dasharray size from javascript?
figure {
background-color: #eee;
display: block;
height: 0;
margin: 0 auto;
position: relative;
font-size:16px;
font-size:1vw;
width: 40em;
padding-bottom: 40em;
}
svg {
display: block;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
overflow: visible;
}
circle {
fill:transparent;
stroke-width:31.8309886184;
stroke-dasharray: 0,0,0,100;
stroke-dashoffset: 25;
animation: pie1 4s ease both;
}
.pie1 {
stroke:pink;
}
.pie2 {
stroke: green;
-webkit-animation-name: pie2;
animation-name: pie2;
}
.pie3 {
stroke: aqua;
-webkit-animation-name: pie3;
animation-name: pie3;
}
#keyframes pie1 {
50%,100% {stroke-dasharray: 40,60,0,0;}
}
#keyframes pie2 {
50%,100% {stroke-dasharray: 0,40,30,30;}
}
#keyframes pie3 {
50%,100% {stroke-dasharray: 0,70,30,0;}
}
<body>
<figure>
<svg class="chart" viewBox="0 0 63.6619772368 63.6619772368">
<circle class="pie1" cx="31.8309886184" cy="31.8309886184" r="15.9154943092" />
<circle class="pie2" cx="31.8309886184" cy="31.8309886184" r="15.9154943092" />
<circle class="pie3" cx="31.8309886184" cy="31.8309886184" r="15.9154943092" />
</svg>
</figure>
</body>
You can't. The getBBox() gets the bounds of the shape. In your case, the shapes are circles centred on the middle of the graph. You would need to use trigonometry to calculate the position for your text.
makeLabel('Pink', 340, 15.9);
makeLabel('Green', 110, 15.9);
makeLabel('Cyan', 210, 15.9);
function makeLabel(text, angle, radius)
{
const chart = document.getElementById("chart");
const label = document.createElementNS(chart.namespaceURI, "text");
label.classList.add("label");
label.setAttribute("x", 31.83 + Math.cos(angle * Math.PI/180) * radius);
label.setAttribute("y", 31.83 + Math.sin(angle * Math.PI/180) * radius);
label.textContent = text;
chart.appendChild(label);
}
figure {
background-color: #eee;
display: block;
height: 0;
margin: 0 auto;
position: relative;
font-size:16px;
font-size:1vw;
width: 40em;
padding-bottom: 40em;
}
svg {
display: block;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
overflow: visible;
}
circle {
fill:transparent;
stroke-width:31.8309886184;
stroke-dasharray: 0,0,0,100;
stroke-dashoffset: 25;
animation: pie1 4s ease both;
}
.pie1 {
stroke:pink;
stroke-dasharray: 40,60,0,0;
}
.pie2 {
stroke: green;
stroke-dasharray: 0,40,30,30;
}
.pie3 {
stroke: aqua;
stroke-dasharray: 0,70,30,0;
}
.label {
font: 3px sans-serif;
text-anchor: middle;
}
<body>
<figure>
<svg id="chart" class="chart" viewBox="0 0 63.6619772368 63.6619772368">
<circle class="pie1" cx="31.8309886184" cy="31.8309886184" r="15.9154943092" />
<circle class="pie2" cx="31.8309886184" cy="31.8309886184" r="15.9154943092" />
<circle class="pie3" cx="31.8309886184" cy="31.8309886184" r="15.9154943092" />
</svg>
</figure>
</body>
BTW, that approach for doing pie graphs works most of the time. And may be alright for your case. But in general it is not recommended. Some browsers have trouble rendering circles drawn that way. You might want to consider switching to drawing proper circular sectors.

How to create SVG radial lines

I am trying to create a scale with radial lines and numbers with range 0-100.
Here is my code:
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<title>SVG Gauge</title>
</head>
<style>
#wrapper {
position: relative;
margin: auto;
}
#meter {
width: 100%;
height: 100%;
transform: rotateX(180deg);
}
.circle {
fill: none;
}
#mask {
stroke: #404040;
stroke-width: 60;
}
.blackie {
fill:none;
stroke: #000000;
stroke-width: 30;
}
.range {
stroke-width: 60;
}
.scale {
stroke: #cccccc;
}
#slider, #lbl {
position: absolute;
}
#slider {
cursor: pointer;
left: 0;
margin: auto;
right: 0;
top: 58%;
width: 94%;
}
#lbl {
background-color: #4B4C51;
border-radius: 2px;
color: white;
font-family: 'courier new';
font-size: 15pt;
font-weight: bold;
padding: 4px 4px 2px 4px;
right: -48px;
top: 57%;
}
#meter_needle {
height: 40%;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 10%;
transform-origin: bottom center;
transform: rotate(270deg);
}
</style>
<body>
<div id="wrapper">
<svg id="meter">
<g class="scale">
<defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(102, 102, 255);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(204, 204, 255);stop-opacity:1" />
</linearGradient>
</defs>
<circle id="high" class="circle range" cx="50%" cy="50%" stroke="url(#grad)">
</circle>
<circle id="mask" class="circle" cx="50%" cy="50%">
</circle>
<circle id="low" class="blackie" cx="50%" cy="50%" r="360">
</circle>
<circle id="outline_ends" class="circle outline" cx="50%" cy="50%">
</circle>
</g>
</svg>
<img id="meter_needle" src="gauge-needle.svg" alt="">
<input id="slider" type="range" min="0" max="100" value="0" />
<label id="lbl" id="value" for="">0%</label>
</div>
<script>
var r = 400;
var circles = document.querySelectorAll('.circle');
var total_circles = circles.length;
for (var i = 0; i < total_circles; i++) {
circles[i].setAttribute('r', r);
}
var meter_dimension = (r * 2) + 100;
var wrapper = document.querySelector('#wrapper');
wrapper.style.width = meter_dimension + 'px';
wrapper.style.height = meter_dimension + 'px';
var cf = 2 * Math.PI * r;
var semi_cf = cf / 2;
var z = 40 * Math.PI;
document.querySelector('#outline_ends')
.setAttribute('stroke-dasharray', 2 + ',' + (semi_cf - 2));
document.querySelector('#high')
.setAttribute('stroke-dasharray', semi_cf + ',' + cf);
document.querySelector('#mask')
.setAttribute('stroke-dasharray', semi_cf + ',' + cf);
document.querySelector('#low')
.setAttribute('stroke-dasharray', semi_cf - z + ',' + cf);
var slider = document.querySelector('#slider');
var lbl = document.querySelector("#lbl");
var svg = document.querySelector('#meter');
var high = document.querySelector('#high');
var mask = document.querySelector('#mask');
var low = document.querySelector('#low');
var meter_needle = document.querySelector('#meter_needle');
function range_change_event() {
var percent = slider.value;
var meter_value = semi_cf - ((percent * semi_cf) / 100);
mask.setAttribute('stroke-dasharray', meter_value + ',' + cf);
meter_needle.style.transform = 'rotate(' + (270 + ((percent * 180) / 100)) + 'deg)';
lbl.textContent = percent + '%';
}
slider.addEventListener('input', range_change_event);
</script>
</body>
</html>
I have found on the web plenty of good examples with HTML canvas and D3js but nothing based on SVG..
I am thinking to create an element "line" and add it across the black arc.
What is the simplest way to create the numeric scale?
Here you go:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-250 -250 500 500" width="500" height="500" id="svg">
<defs>
<style>
line {
stroke: black;
stroke-width: 1px;
}
text {
fill: red;
text-anchor: middle;
font-size: 16px;
font-family: sans-serif;
}
rect {
fill: transparent;
}
#id {
display: none;
}
.origin {
fill: green;
}
.outer {
fill: none;
stroke: black;
}
</style>
</defs>
<circle r="5" cx="0" cy="0" class="origin"/>
<path d="M-180,0 a1,1 0 0,1 360,0" class="outer"/>
<g id="gauge" transform="rotate(-90)">
<g id="noon">
<rect x="-10" y="-220" width="20" height="100"/>
<line x1="0" y1="-190" x2="0" y2="-180"/>
<text x="0" y="-200"></text>
</g>
</g>
</svg>
<script>
for (i=0; i<=180; i = i + 18) {
var new_tick = noon.cloneNode(true);
new_tick.getElementsByTagName('text')[0].textContent = i/180 * 100;
new_tick.removeAttribute("id");
new_tick.setAttribute("transform", "rotate(" + i + " 0 0)");
gauge.appendChild(new_tick);
}
</script>
I think this is fairly self-explanatory. (For example the RECT is just a guide which you can turn on - by changing the fill - if you want to better visualize what's going in inside each G.)
Let me know if you have any follow on questions.
Here's a Codepen, if it helps: https://codepen.io/MSCAU/pen/OoQMdV
What is the simplest way to create the numeric scale?
The easiest way to animate the scaling of the svg circle is to animate its radius
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="400" height="400" viewBox="0 0 400 400" >
<circle cx="200" cy="200" r="10" fill="none" stroke-width="2" stroke="purple" >
<animate attributeName="r" values="1;100;1" dur="4s" repeatCount="indefinite" />
</circle>
</svg>

SVG attribute stroke-dasharray in the Vue component

Help me to represent this element as a vue-component, where
.circle-chart-percent text (user defined value) == .circle-chart-circle stroke-dasharray.
<svg class="circle-chart" viewbox="0 0 33.83098862 33.83098862" width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<circle class="circle-chart-background" stroke="#cccccc" stroke-width="0.35" stroke-dasharray="100" fill="none" cx="16.91549431" cy="16.91549431" r="15.91549431" />
<circle class="circle-chart-circle" stroke="#4f5357" stroke-width="0.5" stroke-dasharray="90" stroke-linecap="round" fill="none" cx="16.91549431" cy="16.91549431" r="15.91549431" />
<g class="circle-chart-info">
<text class="circle-chart-percent" x="16.91549431" y="15.5" alignment-baseline="central" text-anchor="middle" font-size="8">30%</text>
<text class="circle-chart-subline" x="16.91549431" y="20.5" alignment-baseline="central" text-anchor="middle" font-size="2">name</text>
</g>
</svg>
<script>
var svg = document.querySelector('.circle-chart-circle');
att = svg.getAttribute('stroke-dasharray');
text = document.querySelector('text.circle-chart-percent');
if (att === '100') {
text.textContent = 'full';
}
else {
text.textContent = att + '%';
}
</script>
<style>
.circle-chart-circle {
animation: circle-chart-fill 2s reverse;
transform: rotate(-90deg);
transform-origin: center;
}
.circle-chart-info {
animation: circle-chart-appear 2s forwards;
opacity: 0;
transform: translateY(0.3em);
}
#keyframes circle-chart-fill {
to {
stroke-dasharray: 0 100;
}
}
#keyframes circle-chart-appear {
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
https://codepen.io/pershay/pen/gjyKme
Follow Vue Guide: style-binding, then you will reach goal easily.
Below is one simple demo:
bind :stroke-dasharray="progress"
create one computed property=computedText to return the progress description (in your codes, it is text.textContent = 'full'; or att+'%')
Vue.component('svg-circle', {
template: `<svg class="circle-chart" viewBox="0 0 33.83098862 33.83098862" width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<circle class="circle-chart-background" stroke="#cccccc" stroke-width="0.35" stroke-dasharray="100" fill="none" cx="16.91549431" cy="16.91549431" r="15.91549431" />
<circle class="circle-chart-circle" stroke="#4f5357" stroke-width="0.5" :stroke-dasharray="progress" stroke-linecap="round" fill="none" cx="16.91549431" cy="16.91549431" r="15.91549431" />
<g class="circle-chart-info">
<text class="circle-chart-percent" x="16.91549431" y="15.5" alignment-baseline="central" text-anchor="middle" font-size="8">{{computedText}}</text>
<text class="circle-chart-subline" x="16.91549431" y="20.5" alignment-baseline="central" text-anchor="middle" font-size="2">name</text>
</g>
</svg>
`,
props: ['progress'],
computed: {
computedText: function () {
let description = ''
let progress = this.progress < 0 ? 0 : (this.progress > 100 ? 100 : this.progress)
if (progress === '100') {
description = 'full';
}
else {
description = progress + '%';
}
return description
}
}
})
new Vue({
el: '#app',
data() {
return {
progress: 10
}
},
methods:{
changeProgress: function () {
this.progress += 10
}
}
})
.circle-chart-circle {
animation: circle-chart-fill 2s reverse;
transform: rotate(-90deg);
transform-origin: center;
}
.circle-chart-info {
animation: circle-chart-appear 2s forwards;
opacity: 0;
transform: translateY(0.3em);
}
#keyframes circle-chart-fill {
to { stroke-dasharray: 0 100;
}
} #keyframes circle-chart-appear {
to { opacity: 1; transform: translateY(0); }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<button v-on:click="changeProgress()">Change Progress - {{progress}}</button>
<div>
<svg-circle :progress="progress">
</svg-circle>
</div>
</div>

I need to automatically stop recording audio after 90 sec

I'm using jquery plugin from this site https://subinsb.com/html5-record-mic-voice and I have trouble with making audio recording stop after 90 sec
here is code, I'm not very good in JS, so thanks everyone who will help
var recLength = 0,
recBuffersL = [],
recBuffersR = [],
sampleRate;
this.onmessage = function(e) {
switch (e.data.command) {
case 'init':
init(e.data.config);
break;
case 'record':
record(e.data.buffer);
break;
case 'exportWAV':
exportWAV(e.data.type);
break;
case 'getBuffer':
getBuffer();
break;
case 'clear':
clear();
break;
}
};
function init(config) {
sampleRate = config.sampleRate;
}
function record(inputBuffer) {
recBuffersL.push(inputBuffer[0]);
recBuffersR.push(inputBuffer[1]);
recLength += inputBuffer[0].length;
}
function exportWAV(type) {
var bufferL = mergeBuffers(recBuffersL, recLength);
var bufferR = mergeBuffers(recBuffersR, recLength);
var interleaved = interleave(bufferL, bufferR);
var dataview = encodeWAV(interleaved);
var audioBlob = new Blob([dataview], {
type: type
});
this.postMessage(audioBlob);
}
function getBuffer() {
var buffers = [];
buffers.push(mergeBuffers(recBuffersL, recLength));
buffers.push(mergeBuffers(recBuffersR, recLength));
this.postMessage(buffers);
}
function clear() {
recLength = 0;
recBuffersL = [];
recBuffersR = [];
}
function mergeBuffers(recBuffers, recLength) {
var result = new Float32Array(recLength);
var offset = 0;
for (var i = 0; i < recBuffers.length; i++) {
result.set(recBuffers[i], offset);
offset += recBuffers[i].length;
}
return result;
}
function interleave(inputL, inputR) {
var length = inputL.length + inputR.length;
var result = new Float32Array(length);
var index = 0,
inputIndex = 0;
while (index < length) {
result[index++] = inputL[inputIndex];
result[index++] = inputR[inputIndex];
inputIndex++;
}
return result;
}
function floatTo16BitPCM(output, offset, input) {
for (var i = 0; i < input.length; i++, offset += 2) {
var s = Math.max(-1, Math.min(1, input[i]));
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
}
function writeString(view, offset, string) {
for (var i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
function encodeWAV(samples) {
var buffer = new ArrayBuffer(44 + samples.length * 2);
var view = new DataView(buffer);
/* RIFF identifier */
writeString(view, 0, 'RIFF');
/* RIFF chunk length */
view.setUint32(4, 36 + samples.length * 2, true);
/* RIFF type */
writeString(view, 8, 'WAVE');
/* format chunk identifier */
writeString(view, 12, 'fmt ');
/* format chunk length */
view.setUint32(16, 16, true);
/* sample format (raw) */
view.setUint16(20, 1, true);
/* channel count */
view.setUint16(22, 2, true);
/* sample rate */
view.setUint32(24, sampleRate, true);
/* byte rate (sample rate * block align) */
view.setUint32(28, sampleRate * 4, true);
/* block align (channel count * bytes per sample) */
view.setUint16(32, 4, true);
/* bits per sample */
view.setUint16(34, 16, true);
/* data chunk identifier */
writeString(view, 36, 'data');
/* data chunk length */
view.setUint32(40, samples.length * 2, true);
floatTo16BitPCM(view, 44, samples);
return view;
}
//------I guess problem is below--------------------------------------------
function restore() {
$("#record,#live").removeClass("disabled");
$(".one").addClass("disabled");
$.voice.stop();
}
$(document).ready(function() {
$(document).on("click", "#record:not(.disabled)", function() {
$("#ex1").css({
opacity: 1
});
$("#prepare").animate({
backgroundPosition: '0px,0px,0px,0px'
}, 1000).queue(function() {
$("#rec-progress").animate({
backgroundPosition: '0px,0px,0px,0px'
}, 5000);
/*I need to stop recording somewere here, I tried put .delay(30000).stop,
and things like that, but there were wrong ideas*/
elem = $(this);
$.voice.record($("#live").is(":checked"), function() {
elem.addClass("disabled");
$("#live").addClass("disabled");
$("#stop,#play,#download").removeClass("disabled");
});
});
});
$(document).on("click", "#stop:not(.disabled)", function() {
restore();
});
$(document).on("click", "#play:not(.disabled)", function() {
$.voice.export(function(url) {
$("#audio").attr("src", url);
$("#audio")[0].play();
}, "URL");
restore();
});
$(document).on("click", "#download:not(.disabled)", function() {
$.voice.export(function(url) {
$("<a href='" + url + "' download='MyRecording.wav'></a>")[0].click();
}, "URL");
restore();
});
});
* {
margin 0px;
padding: 0px;
}
h2 {
font-family: Tahoma, arial;
font-size: 2em;
width: 500px;
display: block;
margin-left: 50%;
left: -250px;
position: relative;
text-align: center;
}
body {
background: grey;
}
#progress-bar {
height: 100px;
width: 00px;
margin-left: 50%;
z-index: 10;
left: -350px;
position: relative;
}
#prepare {
background: url(https://disk.yandex.ru/preview/?id=/disk/bar.png&size=800x);
z-index: -10;
overflow: hidden;
height: 100px;
background-position: -610px, 0px, 0px, 0px;
background-repeat: no-repeat;
width: 700px;
position: absolute;
top: 0px
}
#rec-progress {
background: url(https://disk.yandex.ru/preview/?id=/disk/rec.png&size=800x);
z-index: -5;
overflow: hidden;
height: 100px;
background-position: -610px, 0px, 0px, 0px;
background-repeat: no-repeat;
width: 700px;
position: absolute;
top: 0px
}
#interface {
width: 596px;
height: 100px;
margin-left: 50%;
left: -315px;
position: relative;
}
#content {
height: 1000px;
}
#ex1 {
background: url(ex1/type1.bmp);
width: 100%;
min-width: 1340px;
min-height: 200px;
background-size: contain;
background-repeat: no-repeat;
opacity: 0;
}
.button {
width: 120px;
display: inline-block;
vertical-align: middle;
margin: 0px auto;
padding: 5px 12px;
cursor: pointer;
outline: none;
font-size: 13px;
text-decoration: none !important;
text-align: center;
color: #fff;
background-color: #4D90FE;
background-image: linear-gradient(top, #4D90FE, #4787ED);
background-image: -ms-linear-gradient(top, #4D90FE, #4787ED);
background-image: -o-linear-gradient(top, #4D90FE, #4787ED);
background-image: linear-gradient(top, #4D90FE, #4787ED);
border: 1px solid #4787ED;
box-shadow: 0 1px 3px #BFBFBF;
}
a.button {
color: #fff;
}
.button:hover {
box-shadow: inset 0px 1px 1px #8C8C8C;
}
.button.disabled {
box-shadow: none;
opacity: 0.7;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="http://lab.subinsb.com/projects/jquery/core/jquery-2.1.1.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="cdn/recorder.js"></script>
<script src="http://lab.subinsb.com/projects/jquery/voice/jquery.voice.min.js"></script>
<link rel="stylesheet" type="text/css" href="cdn/main.css">
<script src="cdn/jquery.backgroundpos.min.js"></script>
<script src="cdn/record.js"></script>
<style src="cdn/main.css"></style>
<title>Title</title>
</head>
<body>
<div id="content">
<h2>Example</h2>
<div id="progress-bar">
<img src="https://disk.yandex.ru/preview/?id=/disk/progress.png&size=800x">
<div id="rec-progress"></div>
<div id="prepare"></div>
</div>
<div id="interface">
<a class="button" id="record">Start</a>
<a class="button disabled one" id="stop">restart</a>
<a class="button disabled one" id="play">listen</a>
<a class="button disabled one" id="download">save</a>
</div>
<div id="ex1"></div>
</div>
<audio controls src="" id="audio"></audio>
</body>
</html>

Resources