Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Created January 8, 2013 05:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save netpoetica/4481375 to your computer and use it in GitHub Desktop.
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.
<!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>
@legomushroom
Copy link

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!

@paulate
Copy link

paulate commented Feb 24, 2014

sol0mka - the issue I had was that SVG was improperly capitalized. It worked when I used 'importSVG' instead of 'importSvg'

@derek-collie
Copy link

Thank you @paulate, very helpful. It's a shame the source code has not been updated.

@netpoetica
Copy link
Author

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!

@derek-collie
Copy link

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