-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
71 lines (62 loc) · 2.83 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>svg-break-apart</title>
<style>
body {
background: white;
color: #333;
display: flex;
font-family: Verdana, Arial, sans-serif;
}
img {
height: 256px;
width: 256px;
}
.col {
width: 50%;
}
</style>
<script src="./path-data-polyfill.js"></script>
</head>
<body>
<div class="col left">
<h2><a href="http://game-icons.net/lorc/originals/falling-blob.html">Original SVG</a></h2>
<p>Composed of two paths: a black background square and a white drawing.</p>
<svg id="original" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="height: 512px; width: 512px;">
<path class="bg" fill="#000" d="M0 0h512v512H0z"></path>
<path class="fg" fill="#fff" d="M227.54 25.87l128.997 177.28-219.59-169.19L259.07 198.92 20.78 26.128h-.005l180.06 232.19L35.878 129.186l169.19 225.433L20.17 219.052l210.703 228.224c59.994 56.81 154.88 55.822 213.664-2.963 58.782-58.783 59.77-153.668 2.962-213.664L227.54 25.87zm128.448 222.855c27.394 0 54.786 10.45 75.686 31.35 41.8 41.8 41.8 109.574 0 151.374s-109.573 41.8-151.373 0c-41.8-41.802-41.8-109.575 0-151.376 20.9-20.9 48.295-31.35 75.688-31.35zm-6.734 20.734c-17.067.057-36.98 7.253-52.576 20.872-13.134 11.468-20.463 25.015-22.217 36.897-1.753 11.88 1.438 21.776 10.216 29.11 17.555 14.667 55.232 11.185 81.5-11.752 13.134-11.47 20.462-25.015 22.217-36.897 1.754-11.88-1.438-21.776-10.215-29.11-7.132-5.96-17.247-9.16-28.924-9.12zm47.572 124.368c-8.597.03-19.063 3.706-27.14 10.65-6.803 5.848-10.36 12.6-11.172 18.01-.812 5.41.45 9.185 4.314 12.364 7.73 6.357 27.337 5.207 40.942-6.49 6.802-5.846 10.36-12.597 11.17-18.008.813-5.412-.45-9.187-4.313-12.366-3.14-2.582-7.918-4.18-13.8-4.16z"></path>
</svg>
</div>
<div class="col right">
<h2>Broken-apart SVG</h2>
<p>The foreground path has been automatically broken apart in several paths with random colors.</p>
</div>
<script>
(() => {
// split the fg pathData on each M segment
// polyfill is needed for now (Feb 2017)
const randomColor = () => '#'+('00000'+(Math.random()*(1<<24)|0).toString(16)).slice(-6)
const ba = document.getElementById('original').cloneNode(true)
document.querySelector('.right').appendChild(ba)
const fg = ba.querySelector('.fg')
// normalize should be used to get back absolute segments
const pathsDatas = fg.getPathData({ normalize: true }).reduce((acc, seg) => {
let pathData = seg.type === 'M' ? [] : acc.pop()
seg.values = seg.values.map(v => Math.round(v * 1000) / 1000)
pathData.push(seg)
acc.push(pathData)
return acc
}, [])
fg.remove()
pathsDatas.forEach((d) => {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
path.setPathData(d)
path.setAttribute('fill', randomColor())
ba.appendChild(path)
})
})()
</script>
</body>
</html>