Skip to content

Instantly share code, notes, and snippets.

@eeropic
Last active August 11, 2024 20:47
Show Gist options
  • Save eeropic/ffc0f06ad2dd10cad6b66481eb47e153 to your computer and use it in GitHub Desktop.
Save eeropic/ffc0f06ad2dd10cad6b66481eb47e153 to your computer and use it in GitHub Desktop.
Paste SVG to Paper.js from clipboard
//CC2018
document.addEventListener('paste', function(evt) {
//Import SVG copied to clipboard from Illustrator
//remove last hidden character that will otherwise break the import
if(document.activeElement.nodeName!="TEXTAREA"){
var str=evt.clipboardData.getData('text/plain').slice(0, -1);
var svg=project.importSVG(str)
svg.clipped=false;
svg.children[0].remove()
svg.parent.insertChildren(svg.index,svg.removeChildren());
svg.remove();
}
})
//CC2019
document.addEventListener('paste', function(evt) {
if(document.activeElement.nodeName!="TEXTAREA"){
var svg=project.importSVG( evt.clipboardData.getData('text/plain') )
svg.clipped=false;
svg.children[0].remove()
svg.parent.insertChildren(svg.index,svg.removeChildren());
svg.remove();
}
})
@eeropic
Copy link
Author

eeropic commented Jan 28, 2018

It seems that the Illustrator artboard information is not carried over / the top-leftmost object
defines the point 0,0 - workaround is to create a dummy rectangle with no fill & stroke

@eeropic
Copy link
Author

eeropic commented Jan 29, 2018

document.addEventListener('copy', function (e) {
//don't block the ace editor copy/paste
if(document.activeElement.nodeName!="TEXTAREA"){
e.preventDefault();
var projectSVG=project.exportSVG({asString:true,precision:3})
if (e.clipboardData) {
e.clipboardData.setData('text/plain', projectSVG);
} else if (window.clipboardData) {
window.clipboardData.setData('Text', projectSVG);
}
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment