Last active
December 10, 2020 17:50
-
-
Save subzey/822b901f2bbd9c4b05dbf704e7677c35 to your computer and use it in GitHub Desktop.
Svg use & canvas path2d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
svg, canvas { outline: black 1px dotted } | |
</style> | |
</head> | |
<body> | |
<h2>SVG <use></h2> | |
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="100" width="120"><use xlink:href="tv-heart.svg#heart"/></svg> | |
<h2>Canvas Path2D</h2> | |
<canvas height="100" width="120"></canvas> | |
<script> | |
const svg = document.querySelector('svg'); | |
requestAnimationFrame(function frame(now) { | |
svg.style.color = `hsl(${ (now / 10) % 360 }, 100%, 50%)`; | |
svg.style.transform = `rotate(${ now / 100}deg)`; | |
requestAnimationFrame(frame); | |
}); | |
(async function() { | |
const res = await fetch('tv-heart.svg'); | |
if (!res.ok) { throw new Error('pas glop!'); } | |
const doc = new DOMParser().parseFromString(await res.text(), "text/xml"); | |
const el = doc.querySelector('#heart path'); | |
const path2d = new Path2D(el.getAttribute('d')); | |
const ctx = document.querySelector('canvas').getContext('2d'); | |
requestAnimationFrame(function frame(now) { | |
ctx.save(); | |
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | |
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2); | |
ctx.rotate(now / 100 / 360 * 2 * Math.PI); | |
ctx.translate(-ctx.canvas.width / 2, -ctx.canvas.height / 2); | |
ctx.fillStyle = `hsl(${ (now / 10) % 360 }, 100%, 50%)`; | |
ctx.fill(path2d); | |
ctx.restore(); | |
requestAnimationFrame(frame); | |
}); | |
})(); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<svg viewBox="0 0 120 100" height="100" width="120" xmlns="http://www.w3.org/2000/svg"> | |
<g id="heart" fill="currentcolor"> | |
<path d="M111.273 12.393A36.287 36.287 0 0 0 83.907 0c-8.79 0-17.314 3.34-23.84 9.083C53.674 3.206 45.15 0 36.093 0 16.115 0 0 15.896 0 35.532a34.747 34.747 0 0 0 5.915 19.443l1.913 2.292 26.619-23.92a9.09 9.09 0 1 1 17.459.43L68.45 48.253a9.055 9.055 0 0 1 3.878-.866c1.173 0 2.293.222 3.322.626zm4.129 6.156L80.794 53.157a9.09 9.09 0 1 1-17.132.57L47.116 39.25a9.055 9.055 0 0 1-3.878.866 9.053 9.053 0 0 1-4.277-1.067L12.8 62.548c.43.355.87.7 1.319 1.036L57.536 97.38c.666.535 1.598.802 2.397.802s1.732-.267 2.398-.802l44.484-34.464C115.205 56.104 120 46.086 120 35.532a34.86 34.86 0 0 0-2.347-12.609z"/> | |
</g> | |
</svg> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment