Перевод SMIL анимации в JS
Добрый день уважаемые, подскажите пожалуйста как перевести параметры из SMIL анимации в разряд JS анимации. Приведу пример:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <svg width="300" height="200"> <defs> <path id="base" d="M150 100m0-50a100 50 0 0 0 0 100a100 50 0 0 0 0-100z" fill="none"> <animate attributeName="stroke-dasharray" from="0 485" to="485 0" begin="0s" dur="2s"/> <animate attributeName="stroke-dashoffset" from="0" to="242.5" begin="0s" dur="2s"/> </path> </defs> <use xlink:href="#base" stroke="orange" stroke-width="14"/> <use xlink:href="#base" stroke="yellow" stroke-width="10"/> </svg> </body> </html> Необходимо что б по непарному клацанью мыши кольцо отрисовывалось, а при парном анимация уходила в обратном направлении (в SMIL анимации это просто поменять местами параметры from и to) как данное чудо организовать на JS ? Посоветуйте что то дельное :victory: |
Black_Star,
код обратной анимации можно? |
Конечно, вот
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<svg width="300" height="200">
<defs>
<path id="base" d="M150 100m0-50a100 50 0 0 0 0 100a100 50 0 0 0 0-100z" fill="none">
<animate attributeName="stroke-dasharray" from="485 0" to="0 485" begin="0s" dur="2s"/>
<animate attributeName="stroke-dashoffset" from="242.5" to="0" begin="0s" dur="2s"/>
</path>
</defs>
<use xlink:href="#base" stroke="orange" stroke-width="14"/>
<use xlink:href="#base" stroke="yellow" stroke-width="10"/>
</svg>
</body>
</html>
|
Black_Star,
как вариант, но предполагаю что js для этого не нужен
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function() {
var f = [["0 485","485 0"],["0","242.5"]];
$('svg').click(function() {
$('animate',this).each(function(indx, el){
var k = f[indx].reverse();
$(el).attr({from : k[0], to : k[1]})
});
})
});
</script>
</head>
<body>
<svg width="300" height="200" id="startButton">
<defs>
<path id="base" d="M150 100m0-50a100 50 0 0 0 0 100a100 50 0 0 0 0-100z" fill="none">
<animate attributeName="stroke-dasharray" from="0 485" to="485 0" begin="0s;startButton.click" dur="2s"/>
<animate attributeName="stroke-dashoffset" from="0" to="242.5" begin="0s;startButton.click" dur="2s"/>
</path>
</defs>
<use xlink:href="#base" stroke="orange" stroke-width="14"/>
<use xlink:href="#base" stroke="yellow" stroke-width="10"/>
</svg>
</body>
</html>
|
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
:hover {
animation-name: anim1a;
}
</style>
</head>
<body>
<svg width="300" height="200">
<defs>
<path id='base' d='M150 100m0-50a100 50 0 0 0 0 100a100 50 0 0 0 0-100z' fill='none'>
<animate attributeName='stroke-dasharray' from='485 10' to='10 485' begin='dblclick + anim2.end' dur='2s' fill='freeze' restart='whenNotActive' id='anim1' />
<animate attributeName='stroke-dasharray' from='10 485' to='485 10' begin='0s; (dblclick + anim1.end)' dur='2s' fill='freeze' restart='whenNotActive' id='anim2' />
<animate attributeName='stroke-dashoffset' from='242.5' to='0' begin='anim1.begin' dur='2s' fill='freeze' />
<animate attributeName='stroke-dashoffset' from='0' to='242.5' begin='anim2.begin' dur='2s' fill='freeze' />
</path>
</defs>
<use xlink:href="#base" stroke="orange" stroke-width="14"/>
<use xlink:href="#base" stroke="yellow" stroke-width="10"/>
</svg>
</body>
</html>
|
Спасибо, рони :thanks:
А есть ли возможность полностью перейти от SMIL к JS ?
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function() {
var f = [["0 485","485 0"],["0",to="242.5"]];
$('svg').click(function() {
$('animate',this).each(function(indx, el){
var k = f[indx].reverse();
$(el).attr({from : k[0], to : k[1]})
});
})
});
</script>
</head>
<body>
<svg width="300" height="200" id="startButton">
<defs>
<path id="base" d="M150 100m0-50a100 50 0 0 0 0 100a100 50 0 0 0 0-100z" fill="none">
<animate attributeName="stroke-dasharray" from="0 485" to="485 0" begin="0s;startButton.click" fill="freeze" dur="500ms"/>
<animate attributeName="stroke-dashoffset" from="0" to="242.5" begin="0s;startButton.click" fill="freeze" dur="500ms"/>
</path>
</defs>
<use xlink:href="#base" stroke="orange" stroke-width="14"/>
<use xlink:href="#base" stroke="yellow" stroke-width="10"/>
</svg>
</body>
</html>
|
Цитата:
|
Цитата:
|
Black_Star,
Библиотека http://snapsvg.io/ и примеры использования http://codepen.io/collection/edpyJ/ |
Спасибо :thanks:
|
| Часовой пояс GMT +3, время: 06:55. |