SVGアニメーション – javascriptを使う(8) – transform の scale 要素の動的変化
アニメーションとして取り扱えるSVGのプロパティの transform の scale を試してみます。
下記のサンプルを用意しました。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>テストHTML</title>
<style>
#recttest {
stroke: #000;
stroke-width: 2;
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg"
width = "500"
height = "250"
viewBox = "0 0 500 250"
>
<rect
id = "recttest"
x = "0"
y = "00"
width = "20"
height = "20"
fill = "#999"
/>
</svg>
<script>
let inter_value = 0;
let millisecond = 10;
let dom_rect = document.querySelector("#recttest");
setInterval(() => {
inter_value += 0.01;
dom_rect.setAttribute('transform', 'translate(10, 10) scale(' + inter_value + ')');
if (inter_value > 30) {
inter_value = 0;
}
}, millisecond)
</script>
</body>
</html>
画面にアクセスすると、SVGのrect要素が出力されます。
javascriptによる transform 要素の scale の動的変更で、SVG画像が拡大することが確認できます。