Created
January 8, 2013 05:06
-
-
Save netpoetica/4481375 to your computer and use it in GitHub Desktop.
In Paper.js, use importSVG to import an svg node to a useable symbol and remove it from the DOM.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>Paperjs - ImportSVG to Symbol</title> | |
<script type="text/javascript" src="js/lib/paper.js"></script> | |
<script type="text/paperscript" canvas="canvas"> | |
function SVGSymbol(elem){ | |
var a = new Symbol(paper.project.importSvg(elem)); | |
elem.parentNode.removeChild(elem); | |
return a; | |
} | |
var a = SVGSymbol(document.getElementById('svg')); | |
var n = 20; | |
while(n--){ | |
var p = a.place(); | |
p.position = new Point(n * 40, 100); | |
p.scale(0.25 + Math.random() * 0.75); | |
} | |
</script> | |
</head> | |
<body> | |
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" style="width:500px; height:1000px;" id="svg"> | |
<circle cx="40" cy="300" r="20" style="fill:orange;stroke:yellow;stroke-width:2px;stroke-dasharray:3px" id="circle1" /> | |
</svg> | |
<canvas id="canvas" width="595px" height="841px"></canvas> | |
</body> | |
</html> |
sol0mka - the issue I had was that SVG was improperly capitalized. It worked when I used 'importSVG' instead of 'importSvg'
Thank you @paulate, very helpful. It's a shame the source code has not been updated.
Sorry Derek, but this is a very old Gist! I suspect that there is a difference between the old version and new versions of paperjs, and unfortunately when I wrote this I didn't specify the paperjs version. I haven't used PaperJS in a long while, wouldn't even know where to begin with this now!
Thanks for your reply @netpoetica, sorry if I sounded rude. I appreciate your very useful original post. All the best Derek.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! What version of paperjs do you use with this snippet? I've got an error Uncaught TypeError: Object [object Object] has no method 'importSvg' . Tanks!