There is a collection of top level functions provided for adding and manipulating images in Framer.
To add an image to the canvas you use the addImage method.
await framer.addImage({
image: "https://example.com/image.png",
name: 'My image',
altText: "Alt description"
})
const fileInput = document.querySelector(`input[type="file"]`)
const file = fileInput.files[0]
const image = await framer.addImage({
image: file,
name: "Uploaded image",
altText: "Alt Description"
})
const image = await framer.addImage(file)
await framer.addImage({
image: "https://example.com/image.png",
name: 'My image',
altText: "Alt description"
})
const fileInput = document.querySelector(`input[type="file"]`)
const file = fileInput.files[0]
const image = await framer.addImage({
image: file,
name: "Uploaded image",
altText: "Alt Description"
})
const image = await framer.addImage(file)
await framer.addImage({
image: "https://example.com/image.png",
name: 'My image',
altText: "Alt description"
})
const fileInput = document.querySelector(`input[type="file"]`)
const file = fileInput.files[0]
const image = await framer.addImage({
image: file,
name: "Uploaded image",
altText: "Alt Description"
})
const image = await framer.addImage(file)To add an image to the current selection you can use the setImage method.
await framer.setImage({ image: "https://example.com/image.png", altText: "Alt Description" })await framer.setImage({ image: "https://example.com/image.png", altText: "Alt Description" })await framer.setImage({ image: "https://example.com/image.png", altText: "Alt Description" })This will apply the image to the current selected Node if it supports setting an image. If a Component Node is selected with a single Image control prop, it will also set the image.
All image-related methods allow you to set the resolution to "auto" (default), "lossless", "small", "medium", "large" or "full", same as through the UI. Effects of each are described in this help article.
await framer.addImage({ image: "https://example.com/image.png", resolution: "lossless" })await framer.addImage({ image: "https://example.com/image.png", resolution: "lossless" })await framer.addImage({ image: "https://example.com/image.png", resolution: "lossless" })You can also upload images to framer without assigning them to a node on the canvas. The API looks similar.
const image = await framer.uploadImage({
image: 'https://example.com/image.png',
name: 'My image',
altText: "alt Description"
})
await framer.createFrameNode({
backgroundImage: image
})const image = await framer.uploadImage({
image: 'https://example.com/image.png',
name: 'My image',
altText: "alt Description"
})
await framer.createFrameNode({
backgroundImage: image
})const image = await framer.uploadImage({
image: 'https://example.com/image.png',
name: 'My image',
altText: "alt Description"
})
await framer.createFrameNode({
backgroundImage: image
})The simplest way to get an image is to use the getImage() function. This will get the image of the current selection if it exists. An example usage looks like this:
const image = await framer.getImage();
const image = await framer.getImage();
const image = await framer.getImage();
To listen to image changes, you can use the subscribeToImage listener. Here is that abstracted into a hook.
import { framer, ImageAsset } from 'framer-plugin'
function useImageSelection() {
const [image, setImage] = useState<ImageAsset | null>(null)
useEffect(() => {
return framer.subscribeToImage(setImage)
}, [])
return image
}import { framer, ImageAsset } from 'framer-plugin'
function useImageSelection() {
const [image, setImage] = useState<ImageAsset | null>(null)
useEffect(() => {
return framer.subscribeToImage(setImage)
}, [])
return image
}import { framer, ImageAsset } from 'framer-plugin'
function useImageSelection() {
const [image, setImage] = useState<ImageAsset | null>(null)
useEffect(() => {
return framer.subscribeToImage(setImage)
}, [])
return image
}In some cases you may want to modify the alt text of an Image without changing the image.
const imageNodes = await framer.getNodesWithAttributeSet("backgroundImage")
for (const imageNode of imageNodes) {
if (!imageNode.backgroundImage) continue
await imageNode.setAttributes({
backgroundImage: imageNode.backgroundImage.cloneWithAttributes({
altText: "Updated Alt Text"
}),
})
}const imageNodes = await framer.getNodesWithAttributeSet("backgroundImage")
for (const imageNode of imageNodes) {
if (!imageNode.backgroundImage) continue
await imageNode.setAttributes({
backgroundImage: imageNode.backgroundImage.cloneWithAttributes({
altText: "Updated Alt Text"
}),
})
}const imageNodes = await framer.getNodesWithAttributeSet("backgroundImage")
for (const imageNode of imageNodes) {
if (!imageNode.backgroundImage) continue
await imageNode.setAttributes({
backgroundImage: imageNode.backgroundImage.cloneWithAttributes({
altText: "Updated Alt Text"
}),
})
}Some plugins may be designed to manipulate images. For these cases, we provide raw image bytes and some utilities to perform image manipulation. A convenient way to perform image manipulation is by rendering the Image bitmap on a HTML canvas. An example implementation of bytesFromCanvas can be found in this gist.
const image = await framer.getImage()
const canvas = document.createElement("canvas")
const ctx = canvas.getContext("2d")
const bitmap = await image.loadBitmap()
ctx.canvas.width = img.width;
ctx.canvas.height = img.height;
ctx.scale(-1, 1);
ctx.drawImage(bitmap, -bitmap.width, 0);
const result = await bytesFromCanvas(canvas);
await framer.addImage({
image: { bytes: result, mimeType },
});
const image = await framer.getImage()
const canvas = document.createElement("canvas")
const ctx = canvas.getContext("2d")
const bitmap = await image.loadBitmap()
ctx.canvas.width = img.width;
ctx.canvas.height = img.height;
ctx.scale(-1, 1);
ctx.drawImage(bitmap, -bitmap.width, 0);
const result = await bytesFromCanvas(canvas);
await framer.addImage({
image: { bytes: result, mimeType },
});
const image = await framer.getImage()
const canvas = document.createElement("canvas")
const ctx = canvas.getContext("2d")
const bitmap = await image.loadBitmap()
ctx.canvas.width = img.width;
ctx.canvas.height = img.height;
ctx.scale(-1, 1);
ctx.drawImage(bitmap, -bitmap.width, 0);
const result = await bytesFromCanvas(canvas);
await framer.addImage({
image: { bytes: result, mimeType },
});Code components can be configured to accept files by adding ControlType.File to the component's property controls. A Plugin can upload files to Framer and assign them to the component's controls using the following code.
const uploadedFile = await framer.uploadFile({
file: "https://example.com/video.mp4",
name: "Example Video"
})
await framer.addComponentInstance({
url: "https://framer.com/m/framer/Video.js#Video",
attributes: {
controls: {
srcType: "Upload",
srcFile: uploadedFile,
},
},
})const uploadedFile = await framer.uploadFile({
file: "https://example.com/video.mp4",
name: "Example Video"
})
await framer.addComponentInstance({
url: "https://framer.com/m/framer/Video.js#Video",
attributes: {
controls: {
srcType: "Upload",
srcFile: uploadedFile,
},
},
})const uploadedFile = await framer.uploadFile({
file: "https://example.com/video.mp4",
name: "Example Video"
})
await framer.addComponentInstance({
url: "https://framer.com/m/framer/Video.js#Video",
attributes: {
controls: {
srcType: "Upload",
srcFile: uploadedFile,
},
},
})Adding an SVG uses the addSVG async function. You can pass in the html for the SVG itself, along with a name of your choosing. Only SVG smaller than 10kb is accepted.
await framer.addSVG({
svg: `<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path d="M5 2.5h10v5h-5Zm0 5h5l5 5H5Zm5 5v5l-5-5Z"/></svg>`,
name: "framer.svg",
})await framer.addSVG({
svg: `<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path d="M5 2.5h10v5h-5Zm0 5h5l5 5H5Zm5 5v5l-5-5Z"/></svg>`,
name: "framer.svg",
})await framer.addSVG({
svg: `<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path d="M5 2.5h10v5h-5Zm0 5h5l5 5H5Zm5 5v5l-5-5Z"/></svg>`,
name: "framer.svg",
})If you need to pass SVG bigger than 10kb, you should pass it's bytes data using the addImage function.
await framer.addImage({
image: {
type: "bytes",
bytes: new TextEncoder().encode(svgInput),
mimeType: "image/svg+xml",
},
name: "SVG",
});await framer.addImage({
image: {
type: "bytes",
bytes: new TextEncoder().encode(svgInput),
mimeType: "image/svg+xml",
},
name: "SVG",
});await framer.addImage({
image: {
type: "bytes",
bytes: new TextEncoder().encode(svgInput),
mimeType: "image/svg+xml",
},
name: "SVG",
});