Last active
March 31, 2024 04:52
-
-
Save Ashung/d572916352dc13570222a98b499b3869 to your computer and use it in GitHub Desktop.
Figma Snippets
This file contains hidden or 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
let layer = figma.currentPage.selection[0]; | |
let vectorPaths = layer.vectorPaths.map(item => { | |
return {windingRule: 'NONZERO', data: item.data} | |
}) | |
layer.vectorPaths = vectorPaths | |
layer.exportAsync({ | |
format: 'SVG' | |
}).then(uint8Array => { | |
console.log(String.fromCharCode.apply(null, uint8Array)) | |
}) | |
// https://ourcodeworld.com/articles/read/164/how-to-convert-an-uint8array-to-string-in-javascript |
This file contains hidden or 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
async function figmaImageDataToCanvas(data: Uint8Array): Promise<HTMLCanvasElement> { | |
const canvas = document.createElement('canvas'); | |
const ctx = canvas.getContext('2d'); | |
const url = URL.createObjectURL(new Blob([data])); | |
const image: HTMLImageElement = await new Promise((resolve, reject) => { | |
const img = new Image(); | |
img.onload = () => resolve(img); | |
img.onerror = () => reject(); | |
img.src = url; | |
}); | |
canvas.width = image.width; | |
canvas.height = image.height; | |
ctx.drawImage(image, 0, 0); | |
return canvas; | |
} | |
function canvasToImageData(canvas: HTMLCanvasElement): ImageData { | |
const ctx = canvas.getContext('2d'); | |
return ctx.getImageData(0, 0, canvas.width, canvas.height); | |
} | |
function canvasToBase64(canvas: HTMLCanvasElement): string { | |
return canvas.toDataURL('image/png'); | |
} | |
function canvasToBlob(canvas: HTMLCanvasElement): Promise<Blob> { | |
return new Promise((resolve, reject) => { | |
canvas.toBlob((blob: Blob) => { | |
resolve(blob); | |
}, 'image/png', 1); | |
}); | |
} | |
function base64ToUint8Array(base64: string): Uint8Array { | |
const binaryString = window.atob(base64); | |
const length = binaryString.length; | |
const bytes = new Uint8Array(length); | |
for (let i = 0; i < length; i++) { | |
bytes[i] = binaryString.charCodeAt(i); | |
} | |
return bytes; | |
} | |
function blobToUint8Array(blob: Blob): Uint8Array { | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onload = () => resolve(new Uint8Array(reader.result)); | |
reader.onerror = () => reject(new Error('Could not read from blob')); | |
reader.readAsArrayBuffer(blob); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment