Last active
August 11, 2024 20:47
-
-
Save eeropic/ffc0f06ad2dd10cad6b66481eb47e153 to your computer and use it in GitHub Desktop.
Paste SVG to Paper.js from clipboard
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
//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(); | |
} | |
}) |
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
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