<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>svg | ユガラボ</title>
	<atom:link href="https://yugalab.net/archives/category/4000_technical/4006_svg/feed" rel="self" type="application/rss+xml" />
	<link>https://yugalab.net</link>
	<description>pursue the beauty with yuga lab.</description>
	<lastBuildDate>Thu, 17 Dec 2015 08:48:21 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>
	<item>
		<title>【SVG】raphael.jsを使ったらすんごい色々捗った</title>
		<link>https://yugalab.net/archives/1374</link>
					<comments>https://yugalab.net/archives/1374#comments</comments>
		
		<dc:creator><![CDATA[ユーリ]]></dc:creator>
		<pubDate>Thu, 16 Apr 2015 10:00:53 +0000</pubDate>
				<category><![CDATA[svg]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">http://yugalab.net/?p=1374</guid>

					<description><![CDATA[<p>こんにちは、ユーリです！ 少し前、SVGでアニメーションを行う記事を書きましたが、 その後の開発でブラウザ間の挙動の違いや、古いブラウザでうまく動かなかったりと色々と苦労いたしました。 うおー、やはり一定数の古い環境は切 [&#8230;]</p>
<p>The post <a href="https://yugalab.net/archives/1374">【SVG】raphael.jsを使ったらすんごい色々捗った</a> first appeared on <a href="https://yugalab.net">ユガラボ</a>.</p>]]></description>
										<content:encoded><![CDATA[<script src="/demo/js/raphael-min.js"></script>
<p>
こんにちは、ユーリです！<br>
少し前、<a href="http://yugalab.net/archives/1154" title="【備忘録】SVGのpathをJavaScriptでぬるぬる動かしたかった"><u>SVGでアニメーションを行う記事</u></a>を書きましたが、<br/>
その後の開発でブラウザ間の挙動の違いや、古いブラウザでうまく動かなかったりと色々と苦労いたしました。<br/>
<br>
<span class="l_green">うおー、やはり一定数の古い環境は切り捨てるしかないのかッ…！対応コードを書けば書くほど煩雑になってゆく！</span><br/>
<br/>
そこに現れた救世主！！<br />
SVG制御用ライブラリ…　<a href="http://raphaeljs.com/" target="_blank"><span class="f_red f150 under" style="background:#DADADA;">Raphael.js</span></a>　っ！！　
<span class="l_blue">↓さんぷる</span>
<div id="post20150415_example_1" style="width:100%;height:200px;" class="sample_run" rel="SVGraphaeljs-Rainbow">
	
</div>
<div class="fold_post">
自前のコードだけでの対応に限界を感じ、ライブラリを使用してめんどくさいところは全部吸収してもらうことにしました。<br/>
まさにうってつけのライブラリが見つかりました！<br/>
それが<span class="f_red bold">raphael.js</span>でございます。<br/><br/>
<br /><h3 class="section_title putit_caption l_green">raphael.jsのココがスゴイ！</h3><br />
<span class="f120 l_blue">■IEやsafqriの古いバージョンでも動作</span><br />
特に何も意識せずとも、raphael.jsからの処理を使ってSVGを操作すれば、古いブラウザでもしっかり表示され、animateも問題なく動作してくれます！<br />
svg要素を直接うごかしていたら、animateが動作しなかったり、変な位置に表示されたりしてしまい、たいへん苦労いたしました。<br />
<span class="f120 l_blue">■jQueryライクな記述で、直感的に要素や値を操作できる</span><br />
jQueryのような値の指定やメソッドチェーンが利用できます。<br/>
jQueryに慣れている方であればすんなり飲み込めるのではないでしょうか。<br/>
<span class="f120 l_blue">■基本的なeventをサポート</span><br />
マウスオーバーやクリックのような処理も、svgの要素単位で実装ができます。<br/>
svg要素はマウス判定の範囲に複雑な形状を利用できるため、リッチな動作を実現できます。<br/>
<br /><h3 class="section_title putit_caption l_green">サンプルコード</h3><br /><h4 class="section_subtitle putit_caption">SVG要素を作成し、図形を描画する</h4>
SVG要素の大きさを指定する方法はいろいろありますが、いろいろ試した結果、<br/>
<span class="under l_blue">divなどのブロック要素で任意の大きさを指定し、その中にsvgをwidth:100%、height:100%で配置する</span>のがいちばん安定しました。<br>
この方法の場合、あとからdiv要素の大きさが変わっても、中身のSVG要素もあわせて伸び縮みしてくれます。<br/>
viewboxでサイズ指定するとさらに堅牢です。<br/>
PCで閲覧している方は、このページを表示しているウィンドウ自体のサイズを変えてみていただけると伸び縮みに対応していることを確認できるハズです。<br/>
<pre class="brush:js highlight:[5,6,7]" title="svgの描画">
<div id="svg_wrap" style="width:100%;height:200px;">
</div>&lt;!--svg配置用のdiv要素--&gt;
&lt;script&gt;
$(function(){
	var paper = Raphael("svg_wrap","100%","100%");	//Raphael()でSVG要素を生成する
					//引数1:生成する対象要素のid　引数2:width 引数3:height
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	paper.add([		//図形を追加する
		{
			type	: "rect" ,	//四角形を追加
			x		: 10 ,		//X座標
			y		: 70 ,		//Y座標
			width	: 240 ,		//width
			height	: 120 ,		//height
			fill	: "#6E6" ,	//fill = 塗りつぶしの色
			stroke	: "#C33" ,	//stroke = 線の色
			"stroke-width": 5,	//線の太さ
		},
		{
			type	: "circle" ,//円を追加
			cx		: 280 ,		//X座標(circle)
			cy		: 40 ,		//Y座標(circle)
			r		: 80 ,		//半径
			fill	: "#AAF" ,	//fill = 塗りつぶしの色
			stroke	: "#66E" ,	//stroke = 線の色
			"stroke-width": 5,	//線の太さ
		}
	]);
	var tri = paper.path("M350,10 L350,170 L 530,90 Z");	//パスを追加する(三角形)
	tri.attr({	//パス(三角形)のプロパティを変更
		fill	: "#C77" ,
		stroke	: "#F33" ,
		opacity	: 0.80	,	//透明度
		"stroke-width" : 10, //線の太さ
	});
});
&lt;/script&gt;
</pre>
<br />
上記のコードで、このように表示されます。
<div id="post20150415_example_2" style="width:100%;height:200px;">
</div>
<br /><h4 class="section_subtitle putit_caption">クリックやマウスオーバーなど、イベントの処理を書く</h4>
SVGを表示できたらぐにぐに動かしたいですよね！<br/>
先ほどようにSVGを生成してから、その要素に対して<span class="l_blue">click、mouseover、mouseout</span>の処理を定義してみます。<br>
SVG要素が複雑な形をしていても、その形の通りに当たり判定が適応されます。<br/>
<pre class="brush:js highlight:[9,18,24]" title="イベント処理部分">
	var paper = Raphael("svg_wrap","100%","100%");
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	var path = paper.path("M548,95v8H349C347,130,326,150,300,150c-19,0-36-11-45-28c-8,16-25,28-45,28s-36-11-45-28c-8,16-25,28-45,28c-26,0-47-20-49.8-45H50v-8h20C72,70,93,50,120,50c19,0,36,11,45,28c8-16,25-28,45-28s36,11,45,28c8-16,25-28,45-28c26,0,47,20,49,45H548z"); //パスを追加する
	path.attr({	//パスのプロパティを変更
		fill	: "#AAA" ,
		stroke	: "#666" ,
		"stroke-width" : 3, //線の太さ
	});
	path.click(function(){	//クリック時の処理
		var r = Math.floor(Math.random () * 256);	//赤ランダム
		var g = Math.floor(Math.random () * 256);	//緑ランダム
		var b = Math.floor(Math.random () * 256);	//青ランダム
		var color = "rgb("+r+","+g+","+b+")";		//ランダム色生成
		path.attr({	//パスのプロパティを変更
			fill  : color,	//塗りつぶしの色を変える
		});
	});
	path.mouseover(function(){	//マウスオーバー時の処理
		path.attr({	//パスのプロパティを変更
			"stroke-width"  : 8,	//線の太さを変える
			opacity			: 0.8	//透明度を変える
		});
	});
	path.mouseout(function(){	//マウスアウト時の処理
		path.attr({	//パスのプロパティを変更
			"stroke-width"  : 3,	//線の太さを戻す
			opacity			: 1.0	//透明度を戻す
		});
	});
</pre>
上記のコードで、このように表示されます。マウスイベントに対応しています！
<div id="post20150415_example_3" style="width:100%;height:200px;" class="sample_run" rel="SVGraphaeljs-ODANGO-colors">
</div>
<br /><h4 class="section_subtitle putit_caption">アニメーションを行う</h4>
前回のSVGの記事で苦労してアニメーション処理を書きましたが、<span class="f_red">raphael.js</span>にかかれば、非常に簡単にアニメーション処理ができてしまいます；；<br/>
記述方法は<span class="l_blue">jQueryのanimate</span>とほぼ同じで、アニメーションさせるプロパティにsvg要素の色や座標を指定すればOKです。<br/>
<pre class="brush:js highlight:[2,5]" title="イベント処理部分">
	path.mouseover(function(){	//マウスオーバー時の処理
		path.animate({path:"M548,95v8H349c-54,7-23,2-49,2c-19.8,0-18,2-45,2c-25,2-25-1-45-1s-19,3-45-1c-27,5-25-1-45-1c-26,0-13,5-49-0.6H50v-8h20c31-2,23-1,49-1c19,0,17-11,45-0.8c25-3,25-3,45-3s22-3,45,3c29-4,25-1.2,45-1c26,0,27-0.7,49,3H548z"},250);	//パスの形を変更
	});
	path.mouseout(function(){	//マウスアウト時の処理
		path.animate({path:"M548,95v8H349C347,130,326,150,300,150c-19,0-36-11-45-28c-8,16-25,28-45,28s-36-11-45-28c-8,16-25,28-45,28c-26,0-47-20-49-45H50v-8h20C72,70,93,50,120,50c19,0,36,11,45,28c8-16,25-28,45-28s36,11,45,28c8-16,25-28,45-28c26,0,47,20,49,45H548z"},100);	//パスの形を戻す
	});
</pre>
マウスオーバーでアニメーションします！
<div id="post20150415_example_4" style="width:100%;height:200px;" class="sample_run" rel="SVGraphaeljs-ODANGO-eat">
</div>
この前の苦労はなんだったのか…。<br/>
<br /><h4 class="section_subtitle putit_caption">集合要素に対して処理を行う</h4>
要素をたくさん並べて処理をしたい場合、<span class="l_blue f120">set</span>という配列のようなクラスを使用して、<span class="l_blue">forEach</span>でそれぞれの要素に対してまとめて処理を行うことができます。<br/>
<pre class="brush:js highlight:[9]" title="forEachでまとめて処理">
	var paper = Raphael("svg_wrap","100%","100%");
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	var rectarray = paper.set();		//svg要素の集合を格納する"set"を作成
	for( var i = 0, j = 20; i < j ; i++ ){	//20回繰り返す
		rectarray.push(						//setに対してsvg要素を追加する
			paper.rect(i*(j+10),100,20,80)	//paperの中に四角形を作成(x,y,w,h)
		);
	}
	rectarray.forEach(function(e,i){		//setの中身の要素それぞれに対して処理(element,index)
		e.attr({	//プロパティを変更
			fill	: "#8CC" ,	//塗りつぶしの色
		});
		e.mouseover(function(){	//マウスオーバー時の処理
			if( e.attr("y") == 0 ){	//yの値が0の場合
				e.animate({y:100,fill:"#88C"},200,"<>");	//下に移動するアニメーション
			}else{					//yの値が0でない場合
				e.animate({y:0,fill:"#C88"},200,"<>");	//上に移動するアニメーション
			}
		});
	});
</pre>
マウスオーバーでアニメーションします！
<div id="post20150415_example_5" style="width:100%;height:200px;" class="sample_run" rel="SVGraphaeljs-DOMINO">
</div>
やっぱりSVGはなめらかでたのしい！<br/>
これくらい手軽に扱えれば、どんどん夢が広がっていきます！<br/>

</div>
<script>
var post20150415_example_1_svg,post20150415_example_1_children;
var post20150415_example_width = 624;
var post20150415_example_height = 150;
var post20150415_example_funcount = 0;
$(function(){
	post20150415_example_1_svg = Raphael("post20150415_example_1","100%","100%");
	post20150415_example_1_svg.setViewBox(0,0,post20150415_example_width,post20150415_example_height,true);
	post20150415_example_1_children = post20150415_example_1_svg.set();
	for( var i = 0, j = 100; i < j ; i++ ){
		var rect_w = post20150415_example_width / j;
		var rect_h = post20150415_example_height / 2;
		post20150415_example_1_children.push(
			post20150415_example_1_svg.rect(rect_w*i,rect_h/2,rect_w,rect_h)
		);
	}
	post20150415_paintset(post20150415_example_1_children);
	$(window).load(function(){
		post20150415_animateset(post20150415_example_1_children);
		post20150415_eventset(post20150415_example_1_children);
		$(window).bind("focus",function(){
			post20150415_example_1_children.forEach(function(e,i){
				var rect_w = post20150415_example_width / 100;
				var rect_h = post20150415_example_height / 2;
				e.stop();
				e.attr({x:rect_w*i,y:rect_h*0.5,width:rect_w,height:rect_h});
			});
			post20150415_animateset(post20150415_example_1_children);
			post20150415_example_funcount = 0;
		});
		post20150415_example1();	//サンプル1を起動
		post20150415_example2();	//サンプル2を起動
		post20150415_example3();	//サンプル3を起動
		post20150415_example4();	//サンプル4を起動
	});
	
	
});

function post20150415_example1(){
	var paper = Raphael("post20150415_example_2","100%","100%");	//Raphael()でSVG要素を生成する
					//引数1:生成する対象要素のid　引数2:width 引数3:height
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	paper.add([		//図形を追加する
		{
			type	: "rect" ,	//四角形を追加
			x		: 10 ,		//X座標
			y		: 70 ,		//Y座標
			width	: 240 ,		//width
			height	: 120 ,		//height
			fill	: "#6E6" ,	//fill = 塗りつぶしの色
			stroke	: "#C33" ,	//stroke = 線の色
			"stroke-width": 5,	//線の太さ
		},
		{
			type	: "circle" ,//円を追加
			cx		: 280 ,		//X座標(circle)
			cy		: 40 ,		//Y座標(circle)
			r		: 80 ,		//半径
			fill	: "#AAF" ,	//fill = 塗りつぶしの色
			stroke	: "#66E" ,	//stroke = 線の色
			"stroke-width": 5,	//線の太さ
		}
	]);
	var tri = paper.path("M350,10 L350,170 L 530,90 Z");	//パスを追加する(三角形)
	tri.attr({	//パス(三角形)のプロパティを変更
		fill	: "#C77" ,
		stroke	: "#F33" ,
		opacity	: 0.80	,	//透明度
		"stroke-width" : 10, //線の太さ
	});
}
function post20150415_example2(){
	var paper = Raphael("post20150415_example_3","100%","100%");
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	var path = paper.path("M548,95v8H349C347,130,326,150,300,150c-19,0-36-11-45-28c-8,16-25,28-45,28s-36-11-45-28c-8,16-25,28-45,28c-26,0-47-20-49-45H50v-8h20C72,70,93,50,120,50c19,0,36,11,45,28c8-16,25-28,45-28s36,11,45,28c8-16,25-28,45-28c26,0,47,20,49,45H548z"); //パスを追加する
	path.attr({	//パスのプロパティを変更
		fill	: "#AAA" ,
		stroke	: "#666" ,
		"stroke-width" : 3, //線の太さ
	});
	path.click(function(){	//クリック時の処理
		var r = Math.floor(Math.random () * 256);	//赤ランダム
		var g = Math.floor(Math.random () * 256);	//緑ランダム
		var b = Math.floor(Math.random () * 256);	//青ランダム
		var color = "rgb("+r+","+g+","+b+")";		//ランダム色生成
		path.attr({	//パスのプロパティを変更
			fill  : color,	//塗りつぶしの色を変える
		});
	});
	path.mouseover(function(){	//マウスオーバー時の処理
		path.attr({	//パスのプロパティを変更
			"stroke-width"  : 8,	//線の太さを変える
			opacity			: 0.8	//透明度を変える
		});
	});
	path.mouseout(function(){	//マウスアウト時の処理
		path.attr({	//パスのプロパティを変更
			"stroke-width"  : 3,	//線の太さを戻す
			opacity			: 1.0	//透明度を戻す
		});
	});
}
function post20150415_example3(){
	var paper = Raphael("post20150415_example_4","100%","100%");
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	var path = paper.path("M548,95v8H349C347,130,326,150,300,150c-19,0-36-11-45-28c-8,16-25,28-45,28s-36-11-45-28c-8,16-25,28-45,28c-26,0-47-20-49-45H50v-8h20C72,70,93,50,120,50c19,0,36,11,45,28c8-16,25-28,45-28s36,11,45,28c8-16,25-28,45-28c26,0,47,20,49,45H548z"); //パスを追加する
	path.attr({	//パスのプロパティを変更
		fill	: "#ED842E" ,
		stroke	: "#321C00" ,
		"stroke-width" : 3, //線の太さ
	});
	path.mouseover(function(){	//マウスオーバー時の処理
		path.animate({path:"M548,95v8H349c-54,7-23,2-49,2c-19.8,0-18,2-45,2c-25,2-25-1-45-1s-19,3-45-1c-27,5-25-1-45-1c-26,0-13,5-49-0.6H50v-8h20c31-2,23-1,49-1c19,0,17-11,45-0.8c25-3,25-3,45-3s22-3,45,3c29-4,25-1.2,45-1c26,0,27-0.7,49,3H548z"},250);	//パスの形を変更
	});
	path.mouseout(function(){	//マウスアウト時の処理
		path.animate({path:"M548,95v8H349C347,130,326,150,300,150c-19,0-36-11-45-28c-8,16-25,28-45,28s-36-11-45-28c-8,16-25,28-45,28c-26,0-47-20-49-45H50v-8h20C72,70,93,50,120,50c19,0,36,11,45,28c8-16,25-28,45-28s36,11,45,28c8-16,25-28,45-28c26,0,47,20,49,45H548z"},100);	//パスの形を戻す
	});
}
function post20150415_example4(){
	var paper = Raphael("post20150415_example_5","100%","100%");
	paper.setViewBox(0,0,600,200,true);	//viewBoxの大きさを指定
	var rectarray = paper.set();		//svg要素の集合を格納する"set"を作成
	for( var i = 0, j = 20; i < j ; i++ ){	//20回繰り返す
		rectarray.push(						//setに対してsvg要素を追加する
			paper.rect(i*(j+10),100,20,80)	//paperの中に四角形を作成(x,y,w,h)
		);
	}
	rectarray.forEach(function(e,i){		//setの中身の要素それぞれに対して処理(element,index)
		e.attr({	//プロパティを変更
			fill	: "#8CC" ,	//塗りつぶしの色
		});
		e.mouseover(function(){	//マウスオーバー時の処理
			if( e.attr("y") == 0 ){	//yの値が0の場合
				e.animate({y:100,fill:"#88C"},200,"<>");	//下に移動するアニメーション
			}else{					//yの値が0でない場合
				e.animate({y:0,fill:"#C88"},200,"<>");		//上に移動するアニメーション
			}
		});
	});
}

function post20150415_paintset(set){
	var r = 240;
	var g = 90;
	var b = 80;
	var cnt = -1;
	set.forEach(function(e,i){
		cnt ++;
		if( cnt <= 15 ){		b+= 10;	
		}else if( cnt <= 30 ){	r-= 10;
		}else if( cnt <= 45 ){	g+= 10;
		}else if( cnt <= 60 ){	b-= 10;
		}else if( cnt <= 75 ){	r+= 10;
		}else{	g-= 10;
		}if( cnt >= 90 ){	cnt = -1;	}
		var color = "rgb("+r+","+g+","+b+")";	//れいんぼーー！
		e.attr({
			stroke	: color	,
			fill	: color ,
			opacity	: 0.85	,
			"stroke-width" : 1,
		});
		e.node.setAttribute('class','post20150415_rect');
	});
	$(".post20150415_rect").css("cursor","pointer");
}
function post20150415_animateset(set){
	set.forEach(function(e,i){
		var updn = function(){
			if( post20150415_example_funcount != 0 ){return false};
			if( e.attr("y") == 0 ){
				e.animate({y:50},3000,"<>",updn);
			}else{
				e.animate({y:0},3000,"<>",updn);
			}
		};
		setTimeout(updn,i*35);
	});
}
function post20150415_eventset(set){
	set.forEach(function(e,i){
		e.mouseover(function(){
			if(post20150415_example_funcount!=2){e.toFront();}
			e.animate({ "stroke-width" : 30 , opacity : 1.0}, 30);
		}).mouseout(function(){
			if(post20150415_example_funcount!=2){e.toBack();}
			e.animate({ "stroke-width" : 1  , opacity : 0.85}, 500);
		}).click(post20150415_rainbow);
	});
}
function post20150415_rainbow(){
	switch(post20150415_example_funcount){
		case 0:
			post20150415_example_1_children.forEach(function(e,i){
				var move = function(){
					e.stop().toFront();
					e.animate({ x : (i%10*62.4), 
								y : Math.floor(i*0.1)*15 ,
								width : 62.4 , height : 15,
								"stroke-width" : 1
								},250);
				};
				setTimeout(move,i*15);
			});
		break;
		case 1:
			post20150415_example_1_children.forEach(function(e,i){
				var move = function(){
					e.stop().toFront();
					e.animate({ x : 0, 
								y : i*1.5 ,
								width : 624 , height : 1.5,
								opacity	: 0.85, "stroke-width" : 1
								},250);
				};
				setTimeout(move,i*15);
			});
		break;
		case 2:
			post20150415_example_1_children.forEach(function(e,i){
				var move = function(){
					e.stop().toFront();
					e.animate({ x : (i*6.24%124.8)*5, 
								y : Math.floor(i*0.05)*30 ,
								width : 31.2 , height : 30,
								opacity	: 0.85, "stroke-width" : 1
								},250);
				};
				setTimeout(move,i*15);
			});
		break;
		case 3:
		default:
			post20150415_example_1_children.forEach(function(e,i){
				var rect_w = post20150415_example_width * 0.01;
				var rect_h = post20150415_example_height * 0.5;
				e.stop();
				var move = function(){
					e.stop().toFront();
					e.animate({ x : i*6.24, 
								y : 50 ,
								width : 6.24 , height : 75,
								opacity	: 0.85, "stroke-width" : 1
								},250,function(){
									if( i == 99 ){
										post20150415_example_funcount = 0;
										post20150415_animateset(post20150415_example_1_children);
									}
					});
				};
				setTimeout(move,i*15);
			});
		break;
	}
	post20150415_example_funcount++;
}
</script>
</p><p>The post <a href="https://yugalab.net/archives/1374">【SVG】raphael.jsを使ったらすんごい色々捗った</a> first appeared on <a href="https://yugalab.net">ユガラボ</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://yugalab.net/archives/1374/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>【備忘録】SVGのpathをJavaScriptでぬるぬる動かしたかった</title>
		<link>https://yugalab.net/archives/1154</link>
					<comments>https://yugalab.net/archives/1154#respond</comments>
		
		<dc:creator><![CDATA[ユーリ]]></dc:creator>
		<pubDate>Wed, 18 Mar 2015 08:10:45 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">http://yugalab.net/?p=1154</guid>

					<description><![CDATA[<p>ぬるんぬるん！ぐねんぐねん！ こちらは、最近流行りのSVG！gif画像ではありません。クリックして色を変えるなどの処理がリアルタイムで行えます。 IEやsafariなど一部のブラウザではうまく動作しないかもしれません。ゴ [&#8230;]</p>
<p>The post <a href="https://yugalab.net/archives/1154">【備忘録】SVGのpathをJavaScriptでぬるぬる動かしたかった</a> first appeared on <a href="https://yugalab.net">ユガラボ</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>
ぬるんぬるん！ぐねんぐねん！<br />
<div style="max-width:624px;max-height:200px;">
<svg version="1.1" id="svg_20150312_1" class="svg_20150312"  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 624 200" enable-background="new 0 0 624 200" xml:space="preserve">
<path fill="#8FC31F" d="M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z" onclick="this.style.fill=this.style.fill=='rgb(243, 207, 215)'?'#8FC31F':'#F3CFD7';" class="sample_run" rel="SVGぐにぐにTOP">
<animate dur="2.4s" attributeName="d" values="M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s72.1-49.3,99.3-49.3S152.7,101.4,152.7,128.7z;
M202.7,128.7c0,27.2-72.1,49.4-99.3,49.4s0.7-22.2,0.7-49.4s72.1-49.3,99.3-49.3S202.7,101.4,202.7,128.7z;
M222.7,158.7c0,27.2-52.1,19.4-79.3,19.4s-19.3-52.2-19.3-79.4s52.1-19.3,79.3-19.3S222.7,131.4,222.7,158.7z;
M316.7,65c0,27.2-132.1,109.4-159.3,109.4s10.7-62.2,10.7-89.4s82.1-59.3,109.3-59.3S316.7,37.7,316.7,65z;
M392.7,85c0,27.2-132.1,29.4-159.3,29.4s32.7-36.2,32.7-63.4s50.1-25.3,77.3-25.3S392.7,57.7,392.7,85z;
M447.7,84c0,27.2-94.1,13.4-121.3,13.4s-27.2-15.3-15.3-57.4s50.1-25.3,77.3-25.3S447.7,57.3,447.7,84.6z;
M498.7,78.6c0,27.2-24.1,63.4-51.3,63.4s-27.2-30.2-65.3-57.4s0.1-45.3,27.3-45.3 S498.7,51.3,498.7,78.6z;
M534.7,96.9c0,27.2-4.1,78.4-31.3,78.4s-27.2-32.2-67.3-59.4s-5.9-53.3,21.3-53.3 S534.7,69.7,534.7,96.9z;
M558.7,130.6c0,27.2-25.1,47.4-52.3,47.4s-55.3-15.2-55.3-42.4s4.1-47.3,31.3-47.3 S558.7,103.3,558.7,130.6z;
M559.7,130.6c0,27.2-25.1,47.4-52.3,47.4s-52.3-20.2-52.3-47.4s24.1-49.3,51.3-49.3 S559.7,103.3,559.7,130.6z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z"  repeatCount="indefinite" />
</path>
</svg>
</div>
こちらは、最近流行りのSVG！gif画像ではありません。<br />クリックして色を変えるなどの処理がリアルタイムで行えます。<br />
<span class="f080 f_red">IEやsafariなど一部のブラウザではうまく動作しないかもしれません。ゴメンナサイ。</span><br/>
これをjavascriptで制御しようとしたところ、いろいろ大変だった話。<br />

<div class="fold_post">
	<br />
	<span class="f_red f080">※この記事は、SVGが動作する環境の方へ向けて書いています。動かない方はゴメンナサイ。</span><br />
	<br />
	SVGは、ドットの集まりではなく、<span class="f_red">パス(点と線)</span>の集まりである為、解像度は無限大です！<br/>
	また、パスの値は数値で書かれていて、ページが表示されてからでも、パスの値を調節すれば、見た目をぐにぐに変えちゃったりもできます！<br/>
	ただし、現時点ではブラウザによって対応していない環境があったり、大きさの指定がややこしかったりと、いろいろ課題もございます。<br/>
	<br/>
	今回は、どうしてもSVGをぬるぬる動かしたい！という案件がございまして、工数はかかりましたが、なんとか要件を満たすことができました。<br/>
	拙いやり方でしたが、とりあえず備忘録として書いておきます。<br/>
	<br/>
	今回は、こちらのSVGを例にご説明いたします。<br/>
	<div style="max-width:180px;max-height:180px;">
	<svg version="1.1" class="svg_20150312" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve">
	<path fill="#8ABA29" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z"/>
	</svg>
	</div>
	このSVGのソースはこちらになります。<br/>
	<pre class="brush:html" title="SVGソース・緑円(パス)">
	&lt;svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve"&gt;
		&lt;path fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" &gt;
		&lt;/path&gt;
	&lt;/svg&gt;
	</pre>
	これを別のhtmlページにコピペすると、上に表示されている緑色の円がそっくりそのまま表示されるはずです。<br/>
	とてもシンプルな構造ですよね！<br/>
	<br/>
	このソースの中には、図形を表すための<span class="l_blue">図形の頂点の位置</span>、<span class="l_blue">図形の線の太さや塗りつぶしの色</span>、などが書かれています。<br/>
<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_1.jpg"><img fetchpriority="high" decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_1.jpg" alt="20150313_1" width="280" height="180" class="alignnone size-full wp-image-1195" /></a><br/>
	ブラウザはこれを読んで描画を行うわけです。<br/>
	例えば、この例の場合、<span class="l_blue">円の形(点と線の集まり)</span>を表しているのはこの部分です。↓<br/>
	<span class="l_blue f080">d=&#8221;M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z&#8221;</span><br/>
	そして、この中に書かれている点の位置を書きかえてしまえば、その場で表示を変えることができる、という寸法です。<br/>
	<br /><h3 class="section_title putit_caption l_green">ここまで前置き。詳しい説明に移ります。</h3><br /><br />
	SVG要素を動的に変更したい場合は、<span class="l_blue">animateタグを</span>使用する、もしくは<span class="l_blue">JavaScriptで直接値を打ち込む</span>、などの方法がございます。<br/>
	まずは、animateタグについて。<br/>
	animateタグの中身には、変更したい<span class="l_blue">要素名</span>と、<span class="l_blue">値</span>、<span class="l_blue">動作スピード</span>、<span class="l_blue">動作条件</span>などを記載します。<br/>
	<pre class="brush:html" title="animateタグの例">
		&lt;animate attributeName="fill" 
					values="#88BB33;#CC6699"
					dur="2.0s"
					repeatCount="indefinite" &gt;
	</pre>
	上記の例では、<br />
	<span class="l_blue">[attributeName]…「fill」</span>という<span class="l_red">要素</span>、この場合<span class="l_red">fill</span>なので<span class="l_red">塗りつぶしの色</span>を、<br />
	<span class="l_blue">[values]…「#88BB33;#CC6699」</span>すなわち<span class="l_red">#88BB33から#CC6699</span>に、<br />
	<span class="l_blue">[dur]…「2.0s」</span><span class="l_red">2秒間かけて変化</span>させる。という命令が書いてあります。<br/>
	また、繰り返し回数<span class="l_blue">[repeatCount]…「indefinite」</span>で、<span class="l_red">アニメーションを無限に繰り返す</span>という指定をしました。<br/>
	<br/>
	このタグをどこに書くかというと、アニメーションさせたいSVG要素の内側に書きます。<br/>
	こんな感じ。
	<pre class="brush:html highlight:[4,5,6,7]" title="animateタグ(fillの変化)">
	&lt;svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve"&gt;
		&lt;path fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" &gt;
			&lt;animate attributeName="fill" 
					values="#88BB33;#CC6699"
					dur="2.0s"
					repeatCount="indefinite" &gt;
		&lt;/path&gt;
	&lt;/svg&gt;
	</pre>
	<br/>
	これを実際に実行したものがこちら。
	<div style="max-width:180px;max-height:180px;">
	<svg version="1.1" class="svg_20150312" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve">
	<path fill="#8ABA29" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z">
		<animate attributeName="fill" values="#88BB33;#CC6699" dur="2.0s" repeatCount="indefinite">
	</path>
	</svg>
	</div>
	色が緑から赤にだんだんと変わっています。<br/>
	延々と変わっています。<br/>
	<br/>
	これくらいなら簡単な変化でありますが、この<span class="l_blue">animateタグは、pathの形状の変化にも使えてしまう</span>のです。<br/>
	<br />
	そのためには、pathについて<span class="l_blue">「動作前の状態」</span>と<span class="l_blue">「動作後の状態」</span>を用意する必要があるわけです。<br/>
	ということで<span class="l_blue">Adobe Illustrator</span>にてパス形状を編集する画面を例にいたします。<br/>
	<span class="l_gray">(SVGを書き出せるソフトウェアでしたら、他のものでも構いません。)</span><br/>
	こちらが、<span class="l_blue">動作前の状態</span>とします。<br />
<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_2.jpg"><img decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_2.jpg" alt="20150313_2" width="423" height="261" class="alignnone size-full wp-image-1196" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_2.jpg 423w, https://yugalab.net/wp-content/uploads/2015/03/20150313_2-300x185.jpg 300w" sizes="(max-width: 423px) 100vw, 423px" /></a><br/>
	次が、<span class="l_blue">動作後の状態</span>とします。<br />
<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_3.jpg"><img decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_3.jpg" alt="20150313_3" width="423" height="261" class="alignnone size-full wp-image-1197" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_3.jpg 423w, https://yugalab.net/wp-content/uploads/2015/03/20150313_3-300x185.jpg 300w" sizes="(max-width: 423px) 100vw, 423px" /></a><br/>
	それぞれ、<span class="l_blue">別名で保存</span>から<span class="l_blue">svgとして保存</span>します。<br />
<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_4.jpg"><img loading="lazy" decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_4.jpg" alt="20150313_4" width="423" height="261" class="alignnone size-full wp-image-1198" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_4.jpg 423w, https://yugalab.net/wp-content/uploads/2015/03/20150313_4-300x185.jpg 300w" sizes="auto, (max-width: 423px) 100vw, 423px" /></a><br/>
	そして、保存したSVGファイルをテキストエディタで開きます。<br />
	図形の形状を表すpathの中の<span class="l_blue">&#8220;d&#8221;の値</span>がそれぞれ違うことが確認できます。<br />
<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_5.jpg"><img loading="lazy" decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_5.jpg" alt="20150313_5" width="578" height="129" class="alignnone size-full wp-image-1199" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_5.jpg 578w, https://yugalab.net/wp-content/uploads/2015/03/20150313_5-300x66.jpg 300w" sizes="auto, (max-width: 578px) 100vw, 578px" /></a>
<br/>
	<span class="l_blue">動作前の&#8221;d&#8221;:<br/>
	<span class="f080">M<span class='f_red'>124</span>,89c0,27-<span class='f_red'>22</span>,49-<span class='f_red'>49</span>,49s-49-22-49-49S47,40,75,40.6S<span class='f_red'>124</span>,62,<span class='f_red'>124</span>,89z</span></span><br/>
	<span class="l_blue">動作後の&#8221;d&#8221;:<br/>
	<span class="f080">M<span class='f_red'>154</span>,89c0,27-<span class='f_red'>52</span>,49-<span class='f_red'>79</span>,49s-49-22-49-49S47,40,75,40.6S<span class='f_red'>154</span>,62,<span class='f_red'>154</span>,89z</span></span><br/>
	この&#8221;d&#8221;の中身の値を抽出して、animateタグに打ち込めば良いわけです！<br />
	animateタグの<span class="l_blue">[attributeName]</span>の中身を、図形の形状を表す<span class="l_blue">&#8220;d&#8221;</span>に変更し、<br/>
	実際に変化する値である、<span class="l_blue">[values]</span>の中身に動作前、動作後の<span class="l_blue">パス形状</span>を&#8221;;&#8221;セミコロンで区切って入力。<br/>
	こんなタグになりました。<br />
	<pre class="brush:html highlight:[4,5]" title="animateタグ(path / d の変化)">
	&lt;svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve"&gt;
		&lt;path fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" &gt;
			&lt;animate attributeName="d" 
					values="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z;
							M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z"
					dur="1.2s"
					repeatCount="indefinite" &gt;
		&lt;/path&gt;
	&lt;/svg&gt;
	</pre>
	動作させてみます。<br/>
	ぬるっと動きました！<br/>
	<div style="max-width:180px;max-height:180px;">
		<svg version="1.1" class="svg_20150312" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve">
    <path fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" >
        <animate attributeName="d"
                values="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z;M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z"
                dur="1.2s"
                repeatCount="indefinite" >
		</path>
		</svg>
	</div>
	しかし、このままでは延々とアニメーションしてしまいます。<br />
	そこで、アニメーションの動作を制御する<span class="l_blue">begin</span>要素を追加します。<br />
	<span class="l_blue">begin=&#8221;indefinite&#8221;</span>とすることで、animateの<span class="l_blue">動作タイミングをJavaScriptで制御</span>できます。<br />
	さらに繰り返し回数<span class="l_blue">[repeatCount]…「1」</span>と設定することで、動作回数を1度だけとします。<br/>
	そしてこのanimateタグにidを付与します。<br />
	また、この後の動作のためにpathのタグにもidを付与しておきます。<br />
	<pre class="brush:html highlight:[3,8,9,10]" title="animateタグ(path / d の変化)">
	&lt;svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve"&gt;
		&lt;path id="circle_path" 
			fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" &gt;
			&lt;animate attributeName="d" 
					values="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z;M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z"
					dur="1.2s"
					repeatCount="1" 
					begin="indefinite" 
					id="circle_animate" &gt;
		&lt;/path&gt;
	&lt;/svg&gt;
	</pre>
	そして、JavaScriptからidをもとにanimateタグをゲットし、<br />
	<span class="l_blue">beginElement();</span>を呼ぶことで、アニメーションを動作させることができます！<br />
	<pre class="brush:js" title="animateタグ(path / d の変化):JavaScript側">
		//アニメを動作させる
		var animate = document.getElementById('circle_animate');
		animate.beginElement();
	</pre>
	ボタンクリックで、<span class="l_blue">beginElement();</span>を呼ぶようにしてみました。<br/>
	<div style="max-width:180px;max-height:180px;">
		<svg version="1.1" class="svg_20150312" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve">
    <path fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" >
        <animate attributeName="d"
                values="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z;M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z"
                dur="1.2s"
                repeatCount="1"
				begin="indefinite" 
				id="post20150313_circle_animate">
		</path>
		</svg>
	</div>
	<div style="text-align:center;width:30%;">
		<span class="exsample_20150313_button sample_run" style="cursor:pointer;" onclick="document.getElementById('post20150313_circle_animate').beginElement();" rel="SVGぐにぐにボタンアニメ">
			クリック！
		</span>
	</div>
	<br/>
	クリックでアニメを動かすことに成功！ひとつ前に進んだ！<br/>
	<br />
	ただし、このままではアニメーションしても元に戻ってしまいます。<br />
	何故かというと、表示されている<span class="l_blue">SVGの形状は、pathの場合&#8221;d&#8221;の中身</span>で決まっていて、<br/>
	<span class="l_blue">アニメーションした後はその値による表示に戻ってしまう</span>ためです。<br/>
	そこで今度は、アニメーションしたあとに<span class="l_blue">path要素自体の&#8221;d&#8221;の値をJavaScriptで書き換えてしまう</span>ようにします。<br/>
	<pre class="brush:js highlight:[5,6]" title="animateタグ(path / d の変化):JavaScript側">
		//アニメを動作させる
		var animate = document.getElementById('circle_animate');
		animate.beginElement();
		//pathそのものの形状を変化させる
		var path = document.getElementById('circle_path');
		path.setAttribute('d','M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z');
	</pre>
	<div style="max-width:180px;max-height:180px;">
		<svg version="1.1" class="svg_20150312" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve">
    <path id="post20150313_circle_path" fill="#88BB33" d="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z" >
        <animate attributeName="d"
                values="M124,89c0,27-22,49-49,49s-49-22-49-49S47,40,75,40.6S124,62,124,89z;M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z"
                dur="1.2s"
                repeatCount="1"
				begin="indefinite" 
				id="post20150313_circle_animate_2">
		</path>
		</svg>
	</div>
	<div style="text-align:center;width:30%;">
		<span class="exsample_20150313_button sample_run" style="cursor:pointer;" onclick="document.getElementById('post20150313_circle_animate_2').beginElement();document.getElementById('post20150313_circle_path').setAttribute('d','M154,89c0,27-52,49-79,49s-49-22-49-49S47,40,75,40.6S154,62,154,89z');" rel="SVGぐにぐにボタンアニメその２">
			むくり！
		</span>
	</div>
	<br/>
	アニメ後も、要素の形状がそのままになりました！<br/>
	<br />
	ここで、疑問が生まれます。
	動作前と後で、<span class="l_blue">どんなパスを入れてもちゃんとアニメーションしてくれるのか？</span>という問題です。<br />
	ためしに、単純なパスと複雑なパスをanimateタグで繋げてみます。<br/>
	<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_6.jpg"><img loading="lazy" decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_6.jpg" alt="20150313_6" width="500" height="200" class="alignnone size-full wp-image-1200" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_6.jpg 500w, https://yugalab.net/wp-content/uploads/2015/03/20150313_6-300x120.jpg 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></a><br/><a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_7.jpg"><img loading="lazy" decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_7.jpg" alt="20150313_7" width="500" height="200" class="alignnone size-full wp-image-1201" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_7.jpg 500w, https://yugalab.net/wp-content/uploads/2015/03/20150313_7-300x120.jpg 300w" sizes="auto, (max-width: 500px) 100vw, 500px" /></a><br/>
	<pre class="brush:html highlight:[5]" title="animateタグ(path / d の変化テスト・複雑パス)">
	&lt;svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
	 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve"&gt;
		&lt;path fill="#88BB33" d="M150.8,93l-58,62.2L29.2,95.8C44,79,69.1,51.3,87.2,33.6L150.8,93z" &gt;
			&lt;animate attributeName="d" 
				values="M150.8,93l-58,62.2L29.2,95.8C44,79,69.1,51.3,87.2,33.6L150.8,93z;
						M112.9,90.5l16.5,32.3l-35.7-6.5L68.2,142l-4.9-36L30.9,89.6c13.9-5.7,19.4-9.5,32.7-15.7L69.2,38l25.1,26.3l35.8-5.8L112.9,90.5z"
                dur="1.2s"
                repeatCount="indefinite"" &gt;
		&lt;/path&gt;
	&lt;/svg&gt;
	</pre>
	<div style="max-width:180px;max-height:180px;">
		<svg version="1.1" class="svg_20150312" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
 y="0px" viewBox="0 0 180 180" enable-background="new 0 0 180 180" xml:space="preserve">
    <path fill="#88BB33" d="M150.8,93l-58,62.2L29.2,95.8C44,79,69.1,51.3,87.2,33.6L150.8,93z" >
        <animate attributeName="d"
                values="M150.8,93l-58,62.2L29.2,95.8C44,79,69.1,51.3,87.2,33.6L150.8,93z;M112.9,90.5l16.5,32.3l-35.7-6.5L68.2,142l-4.9-36L30.9,89.6c13.9-5.7,19.4-9.5,32.7-15.7L69.2,38l25.1,26.3
	l35.8-5.8L112.9,90.5z"
                dur="1.2s"
                repeatCount="indefinite" >
		</path>
		</svg>
	</div>
	
	あれ。ぬるぬるしない。<br />
	調査の結果、どうやら動作前と後で、<span class="l_blue">「パスを表す構成」が同じでなければうまく動いてくれない</span>ようです。<br />
	具体的には、<span class="l_blue">点の数</span>が変わったり、<span class="l_blue">座標の指定方法</span>が変わったりすると、ダメみたいです。<br />
	たとえば頂点が4つしか無い四角形から、頂点が5つある五角形に変形、ということはできないようです。<br />
	ただし、「頂点が5つだけど最初だけ四角形に見えるパス」などを用意すれば、その限りではありません。<br />
<a href="https://yugalab.net/wp-content/uploads/2015/03/20150313_8.jpg"><img loading="lazy" decoding="async" src="https://yugalab.net/wp-content/uploads/2015/03/20150313_8.jpg" alt="20150313_8" width="460" height="215" class="alignnone size-full wp-image-1206" srcset="https://yugalab.net/wp-content/uploads/2015/03/20150313_8.jpg 460w, https://yugalab.net/wp-content/uploads/2015/03/20150313_8-300x140.jpg 300w" sizes="auto, (max-width: 460px) 100vw, 460px" /></a><br/>
	<br />
	さて、脱線しましたが、animateタグは「動作前」と「動作後」だけでなく、<span class="l_blue">一つのタグでいくつも動作をつなげる</span>ことができます。<br />
	変化させたいパスを<span class="l_blue">&#8220;;&#8221;</span>セミコロンでどんどん繋げていけば、こんな動きもできちゃうわけです！<br />
	<div style="max-width:624px;max-height:200px;">
<svg version="1.1" id="svg_20150312_1" class="svg_20150312"  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 624 200" enable-background="new 0 0 624 200" xml:space="preserve">
<path fill="#8FC31F" d="M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z" onclick="this.style.fill=this.style.fill=='rgb(243, 207, 215)'?'#8FC31F':'#F3CFD7';" class="sample_run" rel="SVGぐにぐにフッタ">
<animate dur="2.4s" attributeName="d" values="M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s72.1-49.3,99.3-49.3S152.7,101.4,152.7,128.7z;
M202.7,128.7c0,27.2-72.1,49.4-99.3,49.4s0.7-22.2,0.7-49.4s72.1-49.3,99.3-49.3S202.7,101.4,202.7,128.7z;
M222.7,158.7c0,27.2-52.1,19.4-79.3,19.4s-19.3-52.2-19.3-79.4s52.1-19.3,79.3-19.3S222.7,131.4,222.7,158.7z;
M316.7,65c0,27.2-132.1,109.4-159.3,109.4s10.7-62.2,10.7-89.4s82.1-59.3,109.3-59.3S316.7,37.7,316.7,65z;
M392.7,85c0,27.2-132.1,29.4-159.3,29.4s32.7-36.2,32.7-63.4s50.1-25.3,77.3-25.3S392.7,57.7,392.7,85z;
M447.7,84c0,27.2-94.1,13.4-121.3,13.4s-27.2-15.3-15.3-57.4s50.1-25.3,77.3-25.3S447.7,57.3,447.7,84.6z;
M498.7,78.6c0,27.2-24.1,63.4-51.3,63.4s-27.2-30.2-65.3-57.4s0.1-45.3,27.3-45.3 S498.7,51.3,498.7,78.6z;
M534.7,96.9c0,27.2-4.1,78.4-31.3,78.4s-27.2-32.2-67.3-59.4s-5.9-53.3,21.3-53.3 S534.7,69.7,534.7,96.9z;
M558.7,130.6c0,27.2-25.1,47.4-52.3,47.4s-55.3-15.2-55.3-42.4s4.1-47.3,31.3-47.3 S558.7,103.3,558.7,130.6z;
M559.7,130.6c0,27.2-25.1,47.4-52.3,47.4s-52.3-20.2-52.3-47.4s24.1-49.3,51.3-49.3 S559.7,103.3,559.7,130.6z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z"  repeatCount="indefinite" />
</path>
</svg>
</div>
	<span class="f080 l_gray">↑クリックすると色が変わります(笑)</span><br />
	<pre class="brush:html" title="animateタグ(ながい)">
&lt;animate dur="2.4s" attributeName="d" 
values="M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s72.1-49.3,99.3-49.3S152.7,101.4,152.7,128.7z;
M202.7,128.7c0,27.2-72.1,49.4-99.3,49.4s0.7-22.2,0.7-49.4s72.1-49.3,99.3-49.3S202.7,101.4,202.7,128.7z;
M222.7,158.7c0,27.2-52.1,19.4-79.3,19.4s-19.3-52.2-19.3-79.4s52.1-19.3,79.3-19.3S222.7,131.4,222.7,158.7z;
M316.7,65c0,27.2-132.1,109.4-159.3,109.4s10.7-62.2,10.7-89.4s82.1-59.3,109.3-59.3S316.7,37.7,316.7,65z;
M392.7,85c0,27.2-132.1,29.4-159.3,29.4s32.7-36.2,32.7-63.4s50.1-25.3,77.3-25.3S392.7,57.7,392.7,85z;
M447.7,84c0,27.2-94.1,13.4-121.3,13.4s-27.2-15.3-15.3-57.4s50.1-25.3,77.3-25.3S447.7,57.3,447.7,84.6z;
M498.7,78.6c0,27.2-24.1,63.4-51.3,63.4s-27.2-30.2-65.3-57.4s0.1-45.3,27.3-45.3 S498.7,51.3,498.7,78.6z;
M534.7,96.9c0,27.2-4.1,78.4-31.3,78.4s-27.2-32.2-67.3-59.4s-5.9-53.3,21.3-53.3 S534.7,69.7,534.7,96.9z;
M558.7,130.6c0,27.2-25.1,47.4-52.3,47.4s-55.3-15.2-55.3-42.4s4.1-47.3,31.3-47.3 S558.7,103.3,558.7,130.6z;
M559.7,130.6c0,27.2-25.1,47.4-52.3,47.4s-52.3-20.2-52.3-47.4s24.1-49.3,51.3-49.3 S559.7,103.3,559.7,130.6z;
M152.7,128.7c0,27.2-22.1,49.4-49.3,49.4S54,155.9,54,128.7s22.1-49.3,49.3-49.3S152.7,101.4,152.7,128.7z"
repeatCount="indefinite" &gt;
	</pre>
	さらに、複雑な動作をさせたい方には、<span class="l_blue">keyTime、keySplines</span>という、アニメーションのスピードを細かく設定できる要素もあります！<br/>
	今回は少々手間のかかる方法でしたが、もっと簡単に動かせる仕組みが一般化していくとよいですね！
</div>
<style type="text/css">
<!--
.svg_20150312{
	width:100%;
	heifht:100%;
}
.exsample_20150313_button{
display:inline-block;
margin: 6px auto 6px auto;
padding:8px;
width:100%;
border:1px solid #666;
border-radius:6px;
background-color:#EEE;
box-shadow:-2px -2px 2px #999 inset;
}
.exsample_20150313_button:active{
box-shadow:2px 2px 2px #999 inset;
color:transparent;
text-shadow:2px 2px 0px #666;
}
.exsample_20150313_button:hover{
background-color:#F6F6F6;
}
-->
</style>
</p><p>The post <a href="https://yugalab.net/archives/1154">【備忘録】SVGのpathをJavaScriptでぬるぬる動かしたかった</a> first appeared on <a href="https://yugalab.net">ユガラボ</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://yugalab.net/archives/1154/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
